From a7ba9e4b361623f916cf12009d9279ab3a2c0d39 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Wed, 18 May 2022 22:31:47 +0800 Subject: [PATCH] opt:use acceptNavigationRequest instead of weburlinterceptor --- articleview.cc | 1 + articlewebpage.cpp | 15 +++++++++++++++ articlewebpage.h | 17 +++++++++++++++++ articlewebview.cc | 4 ++++ articlewebview.hh | 2 ++ goldendict.pro | 2 ++ mainwindow.cc | 16 ---------------- mainwindow.hh | 2 -- weburlrequestinterceptor.cpp | 4 ++-- weburlrequestinterceptor.h | 5 ++--- 10 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 articlewebpage.cpp create mode 100644 articlewebpage.h diff --git a/articleview.cc b/articleview.cc index b62f4caf..d8382f98 100644 --- a/articleview.cc +++ b/articleview.cc @@ -262,6 +262,7 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, Au connect(ui.definition, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int))); + connect( ui.definition, SIGNAL( linkClicked( QUrl ) ), this, SLOT( linkClicked( QUrl ) ) ); connect( ui.definition->page(), SIGNAL( titleChanged( QString ) ), this, SLOT( handleTitleChanged( QString ) ) ); diff --git a/articlewebpage.cpp b/articlewebpage.cpp new file mode 100644 index 00000000..9b782f8d --- /dev/null +++ b/articlewebpage.cpp @@ -0,0 +1,15 @@ +#include "articlewebpage.h" + +ArticleWebPage::ArticleWebPage(QObject *parent) + : QWebEnginePage{parent} +{ +} +bool ArticleWebPage::acceptNavigationRequest( const QUrl & url, NavigationType type, bool isMainFrame ) +{ + if( type == QWebEnginePage::NavigationTypeLinkClicked ) + { + emit linkClicked( url ); + return true; + } + return QWebEnginePage::acceptNavigationRequest( url, type, isMainFrame ); +} diff --git a/articlewebpage.h b/articlewebpage.h new file mode 100644 index 00000000..3e1d32d6 --- /dev/null +++ b/articlewebpage.h @@ -0,0 +1,17 @@ +#ifndef ARTICLEWEBPAGE_H +#define ARTICLEWEBPAGE_H + +#include + +class ArticleWebPage : public QWebEnginePage +{ + Q_OBJECT +public: + explicit ArticleWebPage( QObject * parent = nullptr ); +signals: + void linkClicked( const QUrl & url ); +protected: + virtual bool acceptNavigationRequest( const QUrl & url, NavigationType type, bool isMainFrame ); +}; + +#endif // ARTICLEWEBPAGE_H diff --git a/articlewebview.cc b/articlewebview.cc index 0d1ca85e..916f98d7 100644 --- a/articlewebview.cc +++ b/articlewebview.cc @@ -1,6 +1,7 @@ /* This file is (c) 2008-2012 Konstantin Isakov * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ +#include "articlewebpage.h" #include "articlewebview.hh" #include #include @@ -18,6 +19,9 @@ ArticleWebView::ArticleWebView( QWidget *parent ): midButtonPressed( false ), selectionBySingleClick( false ) { + auto page = new ArticleWebPage( this ); + connect( page, &ArticleWebPage::linkClicked, this, &ArticleWebView::linkClicked ); + this->setPage( page ); } ArticleWebView::~ArticleWebView() diff --git a/articlewebview.hh b/articlewebview.hh index ac954bfc..bfab5f71 100644 --- a/articlewebview.hh +++ b/articlewebview.hh @@ -47,6 +47,8 @@ public: /// word, which gets selected by the view in response to double-click. void doubleClicked( QPoint pos ); + void linkClicked( const QUrl & url ); + protected: QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); bool event( QEvent * event ); diff --git a/goldendict.pro b/goldendict.pro index 98d7c7b8..b0f74185 100644 --- a/goldendict.pro +++ b/goldendict.pro @@ -225,6 +225,7 @@ DEFINES += PROGRAM_VERSION=\\\"$$VERSION\\\" # Input HEADERS += folding.hh \ article_inspect.h \ + articlewebpage.h \ globalbroadcaster.h \ iframeschemehandler.h \ inc_case_folding.hh \ @@ -365,6 +366,7 @@ FORMS += groups.ui \ SOURCES += folding.cc \ article_inspect.cpp \ + articlewebpage.cpp \ globalbroadcaster.cpp \ iframeschemehandler.cpp \ main.cc \ diff --git a/mainwindow.cc b/mainwindow.cc index a0de145b..55db62e1 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -162,7 +162,6 @@ MainWindow::MainWindow( Config::Class & cfg_ ): wuri = new WebUrlRequestInterceptor(); QWebEngineProfile::defaultProfile()->setUrlRequestInterceptor( wuri ); - connect( wuri, &WebUrlRequestInterceptor::linkClicked, this, &MainWindow::viewLinkClicked ); if(!cfg.preferences.hideGoldenDictHeader){ QWebEngineProfile::defaultProfile()->setHttpUserAgent(QWebEngineProfile::defaultProfile()->httpUserAgent()+" GoldenDict/webengine"); @@ -3600,21 +3599,6 @@ void MainWindow::unzoom() applyZoomFactor(); } -void MainWindow::viewLinkClicked( const QUrl & url ) -{ - if( scanPopup.get() && scanPopup->isActiveWindow() ) - { - QString word = Utils::Url::getWordFromUrl( url ); - if( !word.isEmpty() ) - { - scanPopup->translateWord( word ); - return; - } - } - ArticleView * view = getCurrentArticleView(); - view->linkClicked( url ); -} - void MainWindow::applyZoomFactor() { // Always call this function synchronously to potentially disable a zoom action, diff --git a/mainwindow.hh b/mainwindow.hh index 12b8c11f..7260ce3c 100644 --- a/mainwindow.hh +++ b/mainwindow.hh @@ -344,8 +344,6 @@ private slots: void zoomout(); void unzoom(); - void viewLinkClicked( const QUrl & url ); - void scaleArticlesByCurrentZoomFactor(); void doWordsZoomIn(); diff --git a/weburlrequestinterceptor.cpp b/weburlrequestinterceptor.cpp index 7255552f..1837f939 100644 --- a/weburlrequestinterceptor.cpp +++ b/weburlrequestinterceptor.cpp @@ -39,7 +39,7 @@ void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) { return; } - emit linkClicked( info.requestUrl() ); - info.block(true); +// emit linkClicked( info.requestUrl() ); +// info.block(true); } } diff --git a/weburlrequestinterceptor.h b/weburlrequestinterceptor.h index e3fdf63e..00e75d70 100644 --- a/weburlrequestinterceptor.h +++ b/weburlrequestinterceptor.h @@ -9,9 +9,8 @@ class WebUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor public: WebUrlRequestInterceptor(QObject *p = Q_NULLPTR); void interceptRequest(QWebEngineUrlRequestInfo &info); -signals: - void linkClicked(const QUrl& url) ; - + signals: + void linkClicked( const QUrl & url ); }; #endif // WEBURLREQUESTINTERCEPTOR_H