-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lazy loading to headwords dialog
- Loading branch information
1 parent
0ea3ad0
commit 473c617
Showing
5 changed files
with
156 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#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<<fileList; | ||
fileSortedList.sort(); | ||
} | ||
return fileSortedList.at(row); | ||
} | ||
|
||
int HeadwordListModel::wordCount() const{ | ||
return wordsCount; | ||
} | ||
|
||
QVariant HeadwordListModel::data(const QModelIndex &index, int role) const | ||
{ | ||
if (!index.isValid()) | ||
return QVariant(); | ||
|
||
if (index.row() >= 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(); | ||
} ); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef HEADWORDSLISTMODEL_H | ||
#define HEADWORDSLISTMODEL_H | ||
|
||
#include "dictionary.hh" | ||
|
||
#include <QAbstractListModel> | ||
#include <QStringList> | ||
|
||
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 |