Skip to content

Commit

Permalink
fix: articleview mainwindow ,fulltextsearch remove QRegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Jun 23, 2024
1 parent 76f9e82 commit 2b9d172
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 41 deletions.
5 changes: 2 additions & 3 deletions src/fulltextsearch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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, " " );

Expand All @@ -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 );
Expand Down
10 changes: 3 additions & 7 deletions src/fulltextsearch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
#include <QSemaphore>
#include <QStringList>

#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
#include <QtCore5Compat/QRegExp>
#else
#include <QRegExp>
#endif
#include <QRegularExpression>

#include "dict/dictionary.hh"
#include "ui_fulltextsearch.h"
Expand Down Expand Up @@ -212,7 +208,7 @@ class FullTextSearchDialog: public QDialog

FtsIndexing & ftsIdx;

QRegExp searchRegExp;
QRegularExpression searchRegExp;
int matchedCount;

public:
Expand Down Expand Up @@ -258,7 +254,7 @@ private slots:
signals:
void showTranslationFor( QString const &,
QStringList const & dictIDs,
QRegExp const & searchRegExp,
QRegularExpression const & searchRegExp,
bool ignoreDiacritics );
void closeDialog();
};
Expand Down
25 changes: 16 additions & 9 deletions src/ui/articleview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <QWebEngineContextMenuData>
#endif
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
#include <QtCore5Compat/QRegExp>
#include <QWebEngineContextMenuRequest>
#include <QWebEngineFindTextResult>
#include <utility>
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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" );
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -958,15 +965,15 @@ 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;
}

if ( Utils::Url::hasQueryItem( url, "dictionaries" ) ) {
// 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;
}

Expand Down Expand Up @@ -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() );
Expand Down
8 changes: 3 additions & 5 deletions src/ui/articleview.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
#include "groupcombobox.hh"
#include "globalbroadcaster.hh"
#include "article_inspect.hh"
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
#include <QtCore5Compat/QRegExp>

#endif
#include <QRegularExpression>
#include "ankiconnector.hh"
#include "webmultimediadownload.hh"
#include "base_type.hh"
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 22 additions & 16 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -3110,16 +3110,18 @@ void MainWindow::printPreviewPaintRequested( QPrinter * printer )
}

static void filterAndCollectResources( QString & html,
QRegExp & rx,
QRegularExpression & rx,
const QString & sep,
const QString & folder,
set< QByteArray > & resourceIncluded,
vector< pair< QUrl, QString > > & downloadResources )
{
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 );

Expand All @@ -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
Expand All @@ -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 );
}
}

Expand Down Expand Up @@ -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( '#' );
Expand All @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 & );
Expand Down

0 comments on commit 2b9d172

Please sign in to comment.