From 3323b83af3bc0cd4feff0e0463718e77d4eabef7 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 20 Dec 2017 12:53:40 +0100 Subject: Can now open links in new tab --- src/mainwindow.cpp | 12 +++++++----- src/mainwindow.h | 4 +++- src/webengine/webview.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- src/webengine/webview.h | 8 ++++++-- src/widgets/mainwindowtabbar.cpp | 6 +++--- src/widgets/mainwindowtabbar.h | 2 +- 6 files changed, 58 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0e2abd7..3546f83 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -169,15 +169,18 @@ void MainWindow::newTab(const QUrl &url) m_tabBarAdded = true; ui->mainToolBar->addWidget(tabBar); } - tabBar->addTab(createWebView(url, m_profile.get())); + tabBar->addTab(createWebView(url, m_profile.get(), this)); } -void MainWindow::newWindow(const QUrl &url) +MainWindow *MainWindow::newWindow(const QUrl &url) { Browser *instance = static_cast(qApp->instance()); MainWindow *window = instance->createWindow(); window->setProfile(m_profile); - window->newTab(url); + if(!url.isEmpty()) { + window->newTab(url); + } + return window; } void MainWindow::focusAddress() @@ -282,8 +285,7 @@ void MainWindow::handleTabChanged(WebView *view) m_addressBar->setUrl(view->url()); m_addressBar->pageAction()->setMenu(view->pageMenu()); - connect(view, SIGNAL(titleChanged(QString)), this, SLOT(handleTitleUpdated(QString))); - connect(view, SIGNAL(linkHovered(QString)), ui->statusBar, SLOT(showMessage(QString))); + connect(view, &WebView::titleChanged, this, &MainWindow::handleTitleUpdated); m_progressBar->connectWebView(view); diff --git a/src/mainwindow.h b/src/mainwindow.h index 3d6e417..31a3d2f 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -34,6 +34,8 @@ class MainWindow : public QMainWindow { Q_OBJECT + friend class WebView; + public: MainWindow(std::shared_ptr config, QWidget *parent = nullptr); ~MainWindow() override; @@ -45,7 +47,7 @@ public slots: void showSettingsDialog(); void newTab(const QUrl &url = QUrl("")); - void newWindow(const QUrl &url = QUrl("")); + MainWindow *newWindow(const QUrl &url = QUrl("")); void setProfile(std::shared_ptr profile); WebEngineProfile *profile(); diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index 5463610..ff2780c 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -28,9 +28,15 @@ #include #include -WebView::WebView(QWidget *parent) : +#include "mainwindow.h" +#include + +WebView::WebView(MainWindow *parentMainWindow, QWidget *parent) : QWebEngineView(parent) { + Q_CHECK_PTR(parentMainWindow); + m_parent = parentMainWindow; + m_pageMenu = new QMenu(); m_pageMenu->setMinimumWidth(240); @@ -125,7 +131,37 @@ void WebView::setPage(QWebEnginePage *page) QWebEngineView::setPage(page); } +WebView *WebView::createWindow(QWebEnginePage::WebWindowType type) +{ + WebView *view = new WebView(m_parent); + switch (type) { + case QWebEnginePage::WebBrowserWindow: + // a complete web browser window + m_parent->newWindow()->tabBar->addTab(view); + break; + + case QWebEnginePage::WebBrowserTab: + // a web browser tab + m_parent->tabBar->setCurrentIndex(m_parent->tabBar->addTab(view)); + break; + + case QWebEnginePage::WebDialog: + // a window without decorations + m_parent->newWindow()->tabBar->addTab(view); + break; + + case QWebEnginePage::WebBrowserBackgroundTab: + // a web browser tab, but don't swap to it + m_parent->tabBar->addTab(view); + break; + } + + return view; +} + void WebView::handleLinkHovered(const QString &url) { - emit linkHovered(url); + if(isVisible()) { + m_parent->statusBar()->showMessage(url); + } } diff --git a/src/webengine/webview.h b/src/webengine/webview.h index 10654fa..d30a1fd 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -12,11 +12,12 @@ #include #include +class MainWindow; class WebView : public QWebEngineView { Q_OBJECT public: - explicit WebView(QWidget *parent = nullptr); + explicit WebView(MainWindow *parentMainWindow, QWidget *parent = nullptr); ~WebView(); QMenu *pageMenu(); @@ -24,13 +25,16 @@ public: void setPage(QWebEnginePage *page); signals: - void linkHovered(const QString &url); void newBookmark(const QString &title, const QUrl &url); +protected: + WebView *createWindow(QWebEnginePage::WebWindowType type); + private slots: void handleLinkHovered(const QString &url); private: + MainWindow *m_parent = nullptr; QMenu *m_pageMenu = nullptr; }; diff --git a/src/widgets/mainwindowtabbar.cpp b/src/widgets/mainwindowtabbar.cpp index e7209c4..9991043 100644 --- a/src/widgets/mainwindowtabbar.cpp +++ b/src/widgets/mainwindowtabbar.cpp @@ -112,7 +112,7 @@ QSize MainWindowTabBar::tabSizeHint(int index) const void MainWindowTabBar::handleCurrentChanged(int index) { if(index < 0) { - addTab(createWebView(m_parent->profile()->homepage(), m_parent->profile())); + addTab(createWebView(m_parent->profile()->homepage(), m_parent->profile(), m_parent)); return; } @@ -141,9 +141,9 @@ void MainWindowTabBar::updateVectorArrangement(int from, int to) m_views.move(from, to); } -WebView *createWebView(const QUrl &url, WebEngineProfile *profile) +WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent) { - WebView *view = new WebView(nullptr); + WebView *view = new WebView(parent); QWebEnginePage *page = new QWebEnginePage(profile); view->setPage(page); page->load(url); diff --git a/src/widgets/mainwindowtabbar.h b/src/widgets/mainwindowtabbar.h index c8ccfb4..67f51e0 100644 --- a/src/widgets/mainwindowtabbar.h +++ b/src/widgets/mainwindowtabbar.h @@ -50,6 +50,6 @@ private: MainWindow *m_parent; }; -WebView *createWebView(const QUrl &url, WebEngineProfile *profile); +WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent); #endif // WEBVIEWTABBAR_H -- cgit v1.2.1