From 2b9d17251b3adceadaf0979c84c2970f2dc9e405 Mon Sep 17 00:00:00 2001 From: YiFang Xiao Date: Sat, 22 Jun 2024 20:01:52 +0800 Subject: [PATCH] fix: articleview mainwindow ,fulltextsearch remove QRegExp --- src/fulltextsearch.cc | 5 ++--- src/fulltextsearch.hh | 10 +++------- src/ui/articleview.cc | 25 ++++++++++++++++--------- src/ui/articleview.hh | 8 +++----- src/ui/mainwindow.cc | 38 ++++++++++++++++++++++---------------- src/ui/mainwindow.hh | 2 +- 6 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/fulltextsearch.cc b/src/fulltextsearch.cc index f59d55d39..0695f125c 100644 --- a/src/fulltextsearch.cc +++ b/src/fulltextsearch.cc @@ -449,7 +449,7 @@ void FullTextSearchDialog::itemClicked( const QModelIndex & idx ) { if ( idx.isValid() && idx.row() < results.size() ) { QString headword = results[ idx.row() ].headword; - QRegExp reg; + QRegularExpression reg; auto searchText = ui.searchLine->text(); searchText.replace( RX::Ftx::tokenBoundary, " " ); @@ -472,8 +472,7 @@ void FullTextSearchDialog::itemClicked( const QModelIndex & idx ) } if ( !firstAvailbeItem.isEmpty() ) { - reg = QRegExp( firstAvailbeItem, Qt::CaseInsensitive, QRegExp::RegExp2 ); - reg.setMinimal( true ); + reg = QRegularExpression( firstAvailbeItem, QRegularExpression::CaseInsensitiveOption ); } emit showTranslationFor( headword, results[ idx.row() ].dictIDs, reg, false ); diff --git a/src/fulltextsearch.hh b/src/fulltextsearch.hh index 26f94b129..1662f2078 100644 --- a/src/fulltextsearch.hh +++ b/src/fulltextsearch.hh @@ -10,11 +10,7 @@ #include #include -#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) - #include -#else - #include -#endif +#include #include "dict/dictionary.hh" #include "ui_fulltextsearch.h" @@ -212,7 +208,7 @@ class FullTextSearchDialog: public QDialog FtsIndexing & ftsIdx; - QRegExp searchRegExp; + QRegularExpression searchRegExp; int matchedCount; public: @@ -258,7 +254,7 @@ private slots: signals: void showTranslationFor( QString const &, QStringList const & dictIDs, - QRegExp const & searchRegExp, + QRegularExpression const & searchRegExp, bool ignoreDiacritics ); void closeDialog(); }; diff --git a/src/ui/articleview.cc b/src/ui/articleview.cc index 9720cbe59..1b58aac40 100644 --- a/src/ui/articleview.cc +++ b/src/ui/articleview.cc @@ -36,7 +36,6 @@ #include #endif #if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) - #include #include #include #include @@ -358,7 +357,7 @@ void ArticleView::showDefinition( QString const & word, void ArticleView::showDefinition( QString const & word, QStringList const & dictIDs, - QRegExp const & searchRegExp, + QRegularExpression const & searchRegExp, unsigned group, bool ignoreDiacritics ) { @@ -380,10 +379,10 @@ void ArticleView::showDefinition( QString const & word, Utils::Url::addQueryItem( req, "word", word ); Utils::Url::addQueryItem( req, "dictionaries", dictIDs.join( "," ) ); Utils::Url::addQueryItem( req, "regexp", searchRegExp.pattern() ); - if ( searchRegExp.caseSensitivity() == Qt::CaseSensitive ) + if ( !searchRegExp.patternOptions().testFlag( QRegularExpression::CaseInsensitiveOption ) ) Utils::Url::addQueryItem( req, "matchcase", "1" ); - if ( searchRegExp.patternSyntax() == QRegExp::WildcardUnix ) - Utils::Url::addQueryItem( req, "wildcards", "1" ); + // if ( searchRegExp.patternSyntax() == QRegExp::WildcardUnix ) + // Utils::Url::addQueryItem( req, "wildcards", "1" ); Utils::Url::addQueryItem( req, "group", QString::number( group ) ); if ( ignoreDiacritics ) Utils::Url::addQueryItem( req, "ignore_diacritics", "1" ); @@ -399,6 +398,14 @@ void ArticleView::showDefinition( QString const & word, emit sendWordToHistory( word ); } +void ArticleView::showDefinition( QString const & word, + QStringList const & dictIDs, + unsigned group, + bool ignoreDiacritics ) +{ + showDefinition( word, dictIDs, {}, group, ignoreDiacritics ); +} + void ArticleView::sendToAnki( QString const & word, QString const & dict_definition, QString const & sentence ) { ankiConnector->sendToAnki( word, dict_definition, sentence ); @@ -942,7 +949,7 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const & if ( Utils::Url::hasQueryItem( ref, "dictionaries" ) ) { QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ).split( ",", Qt::SkipEmptyParts ); - showDefinition( word, dictsList, QRegExp(), getGroup( ref ), false ); + showDefinition( word, dictsList, getGroup( ref ), false ); } else showDefinition( word, getGroup( ref ), scrollTo, contexts ); @@ -958,7 +965,7 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const & // Specific dictionary group from full-text search QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ).split( ",", Qt::SkipEmptyParts ); - showDefinition( url.path().mid( 1 ), dictsList, QRegExp(), getGroup( ref ), false ); + showDefinition( url.path().mid( 1 ), dictsList, getGroup( ref ), false ); return; } @@ -966,7 +973,7 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const & // Specific dictionary group from full-text search QStringList dictsList = Utils::Url::queryItemValue( url, "dictionaries" ).split( ",", Qt::SkipEmptyParts ); - showDefinition( word, dictsList, QRegExp(), getGroup( url ), false ); + showDefinition( word, dictsList, getGroup( url ), false ); return; } @@ -2016,7 +2023,7 @@ void ArticleView::doubleClicked( QPoint pos ) if ( Utils::Url::hasQueryItem( ref, "dictionaries" ) ) { QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ).split( ",", Qt::SkipEmptyParts ); - showDefinition( selectedText, dictsList, QRegExp(), getGroup( ref ), false ); + showDefinition( selectedText, dictsList, getGroup( ref ), false ); } else showDefinition( selectedText, getGroup( ref ), getCurrentArticle() ); diff --git a/src/ui/articleview.hh b/src/ui/articleview.hh index 173c29413..fe6c9caf2 100644 --- a/src/ui/articleview.hh +++ b/src/ui/articleview.hh @@ -16,10 +16,7 @@ #include "groupcombobox.hh" #include "globalbroadcaster.hh" #include "article_inspect.hh" -#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) ) - #include - -#endif +#include #include "ankiconnector.hh" #include "webmultimediadownload.hh" #include "base_type.hh" @@ -131,9 +128,10 @@ public: void showDefinition( QString const & word, QStringList const & dictIDs, - QRegExp const & searchRegExp, + QRegularExpression const & searchRegExp, unsigned group, bool ignoreDiacritics ); + void showDefinition( QString const & word, QStringList const & dictIDs, unsigned group, bool ignoreDiacritics ); void sendToAnki( QString const & word, QString const & text, QString const & sentence ); /// Clears the view and sets the application-global waiting cursor, diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index 0e615f5ba..30311234f 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -2707,7 +2707,7 @@ void MainWindow::showTranslationFor( QString const & word, unsigned inGroup, QSt void MainWindow::showTranslationForDicts( QString const & inWord, QStringList const & dictIDs, - QRegExp const & searchRegExp, + QRegularExpression const & searchRegExp, bool ignoreDiacritics ) { ArticleView * view = getCurrentArticleView(); @@ -3110,7 +3110,7 @@ void MainWindow::printPreviewPaintRequested( QPrinter * printer ) } static void filterAndCollectResources( QString & html, - QRegExp & rx, + QRegularExpression & rx, const QString & sep, const QString & folder, set< QByteArray > & resourceIncluded, @@ -3118,8 +3118,10 @@ static void filterAndCollectResources( QString & html, { int pos = 0; - while ( ( pos = rx.indexIn( html, pos ) ) != -1 ) { - QUrl url( rx.cap( 1 ) ); + auto match = rx.match( html, pos ); + while ( match.hasMatch() ) { + pos = match.capturedStart(); + QUrl url( match.captured( 1 ) ); QString host = url.host(); QString resourcePath = Utils::Url::path( url ); @@ -3129,7 +3131,7 @@ static void filterAndCollectResources( QString & html, resourcePath.insert( 0, '/' ); QCryptographicHash hash( QCryptographicHash::Md5 ); - hash.addData( rx.cap().toUtf8() ); + hash.addData( match.captured().toUtf8() ); if ( resourceIncluded.insert( hash.result() ).second ) { // Gather resource information (url, filename) to be download later @@ -3139,8 +3141,9 @@ static void filterAndCollectResources( QString & html, // Modify original url, set to the native one resourcePath = QString::fromLatin1( QUrl::toPercentEncoding( resourcePath, "/" ) ); QString newUrl = sep + QDir( folder ).dirName() + host + resourcePath + sep; - html.replace( pos, rx.cap().length(), newUrl ); + html.replace( pos, match.captured().length(), newUrl ); pos += newUrl.length(); + match = rx.match( html, pos ); } } @@ -3196,11 +3199,13 @@ void MainWindow::on_saveArticle_triggered() // Convert internal links - QRegExp rx3( R"lit(href="(bword:|gdlookup://localhost/)([^"]+)")lit" ); + static QRegularExpression rx3( R"lit(href="(bword:|gdlookup://localhost/)([^"]+)")lit" ); int pos = 0; QRegularExpression anchorRx( "(g[0-9a-f]{32}_)[0-9a-f]+_" ); - while ( ( pos = rx3.indexIn( html, pos ) ) != -1 ) { - QString name = QUrl::fromPercentEncoding( rx3.cap( 2 ).simplified().toLatin1() ); + auto match = rx3.match( html, pos ); + while ( match.hasMatch() ) { + pos = match.capturedStart(); + QString name = QUrl::fromPercentEncoding( match.captured( 2 ).simplified().toLatin1() ); QString anchor; name.replace( "?gdanchor=", "#" ); int n = name.indexOf( '#' ); @@ -3211,8 +3216,9 @@ void MainWindow::on_saveArticle_triggered() } name.replace( rxName, "_" ); name = QString( R"(href=")" ) + QUrl::toPercentEncoding( name ) + ".html" + anchor + "\""; - html.replace( pos, rx3.cap().length(), name ); + html.replace( pos, match.captured().length(), name ); pos += name.length(); + match = rx3.match( html, pos ); } // MDict anchors @@ -3223,8 +3229,8 @@ void MainWindow::on_saveArticle_triggered() if ( complete ) { QString folder = fi.absoluteDir().absolutePath() + "/" + fi.baseName() + "_files"; - QRegExp rx1( R"lit("((?:bres|gico|gdau|qrcx|qrc|gdvideo)://[^"]+)")lit" ); - QRegExp rx2( "'((?:bres|gico|gdau|qrcx|qrc|gdvideo)://[^']+)'" ); + static QRegularExpression rx1( R"lit("((?:bres|gico|gdau|qrcx|qrc|gdvideo)://[^"]+)")lit" ); + static QRegularExpression rx2( "'((?:bres|gico|gdau|qrcx|qrc|gdvideo)://[^']+)'" ); set< QByteArray > resourceIncluded; vector< pair< QUrl, QString > > downloadResources; @@ -3280,9 +3286,9 @@ void MainWindow::on_rescanFiles_triggered() loadDictionaries( this, true, cfg, dictionaries, dictNetMgr ); dictMap = Dictionary::dictToMap( dictionaries ); - for ( unsigned x = 0; x < dictionaries.size(); x++ ) { - dictionaries[ x ]->setFTSParameters( cfg.preferences.fts ); - dictionaries[ x ]->setSynonymSearchEnabled( cfg.preferences.synonymSearchEnabled ); + for ( const auto & dictionarie : dictionaries ) { + dictionarie->setFTSParameters( cfg.preferences.fts ); + dictionarie->setSynonymSearchEnabled( cfg.preferences.synonymSearchEnabled ); } ftsIndexing.setDictionaries( dictionaries ); @@ -3373,7 +3379,7 @@ void MainWindow::adjustCurrentZoomFactor() void MainWindow::scaleArticlesByCurrentZoomFactor() { for ( int i = 0; i < ui.tabWidget->count(); i++ ) { - ArticleView & view = dynamic_cast< ArticleView & >( *( ui.tabWidget->widget( i ) ) ); + auto & view = dynamic_cast< ArticleView & >( *( ui.tabWidget->widget( i ) ) ); view.setZoomFactor( cfg.preferences.zoomFactor ); } diff --git a/src/ui/mainwindow.hh b/src/ui/mainwindow.hh index 1af063c89..1cf1b74df 100644 --- a/src/ui/mainwindow.hh +++ b/src/ui/mainwindow.hh @@ -388,7 +388,7 @@ private slots: void showTranslationForDicts( QString const &, QStringList const & dictIDs, - QRegExp const & searchRegExp, + QRegularExpression const & searchRegExp, bool ignoreDiacritics ); void showHistoryItem( QString const & );