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" )
{
contentType="image/png";
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
@ -564,12 +565,11 @@ void BlockedNetworkReply::finishedSlot()
emit finished();
}
MySchemeHandler::MySchemeHandler(ArticleNetworkAccessManager &articleNetMgr):mManager(articleNetMgr){
//connect(this, SIGNAL(requestStart(QUrl)),&mManager,SLOT(requestStart(QUrl)),Qt::QueuedConnection );
MySchemeHandler::MySchemeHandler(){
}
void MySchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
{
// ....
QUrl url = requestJob->requestUrl();
// QNetworkRequest* request = new QNetworkRequest(url);

View file

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

View file

@ -261,12 +261,12 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
ui.definition->pageAction( QWebEnginePage::Copy )->setShortcut( QKeySequence::Copy );
ui.definition->addAction( ui.definition->pageAction( QWebEnginePage::Copy ) );
// QAction * selectAll = ui.definition->pageAction( QWebEnginePage::SelectAll );
// selectAll->setShortcut( QKeySequence::SelectAll );
// selectAll->setShortcutContext( Qt::WidgetWithChildrenShortcut );
// ui.definition->addAction( selectAll );
QAction * selectAll = ui.definition->pageAction( QWebEnginePage::SelectAll );
selectAll->setShortcut( QKeySequence::SelectAll );
selectAll->setShortcutContext( Qt::WidgetWithChildrenShortcut );
ui.definition->addAction( selectAll );
// ui.definition->setContextMenuPolicy( Qt::DefaultContextMenu );
ui.definition->setContextMenuPolicy( Qt::CustomContextMenu );
//todo
//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);
}
void ArticleView::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl ){
ui.definition->page()->setContent(data,mimeType,baseUrl);
}
QString ArticleView::getTitle()
{
return ui.definition->page()->title();

View file

@ -167,6 +167,7 @@ public:
QString toHtml();
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
QString getTitle();

View file

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

View file

@ -145,8 +145,9 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
#endif
MySchemeHandler *handler = new MySchemeHandler(articleNetMgr);
MySchemeHandler *handler = new MySchemeHandler();
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gdlookup", handler);
//QWebEngineProfile::defaultProfile()->installUrlSchemeHandler("gico", handler);
connect(handler,SIGNAL(requestStart(QUrl&)),this,SLOT(requestStart(QUrl&)));
qRegisterMetaType< Config::InputPhrase >();
@ -1621,7 +1622,13 @@ void MainWindow::finished(){
ArticleView * view = getCurrentArticleView();
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){