mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-24 16:54:08 +00:00
Merge branch 'staged' into dev
This commit is contained in:
commit
e0b74fea4b
120
dictheadwords.cc
120
dictheadwords.cc
|
@ -17,6 +17,7 @@
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include "wildcard.hh"
|
#include "wildcard.hh"
|
||||||
#include "gddebug.hh"
|
#include "gddebug.hh"
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
#define AUTO_APPLY_LIMIT 150000
|
#define AUTO_APPLY_LIMIT 150000
|
||||||
|
|
||||||
|
@ -282,52 +283,71 @@ void DictHeadwords::saveHeadersToFile()
|
||||||
if( fileName.size() == 0)
|
if( fileName.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
cfg.headwordsDialog.headwordsExportPath = QDir::toNativeSeparators(
|
|
||||||
QFileInfo( fileName ).absoluteDir().absolutePath() );
|
|
||||||
QFile file( fileName );
|
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;
|
QMessageBox::critical( this, "GoldenDict", tr( "Can not open exported file" ) );
|
||||||
n /= 10;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QProgressDialog progress( tr( "Export headwords..."), tr( "Cancel" ),
|
cfg.headwordsDialog.headwordsExportPath = QDir::toNativeSeparators(
|
||||||
0, n, this );
|
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 );
|
progress.setWindowModality( Qt::WindowModal );
|
||||||
|
|
||||||
// Write UTF-8 BOM
|
int totalCount=0;
|
||||||
QByteArray line;
|
for( int i = 0; i < headwordsNumber && i < model->wordCount(); ++i )
|
||||||
line.append( 0xEF ).append( 0xBB ).append( 0xBF );
|
|
||||||
if ( file.write( line ) != line.size() )
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Write headwords
|
|
||||||
|
|
||||||
int i;
|
|
||||||
for( i = 0; i < headwordsNumber&&i<model->wordCount(); ++i )
|
|
||||||
{
|
{
|
||||||
if( i % step == 0 )
|
|
||||||
progress.setValue( i / step );
|
|
||||||
|
|
||||||
if( progress.wasCanceled() )
|
if( progress.wasCanceled() )
|
||||||
break;
|
break;
|
||||||
|
progress.setValue( totalCount++ );
|
||||||
|
|
||||||
QVariant value = model->getRow( i );
|
QVariant value = model->getRow( i );
|
||||||
if( !value.canConvert< QString >() )
|
if( !value.canConvert< QString >() )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
line = value.toString().toUtf8();
|
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()<<model->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( '\n', ' ' );
|
||||||
line.replace( '\r', ' ' );
|
line.replace( '\r', ' ' );
|
||||||
|
@ -338,36 +358,20 @@ void DictHeadwords::saveHeadersToFile()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//continue to write the remaining headword
|
file.close();
|
||||||
int nodeIndex = model->getCurrentIndex();
|
|
||||||
auto headwords = model->getRemainRows(nodeIndex);
|
|
||||||
while(!headwords.isEmpty())
|
|
||||||
{
|
|
||||||
if( progress.wasCanceled() )
|
if( progress.wasCanceled() )
|
||||||
break;
|
{
|
||||||
for(auto & w:headwords){
|
QMessageBox::warning( this, "GoldenDict", tr( "Export process is interrupted" ) );
|
||||||
//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();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
gdWarning( "Headers export error: %s", file.errorString().toUtf8().data() );
|
gdWarning( "Headers export error: %s", file.errorString().toUtf8().data() );
|
||||||
file.close();
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//completed.
|
||||||
|
progress.setValue(headwordsNumber*2);
|
||||||
|
progress.hide();
|
||||||
|
QMessageBox::information( this, "GoldenDict", tr( "Export finished" ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DictHeadwords::helpRequested()
|
void DictHeadwords::helpRequested()
|
||||||
|
|
|
@ -41,7 +41,7 @@ void HeadwordListModel::setFilter( QRegularExpression reg )
|
||||||
}
|
}
|
||||||
filtering = true;
|
filtering = true;
|
||||||
filterWords.clear();
|
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 );
|
connect( sr.get(), &Dictionary::Request::finished, this, &HeadwordListModel::requestFinished, Qt::QueuedConnection );
|
||||||
queuedRequests.push_back( sr );
|
queuedRequests.push_back( sr );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue