diff options
Diffstat (limited to 'examples/webenginewidgets/demobrowser/tabwidget.cpp')
-rw-r--r-- | examples/webenginewidgets/demobrowser/tabwidget.cpp | 76 |
1 files changed, 51 insertions, 25 deletions
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); } |