change to setcontent

This commit is contained in:
yifang 2021-08-21 09:41:40 +08:00
parent b1c4159a3d
commit e5c72f8ecc
6 changed files with 33 additions and 15 deletions

View file

@ -376,6 +376,7 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource(
{ {
if( url.scheme() == "gico" ) if( url.scheme() == "gico" )
{ {
contentType="image/png";
QByteArray bytes; QByteArray bytes;
QBuffer buffer(&bytes); QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
@ -564,12 +565,11 @@ void BlockedNetworkReply::finishedSlot()
emit finished(); emit finished();
} }
MySchemeHandler::MySchemeHandler(ArticleNetworkAccessManager &articleNetMgr):mManager(articleNetMgr){ MySchemeHandler::MySchemeHandler(){
//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);

View file

@ -211,14 +211,12 @@ class MySchemeHandler : public QWebEngineUrlSchemeHandler
{ {
Q_OBJECT Q_OBJECT
public: public:
MySchemeHandler(ArticleNetworkAccessManager& articleNetMgr); MySchemeHandler();
void requestStarted(QWebEngineUrlRequestJob *requestJob); void requestStarted(QWebEngineUrlRequestJob *requestJob);
protected: protected:
signals: signals:
void requestStart(QUrl& url); void requestStart(QUrl& url);
private:
ArticleNetworkAccessManager& mManager;
}; };
#endif #endif

View file

@ -261,12 +261,12 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
ui.definition->pageAction( QWebEnginePage::Copy )->setShortcut( QKeySequence::Copy ); ui.definition->pageAction( QWebEnginePage::Copy )->setShortcut( QKeySequence::Copy );
ui.definition->addAction( ui.definition->pageAction( QWebEnginePage::Copy ) ); ui.definition->addAction( ui.definition->pageAction( QWebEnginePage::Copy ) );
// QAction * selectAll = ui.definition->pageAction( QWebEnginePage::SelectAll ); QAction * selectAll = ui.definition->pageAction( QWebEnginePage::SelectAll );
// selectAll->setShortcut( QKeySequence::SelectAll ); selectAll->setShortcut( QKeySequence::SelectAll );
// selectAll->setShortcutContext( Qt::WidgetWithChildrenShortcut ); selectAll->setShortcutContext( Qt::WidgetWithChildrenShortcut );
// ui.definition->addAction( selectAll ); ui.definition->addAction( selectAll );
// ui.definition->setContextMenuPolicy( Qt::DefaultContextMenu ); ui.definition->setContextMenuPolicy( Qt::CustomContextMenu );
//todo //todo
//ui.definition->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks ); //ui.definition->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
@ -1735,6 +1735,10 @@ void ArticleView::setHtml(const QString& content,const QUrl& baseUrl){
ui.definition->page()->setHtml(content,baseUrl); ui.definition->page()->setHtml(content,baseUrl);
} }
void ArticleView::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl ){
ui.definition->page()->setContent(data,mimeType,baseUrl);
}
QString ArticleView::getTitle() QString ArticleView::getTitle()
{ {
return ui.definition->page()->title(); return ui.definition->page()->title();

View file

@ -167,6 +167,7 @@ public:
QString toHtml(); QString toHtml();
void setHtml(const QString& content, const QUrl& baseUrl); void setHtml(const QString& content, const QUrl& baseUrl);
void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl());
/// Returns current article's title /// Returns current article's title
QString getTitle(); QString getTitle();

View file

@ -294,6 +294,8 @@ int main( int argc, char ** argv )
#endif #endif
QWebEngineUrlScheme webUiScheme("gdlookup"); QWebEngineUrlScheme webUiScheme("gdlookup");
webUiScheme.setFlags(QWebEngineUrlScheme::SecureScheme | webUiScheme.setFlags(QWebEngineUrlScheme::SecureScheme |
QWebEngineUrlScheme::LocalScheme | QWebEngineUrlScheme::LocalScheme |
@ -301,6 +303,12 @@ int main( int argc, char ** argv )
QWebEngineUrlScheme::registerScheme(webUiScheme); QWebEngineUrlScheme::registerScheme(webUiScheme);
QWebEngineUrlScheme gico("gico");
webUiScheme.setFlags(QWebEngineUrlScheme::SecureScheme |
QWebEngineUrlScheme::LocalScheme |
QWebEngineUrlScheme::LocalAccessAllowed);
QWebEngineUrlScheme::registerScheme(gico);
QHotkeyApplication app( "GoldenDict", argc, argv ); QHotkeyApplication app( "GoldenDict", argc, argv );
LogFilePtrGuard logFilePtrGuard; LogFilePtrGuard logFilePtrGuard;

View file

@ -145,8 +145,9 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
#endif #endif
MySchemeHandler *handler = new MySchemeHandler(articleNetMgr); MySchemeHandler *handler = new MySchemeHandler();
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gdlookup", handler); QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gdlookup", handler);
//QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gico", handler);
connect(handler,SIGNAL(requestStart(QUrl&)),this,SLOT(requestStart(QUrl&))); connect(handler,SIGNAL(requestStart(QUrl&)),this,SLOT(requestStart(QUrl&)));
qRegisterMetaType< Config::InputPhrase >(); qRegisterMetaType< Config::InputPhrase >();
@ -1621,7 +1622,13 @@ void MainWindow::finished(){
ArticleView * view = getCurrentArticleView(); ArticleView * view = getCurrentArticleView();
if ( view ) if ( view )
{ {
view->setHtml(QString(reply->readAll()),reply->url()); //view->setHtml(QString(reply->readAll()),reply->url());
QVariant header=reply->header(QNetworkRequest::KnownHeaders::ContentTypeHeader);
QString contentType=header.toString();
//if(contentType=="text/html")
{
view->setContent(reply->readAll(),header.toString());
}
} }
} }
void MainWindow::requestStart(QUrl &url){ void MainWindow::requestStart(QUrl &url){