@@ -28,41 +28,47 @@ class LevelListModel : public QAbstractListModel {
2828 explicit LevelListModel (QObject *parent): QAbstractListModel(parent) {}
2929
3030 void setLevels () {
31+ beginResetModel ();
3132 infoList.clear ();
3233 controller.getList (&infoList);
33- sortItems (compareReleaseDates);
34- beginResetModel ();
35- expandRange ();
34+ for (ListItemData& item : infoList) {
35+ filterList.append (&item);
36+ list.append (&item);
37+ }
38+ endResetModel ();
39+ a = createIndex (0 , 0 );
40+ b = createIndex (99 , 0 );
41+ getMoreCovers ();
3642 }
3743
3844 int rowCount (const QModelIndex &parent = QModelIndex()) const override {
3945 Q_UNUSED (parent);
40- return m_loadedRows ;
46+ return filterList. count () ;
4147 }
4248
4349 QVariant data (const QModelIndex &index, int role) const override {
4450 QVariant result;
4551 int row = index.row ();
4652
47- if (index.isValid () && row < infoList .count ()) {
48- const ListItemData & item = infoList .at (row);
53+ if (index.isValid () && row < filterList .count ()) {
54+ const ListItemData* item = filterList .at (row);
4955 switch (role) {
5056 case Qt::DisplayRole:
51- return item. m_title ;
57+ return item-> m_title ;
5258 case Qt::UserRole + 1 :
53- return item. m_authors ;
59+ return item-> m_authors ;
5460 case Qt::UserRole + 2 :
55- return item. m_type ;
61+ return item-> m_type ;
5662 case Qt::UserRole + 3 :
57- return item. m_class ;
63+ return item-> m_class ;
5864 case Qt::UserRole + 4 :
59- return item. m_difficulty ;
65+ return item-> m_difficulty ;
6066 case Qt::UserRole + 5 :
61- return item. m_releaseDate ;
67+ return item-> m_releaseDate ;
6268 case Qt::UserRole + 6 :
63- return item. m_duration ;
69+ return item-> m_duration ;
6470 case Qt::UserRole + 7 :
65- return item. m_cover ;
71+ return item-> m_cover ;
6672 default :
6773 return QVariant ();
6874 }
@@ -72,86 +78,78 @@ class LevelListModel : public QAbstractListModel {
7278 }
7379
7480 int getLid (const QModelIndex &index) const {
75- const ListItemData & item = infoList .at (index.row ());
76- return item. m_trle_id ;
81+ const ListItemData* item = filterList .at (index.row ());
82+ return item-> m_trle_id ;
7783 }
7884
79- void expandRange () {
80- qint64 a = m_loadedRows;
81- qint64 b = m_loadedRows + 100 ;
82-
83- // Set states
84- if (infoList.count () > b) { // we have more then 100 extra
85- m_loadedRows = b;
86- m_stop = false ;
87- } else { // we have less or equal then 100 extra
88- b = infoList.count () - 1 ;
89- m_loadedRows = b;
90- m_stop = true ;
91- endResetModel ();
92- }
93- qDebug () << " expandRange a:" << a;
94- qDebug () << " expandRange b:" << b;
95- qDebug () << " expandRange m_loadedRows:" << m_loadedRows;
96-
97- // Add the covers
98- pictureList.clear ();
99- for (qint64 i = a; i < b; i++) {
100- pictureList.append (&infoList[i]);
85+ void getMoreCovers () {
86+ cache.clear ();
87+ for (qint64 i = a.row (); i <= b.row (); i++) {
88+ cache.append (list[i]);
10189 }
102- controller.getCoverList (&pictureList );
90+ controller.getCoverList (&cache );
10391 }
10492
105- void loadMoreLevels () {
106- if (!m_stop) {
107- // we stop to prevent extra call while in here.
108- m_stop = true ;
109- endResetModel ();
110- beginResetModel ();
111- expandRange ();
93+ void loadMoreCovers () {
94+ qDebug () << a;
95+ qDebug () << b;
96+ emit dataChanged (a, b, {Qt::DecorationRole});
97+ if (!m_covers_loaded) {
98+ a = createIndex (b.row () + 1 , 0 );
99+ qint64 plus100 = b.row () + 100 ;
100+ if (list.count () > plus100) {
101+ b = createIndex (plus100, 0 );
102+ } else {
103+ b = createIndex (list.count () - 1 , 0 );
104+ m_covers_loaded = true ;
105+ }
106+ getMoreCovers ();
112107 }
113108 }
114109
115110 void sortItems (
116- std::function<bool (ListItemData, ListItemData)> compare) {
111+ std::function<bool (ListItemData* , ListItemData* )> compare) {
117112 beginResetModel ();
118- std::sort (infoList .begin (), infoList .end (), compare);
113+ std::sort (filterList .begin (), filterList .end (), compare);
119114 endResetModel ();
120115 }
121116
122- static bool compareTitles (const ListItemData & a, const ListItemData & b) {
123- return a. m_title .toLower () < b. m_title .toLower ();
117+ static bool compareTitles (const ListItemData* a, const ListItemData* b) {
118+ return a-> m_title .toLower () < b-> m_title .toLower ();
124119 }
125120
126121 static bool compareDifficulties (
127- const ListItemData & a, const ListItemData & b) {
128- return a. m_difficulty > b. m_difficulty ;
122+ const ListItemData* a, const ListItemData* b) {
123+ return a-> m_difficulty > b-> m_difficulty ;
129124 }
130125
131- static bool compareDurations (const ListItemData & a, const ListItemData & b) {
132- return a. m_duration > b. m_duration ;
126+ static bool compareDurations (const ListItemData* a, const ListItemData* b) {
127+ return a-> m_duration > b-> m_duration ;
133128 }
134129
135- static bool compareClasses (const ListItemData & a, const ListItemData & b) {
136- return a. m_class > b. m_class ;
130+ static bool compareClasses (const ListItemData* a, const ListItemData* b) {
131+ return a-> m_class > b-> m_class ;
137132 }
138133
139- static bool compareTypes (const ListItemData & a, const ListItemData & b) {
140- return a. m_type > b. m_type ;
134+ static bool compareTypes (const ListItemData* a, const ListItemData* b) {
135+ return a-> m_type > b-> m_type ;
141136 }
142137
143138 static bool compareReleaseDates (
144- const ListItemData & a, const ListItemData & b) {
139+ const ListItemData* a, const ListItemData* b) {
145140 // descending order
146- return a. m_releaseDate > b. m_releaseDate ;
141+ return a-> m_releaseDate > b-> m_releaseDate ;
147142 }
148143
149144 private:
150145 QVector<ListItemData> infoList;
146+ QVector<ListItemData*> filterList;
147+ QVector<ListItemData*> list;
148+ QVector<ListItemData*> cache;
149+ bool m_covers_loaded = false ;
150+ QModelIndex a;
151+ QModelIndex b;
151152 // QVector<ListOriginalData> originalInfoList;
152- QVector<ListItemData*> pictureList;
153- bool m_stop = false ;
154- qint64 m_loadedRows = 0 ;
155153 Controller& controller = Controller::getInstance();
156154};
157155
0 commit comments