Skip to content

Commit 1c619c5

Browse files
committed
improve ListItemData
It will lose some sharpness but remove the crusty edges from jpeg compression and alos center the cover if needed. Maybe I will just make them smaller instead of using smoothing. Im not sure it looks better with it, when the cover images gets less sharp.
1 parent 9a8181e commit 1c619c5

File tree

4 files changed

+111
-69
lines changed

4 files changed

+111
-69
lines changed

src/Data.hpp

Lines changed: 82 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@
1818
#include <QFileInfo>
1919
#include <QIcon>
2020
#include <QObject>
21+
#include <QPainter>
2122
#include <QPixmap>
23+
#include <QSize>
2224
#include <QSqlDatabase>
2325
#include <QSqlError>
2426
#include <QSqlQuery>
2527

2628
/**
2729
* @struct FolderNames
28-
* @brief Folder names game used on windows
29-
* @details These names are used by steam and installed in the common folder on linux.
30+
* @brief Folder names game used on Windows
31+
* These names are used by steam and installed in the common folder on Linux.
32+
* Except for `TombEngine (TEN)` I made that one up.
3033
*/
3134
struct FolderNames {
3235
const QString TR1 = "/Tomb Raider (I)";
3336
const QString TR2 = "/Tomb Raider (II)";
3437
const QString TR3 = "/TombRaider (III)";
3538
const QString TR4 = "/Tomb Raider (IV) The Last Revelation";
3639
const QString TR5 = "/Tomb Raider (V) Chronicles";
40+
const QString TEN = "TombEngine (TEN)";
3741
};
3842

