Skip to content

Commit

Permalink
opt: replace some inefficient uses of QUrlQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying authored Nov 28, 2024
1 parent 00dbc74 commit a16e560
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
35 changes: 21 additions & 14 deletions src/ui/articleview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,28 @@ void ArticleView::showDefinition( QString const & word,
audioLink_.clear();

QUrl req;
QUrlQuery reqQuery;
Contexts contexts( contexts_ );

req.setScheme( "gdlookup" );
req.setHost( "localhost" );
Utils::Url::addQueryItem( req, "word", word );
Utils::Url::addQueryItem( req, "group", QString::number( group ) );
reqQuery.addQueryItem( "word", word );
reqQuery.addQueryItem( "group", QString::number( group ) );
if ( cfg.preferences.ignoreDiacritics ) {
Utils::Url::addQueryItem( req, "ignore_diacritics", "1" );
reqQuery.addQueryItem( "ignore_diacritics", "1" );
}

if ( scrollTo.size() ) {
Utils::Url::addQueryItem( req, "scrollto", scrollTo );
reqQuery.addQueryItem( "scrollto", scrollTo );
}

if ( delayedHighlightText.size() ) {
Utils::Url::addQueryItem( req, "regexp", delayedHighlightText );
reqQuery.addQueryItem( "regexp", delayedHighlightText );
delayedHighlightText.clear();
}

if ( Contexts::Iterator pos = contexts.find( "gdanchor" ); pos != contexts.end() ) {
Utils::Url::addQueryItem( req, "gdanchor", contexts[ "gdanchor" ] );
reqQuery.addQueryItem( "gdanchor", contexts[ "gdanchor" ] );
contexts.erase( pos );
}

Expand All @@ -329,15 +330,17 @@ void ArticleView::showDefinition( QString const & word,
stream << contexts;
buf.close();

Utils::Url::addQueryItem( req, "contexts", QString::fromLatin1( buf.buffer().toBase64() ) );
reqQuery.addQueryItem( "contexts", QString::fromLatin1( buf.buffer().toBase64() ) );
}

QString mutedDicts = getMutedForGroup( group );

if ( mutedDicts.size() ) {
Utils::Url::addQueryItem( req, "muted", mutedDicts );
reqQuery.addQueryItem( "muted", mutedDicts );
}

req.setQuery( reqQuery );

// Any search opened is probably irrelevant now
closeSearch();
//QApplication::setOverrideCursor( Qt::WaitCursor );
Expand Down Expand Up @@ -369,22 +372,26 @@ void ArticleView::showDefinition( QString const & word,
audioLink_.clear();

QUrl req;
QUrlQuery reqQuery;

req.setScheme( "gdlookup" );
req.setHost( "localhost" );
Utils::Url::addQueryItem( req, "word", word );
Utils::Url::addQueryItem( req, "dictionaries", dictIDs.join( "," ) );
Utils::Url::addQueryItem( req, "regexp", searchRegExp.pattern() );

reqQuery.addQueryItem( "word", word );
reqQuery.addQueryItem( "dictionaries", dictIDs.join( "," ) );
reqQuery.addQueryItem( "regexp", searchRegExp.pattern() );
if ( !searchRegExp.patternOptions().testFlag( QRegularExpression::CaseInsensitiveOption ) ) {
Utils::Url::addQueryItem( req, "matchcase", "1" );
reqQuery.addQueryItem( "matchcase", "1" );
}
// if ( searchRegExp.patternSyntax() == QRegExp::WildcardUnix )
// Utils::Url::addQueryItem( req, "wildcards", "1" );
Utils::Url::addQueryItem( req, "group", QString::number( group ) );
reqQuery.addQueryItem( "group", QString::number( group ) );
if ( ignoreDiacritics ) {
Utils::Url::addQueryItem( req, "ignore_diacritics", "1" );
reqQuery.addQueryItem( "ignore_diacritics", "1" );
}

req.setQuery( reqQuery );

// Any search opened is probably irrelevant now
closeSearch();

Expand Down
8 changes: 5 additions & 3 deletions src/ui/articlewebpage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ ArticleWebPage::ArticleWebPage( QObject * parent ):
bool ArticleWebPage::acceptNavigationRequest( const QUrl & resUrl, NavigationType type, bool isMainFrame )
{
QUrl url = resUrl;
QUrlQuery urlQuery{ url };
if ( url.scheme() == "bword" || url.scheme() == "entry" ) {
url.setScheme( "gdlookup" );
url.setHost( "localhost" );
url.setPath( "" );
auto [ valid, word ] = Utils::Url::getQueryWord( resUrl );
Utils::Url::addQueryItem( url, "word", word );
Utils::Url::addQueryItem( url, "group", lastReq.group );
Utils::Url::addQueryItem( url, "muted", lastReq.mutedDicts );
urlQuery.addQueryItem( "word", word );
urlQuery.addQueryItem( "group", lastReq.group );
urlQuery.addQueryItem( "muted", lastReq.mutedDicts );
url.setQuery( urlQuery );
setUrl( url );
return false;
}
Expand Down

0 comments on commit a16e560

Please sign in to comment.