Skip to content

Commit 74a2211

Browse files
committed
fix cover loading and selection
1 parent bd86691 commit 74a2211

File tree

4 files changed

+77
-76
lines changed

4 files changed

+77
-76
lines changed

src/Data.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ QVector<ListItemData> Data::getListItems() {
5454
"JOIN Info ON Level.infoID = Info.InfoID "
5555
"JOIN AuthorList ON Level.LevelID = AuthorList.levelID "
5656
"JOIN Author ON AuthorList.authorID = Author.AuthorID "
57-
"GROUP BY Info.trleID");
57+
"GROUP BY Info.trleID "
58+
"ORDER BY Info.release DESC");
5859
if (!status) {
5960
qDebug() << "Error preparing query getListItems:"
6061
<< query.lastError().text();
@@ -99,15 +100,17 @@ void Data::getCoverPictures(QVector<ListItemData*>* items) {
99100

100101
if (status) {
101102
for (ListItemData* item : *items) {
102-
query.bindValue(":id", item->m_trle_id);
103-
if (query.exec()) {
104-
if (query.next() == true) {
105-
item->addPicture(
106-
query.value("Picture.data").toByteArray());
103+
if (item->m_cover.isNull()) {
104+
query.bindValue(":id", item->m_trle_id);
105+
if (query.exec()) {
106+
if (query.next() == true) {
107+
item->addPicture(
108+
query.value("Picture.data").toByteArray());
109+
}
110+
} else {
111+
qDebug() << "Error executing query getPictures:"
112+
<< query.lastError().text();
107113
}
108-
} else {
109-
qDebug() << "Error executing query getPictures:"
110-
<< query.lastError().text();
111114
}
112115
}
113116
}

src/TombRaiderLinuxLauncher.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ TombRaiderLinuxLauncher::TombRaiderLinuxLauncher(QWidget *parent)
5050

5151
//
5252
connect(&Controller::getInstance(), SIGNAL(controllerReloadLevelList()),
53-
this, SLOT(loadMoreLevels()));
53+
this, SLOT(loadMoreCovers()));
5454

5555
// Thread work done signal connections
5656
connect(&Controller::getInstance(),
@@ -396,8 +396,8 @@ void TombRaiderLinuxLauncher::backClicked() {
396396
}
397397
}
398398

399-
void TombRaiderLinuxLauncher::loadMoreLevels() {
400-
model->loadMoreLevels();
399+
void TombRaiderLinuxLauncher::loadMoreCovers() {
400+
model->loadMoreCovers();
401401
}
402402

403403
void TombRaiderLinuxLauncher::workTick() {

src/TombRaiderLinuxLauncher.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class TombRaiderLinuxLauncher : public QMainWindow {
8989
/**
9090
* Try loading 100 more levels cards by calling for more cover pictures.
9191
*/
92-
void loadMoreLevels();
92+
void loadMoreCovers();
9393
/**
9494
* Updates progress by 1% of total work steps.
9595
*/

src/levelViewList.hpp

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)