From 101a673cf6629ad160d98dee507874b5b2e0ba99 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Sat, 31 Dec 2022 11:01:45 +0800 Subject: [PATCH 1/2] fix: filter headword return less than expected matched results. --- headwordsmodel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/headwordsmodel.cpp b/headwordsmodel.cpp index bafd685f..3c266358 100644 --- a/headwordsmodel.cpp +++ b/headwordsmodel.cpp @@ -41,7 +41,7 @@ void HeadwordListModel::setFilter( QRegularExpression reg ) } filtering = true; filterWords.clear(); - auto sr = _dict->prefixMatch( gd::toWString( reg.pattern() ), 30 ); + auto sr = _dict->prefixMatch( gd::toWString( reg.pattern() ), 500 ); connect( sr.get(), &Dictionary::Request::finished, this, &HeadwordListModel::requestFinished, Qt::QueuedConnection ); queuedRequests.push_back( sr ); } From 9acbd3aa2fa679e21ef6b38bc3fadc5de0fffe0f Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Sat, 31 Dec 2022 14:01:48 +0800 Subject: [PATCH 2/2] fix: headwords dialog export function sorted the exported headwords and remove duplicated items. --- dictheadwords.cc | 162 ++++++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 79 deletions(-) diff --git a/dictheadwords.cc b/dictheadwords.cc index 9f5e2088..73e7093b 100644 --- a/dictheadwords.cc +++ b/dictheadwords.cc @@ -17,6 +17,7 @@ #include #include "wildcard.hh" #include "gddebug.hh" +#include #define AUTO_APPLY_LIMIT 150000 @@ -282,92 +283,95 @@ void DictHeadwords::saveHeadersToFile() if( fileName.size() == 0) return; - cfg.headwordsDialog.headwordsExportPath = QDir::toNativeSeparators( - QFileInfo( fileName ).absoluteDir().absolutePath() ); QFile file( fileName ); - for(;;) + if ( !file.open( QFile::WriteOnly | QIODevice::Text ) ) { - if ( !file.open( QFile::WriteOnly | QIODevice::Text ) ) - break; - - int headwordsNumber = model->totalCount(); - - // Setup progress dialog - int n = headwordsNumber; - int step = 1; - while( n > 1000 ) - { - step *= 10; - n /= 10; - } - - QProgressDialog progress( tr( "Export headwords..."), tr( "Cancel" ), - 0, n, this ); - progress.setWindowModality( Qt::WindowModal ); - - // Write UTF-8 BOM - QByteArray line; - line.append( 0xEF ).append( 0xBB ).append( 0xBF ); - if ( file.write( line ) != line.size() ) - break; - - // Write headwords - - int i; - for( i = 0; i < headwordsNumber&&iwordCount(); ++i ) - { - if( i % step == 0 ) - progress.setValue( i / step ); - - if( progress.wasCanceled() ) - break; - - QVariant value = model->getRow(i); - if( !value.canConvert< QString >() ) - continue; - - line = value.toString().toUtf8(); - - line.replace( '\n', ' ' ); - line.replace( '\r', ' ' ); - - line += "\n"; - - if ( file.write( line ) != line.size() ) - break; - } - - //continue to write the remaining headword - int nodeIndex = model->getCurrentIndex(); - auto headwords = model->getRemainRows(nodeIndex); - while(!headwords.isEmpty()) - { - if( progress.wasCanceled() ) - break; - for(auto & w:headwords){ - //progress - if( ++i % step == 0 ) - progress.setValue( i / step ); - - line = w.toUtf8(); - line += "\n"; - - if ( file.write( line ) != line.size() ) - break; - } - headwords = model->getRemainRows(nodeIndex); - } - - if( i < headwordsNumber && !progress.wasCanceled() ) - break; - - file.close(); + QMessageBox::critical( this, "GoldenDict", tr( "Can not open exported file" ) ); return; } - gdWarning( "Headers export error: %s", file.errorString().toUtf8().data() ); + cfg.headwordsDialog.headwordsExportPath = QDir::toNativeSeparators( + QFileInfo( fileName ).absoluteDir().absolutePath() ); + + QSet< QString > allHeadwords; + int headwordsNumber = model->totalCount(); + + //headwordsNumber*2 , read + write + QProgressDialog progress( tr( "Export headwords..." ), tr( "Cancel" ), 0, headwordsNumber*2, this ); + progress.setWindowModality( Qt::WindowModal ); + + int totalCount=0; + for( int i = 0; i < headwordsNumber && i < model->wordCount(); ++i ) + { + if( progress.wasCanceled() ) + break; + progress.setValue( totalCount++ ); + + QVariant value = model->getRow( i ); + if( !value.canConvert< QString >() ) + continue; + + allHeadwords.insert( value.toString() ); + } + + // continue to write the remaining headword + int nodeIndex = model->getCurrentIndex(); + auto headwords = model->getRemainRows( nodeIndex ); + while( !headwords.isEmpty() ) + { + if( progress.wasCanceled() ) + break; + allHeadwords.unite(headwords); + + totalCount += headwords.size(); + progress.setValue( totalCount ); + + headwords = model->getRemainRows( nodeIndex ); + } + + qDebug()<getCurrentIndex(); + + // Write UTF-8 BOM + QByteArray line; + line.append( 0xEF ).append( 0xBB ).append( 0xBF ); + file.write( line ); + + QList< QString > sortedWords = allHeadwords.values(); + sortedWords.sort(); + + // Write headwords + for( auto const & word : sortedWords ) + { + if( progress.wasCanceled() ) + break; + progress.setValue( totalCount++ ); + + line = word.toUtf8(); + + line.replace( '\n', ' ' ); + line.replace( '\r', ' ' ); + + line += "\n"; + + if( file.write( line ) != line.size() ) + break; + } + file.close(); + + if( progress.wasCanceled() ) + { + QMessageBox::warning( this, "GoldenDict", tr( "Export process is interrupted" ) ); + gdWarning( "Headers export error: %s", file.errorString().toUtf8().data() ); + } + else + { + //completed. + progress.setValue(headwordsNumber*2); + progress.hide(); + QMessageBox::information( this, "GoldenDict", tr( "Export finished" ) ); + } } void DictHeadwords::helpRequested()