diff options
Diffstat (limited to 'examples')
20 files changed, 827 insertions, 92 deletions
diff --git a/examples/webengine/quicknanobrowser/ApplicationRoot.qml b/examples/webengine/quicknanobrowser/ApplicationRoot.qml index 6735be932..78defab80 100644 --- a/examples/webengine/quicknanobrowser/ApplicationRoot.qml +++ b/examples/webengine/quicknanobrowser/ApplicationRoot.qml @@ -70,18 +70,18 @@ QtObject { onClosing: destroy() } function createWindow(profile) { - var newWindow = browserWindowComponent.createObject(root) - newWindow.currentWebView.profile = profile - profile.downloadRequested.connect(newWindow.onDownloadRequested) - return newWindow + var newWindow = browserWindowComponent.createObject(root); + newWindow.currentWebView.profile = profile; + profile.downloadRequested.connect(newWindow.onDownloadRequested); + return newWindow; } function createDialog(profile) { - var newDialog = browserDialogComponent.createObject(root) - newDialog.currentWebView.profile = profile - return newDialog + var newDialog = browserDialogComponent.createObject(root); + newDialog.currentWebView.profile = profile; + return newDialog; } function load(url) { - var browserWindow = createWindow(defaultProfile) - browserWindow.currentWebView.url = url + var browserWindow = createWindow(defaultProfile); + browserWindow.currentWebView.url = url; } } diff --git a/examples/webengine/quicknanobrowser/BrowserWindow.qml b/examples/webengine/quicknanobrowser/BrowserWindow.qml index c008425d9..596e4a76e 100644 --- a/examples/webengine/quicknanobrowser/BrowserWindow.qml +++ b/examples/webengine/quicknanobrowser/BrowserWindow.qml @@ -48,15 +48,16 @@ ** ****************************************************************************/ +import Qt.labs.settings 1.0 +import QtQml 2.2 import QtQuick 2.2 -import QtWebEngine 1.2 import QtQuick.Controls 1.0 +import QtQuick.Controls.Private 1.0 import QtQuick.Controls.Styles 1.0 +import QtQuick.Dialogs 1.2 import QtQuick.Layouts 1.0 import QtQuick.Window 2.1 -import QtQuick.Controls.Private 1.0 -import QtQuick.Dialogs 1.2 -import Qt.labs.settings 1.0 +import QtWebEngine 1.3 ApplicationWindow { id: browserWindow @@ -79,19 +80,19 @@ ApplicationWindow { Settings { id : appSettings - property alias autoLoadImages: loadImages.checked; - property alias javaScriptEnabled: javaScriptEnabled.checked; - property alias errorPageEnabled: errorPageEnabled.checked; - property alias pluginsEnabled: pluginsEnabled.checked; - property alias fullScreenSupportEnabled: fullScreenSupportEnabled.checked; - property alias autoLoadIconsForPage: autoLoadIconsForPage.checked; - property alias touchIconsEnabled: touchIconsEnabled.checked; + property alias autoLoadImages: loadImages.checked + property alias javaScriptEnabled: javaScriptEnabled.checked + property alias errorPageEnabled: errorPageEnabled.checked + property alias pluginsEnabled: pluginsEnabled.checked + property alias fullScreenSupportEnabled: fullScreenSupportEnabled.checked + property alias autoLoadIconsForPage: autoLoadIconsForPage.checked + property alias touchIconsEnabled: touchIconsEnabled.checked } Action { shortcut: "Ctrl+D" onTriggered: { - downloadView.visible = !downloadView.visible + downloadView.visible = !downloadView.visible; } } Action { @@ -106,14 +107,14 @@ ApplicationWindow { shortcut: StandardKey.Refresh onTriggered: { if (currentWebView) - currentWebView.reload() + currentWebView.reload(); } } Action { shortcut: StandardKey.AddTab onTriggered: { - tabs.createEmptyTab(currentWebView.profile) - tabs.currentIndex = tabs.count - 1 + tabs.createEmptyTab(currentWebView.profile); + tabs.currentIndex = tabs.count - 1; addressBar.forceActiveFocus(); addressBar.selectAll(); } @@ -128,23 +129,23 @@ ApplicationWindow { shortcut: "Escape" onTriggered: { if (currentWebView.state == "FullScreen") { - browserWindow.visibility = browserWindow.previousVisibility - fullScreenNotification.hide() + browserWindow.visibility = browserWindow.previousVisibility; + fullScreenNotification.hide(); currentWebView.triggerWebAction(WebEngineView.ExitFullScreen); } } } Action { shortcut: "Ctrl+0" - onTriggered: currentWebView.zoomFactor = 1.0; + onTriggered: currentWebView.zoomFactor = 1.0 } Action { shortcut: StandardKey.ZoomOut - onTriggered: currentWebView.zoomFactor -= 0.1; + onTriggered: currentWebView.zoomFactor -= 0.1 } Action { shortcut: StandardKey.ZoomIn - onTriggered: currentWebView.zoomFactor += 0.1; + onTriggered: currentWebView.zoomFactor += 0.1 } Action { @@ -187,7 +188,7 @@ ApplicationWindow { toolBar: ToolBar { id: navigationBar RowLayout { - anchors.fill: parent; + anchors.fill: parent ToolButton { enabled: currentWebView && (currentWebView.canGoBack || currentWebView.canGoForward) menu:Menu { @@ -294,7 +295,7 @@ ApplicationWindow { id: httpDiskCacheEnabled text: "HTTP Disk Cache" checkable: !currentWebView.profile.offTheRecord - checked: (currentWebView.profile.httpCacheType == WebEngineProfile.DiskHttpCache) + checked: (currentWebView.profile.httpCacheType === WebEngineProfile.DiskHttpCache) onToggled: currentWebView.profile.httpCacheType = checked ? WebEngineProfile.DiskHttpCache : WebEngineProfile.MemoryHttpCache } MenuItem { @@ -336,12 +337,12 @@ ApplicationWindow { TabView { id: tabs function createEmptyTab(profile) { - var tab = addTab("", tabComponent) + var tab = addTab("", tabComponent); // We must do this first to make sure that tab.active gets set so that tab.item gets instantiated immediately. - tab.active = true - tab.title = Qt.binding(function() { return tab.item.title }) - tab.item.profile = profile - return tab + tab.active = true; + tab.title = Qt.binding(function() { return tab.item.title }); + tab.item.profile = profile; + return tab; } anchors.fill: parent @@ -355,10 +356,10 @@ ApplicationWindow { onLinkHovered: { if (hoveredUrl == "") - resetStatusText.start() + resetStatusText.start(); else { - resetStatusText.stop() - statusText.text = hoveredUrl + resetStatusText.stop(); + statusText.text = hoveredUrl; } } @@ -385,69 +386,69 @@ ApplicationWindow { settings.touchIconsEnabled: appSettings.touchIconsEnabled onCertificateError: { - error.defer() - sslDialog.enqueue(error) + error.defer(); + sslDialog.enqueue(error); } onNewViewRequested: { if (!request.userInitiated) - print("Warning: Blocked a popup window.") + print("Warning: Blocked a popup window."); else if (request.destination == WebEngineView.NewViewInTab) { - var tab = tabs.createEmptyTab(currentWebView.profile) - tabs.currentIndex = tabs.count - 1 - request.openIn(tab.item) + var tab = tabs.createEmptyTab(currentWebView.profile); + tabs.currentIndex = tabs.count - 1; + request.openIn(tab.item); } else if (request.destination == WebEngineView.NewViewInBackgroundTab) { - var tab = tabs.createEmptyTab(currentWebView.profile) - request.openIn(tab.item) + var backgroundTab = tabs.createEmptyTab(currentWebView.profile); + request.openIn(backgroundTab.item); } else if (request.destination == WebEngineView.NewViewInDialog) { - var dialog = applicationRoot.createDialog(currentWebView.profile) - request.openIn(dialog.currentWebView) + var dialog = applicationRoot.createDialog(currentWebView.profile); + request.openIn(dialog.currentWebView); } else { - var window = applicationRoot.createWindow(currentWebView.profile) - request.openIn(window.currentWebView) + var window = applicationRoot.createWindow(currentWebView.profile); + request.openIn(window.currentWebView); } } onFullScreenRequested: { if (request.toggleOn) { - webEngineView.state = "FullScreen" - browserWindow.previousVisibility = browserWindow.visibility - browserWindow.showFullScreen() - fullScreenNotification.show() + webEngineView.state = "FullScreen"; + browserWindow.previousVisibility = browserWindow.visibility; + browserWindow.showFullScreen(); + fullScreenNotification.show(); } else { - webEngineView.state = "" - browserWindow.visibility = browserWindow.previousVisibility - fullScreenNotification.hide() + webEngineView.state = ""; + browserWindow.visibility = browserWindow.previousVisibility; + fullScreenNotification.hide(); } - request.accept() + request.accept(); } onRenderProcessTerminated: { - var status = "" + var status = ""; switch (terminationStatus) { case WebEngineView.NormalTerminationStatus: - status = "(normal exit)" + status = "(normal exit)"; break; case WebEngineView.AbnormalTerminationStatus: - status = "(abnormal exit)" + status = "(abnormal exit)"; break; case WebEngineView.CrashedTerminationStatus: - status = "(crashed)" + status = "(crashed)"; break; case WebEngineView.KilledTerminationStatus: - status = "(killed)" + status = "(killed)"; break; } - print("Render process exited with code " + exitCode + " " + status) - reloadTimer.running = true + print("Render process exited with code " + exitCode + " " + status); + reloadTimer.running = true; } onWindowCloseRequested: { if (tabs.count == 1) - browserWindow.close() + browserWindow.close(); else - tabs.removeTab(tabs.currentIndex) + tabs.removeTab(tabs.currentIndex); } Timer { @@ -473,19 +474,19 @@ ApplicationWindow { "you may not be connected with the host you tried to connect to.\n" + "Do you wish to override the security check and continue?" onYes: { - certErrors.shift().ignoreCertificateError() - presentError() + certErrors.shift().ignoreCertificateError(); + presentError(); } onNo: reject() onRejected: reject() function reject(){ - certErrors.shift().rejectCertificate() - presentError() + certErrors.shift().rejectCertificate(); + presentError(); } function enqueue(error){ - certErrors.push(error) - presentError() + certErrors.push(error); + presentError(); } function presentError(){ visible = certErrors.length > 0 @@ -503,9 +504,9 @@ ApplicationWindow { } function onDownloadRequested(download) { - downloadView.visible = true - downloadView.append(download) - download.accept() + downloadView.visible = true; + downloadView.append(download); + download.accept(); } Rectangle { diff --git a/examples/webengine/quicknanobrowser/DownloadView.qml b/examples/webengine/quicknanobrowser/DownloadView.qml index 13be4bd78..ed28c761c 100644 --- a/examples/webengine/quicknanobrowser/DownloadView.qml +++ b/examples/webengine/quicknanobrowser/DownloadView.qml @@ -64,8 +64,8 @@ Rectangle { } function append(download) { - downloadModel.append(download) - downloadModel.downloads.push(download) + downloadModel.append(download); + downloadModel.downloads.push(download); } Component { @@ -113,14 +113,14 @@ Rectangle { anchors.right: parent.right iconSource: "icons/process-stop.png" onClicked: { - var download = downloadModel.downloads[index] + var download = downloadModel.downloads[index]; - download.cancel() + download.cancel(); downloadModel.downloads = downloadModel.downloads.filter(function (el) { return el.id !== download.id; }); - downloadModel.remove(index) + downloadModel.remove(index); } } } @@ -167,7 +167,7 @@ Rectangle { text: "OK" anchors.centerIn: parent onClicked: { - downloadView.visible = false + downloadView.visible = false; } } } diff --git a/examples/webengine/quicknanobrowser/FullScreenNotification.qml b/examples/webengine/quicknanobrowser/FullScreenNotification.qml index 80a63d479..f0487e868 100644 --- a/examples/webengine/quicknanobrowser/FullScreenNotification.qml +++ b/examples/webengine/quicknanobrowser/FullScreenNotification.qml @@ -51,14 +51,14 @@ Rectangle { opacity: 0 function show() { - visible = true - opacity = 1 - reset.start() + visible = true; + opacity = 1; + reset.start(); } function hide() { - reset.stop() - opacity = 0 + reset.stop(); + opacity = 0; } Behavior on opacity { @@ -66,7 +66,7 @@ Rectangle { duration: 750 onStopped: { if (opacity == 0) - visible = false + visible = false; } } } diff --git a/examples/webenginewidgets/markdowneditor/document.h b/examples/webenginewidgets/markdowneditor/document.h index 3c16c251d..bc6552731 100644 --- a/examples/webenginewidgets/markdowneditor/document.h +++ b/examples/webenginewidgets/markdowneditor/document.h @@ -57,7 +57,7 @@ class Document : public QObject { Q_OBJECT - Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged) + Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged FINAL) public: explicit Document(QObject *parent = nullptr) : QObject(parent) {} diff --git a/examples/webenginewidgets/markdowneditor/mainwindow.h b/examples/webenginewidgets/markdowneditor/mainwindow.h index ad0320373..817f626d8 100644 --- a/examples/webenginewidgets/markdowneditor/mainwindow.h +++ b/examples/webenginewidgets/markdowneditor/mainwindow.h @@ -67,7 +67,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(QWidget *parent = 0); + explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); void openFile(const QString &path); diff --git a/examples/webenginewidgets/simplebrowser/browserwindow.cpp b/examples/webenginewidgets/simplebrowser/browserwindow.cpp index c01f912d3..762d56c85 100644 --- a/examples/webenginewidgets/simplebrowser/browserwindow.cpp +++ b/examples/webenginewidgets/simplebrowser/browserwindow.cpp @@ -105,6 +105,13 @@ BrowserWindow::BrowserWindow(QWidget *parent, Qt::WindowFlags flags) m_urlLineEdit->setFavIcon(QIcon(QStringLiteral(":defaulticon.png"))); + QAction *focusUrlLineEditAction = new QAction(this); + addAction(focusUrlLineEditAction); + focusUrlLineEditAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_L)); + connect(focusUrlLineEditAction, &QAction::triggered, this, [this] () { + m_urlLineEdit->setFocus(Qt::ShortcutFocusReason); + }); + handleWebViewTitleChanged(tr("Qt Simple Browser")); m_tabWidget->createTab(); } diff --git a/examples/webenginewidgets/videoplayer/data/index.html b/examples/webenginewidgets/videoplayer/data/index.html new file mode 100644 index 000000000..4a1bdc33e --- /dev/null +++ b/examples/webenginewidgets/videoplayer/data/index.html @@ -0,0 +1,23 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <style type="text/css"> + #ytplayer { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + } + </style> + </head> + <body> + <iframe + id="ytplayer" + src="https://www.youtube.com/embed/CjyjEUFn_FI" + frameborder="0" + allowfullscreen> + </iframe> + </body> +</html> diff --git a/examples/webenginewidgets/videoplayer/data/videoplayer.qrc b/examples/webenginewidgets/videoplayer/data/videoplayer.qrc new file mode 100644 index 000000000..c3322b454 --- /dev/null +++ b/examples/webenginewidgets/videoplayer/data/videoplayer.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>index.html</file> + </qresource> +</RCC> diff --git a/examples/webenginewidgets/videoplayer/doc/images/videoplayer-example.png b/examples/webenginewidgets/videoplayer/doc/images/videoplayer-example.png Binary files differnew file mode 100644 index 000000000..9cf51d84a --- /dev/null +++ b/examples/webenginewidgets/videoplayer/doc/images/videoplayer-example.png diff --git a/examples/webenginewidgets/videoplayer/doc/src/videoplayer.qdoc b/examples/webenginewidgets/videoplayer/doc/src/videoplayer.qdoc new file mode 100644 index 000000000..599e13e6c --- /dev/null +++ b/examples/webenginewidgets/videoplayer/doc/src/videoplayer.qdoc @@ -0,0 +1,186 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example webenginewidgets/videoplayer + \title WebEngine Widgets Video Player Example + \ingroup webengine-widgetexamples + \brief Displays full screen video using \l QWebEngineView + + \image videoplayer-example.png + + \e {Video Player} demonstrates how to support full screen playback of HTML5 + video using \l QWebEngineView. + + \l {https://fullscreen.spec.whatwg.org/}{The Fullscreen API} is a + cross-browser Javascript API that enables a web page to request that one of + its HTML elements be made to occupy the user's entire screen. It is + commonly used for full screen video playback via the \c <video> element, but + can in principle be used to display any HTML content in full screen mode. Qt + WebEngine supports this API, however it is disabled by default. This example + shows the steps needed to switch it on, including: + + \list + \li Enabling it in \l QWebEngineSettings. + \li Handling the \l QWebEnginePage::fullScreenRequested signal by creating + a new full screen window. + \li Displaying a notification popup to ensure that the user is aware + that something is being displayed full screen. + \endlist + + \include examples-run.qdocinc + + \section1 Overview + + Once started, the example program will create a normal (non-fullscreen) + window with a \l QWebEngineView showing an embedded YouTube video player. + You can then click on the full screen toggle button (bottom-right corner) to + enter full screen mode. This should also display a centered notification + overlay informing you that you can exit full screen mode by pressing the + escape key. + + Implementation-wise entering full screen mode entails creating a new full + screen window with a separate \l QWebEngineView instance and migrating the + \l QWebEnginePage from the normal window's \l QWebEngineView to this new \l + QWebEngineView. Exiting full screen mode reverses this migration. + + The example code is divided between three classes, \c MainWindow, \c + FullScreenWindow, and \c FullScreenNotification. The classes \c MainWindow + and \c FullScreenWindow are each responsible for managing one top-level + window, while \c FullScreenNotification is responsible for styling and + animating the notification box. A \c MainWindow is created on startup and + lives for the entire program runtime, while a new \c FullScreenWindow is + created every time full screen mode is entered. + + \section1 MainWindow Class Declaration + + A \c MainWindow is a \l QMainWindow with a \l QWebEngineView as the central + widget: + + \quotefromfile webenginewidgets/videoplayer/mainwindow.h + \skipto #include + \printuntil /^\}/ + + \section1 MainWindow Class Definition + + In the constructor we start by setting up the \l QWebEngineView as the + central widget: + + \quotefromfile webenginewidgets/videoplayer/mainwindow.cpp + \skipto MainWindow::MainWindow + \printuntil setCentralWidget + + We then configure Qt WebEngine to advertise support for the Fullscreen API: + + \printline QWebEngineSettings + + Without this line the full screen toggle button would be disabled (grayed + out) as the Javascript running on the page can detect that our browser + does not support full screen mode. + + Next we connect the \c fullScreenRequested signal to our slot: + + \printuntil &MainWindow::fullScreenRequested + + This signal is emitted whenever the Javascript on the page wants to enter or + exit full screen mode. Without handling this signal (but still keeping the + \c FullScreenSupportEnabled attribute as \c true) the toggle button will be + enabled but clicking on it will have no effect as Javascript's full screen + request will be denied. + + Finally, we load some HTML (see + \l{webenginewidgets/videoplayer/data/index.html}{index.html} included with + the example) into our \l QWebEngineView: + + \printline load + + The second part of \c MainWindow is handling the full screen requests: + + \skipto MainWindow::fullScreenRequested + \printuntil /^\}/ + + We create a new \c FullScreenWindow when entering full screen mode, and + delete it when exiting. + + \section1 FullScreenWindow Class Declaration + + A \c FullScreenWindow is a \l QWidget containing a \l QWebEngineView and a + \c FullScreenNotification. + + \quotefromfile webenginewidgets/videoplayer/fullscreenwindow.h + \skipto #include + \printuntil /^\}/ + + \section1 FullScreenWindow Class Definition + + The constructor is responsible for hiding the normal window (while saving + its geometry) and showing the new \c FullScreenWindow instead: + + \quotefromfile webenginewidgets/videoplayer/fullscreenwindow.cpp + \skipto FullScreenWindow::FullScreenWindow + \printuntil /^\}/ + + The call to \l QWebEngineView::setPage will move the web page from the \c + MainWindow's view to \c FullScreenWindow's view. + + In the destructor we use the same method to move the page back, after which + we restore the main window's geometry and visibility: + + \skipto FullScreenWindow::~FullScreenWindow + \printuntil /^\}/ + + We override \l QWidget::resizeEvent to do manual layout, keeping the \l + QWebEngineView maximized, and the \c FullScreenNotification centered within + the window: + + \skipto FullScreenWindow::resizeEvent + \printuntil /^\}/ + + \section1 FullScreenNotification Class Declaration + + A \c FullScreenNotification is just a \l QLabel with some styling and + animation: + + \quotefromfile webenginewidgets/videoplayer/fullscreennotification.h + \skipto #include + \printuntil /^\}/ + + \section1 FullScreenWindow Class Definition + + In the constructor we configure the QLabel and set up a delayed fade-out + animation using \l {The Animation Framework}: + + \quotefromfile webenginewidgets/videoplayer/fullscreennotification.cpp + \skipto FullScreenNotification::FullScreenNotification + \printuntil /^\}/ + + The custom signal \c shown, which we use to trigger the animation, is + emitted from the \c showEvent method: + + \skipto FullScreenNotification::showEvent + \printuntil /^\}/ +*/ diff --git a/examples/webenginewidgets/videoplayer/fullscreennotification.cpp b/examples/webenginewidgets/videoplayer/fullscreennotification.cpp new file mode 100644 index 000000000..e65e2cbad --- /dev/null +++ b/examples/webenginewidgets/videoplayer/fullscreennotification.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "fullscreennotification.h" + +#include <QGraphicsOpacityEffect> +#include <QPropertyAnimation> +#include <QSequentialAnimationGroup> + +FullScreenNotification::FullScreenNotification(QWidget *parent) + : QLabel(parent) + , m_previouslyVisible(false) +{ + setText(tr("You are now in full screen mode. Press ESC to quit!")); + setStyleSheet( + "font-size: 24px;" + "color: white;" + "background-color: black;" + "border-color: white;" + "border-width: 2px;" + "border-style: solid;" + "padding: 100px"); + setAttribute(Qt::WA_TransparentForMouseEvents); + + auto effect = new QGraphicsOpacityEffect; + effect->setOpacity(1); + setGraphicsEffect(effect); + + auto animations = new QSequentialAnimationGroup(this); + animations->addPause(3000); + auto opacityAnimation = new QPropertyAnimation(effect, "opacity", animations); + opacityAnimation->setDuration(2000); + opacityAnimation->setStartValue(1.0); + opacityAnimation->setEndValue(0.0); + opacityAnimation->setEasingCurve(QEasingCurve::OutQuad); + animations->addAnimation(opacityAnimation); + + connect(this, &FullScreenNotification::shown, + [animations](){ animations->start(); }); + + connect(animations, &QAbstractAnimation::finished, + [this](){ this->hide(); }); +} + +void FullScreenNotification::showEvent(QShowEvent *event) +{ + QLabel::showEvent(event); + if (!m_previouslyVisible && isVisible()) + emit shown(); + m_previouslyVisible = isVisible(); +} diff --git a/examples/webenginewidgets/videoplayer/fullscreennotification.h b/examples/webenginewidgets/videoplayer/fullscreennotification.h new file mode 100644 index 000000000..9f1befb9f --- /dev/null +++ b/examples/webenginewidgets/videoplayer/fullscreennotification.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef FULLSCREENNOTIFICATION_H +#define FULLSCREENNOTIFICATION_H + +#include <QLabel> + +class FullScreenNotification : public QLabel +{ + Q_OBJECT +public: + FullScreenNotification(QWidget *parent = nullptr); + +protected: + void showEvent(QShowEvent *event) override; + +signals: + void shown(); + +private: + bool m_previouslyVisible; +}; + +#endif // FULLSCREENNOTIFICATION_H diff --git a/examples/webenginewidgets/videoplayer/fullscreenwindow.cpp b/examples/webenginewidgets/videoplayer/fullscreenwindow.cpp new file mode 100644 index 000000000..df28839a2 --- /dev/null +++ b/examples/webenginewidgets/videoplayer/fullscreenwindow.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "fullscreenwindow.h" + +#include "fullscreennotification.h" + +#include <QAction> +#include <QLabel> +#include <QWebEngineView> + +FullScreenWindow::FullScreenWindow(QWebEngineView *oldView, QWidget *parent) + : QWidget(parent) + , m_view(new QWebEngineView(this)) + , m_notification(new FullScreenNotification(this)) + , m_oldView(oldView) + , m_oldGeometry(oldView->window()->geometry()) +{ + m_view->stackUnder(m_notification); + + auto exitAction = new QAction(this); + exitAction->setShortcut(Qt::Key_Escape); + connect(exitAction, &QAction::triggered, [this]() { + m_view->triggerPageAction(QWebEnginePage::ExitFullScreen); + }); + addAction(exitAction); + + m_view->setPage(m_oldView->page()); + setGeometry(m_oldGeometry); + showFullScreen(); + m_oldView->window()->hide(); +} + +FullScreenWindow::~FullScreenWindow() +{ + m_oldView->setPage(m_view->page()); + m_oldView->window()->setGeometry(m_oldGeometry); + m_oldView->window()->show(); + hide(); +} + +void FullScreenWindow::resizeEvent(QResizeEvent *event) +{ + QRect viewGeometry(QPoint(0, 0), size()); + m_view->setGeometry(viewGeometry); + + QRect notificationGeometry(QPoint(0, 0), m_notification->sizeHint()); + notificationGeometry.moveCenter(viewGeometry.center()); + m_notification->setGeometry(notificationGeometry); + + QWidget::resizeEvent(event); +} diff --git a/examples/webenginewidgets/videoplayer/fullscreenwindow.h b/examples/webenginewidgets/videoplayer/fullscreenwindow.h new file mode 100644 index 000000000..dda0a9885 --- /dev/null +++ b/examples/webenginewidgets/videoplayer/fullscreenwindow.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef FULLSCREENWINDOW_H +#define FULLSCREENWINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QWebEngineView; +QT_END_NAMESPACE + +class FullScreenNotification; + +class FullScreenWindow : public QWidget +{ + Q_OBJECT +public: + explicit FullScreenWindow(QWebEngineView *oldView, QWidget *parent = nullptr); + ~FullScreenWindow(); + +protected: + void resizeEvent(QResizeEvent *event) override; + +private: + QWebEngineView *m_view; + FullScreenNotification *m_notification; + QWebEngineView *m_oldView; + QRect m_oldGeometry; +}; + +#endif // FULLSCREENWINDOW_H diff --git a/examples/webenginewidgets/videoplayer/main.cpp b/examples/webenginewidgets/videoplayer/main.cpp new file mode 100644 index 000000000..fcddd988e --- /dev/null +++ b/examples/webenginewidgets/videoplayer/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication app(argc, argv); + + MainWindow mainWindow; + mainWindow.show(); + + return app.exec(); +} diff --git a/examples/webenginewidgets/videoplayer/mainwindow.cpp b/examples/webenginewidgets/videoplayer/mainwindow.cpp new file mode 100644 index 000000000..55885e0c5 --- /dev/null +++ b/examples/webenginewidgets/videoplayer/mainwindow.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "mainwindow.h" + +#include <QWebEngineView> +#include <QWebEngineSettings> +#include <QWebEngineFullScreenRequest> + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , m_view(new QWebEngineView(this)) +{ + setCentralWidget(m_view); + m_view->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true); + connect(m_view->page(), + &QWebEnginePage::fullScreenRequested, + this, + &MainWindow::fullScreenRequested); + m_view->load(QUrl(QStringLiteral("qrc:/index.html"))); +} + +void MainWindow::fullScreenRequested(QWebEngineFullScreenRequest request) +{ + if (request.toggleOn()) { + if (m_fullScreenWindow) + return; + request.accept(); + m_fullScreenWindow.reset(new FullScreenWindow(m_view)); + } else { + if (!m_fullScreenWindow) + return; + request.accept(); + m_fullScreenWindow.reset(); + } +} diff --git a/examples/webenginewidgets/videoplayer/mainwindow.h b/examples/webenginewidgets/videoplayer/mainwindow.h new file mode 100644 index 000000000..a270c6295 --- /dev/null +++ b/examples/webenginewidgets/videoplayer/mainwindow.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include "fullscreenwindow.h" + +#include <QMainWindow> +#include <QWebEngineView> +#include <QWebEngineFullScreenRequest> + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + +private slots: + void fullScreenRequested(QWebEngineFullScreenRequest request); + +private: + QWebEngineView *m_view; + QScopedPointer<FullScreenWindow> m_fullScreenWindow; +}; + +#endif // MAINWINDOW_H diff --git a/examples/webenginewidgets/videoplayer/videoplayer.pro b/examples/webenginewidgets/videoplayer/videoplayer.pro new file mode 100644 index 000000000..ab55992e0 --- /dev/null +++ b/examples/webenginewidgets/videoplayer/videoplayer.pro @@ -0,0 +1,19 @@ +TEMPLATE = app + +QT += webenginewidgets + +HEADERS += \ + mainwindow.h \ + fullscreenwindow.h \ + fullscreennotification.h + +SOURCES += main.cpp \ + mainwindow.cpp \ + fullscreenwindow.cpp \ + fullscreennotification.cpp + +RESOURCES += \ + data/videoplayer.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/webenginewidgets/videoplayer +INSTALLS += target diff --git a/examples/webenginewidgets/webenginewidgets.pro b/examples/webenginewidgets/webenginewidgets.pro index 353104d6a..41582bbf7 100644 --- a/examples/webenginewidgets/webenginewidgets.pro +++ b/examples/webenginewidgets/webenginewidgets.pro @@ -6,7 +6,8 @@ SUBDIRS += \ cookiebrowser \ demobrowser \ markdowneditor \ - simplebrowser + simplebrowser \ + videoplayer contains(WEBENGINE_CONFIG, use_spellchecker):!cross_compile { !contains(WEBENGINE_CONFIG, use_native_spellchecker) { |