connect reply content to article view

This commit is contained in:
xiaoyifang 2021-08-14 15:25:10 +08:00
parent 5967f70c4b
commit eddfa075bd
6 changed files with 64 additions and 35 deletions

View file

@ -1,4 +1,4 @@
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org> /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#if defined( _MSC_VER ) && _MSC_VER < 1800 // VS2012 and older #if defined( _MSC_VER ) && _MSC_VER < 1800 // VS2012 and older
@ -287,9 +287,10 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
#endif #endif
} }
void ArticleNetworkAccessManager:: requestStart(QUrl url){ void ArticleNetworkAccessManager:: requestStart(QUrl& url){
qDebug(u8"slots executes执行了"); QNetworkRequest request;
qDebug()<<url; request.setUrl( url );
QNetworkReply* reply = createRequest(QNetworkAccessManager::GetOperation,request,NULL);
} }
sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource(
@ -563,24 +564,20 @@ void BlockedNetworkReply::finishedSlot()
emit finished(); emit finished();
} }
MySchemeHandler::MySchemeHandler(ArticleNetworkAccessManager& articleNetMgr):mManager(articleNetMgr){ MySchemeHandler::MySchemeHandler(ArticleNetworkAccessManager &articleNetMgr):mManager(articleNetMgr){
} //connect(this, SIGNAL(requestStart(QUrl)),&mManager,SLOT(requestStart(QUrl)),Qt::QueuedConnection );
void MySchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) }
{ void MySchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
// .... {
QUrl url = requestJob->requestUrl(); // ....
QUrl url = requestJob->requestUrl();
// QNetworkRequest* request = new QNetworkRequest(url); // 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);
emit requestStart(url);
// Reply segment }
requestJob->reply("text/html", reply);
connect(this, SIGNAL(requestStart(QUrl)),&mManager,SLOT(requestStart(QUrl)),Qt::QueuedConnection );
emit requestStart(url);
}

View file

@ -1,4 +1,4 @@
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org> /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __ARTICLE_NETMGR_HH_INCLUDED__ #ifndef __ARTICLE_NETMGR_HH_INCLUDED__
@ -133,7 +133,7 @@ public:
QIODevice * outgoingData ); QIODevice * outgoingData );
private slots: private slots:
void requestStart(QUrl url); void requestStart(QUrl& url);
}; };
class ArticleResourceReply: public QNetworkReply class ArticleResourceReply: public QNetworkReply
@ -216,7 +216,7 @@ public:
protected: protected:
signals: signals:
void requestStart(QUrl url); void requestStart(QUrl& url);
private: private:
ArticleNetworkAccessManager& mManager; ArticleNetworkAccessManager& mManager;

View file

@ -336,7 +336,6 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
QWebEngineSettings * settings = ui.definition->page()->settings(); QWebEngineSettings * settings = ui.definition->page()->settings();
settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true ); settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true );
settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true ); settings->globalSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true );
// Load the default blank page instantly, so there would be no flicker. // Load the default blank page instantly, so there would be no flicker.
QString contentType; QString contentType;
@ -509,7 +508,11 @@ void ArticleView::showAnticipation()
void ArticleView::loadFinished( bool ) void ArticleView::loadFinished( bool )
{ {
QUrl url = ui.definition->url(); QUrl url = ui.definition->url();
QObject* obj=sender();
qDebug()<<"article view loaded url is :"<<url<<" sender class is :"<<obj->metaObject()->className();
// See if we have any iframes in need of expansion // See if we have any iframes in need of expansion
ui.definition->page()->runJavaScript(QString("var frames=windows.frames;" ui.definition->page()->runJavaScript(QString("var frames=windows.frames;"
@ -1145,13 +1148,13 @@ void ArticleView::linkHovered ( const QString & link )
void ArticleView::attachToJavaScript() void ArticleView::attachToJavaScript()
{ {
QWebEngineScript script; QWebEngineScript script;
script.setInjectionPoint(QWebEngineScript::DocumentReady); script.setInjectionPoint(QWebEngineScript::DocumentReady);
script.setRunsOnSubFrames(false); script.setRunsOnSubFrames(false);
script.setWorldId(QWebEngineScript::MainWorld); script.setWorldId(QWebEngineScript::MainWorld);
script.setSourceCode(QString("articleview")); 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() QString ArticleView::toHtml()
{ {
QSemaphore sem(1);
sem.acquire(1);
QString html; QString html;
ui.definition->page()->toHtml([&](const QString& content) { ui.definition->page()->toHtml([&](const QString& content) {
html = content; html = content;
sem.release();
}); });
sem.acquire(1);
return html; return html;
} }
void ArticleView::setHtml(QString& content,QUrl& baseUrl){
ui.definition->page()->setHtml(content,baseUrl);
}
QString ArticleView::getTitle() QString ArticleView::getTitle()
{ {
return ui.definition->page()->title(); return ui.definition->page()->title();

View file

@ -166,6 +166,8 @@ public:
/// Returns current article's text in .html format /// Returns current article's text in .html format
QString toHtml(); QString toHtml();
void setHtml(QString& content,QUrl& baseUrl);
/// Returns current article's title /// Returns current article's title
QString getTitle(); QString getTitle();

View file

@ -1,4 +1,4 @@
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org> /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef NO_EPWING_SUPPORT #ifndef NO_EPWING_SUPPORT
@ -147,6 +147,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
MySchemeHandler *handler = new MySchemeHandler(articleNetMgr); MySchemeHandler *handler = new MySchemeHandler(articleNetMgr);
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gdlookup", handler); QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gdlookup", handler);
connect(handler,SIGNAL(requestStart(QUrl&)),this,SLOT(requestStart(QUrl&)));
qRegisterMetaType< Config::InputPhrase >(); qRegisterMetaType< Config::InputPhrase >();
@ -1614,6 +1615,25 @@ void MainWindow::addNewTab()
{ {
createNewTab( true, tr( "(untitled)" ) ); createNewTab( true, tr( "(untitled)" ) );
} }
void MainWindow::finished(){
QNetworkReply *reply=qobject_cast<QNetworkReply*>(sender());
ArticleView * view = getCurrentArticleView();
if ( view )
{
view->setHtml(QString(reply->readAll()),reply->url());
}
}
void MainWindow::requestStart(QUrl &url){
qDebug("current loaded url is: ");
qDebug()<<url;
QNetworkRequest request;
request.setUrl( url );
QNetworkReply* reply = articleNetMgr.createRequest(QNetworkAccessManager::GetOperation,request,NULL);
connect(reply,SIGNAL(finished()),this,SLOT(finished()));
}
ArticleView * MainWindow::createNewTab( bool switchToIt, ArticleView * MainWindow::createNewTab( bool switchToIt,
QString const & name ) QString const & name )

View file

@ -277,6 +277,11 @@ private slots:
/// the timer. Does nothing otherwise. /// the timer. Does nothing otherwise.
void prepareNewReleaseChecks(); void prepareNewReleaseChecks();
void finished();
void requestStart(QUrl& url);
private slots: private slots:
/// Does the new release check. /// Does the new release check.