2020#include < algorithm>
2121
2222#include " ../src/Controller.hpp"
23+ #include " ../src/staticData.hpp"
2324
2425class LevelListModel : public QAbstractListModel {
2526 Q_OBJECT
@@ -29,78 +30,82 @@ class LevelListModel : public QAbstractListModel {
2930
3031 void setLevels () {
3132 beginResetModel ();
32- infoList .clear ();
33- controller.getList (&infoList );
34- for (ListItemData& item : infoList ) {
35- filterList .append (&item);
36- list .append (&item);
33+ m_mainList .clear ();
34+ controller.getList (&m_mainList );
35+ for (ListItemData& item : m_mainList ) {
36+ m_filterList .append (&item);
37+ m_sortList .append (&item);
3738 }
3839 endResetModel ();
39- a = createIndex (0 , 0 );
40- b = createIndex (99 , 0 );
40+ m_a = createIndex (0 , 0 );
41+ m_b = createIndex (99 , 0 );
4142 getMoreCovers ();
4243 }
4344
4445 int rowCount (const QModelIndex &parent = QModelIndex()) const override {
4546 Q_UNUSED (parent);
46- return filterList .count ();
47+ return m_filterList .count ();
4748 }
4849
4950 QVariant data (const QModelIndex &index, int role) const override {
5051 QVariant result;
5152 int row = index.row ();
5253
53- if (index.isValid () && row < filterList .count ()) {
54- const ListItemData* item = filterList .at (row);
54+ if (( index.isValid ()) && ( row < m_filterList .count () )) {
55+ const ListItemData* item = m_filterList .at (row);
5556 switch (role) {
5657 case Qt::DisplayRole:
57- return item->m_title ;
58+ result = item->m_title ;
59+ break ;
5860 case Qt::UserRole + 1 :
59- return item->m_authors ;
61+ result = item->m_authors ;
62+ break ;
6063 case Qt::UserRole + 2 :
61- return item->m_type ;
64+ result = item->m_type ;
65+ break ;
6266 case Qt::UserRole + 3 :
63- return item->m_class ;
67+ result = item->m_class ;
68+ break ;
6469 case Qt::UserRole + 4 :
65- return item->m_difficulty ;
70+ result = item->m_difficulty ;
71+ break ;
6672 case Qt::UserRole + 5 :
67- return item->m_releaseDate ;
73+ result = item->m_releaseDate ;
74+ break ;
6875 case Qt::UserRole + 6 :
69- return item->m_duration ;
76+ result = item->m_duration ;
77+ break ;
7078 case Qt::UserRole + 7 :
71- return item->m_cover ;
72- default :
73- return QVariant ();
79+ result = item->m_cover ;
80+ break ;
7481 }
7582 }
7683
7784 return result;
7885 }
7986
8087 int getLid (const QModelIndex &index) const {
81- const ListItemData* item = filterList .at (index.row ());
88+ const ListItemData* item = m_filterList .at (index.row ());
8289 return item->m_trle_id ;
8390 }
8491
8592 void getMoreCovers () {
86- cache .clear ();
87- for (qint64 i = a .row (); i <= b .row (); i++) {
88- cache .append (list [i]);
93+ m_cache .clear ();
94+ for (qint64 i = m_a .row (); i <= m_b .row (); i++) {
95+ m_cache .append (&m_mainList [i]);
8996 }
90- controller.getCoverList (&cache );
97+ controller.getCoverList (&m_cache );
9198 }
9299
93100 void loadMoreCovers () {
94- qDebug () << a;
95- qDebug () << b;
96- emit dataChanged (a, b, {Qt::DecorationRole});
101+ emit dataChanged (m_a, m_b, {Qt::DecorationRole});
97102 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 );
103+ m_a = createIndex (m_b .row () + 1 , 0 );
104+ qint64 plus100 = m_b .row () + 100 ;
105+ if (m_mainList .count () > plus100) {
106+ m_b = createIndex (plus100, 0 );
102107 } else {
103- b = createIndex (list .count () - 1 , 0 );
108+ m_b = createIndex (m_mainList .count () - 1 , 0 );
104109 m_covers_loaded = true ;
105110 }
106111 getMoreCovers ();
@@ -110,8 +115,9 @@ class LevelListModel : public QAbstractListModel {
110115 void sortItems (
111116 std::function<bool (ListItemData*, ListItemData*)> compare) {
112117 beginResetModel ();
113- std::sort (filterList .begin (), filterList .end (), compare);
118+ std::sort (m_sortList .begin (), m_sortList .end (), compare);
114119 endResetModel ();
120+ reFilter ();
115121 }
116122
117123 static bool compareTitles (const ListItemData* a, const ListItemData* b) {
@@ -141,14 +147,96 @@ class LevelListModel : public QAbstractListModel {
141147 return a->m_releaseDate > b->m_releaseDate ;
142148 }
143149
150+ void filterClass (const QString &class_) {
151+ beginResetModel ();
152+ m_filter = QString (" class:%1" ).arg (class_);
153+ m_filterList.clear ();
154+
155+ for (ListItemData* item : m_sortList) {
156+ if (StaticData ().getClass ()[item->m_class ] == class_) {
157+ m_filterList.append (item);
158+ }
159+ }
160+
161+ endResetModel ();
162+ }
163+
164+ void filterType (const QString &type) {
165+ beginResetModel ();
166+ m_filter = QString (" type:%1" ).arg (type);
167+ m_filterList.clear ();
168+
169+ for (ListItemData* item : m_sortList) {
170+ if (StaticData ().getType ()[item->m_type ] == type) {
171+ m_filterList.append (item);
172+ }
173+ }
174+
175+ endResetModel ();
176+ }
177+
178+ void filterDifficulty (const QString &difficulty) {
179+ beginResetModel ();
180+ m_filter = QString (" difficulty:%1" ).arg (difficulty);
181+ m_filterList.clear ();
182+
183+ for (ListItemData* item : m_sortList) {
184+ if (StaticData ()
185+ .getDifficulty ()[item->m_difficulty ] == difficulty) {
186+ m_filterList.append (item);
187+ }
188+ }
189+
190+ endResetModel ();
191+ }
192+
193+ void filterDuration (const QString &duration) {
194+ beginResetModel ();
195+ m_filter = QString (" duration:%1" ).arg (duration);
196+ m_filterList.clear ();
197+
198+ for (ListItemData* item : m_sortList) {
199+ if (StaticData ().getType ()[item->m_duration ] == duration) {
200+ m_filterList.append (item);
201+ }
202+ }
203+
204+ endResetModel ();
205+ }
206+
207+ void reFilter () {
208+ if (m_filter.startsWith (" class:" )) {
209+ QString const class_ = m_filter.mid (6 );
210+ filterClass (class_);
211+ } else if (m_filter.startsWith (" type:" )) {
212+ QString const type = m_filter.mid (5 );
213+ filterType (type);
214+ } else if (m_filter.startsWith (" difficulty:" )) {
215+ QString const difficulty = m_filter.mid (11 );
216+ filterDifficulty (difficulty);
217+ } else if (m_filter.startsWith (" duration:" )) {
218+ QString const duration = m_filter.mid (9 );
219+ filterDuration (duration);
220+ } else {
221+ beginResetModel ();
222+ m_filterList.clear ();
223+ for (ListItemData* item : m_sortList) {
224+ m_filterList.append (item);
225+ }
226+ endResetModel ();
227+ }
228+ }
229+
144230 private:
145- QVector<ListItemData> infoList;
146- QVector<ListItemData*> filterList;
147- QVector<ListItemData*> list;
148- QVector<ListItemData*> cache;
231+ QVector<ListItemData> m_mainList;
232+ QVector<ListItemData*> m_sortList;
233+ QVector<ListItemData*> m_filterList;
234+ QString m_filter;
235+ QString m_search;
236+ QVector<ListItemData*> m_cache;
149237 bool m_covers_loaded = false ;
150- QModelIndex a ;
151- QModelIndex b ;
238+ QModelIndex m_a ;
239+ QModelIndex m_b ;
152240 // QVector<ListOriginalData> originalInfoList;
153241 Controller& controller = Controller::getInstance();
154242};
@@ -160,18 +248,21 @@ class CardItemDelegate : public QStyledItemDelegate {
160248 void paint (QPainter *painter, const QStyleOptionViewItem &option,
161249 const QModelIndex &index) const override {
162250 painter->save ();
251+ const bool selected = option.state & QStyle::State_Selected;
163252
164253 // Card background
165254 painter->setRenderHint (QPainter::Antialiasing);
166255 QRectF cardRect = option.rect .adjusted (4 , 4 , -4 , -4 );
167- QColor bgColor = option. state & QStyle::State_Selected ? QColor (" #e0f7fa" ) : QColor (" #ffffff" );
256+ QColor bgColor = selected ? QColor (" #e0f7fa" ) : QColor (" #ffffff" );
168257 painter->setBrush (bgColor);
169258 painter->setPen (Qt::NoPen);
170259 painter->drawRoundedRect (cardRect, 10 , 10 );
171260
172261 // Picture space (left side)
173- QRect imageRect = QRect (cardRect.left () + 10 , cardRect.top () + 10 , 320 , 240 );
174- painter->setBrush (QColor (" #cccccc" )); // Placeholder color
262+ qint64 x = cardRect.left () + 10 ;
263+ qint64 y = cardRect.top () + 10 ;
264+ QRect imageRect = QRect (x, y, 320 , 240 );
265+ painter->setBrush (QColor (" #cccccc" ));
175266 QPixmap cover = index.data (Qt::UserRole + 7 ).value <QPixmap>();
176267 if (!cover.isNull ()) {
177268 painter->drawPixmap (imageRect, cover);
@@ -184,8 +275,14 @@ class CardItemDelegate : public QStyledItemDelegate {
184275 int textY = cardRect.top () + 10 ;
185276
186277 QString title = index.data (Qt::DisplayRole).toString ();
187- QString authors = index.data (Qt::UserRole + 1 ).toStringList ().join (" , " );
188- QString type = index.data (Qt::UserRole + 2 ).toString ();
278+ QStringList authorsList = index.data (Qt::UserRole + 1 ).toStringList ();
279+ QString authors = authorsList.join (" , " );
280+ if (authors.size () > 100 ) {
281+ authors = " Various" ;
282+ }
283+
284+ qint64 typeId = index.data (Qt::UserRole + 2 ).toInt ();
285+ QString type = StaticData ().getType ()[typeId];
189286 QString release = index.data (Qt::UserRole + 5 ).toString ();
190287
191288 QFont boldFont = option.font ;
@@ -201,12 +298,14 @@ class CardItemDelegate : public QStyledItemDelegate {
201298 painter->drawText (QPoint (textX, textY + 35 ), " By: " + authors);
202299 painter->drawText (QPoint (textX, textY + 50 ), " Type: " + type);
203300 painter->drawText (QPoint (textX, textY + 65 ), " Released: " + release);
301+ painter->drawText (QPoint (textX, textY + 65 ), " Released: " + release);
204302
205303 painter->restore ();
206304 }
207305
208- QSize sizeHint (const QStyleOptionViewItem &, const QModelIndex &) const override {
209- return QSize (600 , 300 ); // Adjust size as needed
306+ QSize sizeHint (
307+ const QStyleOptionViewItem&, const QModelIndex&) const override {
308+ return QSize (600 , 300 );
210309 }
211310};
212311
0 commit comments