3943
struct ZipData {
@@ -45,18 +49,10 @@ struct ZipData {
4549
*/
4650
ZipData() {}
4751
ZipData(
48-
QString zipName,
49-
float zipSize,
50-
QString md5sum,
51-
QString url,
52-
int version,
53-
const QString& release) :
54-
name(zipName),
55-
megabyteSize(zipSize),
56-
md5sum(md5sum),
57-
url(url),
58-
version(version),
59-
release(release) {}
52+
QString zipName, float zipSize, QString md5sum, QString url,
53+
int version, const QString& release) :
54+
name(zipName), megabyteSize(zipSize), md5sum(md5sum),
55+
url(url), version(version), release(release) {}
6056
QString name;
6157
float megabyteSize;
6258
QString md5sum;
@@ -65,51 +61,90 @@ struct ZipData {
6561
QString release;
6662
};
6763

64+
/**
65+
* @struct ListItemData
66+
* @brief Represents a Tomb Raider Level Entry Card.
67+
*
68+
* This struct is designed to store a single TRLE (Tomb Raider Level Editor) level record.
69+
* Each record contains metadata and a cover image displayed as a card in the application.
70+
* The struct includes properties to facilitate searching, filtering, and sorting.
71+
*/
6872
struct ListItemData {
6973
/**
70-
* @struct InfoData
71-
* @brief
72-
* @param
73-
* @details
74+
* @brief Default constructor for `ListItemData`.
75+
*
76+
* Initializes an empty instance of `ListItemData`.
7477
*/
7578
ListItemData() {}
79+
80+
/**
81+
* @brief Parameterized constructor for `ListItemData`.
82+
*
83+
* This constructor initializes a `ListItemData` object with metadata and a cover image.
84+
* The image is converted from raw `QByteArray` to a `QIcon` after scaling it to
85+
* fit within 640x480 dimensions. The scaling maintains the aspect ratio and
86+
* smooths out pixels using `Qt::SmoothTransformation`. The image is centered
87+
* within a transparent background if its aspect ratio does not perfectly match the target.
88+
*
89+
* @param title The TRLE title. Expected to contain a single name.
90+
* @param author The TRLE author(s). Can be a single name or multiple names separated by commas and spaces.
91+
* @param type The TRLE type, represented by a numeric ID.
92+
* @param classInput The TRLE class, represented by a numeric ID.
93+
* @param releaseDate The release date in the format "DD-MMM-YYYY" (e.g., "01-Jan-2000").
94+
* @param difficulty The TRLE difficulty, represented by a numeric ID.
95+
* @param duration The TRLE duration, represented by a numeric ID.
96+
* @param imageData The cover image as a `QByteArray`. Supported formats include JPEG, PNG, and WEBP.
97+
*/
7698
ListItemData(
77-
QString title,
78-
QString author,
79-
qint64 type,
80-
qint64 classIn,
81-
QString releaseDate,
82-
qint64 difficulty,
83-
qint64 duration,
99+
QString title, QString author, qint64 type, qint64 classInput,
100+
QString releaseDate, qint64 difficulty, qint64 duration,
84101
QByteArray imageData):
85-
title(title),
86-
author(author),
87-
type(type),
88-
class_(classIn),
89-
releaseDate(releaseDate),
90-
difficulty(difficulty),
91-
duration(duration) {
102+
m_title(title), m_author(author), m_type(type),
103+
m_class(classInput), m_releaseDate(releaseDate),
104+
m_difficulty(difficulty), m_duration(duration) {
105+
// Load the image from the byte array
92106
QPixmap pixmap;
93107
pixmap.loadFromData(imageData, "WEBP");
94-
picture.addPixmap(pixmap);
95-
// Scale the pixmap while maintaining aspect ratio
96-
QSize newSize = pixmap.size().scaled(640, 480, Qt::KeepAspectRatio);
97-
// Resize the pixmap to the scaled size
98-
pixmap = pixmap.scaled(
108+
109+
// Define target dimensions and maintain aspect ratio
110+
QSize targetSize(640, 480);
111+
QSize newSize = pixmap.size().scaled(targetSize, Qt::KeepAspectRatio);
112+
113+
// Scale the pixmap
114+
QPixmap scaledPixmap = pixmap.scaled(
99115
newSize,
100116
Qt::KeepAspectRatio,
101117
Qt::SmoothTransformation);
102-
// Create QIcon and add the scaled pixmap
103-
picture.addPixmap(pixmap);
118+
119+
// Create a centered pixmap with a transparent background
120+
QPixmap centeredPixmap(targetSize);
121+
// Ensure a transparent background
122+
centeredPixmap.fill(Qt::transparent);
123+
124+
// Calculate offsets for centering the scaled image
125+
qint64 xOffset = (targetSize.width() - newSize.width()) / 2;
126+
qint64 yOffset = (targetSize.height() - newSize.height()) / 2;
127+
128+
// Draw the scaled image onto the centered pixmap
129+
QPainter painter(&centeredPixmap);
130+
painter.setRenderHint(QPainter::Antialiasing, true);
131+
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
132+
painter.drawPixmap(xOffset, yOffset, scaledPixmap);
133+
painter.end();
134+
135+
// Store the resulting pixmap in a QIcon
136+
m_picture.addPixmap(centeredPixmap);
104137
}
105-
QString title;
106-
QString author;
107-
qint64 type;
108-
qint64 class_;
109-
QString releaseDate;
110-
qint64 difficulty;
111-
qint64 duration;
112-
QIcon picture;
138+
139+
// Data members
140+
QString m_title; ///< The TRLE level title.
141+
QString m_author; ///< The TRLE author(s), as a string.
142+
qint64 m_type; ///< ID of the type of level.
143+
qint64 m_class; ///< ID of the class of the level.
144+
QString m_releaseDate; ///< The release date in "DD-MMM-YYYY" format.
145+
qint64 m_difficulty; ///< ID of the difficulty of the level.
146+
qint64 m_duration; ///< ID of the estimated duration of the level.
147+
QIcon m_picture; ///< The cover image.
113148
};
114149

115150
/**

src/FileManager.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,29 +248,34 @@ bool FileManager::makeRelativeLink(
248248
const QString& levelDir,
249249
const QString& from,
250250
const QString& to) {
251-
const QString& levelPath = m_levelDir.absolutePath() + levelDir;
252-
const QString& fromPath = levelPath + from;
253-
const QString& toPath = levelPath + to;
251+
bool status = false;
252+
const QString levelPath = QString("%1%2")
253+
.arg(m_levelDir.absolutePath(), levelDir);
254+
const QString fromPath = QString("%1%2")
255+
.arg(levelPath, from);
256+
const QString toPath = QString("%1%2")
257+
.arg(levelPath, to);
254258

255259
if (QFile::link(fromPath, toPath)) {
256260
qDebug() << "Symbolic link created successfully.";
257-
return 0;
261+
status = true;
258262
} else {
259263
QFileInfo i(toPath);
260264
if (i.isSymLink()) {
261265
QFile::remove(toPath);
262266
if (QFile::link(fromPath, toPath)) {
263267
qDebug() << "Symbolic link created successfully.";
264-
return 0;
268+
status = true;
265269
} else {
266270
qDebug() << "Failed to create symbolic link.";
267-
return 1;
271+
status = false;
268272
}
269273
} else {
270274
qDebug() << "Failed to create symbolic link.";
271-
return 1;
275+
status = false;
272276
}
273277
}
278+
return status;
274279
}
275280

276281
qint64 FileManager::removeFileOrDirectory(

src/Model.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "Model.hpp"
1515

1616
// Those lambda should be in another header file
17+
// I hate this and it should be able to recognize both the directory
18+
// when linking and the game exe to make a symbolic link to automatically
1719
Model::Model(QObject *parent) : QObject(parent), checkCommonFilesIndex_m(1) {
1820
instructionManager.addInstruction(4, [this](int id) {
1921
qDebug() << "Perform Operation A";

src/TombRaiderLinuxLauncher.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,30 @@ void TombRaiderLinuxLauncher::generateList() {
195195
const qint64 s = list.size();
196196
for (qint64 i = 0; i < s; i++) {
197197
QString tag = QString("%1 by %2\n")
198-
.arg(list[i].title)
199-
.arg(list[i].author);
198+
.arg(list[i].m_title)
199+
.arg(list[i].m_author);
200200

201201
tag += QString(
202202
"Type: %1\nClass: %2\nDifficulty: %3\nDuration: %4\nDate:%5")
203-
.arg(mapType.at(list[i].type))
204-
.arg(mapClass.at(list[i].class_))
205-
.arg(mapDifficulty.at(list[i].difficulty))
206-
.arg(mapDuration.at(list[i].duration))
207-
.arg(list[i].releaseDate);
203+
.arg(mapType.at(list[i].m_type))
204+
.arg(mapClass.at(list[i].m_class))
205+
.arg(mapDifficulty.at(list[i].m_difficulty))
206+
.arg(mapDuration.at(list[i].m_duration))
207+
.arg(list[i].m_releaseDate);
208208

209209
QListWidgetItem *wi =
210-
new QListWidgetItem(list[i].picture, tag);
210+
new QListWidgetItem(list[i].m_picture, tag);
211211

212212

213213
wi->setData(Qt::UserRole, QVariant(i + 1));
214214
QVariantMap itemData;
215-
itemData["title"] = list[i].title;
216-
itemData["author"] = list[i].author;
217-
itemData["type"] = list[i].type;
218-
itemData["class_"] = list[i].class_;
219-
itemData["releaseDate"] = list[i].releaseDate;
220-
itemData["difficulty"] = list[i].difficulty;
221-
itemData["duration"] = list[i].duration;
215+
itemData["title"] = list[i].m_title;
216+
itemData["author"] = list[i].m_author;
217+
itemData["type"] = list[i].m_type;
218+
itemData["class_"] = list[i].m_class;
219+
itemData["releaseDate"] = list[i].m_releaseDate;
220+
itemData["difficulty"] = list[i].m_difficulty;
221+
itemData["duration"] = list[i].m_duration;
222222
// qDebug() << itemData << Qt::endl;
223223
wi->setData(Qt::UserRole + 1, itemData);
224224
ui->listWidgetModds->addItem(wi);

0 commit comments

Comments
 (0)