From ffb1922bd7ee71f018ba9052962f8ccd09e39a52 Mon Sep 17 00:00:00 2001 From: YiFang Date: Thu, 5 May 2022 20:59:50 +0800 Subject: [PATCH] Revert "fix:head lazy load" This reverts commit 15702d860b7ec018feaa1661d40161e47f689f58. Revert "add lazy loading to headwords dialog" This reverts commit 473c61724fff28acb5eeb9f9e62c988268526374. --- dictheadwords.cc | 44 ++++++++++----------- dictheadwords.hh | 5 +-- dictheadwords.ui | 81 ++++++++++++++++---------------------- goldendict.pro | 2 - headwordslistmodel.cpp | 88 ------------------------------------------ headwordslistmodel.h | 42 -------------------- 6 files changed, 58 insertions(+), 204 deletions(-) delete mode 100644 headwordslistmodel.cpp delete mode 100644 headwordslistmodel.h diff --git a/dictheadwords.cc b/dictheadwords.cc index 59eb62a7..3e370e3d 100644 --- a/dictheadwords.cc +++ b/dictheadwords.cc @@ -51,12 +51,9 @@ DictHeadwords::DictHeadwords( QWidget *parent, Config::Class & cfg_, ui.matchCase->setChecked( cfg.headwordsDialog.matchCase ); - model = new HeadwordListModel( this ); + model = new QStringListModel( this ); + model->setStringList( headers ); - connect(model,&HeadwordListModel::finished,this,[this](){ - ui.exportButton->setEnabled(true); - ui.progressBar->hide(); - }); proxy = new QSortFilterProxyModel( this ); proxy->setSourceModel( model ); @@ -137,12 +134,16 @@ void DictHeadwords::setup( Dictionary::Class *dict_ ) setWindowTitle( QString::fromUtf8( dict->getName().c_str() ) ); - auto size = dict->getWordCount(); - model->setDict(dict); + headers.clear(); + model->setStringList( headers ); + + dict->getHeadwords( headers ); + model->setStringList( headers ); + proxy->sort( 0 ); filterChanged(); - if( size > AUTO_APPLY_LIMIT ) + if( headers.size() > AUTO_APPLY_LIMIT ) { cfg.headwordsDialog.autoApply = ui.autoApply->isChecked(); ui.autoApply->setChecked( false ); @@ -154,7 +155,6 @@ void DictHeadwords::setup( Dictionary::Class *dict_ ) ui.autoApply->setChecked( cfg.headwordsDialog.autoApply ); } - ui.exportButton->setEnabled(false); ui.applyButton->setEnabled( !ui.autoApply->isChecked() ); setWindowIcon( dict->getIcon() ); @@ -169,7 +169,7 @@ void DictHeadwords::savePos() cfg.headwordsDialog.searchMode = ui.searchModeCombo->currentIndex(); cfg.headwordsDialog.matchCase = ui.matchCase->isChecked(); - if( model->totalCount() <= AUTO_APPLY_LIMIT ) + if( headers.size() <= AUTO_APPLY_LIMIT ) cfg.headwordsDialog.autoApply = ui.autoApply->isChecked(); cfg.headwordsDialog.headwordsDialogGeometry = saveGeometry(); @@ -226,15 +226,12 @@ void DictHeadwords::filterChanged() QString pattern; switch( syntax ) { - case QRegExp::FixedString: - pattern = QRegularExpression::escape( ui.filterLine->text() ); - break; - case QRegExp::WildcardUnix: - pattern = wildcardsToRegexp( ui.filterLine->text() ); - break; - default: - pattern = ui.filterLine->text(); - break; + case QRegExp::FixedString: pattern = QRegularExpression::escape( ui.filterLine->text() ); + break; + case QRegExp::WildcardUnix: pattern = wildcardsToRegexp( ui.filterLine->text() ); + break; + default: pattern = ui.filterLine->text(); + break; } QRegularExpression regExp( pattern, options ); @@ -248,6 +245,8 @@ void DictHeadwords::filterChanged() QApplication::setOverrideCursor( Qt::WaitCursor ); proxy->setFilterRegularExpression( regExp ); + + proxy->sort( 0 ); QApplication::restoreOverrideCursor(); @@ -273,7 +272,8 @@ void DictHeadwords::autoApplyStateChanged( int state ) void DictHeadwords::showHeadwordsNumber() { ui.headersNumber->setText( tr( "Unique headwords total: %1, filtered: %2" ) - .arg( QString::number( model->totalCount() ), QString::number( proxy->rowCount() ) ) ); + .arg( QString::number( headers.size() ) ) + .arg( QString::number( proxy->rowCount() ) ) ); } void DictHeadwords::saveHeadersToFile() @@ -303,7 +303,7 @@ void DictHeadwords::saveHeadersToFile() if ( !file.open( QFile::WriteOnly | QIODevice::Text ) ) break; - int headwordsNumber = model->totalCount(); + int headwordsNumber = proxy->rowCount(); // Setup progress dialog int n = headwordsNumber; @@ -335,7 +335,7 @@ void DictHeadwords::saveHeadersToFile() if( progress.wasCanceled() ) break; - QVariant value = model->getRow(i); + QVariant value = proxy->data( proxy->index( i, 0 ) ); if( !value.canConvert< QString >() ) continue; diff --git a/dictheadwords.hh b/dictheadwords.hh index 59676993..e2053cce 100644 --- a/dictheadwords.hh +++ b/dictheadwords.hh @@ -14,7 +14,6 @@ #include "dictionary.hh" #include "delegate.hh" #include "helpwindow.hh" -#include "headwordslistmodel.h" class DictHeadwords : public QDialog { @@ -30,8 +29,8 @@ public: protected: Config::Class & cfg; Dictionary::Class * dict; - - HeadwordListModel * model; + QStringList headers; + QStringListModel * model; QSortFilterProxyModel * proxy; WordListItemDelegate * delegate; QString dictId; diff --git a/dictheadwords.ui b/dictheadwords.ui index c23dd94b..4989edac 100644 --- a/dictheadwords.ui +++ b/dictheadwords.ui @@ -19,46 +19,6 @@ - - - - If checked any filter changes will we immediately applied to headwords list - - - Auto apply - - - - - - - Filter string (fixed string, wildcards or regular expression) - - - - - - - Filter: - - - - - - - Press this button to apply filter to headwords list - - - Apply - - - false - - - true - - - @@ -135,17 +95,44 @@ - - - - 0 + + + + Press this button to apply filter to headwords list - - -1 + + Apply - + false + + true + + + + + + + If checked any filter changes will we immediately applied to headwords list + + + Auto apply + + + + + + + Filter: + + + + + + + Filter string (fixed string, wildcards or regular expression) + diff --git a/goldendict.pro b/goldendict.pro index 59ebe6b2..8c119681 100644 --- a/goldendict.pro +++ b/goldendict.pro @@ -226,7 +226,6 @@ DEFINES += PROGRAM_VERSION=\\\"$$VERSION\\\" HEADERS += folding.hh \ article_inspect.h \ globalbroadcaster.h \ - headwordslistmodel.h \ inc_case_folding.hh \ inc_diacritic_folding.hh \ mainwindow.hh \ @@ -367,7 +366,6 @@ FORMS += groups.ui \ SOURCES += folding.cc \ article_inspect.cpp \ globalbroadcaster.cpp \ - headwordslistmodel.cpp \ main.cc \ dictionary.cc \ config.cc \ diff --git a/headwordslistmodel.cpp b/headwordslistmodel.cpp deleted file mode 100644 index bf40a0fd..00000000 --- a/headwordslistmodel.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "headwordslistmodel.h" - -HeadwordListModel::HeadwordListModel(QObject *parent) - : QAbstractListModel(parent), wordsCount(0) -{} - -int HeadwordListModel::rowCount(const QModelIndex &parent) const -{ - return parent.isValid() ? 0 : wordsCount; -} - -int HeadwordListModel::totalCount() const{ - return totalSize; -} - -bool HeadwordListModel::isFinish() const{ - return wordsCount >=totalSize; -} - -QString HeadwordListModel::getRow(int row) -{ - if(fileSortedList.empty()){ - fileSortedList<= totalSize || index.row() < 0) - return QVariant(); - - if (role == Qt::DisplayRole) { - return fileList.at(index.row()); - } - return QVariant(); -} - -bool HeadwordListModel::canFetchMore(const QModelIndex &parent) const -{ - if (parent.isValid()) - return false; - return (wordsCount < totalSize); -} - -void HeadwordListModel::fetchMore(const QModelIndex &parent) -{ - if (parent.isValid()) - return; - int remainder = fileList.size() - wordsCount; - int itemsToFetch = qMin(100, remainder); - - if (itemsToFetch <= 0) - return; - - beginInsertRows(QModelIndex(), wordsCount, wordsCount + itemsToFetch - 1); - - wordsCount+= itemsToFetch; - - endInsertRows(); - - emit numberPopulated(wordsCount); -} - -void HeadwordListModel::setDict(Dictionary::Class * dict){ - _dict = dict; - totalSize = _dict->getWordCount(); - wordsCount=0; - QThreadPool::globalInstance()->start( - [ this ]() - { - beginResetModel(); - _dict->getHeadwords( fileList ); - totalSize = fileList.size(); - emit finished(totalSize); - endResetModel(); - } ); - -} - diff --git a/headwordslistmodel.h b/headwordslistmodel.h deleted file mode 100644 index f1de1f34..00000000 --- a/headwordslistmodel.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef HEADWORDSLISTMODEL_H -#define HEADWORDSLISTMODEL_H - -#include "dictionary.hh" - -#include -#include - -class HeadwordListModel : public QAbstractListModel -{ - Q_OBJECT - -public: - HeadwordListModel(QObject *parent = nullptr); - - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int totalCount() const; - int wordCount() const; - bool isFinish() const; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - QString getRow(int row); -signals: - void numberPopulated(int number); - void finished(int number); - -public slots: - void setDict(Dictionary::Class * dict); - -protected: - bool canFetchMore(const QModelIndex &parent) const override; - void fetchMore(const QModelIndex &parent) override; - - -private: - QStringList fileList; - QStringList fileSortedList; - long wordsCount; - long totalSize; - Dictionary::Class * _dict; -}; - -#endif // HEADWORDSLISTMODEL_H