diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-01-12 11:32:02 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-01-12 11:35:45 +0100 |
commit | a077399f4c17f57e911334867c918cc6ddeb15fc (patch) | |
tree | c8e4d326d7a074e9d16b68399ecc5f728f8533fe /examples | |
parent | 3d698f5de377bde2293e222536bc50171cfdf1b8 (diff) | |
parent | 12dd6ff845656eb625e2ee3d0e73392bc2c61983 (diff) |
Merge branch '5.6' into dev
Change-Id: I4272eb59cac08c69eaa58dd4d94debf1b8c5cf78
Diffstat (limited to 'examples')
10 files changed, 187 insertions, 56 deletions
diff --git a/examples/examples.pro b/examples/examples.pro index c93886989..fdd25664f 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -6,7 +6,7 @@ qtHaveModule(webengine) { qtHaveModule(webenginewidgets) { SUBDIRS += \ - webenginewidgets/demobrowser \ webenginewidgets/contentmanipulation \ + webenginewidgets/demobrowser \ webenginewidgets/markdowneditor } diff --git a/examples/webengine/quicknanobrowser/BrowserWindow.qml b/examples/webengine/quicknanobrowser/BrowserWindow.qml index e8a9cb9ce..ed49040ca 100644 --- a/examples/webengine/quicknanobrowser/BrowserWindow.qml +++ b/examples/webengine/quicknanobrowser/BrowserWindow.qml @@ -54,18 +54,6 @@ ApplicationWindow { property Item currentWebView: tabs.currentIndex < tabs.count ? tabs.getTab(tabs.currentIndex).item : null property int previousVisibility: Window.Windowed - property bool isFullScreen: visibility == Window.FullScreen - onIsFullScreenChanged: { - // This is for the case where the system forces us to leave fullscreen. - if (currentWebView && !isFullScreen) { - currentWebView.state = "" - if (currentWebView.isFullScreen) { - currentWebView.fullScreenCancelled() - fullScreenNotification.hide() - } - } - } - width: 1300 height: 900 visible: true @@ -127,8 +115,11 @@ ApplicationWindow { Action { shortcut: "Escape" onTriggered: { - if (browserWindow.isFullScreen) + if (currentWebView.state == "FullScreen") { browserWindow.visibility = browserWindow.previousVisibility + fullScreenNotification.hide() + currentWebView.triggerWebAction(WebEngineView.ExitFullScreen); + } } } Action { @@ -271,7 +262,7 @@ ApplicationWindow { id: pluginsEnabled text: "Plugins On" checkable: true - checked: WebEngine.settings.pluginsEnabled + checked: true } MenuItem { id: fullScreenSupportEnabled diff --git a/examples/webengine/quicknanobrowser/doc/src/quicknanobrowser.qdoc b/examples/webengine/quicknanobrowser/doc/src/quicknanobrowser.qdoc index 211951929..3643cfce7 100644 --- a/examples/webengine/quicknanobrowser/doc/src/quicknanobrowser.qdoc +++ b/examples/webengine/quicknanobrowser/doc/src/quicknanobrowser.qdoc @@ -31,11 +31,109 @@ \ingroup webengine-examples \brief A web browser implemented using the WebEngineView QML type. - The Quick Nano Browser demo shows the \l{Qt WebEngine} module in action, - providing a little QML web browser with support for tabs and keyboard - shortcuts. - \image quicknanobrowser-demo.jpg + \e {Quick Nano Browser} demonstrates how to use the \l{Qt WebEngine QML Types} + {Qt WebEngine QML types} to develop a small web browser application that consists of a browser + window with a title bar, toolbar, tab view, and status bar. The web content is loaded in a web + engine view within the tab view. If certificate errors occur, users are prompted for action in a + message dialog. The status bar pops up to display the URL of a hovered link. + + A web page can issue a request for being displayed in fullscreen mode. Users can allow full + screen mode by using a toolbar button. They can leave fullscreen mode by using a keyboard + shortcut. Additional toolbar buttons enable moving backwards and forwards in the browser + history, reloading tab content, and opening a settings menu for enabling the following features: + JavaScript, plugins, fullscreen mode, off the record, HTTP disc cache, autoloading images, and + ignoring certificate errors. + \include examples-run.qdocinc + + \section1 Creating the Main Browser Window + + When the browser main window is loaded, it creates an empty tab using the default profile. Each + tab is a web engine view that fills the main window. + + We create the main window in the \e BrowserWindow.qml file using the ApplicationWindow type: + + \quotefromfile webengine/quicknanobrowser/BrowserWindow.qml + \skipto ApplicationWindow + \printuntil currentWebView + \dots + \skipto width + \printuntil title + + We use the TabView Qt Quick control to create an empty tab view that fills the main window. We + set the tab active first, to make sure that the tab item is immediately instantiated: + + \skipto TabView + \printuntil Component.onCompleted + + The tab contains a web engine view that loads web content: + + \printuntil focus + + We use the \l Action type to create new tabs: + + \quotefromfile webengine/quicknanobrowser/BrowserWindow.qml + \skipto reload + \skipto Action + \printuntil } + + We use the \l TextField Qt Quick Control within a \l ToolBar to create an address bar that + shows the current URL and where users can enter another URL: + + \skipto toolBar + \printuntil anchors.fill + \dots + \skipto TextField + \printuntil addressBar + \dots + \skipto focus + \printuntil /^\ {16}\}/ + + \section1 Handling Certificate Errors + + If the certificate of the site being loaded triggers a certificate error, we call the + \l{WebEngineCertificateError::}{defer()} QML method to pause the URL request and wait for user + input: + + \quotefromfile webengine/quicknanobrowser/BrowserWindow.qml + \skipto onCertificateError + \printuntil } + + We use the MessageDialog type to prompt users to continue or cancel the loading of the web page. + If users select \uicontrol Yes, we call the + \l{WebEngineCertificateError::}{ignoreCertificateError()} method to ignore the error and + continue loading content from the URL. If users select \uicontrol No, we call the + \l{WebEngineCertificateError::}{rejectCertificate()} method to reject the request and stop + loading content from the URL: + + \skipto MessageDialog + \printuntil /^\ {4}\}/ + + \section1 Entering and Leaving Fullscreen Mode + + We create a menu item for allowing fullscreen mode in a settings menu that we place on the tool + bar. Also, we create an action for leaving fullscreen mode by using a keyboard shortcut. + We call the \l{WebEngineFullScreenRequest::}{accept()} method to accept the fullscreen request. + The methdod sets the \l{WebEngineView::}{isFullScreen} property to be equal to the + \l{WebEngineFullScreenRequest::}{toggleOn} property. + + \quotefromfile webengine/quicknanobrowser/BrowserWindow.qml + \skipto onFullScreenRequested + \printuntil /^\ {16}\}/ + + When entering fullscreen mode, we display a notification using the FullScreenNotification custom + type that we create in \e FullScreenNotification.qml. + + We use the \l Action type in the settings menu to create a shortcut for leaving fullscreen mode + by pressing the escape key: + + \quotefromfile webengine/quicknanobrowser/BrowserWindow.qml + \skipto Settings + \printuntil appSettings + \skipto fullScreenSupportEnabled + \printuntil Action + \skipto Escape + \printuntil /^\ {4}\}/ */ diff --git a/examples/webenginewidgets/demobrowser/downloaditem.ui b/examples/webenginewidgets/demobrowser/downloaditem.ui index b7f7deb72..e506f28b6 100644 --- a/examples/webenginewidgets/demobrowser/downloaditem.ui +++ b/examples/webenginewidgets/demobrowser/downloaditem.ui @@ -13,7 +13,16 @@ <string>Form</string> </property> <layout class="QHBoxLayout" name="horizontalLayout" > - <property name="margin" > + <property name="leftMargin"> + <number>5</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>5</number> + </property> + <property name="bottomMargin"> <number>0</number> </property> <item> diff --git a/examples/webenginewidgets/demobrowser/fullscreennotification.h b/examples/webenginewidgets/demobrowser/fullscreennotification.h index 051075ab3..f515bfecb 100644 --- a/examples/webenginewidgets/demobrowser/fullscreennotification.h +++ b/examples/webenginewidgets/demobrowser/fullscreennotification.h @@ -44,9 +44,11 @@ #include <QWidget> +QT_BEGIN_NAMESPACE class QLabel; class QGridLayout; class QPropertyAnimation; +QT_END_NAMESPACE class FullScreenNotification : public QWidget { @@ -72,5 +74,4 @@ private: int y; }; - #endif // FULLSCREENNOTIFICATION_H diff --git a/examples/webenginewidgets/demobrowser/searchlineedit.cpp b/examples/webenginewidgets/demobrowser/searchlineedit.cpp index 5152ac1c0..1573b1046 100644 --- a/examples/webenginewidgets/demobrowser/searchlineedit.cpp +++ b/examples/webenginewidgets/demobrowser/searchlineedit.cpp @@ -45,7 +45,7 @@ #include <QtGui/QMouseEvent> #include <QtWidgets/QMenu> #include <QtWidgets/QStyle> -#include <QtWidgets/QStyleOptionFrameV2> +#include <QtWidgets/QStyleOptionFrame> ClearButton::ClearButton(QWidget *parent) : QAbstractButton(parent) @@ -181,7 +181,7 @@ void SearchLineEdit::paintEvent(QPaintEvent *event) { if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) { ExLineEdit::paintEvent(event); - QStyleOptionFrameV2 panel; + QStyleOptionFrame panel; initStyleOption(&panel); QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); QFontMetrics fm = fontMetrics(); diff --git a/examples/webenginewidgets/demobrowser/tabwidget.cpp b/examples/webenginewidgets/demobrowser/tabwidget.cpp index 23903e88a..b744fad90 100644 --- a/examples/webenginewidgets/demobrowser/tabwidget.cpp +++ b/examples/webenginewidgets/demobrowser/tabwidget.cpp @@ -171,7 +171,17 @@ void TabBar::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) m_dragStartPos = event->pos(); + QTabBar::mousePressEvent(event); + + // Middle click on tab should close it. + if (event->button() == Qt::MiddleButton) { + const QPoint pos = event->pos(); + int index = tabAt(pos); + if (index != -1) { + emit closeTab(index); + } + } } void TabBar::mouseMoveEvent(QMouseEvent *event) @@ -259,6 +269,7 @@ TabWidget::TabWidget(QWidget *parent) connect(m_tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int))); connect(m_tabBar, SIGNAL(reloadAllTabs()), this, SLOT(reloadAllTabs())); connect(m_tabBar, SIGNAL(tabMoved(int,int)), this, SLOT(moveTab(int,int))); + connect(m_tabBar, SIGNAL(tabBarDoubleClicked(int)), this, SLOT(handleTabBarDoubleClicked(int))); connect(m_tabBar, SIGNAL(muteTab(int,bool)), this, SLOT(setAudioMutedForTab(int,bool))); setTabBar(m_tabBar); setDocumentMode(true); @@ -427,6 +438,12 @@ void TabWidget::fullScreenRequested(QWebEngineFullScreenRequest request) } } +void TabWidget::handleTabBarDoubleClicked(int index) +{ + if (index != -1) return; + newTab(); +} + QAction *TabWidget::newTabAction() const { return m_newTabAction; @@ -500,6 +517,36 @@ int TabWidget::webViewIndex(WebView *webView) const return index; } +void TabWidget::setupPage(QWebEnginePage* page) +{ + connect(page, SIGNAL(windowCloseRequested()), + this, SLOT(windowCloseRequested())); + connect(page, SIGNAL(geometryChangeRequested(QRect)), + this, SIGNAL(geometryChangeRequested(QRect))); +#if defined(QWEBENGINEPAGE_PRINTREQUESTED) + connect(page, SIGNAL(printRequested(QWebEngineFrame*)), + this, SIGNAL(printRequested(QWebEngineFrame*))); +#endif +#if defined(QWEBENGINEPAGE_MENUBARVISIBILITYCHANGEREQUESTED) + connect(page, SIGNAL(menuBarVisibilityChangeRequested(bool)), + this, SIGNAL(menuBarVisibilityChangeRequested(bool))); +#endif +#if defined(QWEBENGINEPAGE_STATUSBARVISIBILITYCHANGEREQUESTED) + connect(page, SIGNAL(statusBarVisibilityChangeRequested(bool)), + this, SIGNAL(statusBarVisibilityChangeRequested(bool))); +#endif +#if defined(QWEBENGINEPAGE_TOOLBARVISIBILITYCHANGEREQUESTED) + connect(page, SIGNAL(toolBarVisibilityChangeRequested(bool)), + this, SIGNAL(toolBarVisibilityChangeRequested(bool))); +#endif + + // webview actions + for (int i = 0; i < m_actions.count(); ++i) { + WebActionMapper *mapper = m_actions[i]; + mapper->addChild(page->action(mapper->webAction())); + } +} + WebView *TabWidget::newTab(bool makeCurrent) { // line edit @@ -551,35 +598,13 @@ WebView *TabWidget::newTab(bool makeCurrent) this, SLOT(webPageMutedOrAudibleChanged())); connect(webView, SIGNAL(urlChanged(QUrl)), this, SLOT(webViewUrlChanged(QUrl))); - connect(webView->page(), SIGNAL(windowCloseRequested()), - this, SLOT(windowCloseRequested())); - connect(webView->page(), SIGNAL(geometryChangeRequested(QRect)), - this, SIGNAL(geometryChangeRequested(QRect))); -#if defined(QWEBENGINEPAGE_PRINTREQUESTED) - connect(webView->page(), SIGNAL(printRequested(QWebEngineFrame*)), - this, SIGNAL(printRequested(QWebEngineFrame*))); -#endif -#if defined(QWEBENGINEPAGE_MENUBARVISIBILITYCHANGEREQUESTED) - connect(webView->page(), SIGNAL(menuBarVisibilityChangeRequested(bool)), - this, SIGNAL(menuBarVisibilityChangeRequested(bool))); -#endif -#if defined(QWEBENGINEPAGE_STATUSBARVISIBILITYCHANGEREQUESTED) - connect(webView->page(), SIGNAL(statusBarVisibilityChangeRequested(bool)), - this, SIGNAL(statusBarVisibilityChangeRequested(bool))); -#endif -#if defined(QWEBENGINEPAGE_TOOLBARVISIBILITYCHANGEREQUESTED) - connect(webView->page(), SIGNAL(toolBarVisibilityChangeRequested(bool)), - this, SIGNAL(toolBarVisibilityChangeRequested(bool))); -#endif + + addTab(webView, tr("(Untitled)")); if (makeCurrent) setCurrentWidget(webView); - // webview actions - for (int i = 0; i < m_actions.count(); ++i) { - WebActionMapper *mapper = m_actions[i]; - mapper->addChild(webView->page()->action(mapper->webAction())); - } + setupPage(webView->page()); if (count() == 1) currentChanged(currentIndex()); @@ -685,6 +710,7 @@ void TabWidget::setProfile(QWebEngineProfile *profile) QWidget *tabWidget = widget(i); if (WebView *tab = qobject_cast<WebView*>(tabWidget)) { WebPage* webPage = new WebPage(m_profile, tab); + setupPage(webPage); webPage->load(tab->page()->url()); tab->setPage(webPage); } diff --git a/examples/webenginewidgets/demobrowser/tabwidget.h b/examples/webenginewidgets/demobrowser/tabwidget.h index f4ad9c02d..c6eef58f9 100644 --- a/examples/webenginewidgets/demobrowser/tabwidget.h +++ b/examples/webenginewidgets/demobrowser/tabwidget.h @@ -131,13 +131,16 @@ private: #include <QtCore/QUrl> #include <QtWidgets/QTabWidget> -QT_BEGIN_NAMESPACE + class FullScreenNotification; + +QT_BEGIN_NAMESPACE class QCompleter; class QLineEdit; class QMenu; class QStackedWidget; QT_END_NAMESPACE + /*! TabWidget that contains WebViews and a stack widget of associated line edits. @@ -222,9 +225,12 @@ private slots: void windowCloseRequested(); void moveTab(int fromIndex, int toIndex); void fullScreenRequested(QWebEngineFullScreenRequest request); + void handleTabBarDoubleClicked(int index); void webPageMutedOrAudibleChanged(); private: + void setupPage(QWebEnginePage* page); + QAction *m_recentlyClosedTabsAction; QAction *m_newTabAction; QAction *m_closeTabAction; diff --git a/examples/webenginewidgets/demobrowser/urllineedit.cpp b/examples/webenginewidgets/demobrowser/urllineedit.cpp index 685277d9c..0f4e59c4b 100644 --- a/examples/webenginewidgets/demobrowser/urllineedit.cpp +++ b/examples/webenginewidgets/demobrowser/urllineedit.cpp @@ -57,7 +57,7 @@ #include <QtGui/QDrag> #include <QtGui/QPainter> #include <QtWidgets/QStyle> -#include <QtWidgets/QStyleOptionFrameV2> +#include <QtWidgets/QStyleOptionFrame> #include <QtCore/QDebug> @@ -112,7 +112,7 @@ void ExLineEdit::resizeEvent(QResizeEvent *event) void ExLineEdit::updateGeometries() { - QStyleOptionFrameV2 panel; + QStyleOptionFrame panel; initStyleOption(&panel); QRect rect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); @@ -131,7 +131,7 @@ void ExLineEdit::updateGeometries() clearButtonWidth, this->height()); } -void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const +void ExLineEdit::initStyleOption(QStyleOptionFrame *option) const { option->initFrom(this); option->rect = contentsRect(); @@ -144,7 +144,7 @@ void ExLineEdit::initStyleOption(QStyleOptionFrameV2 *option) const if (hasEditFocus()) option->state |= QStyle::State_HasEditFocus; #endif - option->features = QStyleOptionFrameV2::None; + option->features = QStyleOptionFrame::None; } QSize ExLineEdit::sizeHint() const @@ -189,7 +189,7 @@ bool ExLineEdit::event(QEvent *event) void ExLineEdit::paintEvent(QPaintEvent *) { QPainter p(this); - QStyleOptionFrameV2 panel; + QStyleOptionFrame panel; initStyleOption(&panel); style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this); } @@ -322,7 +322,7 @@ void UrlLineEdit::paintEvent(QPaintEvent *event) ExLineEdit::paintEvent(event); QPainter painter(this); - QStyleOptionFrameV2 panel; + QStyleOptionFrame panel; initStyleOption(&panel); QRect backgroundRect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); if (m_webView && !hasFocus()) { diff --git a/examples/webenginewidgets/demobrowser/urllineedit.h b/examples/webenginewidgets/demobrowser/urllineedit.h index 775aa7d18..b9f2cff7c 100644 --- a/examples/webenginewidgets/demobrowser/urllineedit.h +++ b/examples/webenginewidgets/demobrowser/urllineedit.h @@ -77,7 +77,7 @@ protected: protected: void updateGeometries(); - void initStyleOption(QStyleOptionFrameV2 *option) const; + void initStyleOption(QStyleOptionFrame *option) const; QWidget *m_leftWidget; QLineEdit *m_lineEdit; |