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 */
#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()<<url;
void ArticleNetworkAccessManager:: requestStart(QUrl& url){
QNetworkRequest request;
request.setUrl( url );
QNetworkReply* reply = createRequest(QNetworkAccessManager::GetOperation,request,NULL);
}
sptr< Dictionary::DataRequest > 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);
}

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 */
#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;

View file

@ -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 :"<<url<<" sender class is :"<<obj->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();

View file

@ -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();

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 */
#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<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,
QString const & name )

View file

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