Skip to content

Commit 98f8979

Browse files
committed
sort and clean up
1 parent e2096b6 commit 98f8979

File tree

4 files changed

+189
-306
lines changed

4 files changed

+189
-306
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ set(SOURCES_MC
8787
src/Runner.hpp
8888
src/binary.hpp
8989
src/binary.cpp
90+
src/levelViewList.hpp
9091
src/main.cpp
9192
src/staticData.hpp
9293
)

src/TombRaiderLinuxLauncher.cpp

Lines changed: 12 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ TombRaiderLinuxLauncher::TombRaiderLinuxLauncher(QWidget *parent)
114114

115115
model = new LevelListModel(this);
116116
ui->listViewLevels->setModel(model);
117-
ui->listViewLevels->setItemDelegate(new CardItemDelegate(ui->listViewLevels));
117+
CardItemDelegate* delegate = new CardItemDelegate(ui->listViewLevels);
118+
ui->listViewLevels->setItemDelegate(delegate);
118119
ui->listViewLevels->setSpacing(8);
119120
connect(
120121
ui->listViewLevels->selectionModel(),
@@ -133,217 +134,35 @@ TombRaiderLinuxLauncher::TombRaiderLinuxLauncher(QWidget *parent)
133134

134135
void TombRaiderLinuxLauncher::generateList(const QList<int>& availableGames) {
135136
model->setLevels();
136-
/*
137-
ui->listViewLevels->clear();
138-
const QString pictures = ":/pictures/";
139-
OriginalGameData pictueData;
140-
141-
foreach(const int &id, availableGames) {
142-
int IdPositive;
143-
bool linkedGameDir;
144-
if (id < 0) {
145-
linkedGameDir = false;
146-
IdPositive = id*(-1);
147-
} else {
148-
linkedGameDir = true;
149-
IdPositive = id;
150-
}
151-
152-
// Picture and title
153-
QString iconPath = pictures + pictueData.getPicture(IdPositive);
154-
QString itemName =
155-
QString("Tomb Raider %1 Original")
156-
.arg(pictueData.romanNumerals[IdPositive]);
157-
158-
qDebug() << "iconPath :" << iconPath;
159-
qDebug() << "itemName :" << itemName;
160-
QListWidgetItem *wi = new QListWidgetItem(QIcon(iconPath), itemName);
161-
wi->setData(Qt::UserRole, QVariant(IdPositive*(-1)));
162-
wi->setData(Qt::UserRole + 1, QVariant(linkedGameDir));
163-
ui->listViewLevels->addItem(wi);
164-
originalGamesSet_m.insert(wi);
165-
originalGamesList_m.append(wi);
166-
}
167-
168-
StaticData staticData;
169-
auto mapType = staticData.getType();
170-
auto mapClass = staticData.getClass();
171-
auto mapDifficulty = staticData.getDifficulty();
172-
auto mapDuration = staticData.getDuration();
173-
174-
const qint64 s = list.size();
175-
for (qint64 i = 0; i < s; i++) {
176-
QString tag = QString("%1 by %2\n")
177-
.arg(list[i].m_title)
178-
.arg(list[i].m_author);
179-
180-
// list[i].m_type
181-
// list[i].m_class
182-
// list[i].m_difficulty
183-
// list[i].m_duration
184-
185-
tag += QString(
186-
"Type: %1\nClass: %2\nDifficulty: %3\nDuration: %4\nDate:%5")
187-
.arg(mapType.at(list[i].m_type))
188-
.arg(mapClass.at(list[i].m_class))
189-
.arg(mapDifficulty.at(list[i].m_difficulty))
190-
.arg(mapDuration.at(list[i].m_duration))
191-
.arg(list[i].m_releaseDate);
192-
193-
QListWidgetItem *wi =
194-
new QListWidgetItem(list[i].m_picture, tag);
195-
196-
wi->setData(Qt::UserRole, QVariant(list[i].m_id));
197-
QVariantMap itemData;
198-
itemData["title"] = list[i].m_title;
199-
itemData["author"] = list[i].m_author;
200-
itemData["type"] = list[i].m_type;
201-
itemData["class_"] = list[i].m_class;
202-
itemData["releaseDate"] = list[i].m_releaseDate;
203-
itemData["difficulty"] = list[i].m_difficulty;
204-
itemData["duration"] = list[i].m_duration;
205-
// qDebug() << itemData << Qt::endl;
206-
wi->setData(Qt::UserRole + 1, itemData);
207-
ui->listViewLevels->addItem(wi);
208-
}
209-
sortByTitle();
210-
*/
211-
}
212-
213-
void TombRaiderLinuxLauncher::sortItems(
214-
std::function<bool(QListWidgetItem*, QListWidgetItem*)> compare) {
215-
QList<QListWidgetItem*> items;
216-
217-
// Step 1: Extract items from the QListWidget
218-
int count = 0;
219-
if (ui->listViewLevels->model()) {
220-
count = ui->listViewLevels->model()->rowCount();
221-
}
222-
if (ui->checkBoxOriginalFirst->isChecked() == true) {
223-
for (int i = 0; i < count; ++i) {
224-
/*
225-
QListWidgetItem* item = ui->listViewLevels->item(i);
226-
// Append the item only if it's not in originalGamesSet_m
227-
if (!originalGamesSet_m.contains(item)) {
228-
items.append(item);
229-
}
230-
*/
231-
}
232-
} else {
233-
for (int i = 0; i < count; ++i) {
234-
// items.append(ui->listViewLevels->item(i));
235-
}
236-
}
237-
238-
// Step 2: Sort the items using the provided comparison lambda
239-
std::sort(items.begin(), items.end(), compare);
240-
241-
// Step 3: Adjust the position of each item without clearing the list
242-
qint64 originalGamesSize =
243-
ui->checkBoxOriginalFirst->isChecked() ? originalGamesList_m.size() : 0;
244-
245-
qint64 size = originalGamesSize + items.size();
246-
for (qint64 i = 0; i < size; ++i) {
247-
if (i < originalGamesSize) {
248-
// Handle originalGamesList_m items
249-
/*
250-
item = ui->listViewLevels->takeItem(
251-
ui->listViewLevels->row(originalGamesList_m[i]));
252-
ui->listViewLevels->insertItem(i, item);
253-
*/
254-
} else {
255-
// Handle items list
256-
/*
257-
item = ui->listViewLevels->takeItem(
258-
ui->listViewLevels->row(items[i - originalGamesSize]));
259-
ui->listViewLevels->insertItem(i, item);
260-
*/
261-
}
262-
}
263-
}
264-
265-
bool compareTitles(QListWidgetItem* a, QListWidgetItem* b) {
266-
const QVariantMap aData = a->data(Qt::UserRole + 1).toMap();
267-
const QVariantMap bData = b->data(Qt::UserRole + 1).toMap();
268-
269-
return aData.value("title").toString().toLower()
270-
< bData.value("title").toString().toLower();
271-
}
272-
273-
bool compareAuthors(QListWidgetItem* a, QListWidgetItem* b) {
274-
const QVariantMap aData = a->data(Qt::UserRole + 1).toMap();
275-
const QVariantMap bData = b->data(Qt::UserRole + 1).toMap();
276-
return aData.value("author").toString().toLower()
277-
< bData.value("author").toString().toLower();
278-
}
279-
280-
bool compareDifficulties(QListWidgetItem* a, QListWidgetItem* b) {
281-
const QVariantMap aData = a->data(Qt::UserRole + 1).toMap();
282-
const QVariantMap bData = b->data(Qt::UserRole + 1).toMap();
283-
return aData.value("difficulty").toInt()
284-
> bData.value("difficulty").toInt();
285-
}
286-
287-
bool compareDurations(QListWidgetItem* a, QListWidgetItem* b) {
288-
const QVariantMap aData = a->data(Qt::UserRole + 1).toMap();
289-
const QVariantMap bData = b->data(Qt::UserRole + 1).toMap();
290-
return aData.value("duration").toInt()
291-
> bData.value("duration").toInt();
292-
}
293-
294-
bool compareClasses(QListWidgetItem* a, QListWidgetItem* b) {
295-
const QVariantMap aData = a->data(Qt::UserRole + 1).toMap();
296-
const QVariantMap bData = b->data(Qt::UserRole + 1).toMap();
297-
return aData.value("class_").toInt()
298-
> bData.value("class_").toInt();
299-
}
300-
301-
bool compareTypes(QListWidgetItem* a, QListWidgetItem* b) {
302-
const QVariantMap aData = a->data(Qt::UserRole + 1).toMap();
303-
const QVariantMap bData = b->data(Qt::UserRole + 1).toMap();
304-
return aData.value("type").toInt()
305-
> bData.value("type").toInt();
306-
}
307-
308-
bool compareReleaseDates(QListWidgetItem* a, QListWidgetItem* b) {
309-
const QVariantMap aData = a->data(Qt::UserRole + 1).toMap();
310-
const QVariantMap bData = b->data(Qt::UserRole + 1).toMap();
311-
312-
const QDate dateA = QDate::fromString(
313-
aData.value("releaseDate").toString(), "dd-MMM-yyyy");
314-
const QDate dateB = QDate::fromString(
315-
bData.value("releaseDate").toString(), "dd-MMM-yyyy");
316-
317-
return dateA > dateB; // descending order
318137
}
319138

320139

321140
void TombRaiderLinuxLauncher::sortByTitle() {
322-
sortItems(compareTitles);
141+
model->sortItems(model->compareTitles);
323142
}
324143

325144
void TombRaiderLinuxLauncher::sortByAuthor() {
326-
sortItems(compareAuthors);
145+
// sortItems(compareAuthors);
327146
}
328147

329148
void TombRaiderLinuxLauncher::sortByDifficulty() {
330-
sortItems(compareDifficulties);
149+
model->sortItems(model->compareDifficulties);
331150
}
332151

333152
void TombRaiderLinuxLauncher::sortByDuration() {
334-
sortItems(compareDurations);
153+
model->sortItems(model->compareDurations);
335154
}
336155

337156
void TombRaiderLinuxLauncher::sortByClass() {
338-
sortItems(compareClasses);
157+
model->sortItems(model->compareClasses);
339158
}
340159

341160
void TombRaiderLinuxLauncher::sortByType() {
342-
sortItems(compareTypes);
161+
model->sortItems(model->compareTypes);
343162
}
344163

345164
void TombRaiderLinuxLauncher::sortByReleaseDate() {
346-
sortItems(compareReleaseDates);
165+
model->sortItems(model->compareReleaseDates);
347166
}
348167

349168
void TombRaiderLinuxLauncher::readSavedSettings() {
@@ -558,8 +377,9 @@ void TombRaiderLinuxLauncher::walkthroughClicked() {
558377
if (current.isValid()) {
559378
qint64 id = model->getLid(current);
560379
if (id != 0) {
561-
ui->walkthroughWebEngineView->setHtml(controller.getWalkthrough(id));
562-
ui->walkthroughWebEngineView->show();
380+
QWebEngineView* w = ui->walkthroughWebEngineView;
381+
w->setHtml(controller.getWalkthrough(id));
382+
w->show();
563383
ui->stackedWidget->setCurrentWidget(
564384
ui->stackedWidget->findChild<QWidget*>("walkthrough"));
565385
}

src/TombRaiderLinuxLauncher.hpp

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -20,130 +20,18 @@
2020
#include <QScrollArea>
2121
#include <QVBoxLayout>
2222
#include <QStringList>
23-
#include <QListWidgetItem>
24-
#include <QStyledItemDelegate>
2523
#include <QSet>
2624
#include <QDebug>
2725
#include <QVector>
2826
#include <QString>
2927

3028
#include "../src/Controller.hpp"
29+
#include "../src/levelViewList.hpp"
3130

3231
QT_BEGIN_NAMESPACE
3332
namespace Ui { class TombRaiderLinuxLauncher; }
3433
QT_END_NAMESPACE
3534

36-
class LevelListModel : public QAbstractListModel {
37-
Q_OBJECT
38-
39-
public:
40-
explicit LevelListModel(QObject *parent): QAbstractListModel(parent) {}
41-
42-
void setLevels() {
43-
beginResetModel();
44-
infoList.clear();
45-
controller.getList(&infoList);
46-
endResetModel();
47-
}
48-
49-
int rowCount(const QModelIndex &parent = QModelIndex()) const override {
50-
Q_UNUSED(parent);
51-
return infoList.count();
52-
}
53-
54-
QVariant data(const QModelIndex &index, int role) const override {
55-
QVariant result;
56-
int row = index.row();
57-
58-
if (index.isValid() && row < infoList.size()) {
59-
const ListItemData &item = infoList.at(row);
60-
switch (role) {
61-
case Qt::DisplayRole:
62-
return item.m_title;
63-
case Qt::UserRole + 1:
64-
return item.m_authors;
65-
case Qt::UserRole + 2:
66-
return item.m_type;
67-
case Qt::UserRole + 3:
68-
return item.m_class;
69-
case Qt::UserRole + 4:
70-
return item.m_difficulty;
71-
case Qt::UserRole + 5:
72-
return item.m_releaseDate;
73-
case Qt::UserRole + 6:
74-
return item.m_duration;
75-
default:
76-
return QVariant();
77-
}
78-
}
79-
80-
return result;
81-
}
82-
83-
int getLid(const QModelIndex &index) const {
84-
const ListItemData &item = infoList.at(index.row());
85-
return item.m_trle_id;
86-
}
87-
88-
private:
89-
Controller& controller = Controller::getInstance();
90-
QVector<ListItemData> infoList;
91-
QVector<ListItemPicture> pictureList;
92-
};
93-
94-
class CardItemDelegate : public QStyledItemDelegate {
95-
public:
96-
using QStyledItemDelegate::QStyledItemDelegate;
97-
98-
void paint(QPainter *painter, const QStyleOptionViewItem &option,
99-
const QModelIndex &index) const override {
100-
painter->save();
101-
102-
// Card background
103-
painter->setRenderHint(QPainter::Antialiasing);
104-
QRectF cardRect = option.rect.adjusted(4, 4, -4, -4);
105-
QColor bgColor = option.state & QStyle::State_Selected ? QColor("#e0f7fa") : QColor("#ffffff");
106-
painter->setBrush(bgColor);
107-
painter->setPen(Qt::NoPen);
108-
painter->drawRoundedRect(cardRect, 10, 10);
109-
110-
// Picture space (left side)
111-
QRect imageRect = QRect(cardRect.left() + 10, cardRect.top() + 10, 80, 60);
112-
painter->setBrush(QColor("#cccccc")); // Placeholder color
113-
painter->drawRect(imageRect);
114-
115-
// Text section (right side of image)
116-
int textX = imageRect.right() + 10;
117-
int textY = cardRect.top() + 10;
118-
119-
QString title = index.data(Qt::DisplayRole).toString();
120-
QString authors = index.data(Qt::UserRole + 1).toStringList().join(", ");
121-
QString type = index.data(Qt::UserRole + 2).toString();
122-
QString release = index.data(Qt::UserRole + 5).toString();
123-
124-
QFont boldFont = option.font;
125-
boldFont.setBold(true);
126-
painter->setFont(boldFont);
127-
painter->setPen(Qt::black);
128-
painter->drawText(QPoint(textX, textY + 15), title);
129-
130-
QFont normalFont = option.font;
131-
normalFont.setPointSizeF(option.font.pointSizeF() - 1);
132-
painter->setFont(normalFont);
133-
134-
painter->drawText(QPoint(textX, textY + 35), "By: " + authors);
135-
painter->drawText(QPoint(textX, textY + 50), "Type: " + type);
136-
painter->drawText(QPoint(textX, textY + 65), "Released: " + release);
137-
138-
painter->restore();
139-
}
140-
141-
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override {
142-
return QSize(300, 80); // Adjust size as needed
143-
}
144-
};
145-
146-
14735
/**
14836
* View component in the MVC pattern; Main UI class for the launcher.
14937
*/
@@ -185,7 +73,8 @@ class TombRaiderLinuxLauncher : public QMainWindow {
18573
/**
18674
* Updates button states based on selected menu level.
18775
*/
188-
void onCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous);
76+
void onCurrentItemChanged(
77+
const QModelIndex &current, const QModelIndex &previous);
18978
/**
19079
* Updates progress by 1% of total work steps.
19180
*/

0 commit comments

Comments
 (0)