refactor code.F12 optimize

This commit is contained in:
yifang 2021-12-12 13:35:32 +08:00
parent cb5ac438fe
commit a2baf836e4
2 changed files with 46 additions and 53 deletions

View file

@ -26,14 +26,11 @@
#include <QRegularExpression> #include <QRegularExpression>
#include "wildcard.hh" #include "wildcard.hh"
#include "utils.hh" #include "utils.hh"
#include <assert.h> #include <assert.h>
#ifdef Q_OS_WIN32 #ifdef Q_OS_WIN32
#include <windows.h> #include <windows.h>
#include <QPainter> #include <QPainter>
#endif #endif
@ -178,11 +175,9 @@ void ArticleView::emitJavascriptFinished(){
//a better solution would be to replace it with callback etc. //a better solution would be to replace it with callback etc.
QString ArticleView::runJavaScriptSync(QWebEnginePage* frame, const QString& variable) QString ArticleView::runJavaScriptSync(QWebEnginePage* frame, const QString& variable)
{ {
qDebug(QString("runJavascriptScriptSync:%1").arg(variable).toLatin1().data()); qDebug("%s", QString("runJavascriptScriptSync:%1").arg(variable).toLatin1().data());
QString result; QString result;
//QEventLoop loop;
//QObject::connect(this, SIGNAL(notifyJavascriptFinished()), &loop, SLOT(quit()));
QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop()); QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
QTimer::singleShot(1000, loop.data(), &QEventLoop::quit); QTimer::singleShot(1000, loop.data(), &QEventLoop::quit);
frame->runJavaScript(variable, [loop,&result](const QVariant &v) frame->runJavaScript(variable, [loop,&result](const QVariant &v)
@ -192,8 +187,6 @@ QString ArticleView::runJavaScriptSync(QWebEnginePage* frame, const QString& var
result = v.toString(); result = v.toString();
loop->quit(); loop->quit();
} }
//this->emitJavascriptFinished();
}); });
loop->exec(); loop->exec();
@ -334,15 +327,15 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm,
ui.definition->addAction( &inspectAction ); ui.definition->addAction( &inspectAction );
//connect( &inspectAction, SIGNAL( triggered() ), this, SLOT( inspect() ) ); //connect( &inspectAction, SIGNAL( triggered() ), this, SLOT( inspect() ) );
QWebEngineView *m_pNewView;
QWebEnginePage *page = ui.definition->page(); QWebEnginePage *page = ui.definition->page();
connect(&inspectAction, &QAction::triggered, this, [page, m_pNewView]() mutable connect(&inspectAction, &QAction::triggered, this, [page, this]() {
{ if (inspectView == nullptr || !inspectView->isVisible()) {
m_pNewView = new QWebEngineView(); inspectView = new QWebEngineView();
page->setDevToolsPage(m_pNewView->page()); page->setDevToolsPage(inspectView->page());
page->triggerAction(QWebEnginePage::InspectElement); page->triggerAction(QWebEnginePage::InspectElement);
m_pNewView->show(); inspectView->show();
}); }
});
ui.definition->installEventFilter( this ); ui.definition->installEventFilter( this );
ui.searchFrame->installEventFilter( this ); ui.searchFrame->installEventFilter( this );
@ -1677,17 +1670,21 @@ void ArticleView::playSound()
ui.definition->page()->runJavaScript(variable); ui.definition->page()->runJavaScript(variable);
} }
// use eventloop to turn the async callback to sync execution.
QString ArticleView::toHtml() QString ArticleView::toHtml()
{ {
QString html; QString result;
ui.definition->page()->toHtml([&](const QString& content) { QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
QTimer::singleShot(1000, loop.data(), &QEventLoop::quit);
html = content; ui.definition->page()->toHtml([loop, &result](const QString &content) {
if (loop->isRunning()) {
}); result = content;
loop->quit();
return html; }
});
loop->exec();
return result;
} }
void ArticleView::setHtml(const QString& content,const QUrl& baseUrl){ void ArticleView::setHtml(const QString& content,const QUrl& baseUrl){
@ -2645,36 +2642,28 @@ void ArticleView::performFtsFindOperation( bool backwards )
runJavaScript( QString( "var sel=window.getSelection();sel.removeAllRanges();sel.addRange(%1);_=0;" ) runJavaScript( QString( "var sel=window.getSelection();sel.removeAllRanges();sel.addRange(%1);_=0;" )
.arg( rangeVarName ) ); .arg( rangeVarName ) );
bool res; if (backwards) {
if( backwards ) if (ftsPosition > 0) {
{ ftsPosition -= 1;
if( ftsPosition > 0 ) }
{
ui.definition->findText( allMatches.at( ftsPosition - 1 ),
flags | QWebEnginePage::FindBackward );
ftsPosition -= 1;
}
else
ui.definition->findText( allMatches.at( ftsPosition ),
flags | QWebEnginePage::FindBackward );
// ui.ftsSearchPrevious->setEnabled( res ); ui.definition->findText(allMatches.at(ftsPosition),
// if( !ui.ftsSearchNext->isEnabled() ) flags | QWebEnginePage::FindBackward,
// ui.ftsSearchNext->setEnabled( res ); [this](bool res) {
} ui.ftsSearchPrevious->setEnabled(res);
else if (!ui.ftsSearchNext->isEnabled())
{ ui.ftsSearchNext->setEnabled(res);
if( ftsPosition < allMatches.size() - 1 ) });
{ } else {
ui.definition->findText( allMatches.at( ftsPosition + 1 ), flags ); if (ftsPosition < allMatches.size() - 1) {
ftsPosition += 1; ftsPosition += 1;
} }
else
ui.definition->findText( allMatches.at( ftsPosition ), flags );
// ui.ftsSearchNext->setEnabled( res ); ui.definition->findText(allMatches.at(ftsPosition), flags, [this](bool res) {
// if( !ui.ftsSearchPrevious->isEnabled() ) ui.ftsSearchNext->setEnabled(res);
// ui.ftsSearchPrevious->setEnabled( res ); if (!ui.ftsSearchPrevious->isEnabled())
ui.ftsSearchPrevious->setEnabled(res);
});
} }
// Store new highlighted selection // Store new highlighted selection

View file

@ -41,6 +41,9 @@ class ArticleView: public QFrame
QString articleToJump; QString articleToJump;
QString rangeVarName; QString rangeVarName;
//used to hold the F12 inspect source view.
QWebEngineView *inspectView = nullptr;
/// Any resource we've decided to download off the dictionary gets stored here. /// Any resource we've decided to download off the dictionary gets stored here.
/// Full vector capacity is used for search requests, where we have to make /// Full vector capacity is used for search requests, where we have to make
/// a multitude of requests. /// a multitude of requests.
@ -371,8 +374,9 @@ private:
QStringList getMutedDictionaries(unsigned group); QStringList getMutedDictionaries(unsigned group);
protected: void findTextCallback();
protected:
// We need this to hide the search bar when we're showed // We need this to hide the search bar when we're showed
void showEvent( QShowEvent * ); void showEvent( QShowEvent * );