From eddfa075bd9d8ba767872f91885374d8be891e99 Mon Sep 17 00:00:00 2001 From: xiaoyifang Date: Sat, 14 Aug 2021 15:25:10 +0800 Subject: [PATCH] connect reply content to article view --- article_netmgr.cc | 35 ++++++++++++++++------------------- article_netmgr.hh | 6 +++--- articleview.cc | 29 +++++++++++++++++------------ articleview.hh | 2 ++ mainwindow.cc | 22 +++++++++++++++++++++- mainwindow.hh | 5 +++++ 6 files changed, 64 insertions(+), 35 deletions(-) diff --git a/article_netmgr.cc b/article_netmgr.cc index 6152f533..3c0a9062 100644 --- a/article_netmgr.cc +++ b/article_netmgr.cc @@ -1,4 +1,4 @@ -/* This file is (c) 2008-2012 Konstantin Isakov +/* This file is (c) 2008-2012 Konstantin Isakov * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ #if defined( _MSC_VER ) && _MSC_VER < 1800 // VS2012 and older @@ -287,9 +287,10 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op, #endif } -void ArticleNetworkAccessManager:: requestStart(QUrl url){ - qDebug(u8"slots executes,执行了"); - qDebug()< ArticleNetworkAccessManager::getResource( @@ -563,24 +564,20 @@ void BlockedNetworkReply::finishedSlot() emit finished(); } -MySchemeHandler::MySchemeHandler(ArticleNetworkAccessManager& articleNetMgr):mManager(articleNetMgr){ - } - void MySchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) - { - // .... - QUrl url = requestJob->requestUrl(); +MySchemeHandler::MySchemeHandler(ArticleNetworkAccessManager &articleNetMgr):mManager(articleNetMgr){ + //connect(this, SIGNAL(requestStart(QUrl)),&mManager,SLOT(requestStart(QUrl)),Qt::QueuedConnection ); +} +void MySchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) +{ + // .... + QUrl url = requestJob->requestUrl(); // QNetworkRequest* request = new QNetworkRequest(url); - QNetworkRequest request; - request.setUrl( url ); - QNetworkReply* reply = mManager. createRequest(QNetworkAccessManager::GetOperation,request,NULL); + // Reply segment + // requestJob->reply("text/html", reply); - - // Reply segment - requestJob->reply("text/html", reply); - connect(this, SIGNAL(requestStart(QUrl)),&mManager,SLOT(requestStart(QUrl)),Qt::QueuedConnection ); - emit requestStart(url); - } + emit requestStart(url); +} diff --git a/article_netmgr.hh b/article_netmgr.hh index a91cd735..c5c17da5 100644 --- a/article_netmgr.hh +++ b/article_netmgr.hh @@ -1,4 +1,4 @@ -/* This file is (c) 2008-2012 Konstantin Isakov +/* This file is (c) 2008-2012 Konstantin Isakov * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ #ifndef __ARTICLE_NETMGR_HH_INCLUDED__ @@ -133,7 +133,7 @@ public: QIODevice * outgoingData ); private slots: - void requestStart(QUrl url); + void requestStart(QUrl& url); }; class ArticleResourceReply: public QNetworkReply @@ -216,7 +216,7 @@ public: protected: signals: - void requestStart(QUrl url); + void requestStart(QUrl& url); private: ArticleNetworkAccessManager& mManager; diff --git a/articleview.cc b/articleview.cc index 8127876a..685978b5 100644 --- a/articleview.cc +++ b/articleview.cc @@ -336,7 +336,6 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, QWebEngineSettings * settings = ui.definition->page()->settings(); settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true ); settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true ); - // Load the default blank page instantly, so there would be no flicker. QString contentType; @@ -509,7 +508,11 @@ void ArticleView::showAnticipation() void ArticleView::loadFinished( bool ) { - QUrl url = ui.definition->url(); + QUrl url = ui.definition->url(); + QObject* obj=sender(); + qDebug()<<"article view loaded url is :"<metaObject()->className(); + + // See if we have any iframes in need of expansion ui.definition->page()->runJavaScript(QString("var frames=windows.frames;" @@ -1145,13 +1148,13 @@ void ArticleView::linkHovered ( const QString & link ) void ArticleView::attachToJavaScript() { - QWebEngineScript script; - script.setInjectionPoint(QWebEngineScript::DocumentReady); - script.setRunsOnSubFrames(false); - script.setWorldId(QWebEngineScript::MainWorld); - script.setSourceCode(QString("articleview")); + QWebEngineScript script; + script.setInjectionPoint(QWebEngineScript::DocumentReady); + script.setRunsOnSubFrames(false); + script.setWorldId(QWebEngineScript::MainWorld); + script.setSourceCode(QString("articleview")); - ui.definition->page()->scripts().insert(script); + ui.definition->page()->scripts().insert(script); } @@ -1717,19 +1720,21 @@ void ArticleView::playSound() QString ArticleView::toHtml() { - QSemaphore sem(1); - sem.acquire(1); QString html; ui.definition->page()->toHtml([&](const QString& content) { html = content; - sem.release(); + }); - sem.acquire(1); + return html; } +void ArticleView::setHtml(QString& content,QUrl& baseUrl){ + ui.definition->page()->setHtml(content,baseUrl); +} + QString ArticleView::getTitle() { return ui.definition->page()->title(); diff --git a/articleview.hh b/articleview.hh index 3f5335de..b1647b9f 100644 --- a/articleview.hh +++ b/articleview.hh @@ -166,6 +166,8 @@ public: /// Returns current article's text in .html format QString toHtml(); + void setHtml(QString& content,QUrl& baseUrl); + /// Returns current article's title QString getTitle(); diff --git a/mainwindow.cc b/mainwindow.cc index 424807b4..a208717f 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -1,4 +1,4 @@ -/* This file is (c) 2008-2012 Konstantin Isakov +/* This file is (c) 2008-2012 Konstantin Isakov * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ #ifndef NO_EPWING_SUPPORT @@ -147,6 +147,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ): MySchemeHandler *handler = new MySchemeHandler(articleNetMgr); QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gdlookup", handler); + connect(handler,SIGNAL(requestStart(QUrl&)),this,SLOT(requestStart(QUrl&))); qRegisterMetaType< Config::InputPhrase >(); @@ -1614,6 +1615,25 @@ void MainWindow::addNewTab() { createNewTab( true, tr( "(untitled)" ) ); } +void MainWindow::finished(){ + QNetworkReply *reply=qobject_cast(sender()); + + ArticleView * view = getCurrentArticleView(); + if ( view ) + { + view->setHtml(QString(reply->readAll()),reply->url()); + } +} +void MainWindow::requestStart(QUrl &url){ + qDebug("current loaded url is: "); + qDebug()<