From a9594618443ccee3a35ab3293b6c672556e8c4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20B=C3=A5ngens?= Date: Sun, 15 Dec 2024 02:18:02 +0100 Subject: [PATCH] improve InfoData struct --- src/Data.hpp | 83 +++++++++++++++++++++++---------- src/TombRaiderLinuxLauncher.cpp | 6 +-- src/binary.hpp | 6 +-- src/main.cpp | 5 +- 4 files changed, 68 insertions(+), 32 deletions(-) diff --git a/src/Data.hpp b/src/Data.hpp index ea7390a..7db7494 100644 --- a/src/Data.hpp +++ b/src/Data.hpp @@ -14,13 +14,14 @@ #ifndef SRC_DATA_HPP_ #define SRC_DATA_HPP_ -#include -#include -#include #include -#include +#include #include +#include #include +#include +#include +#include /** * @struct FolderNames @@ -112,25 +113,48 @@ struct ListItemData { QIcon picture; }; +/** + * @struct InfoData + * @brief Store HTML data and a list of icons generated from image WEBP data. + * + * This struct is designed to store a body of HTML and convert a list of image data + * (provided as `QByteArray`) into `QIcon` objects. The `QIcon` objects can then + * be used in Qt-based applications with QtWebEngine and QIcons in a split view manner. + */ struct InfoData { /** - * @struct InfoData - * @brief - * @param - * @details + * @brief Default constructor for `InfoData`. + * + * Initializes an empty body and an empty list of icons. */ InfoData() {} - InfoData(QString body, QVector imageList) : body(body) { - for (const QByteArray &image : imageList) { + + /** + * @brief Constructs an `InfoData` object with the given body and image list. + * + * Converts each image in the provided `QVector` to a `QIcon` object + * using the "WEBP" format and stores them in the icon list. + * + * @param body A string representing the main textual content. + * @param imageList A vector of image data in `QByteArray` format. + */ + InfoData(const QString& body, const QVector& imageList) + : m_body(body) { + for (const QByteArray& image : imageList) { QPixmap pixmap; - QIcon final; - pixmap.loadFromData(image, "WEBP"); - final.addPixmap(pixmap); - this->imageList.push_back(final); + QIcon finalIcon; + + // Load image data into a QPixmap and convert it to a QIcon + if (pixmap.loadFromData(image, "WEBP")) { + finalIcon.addPixmap(pixmap); + } + + m_imageList.push_back(finalIcon); } } - QString body; - QVector imageList; + + QString m_body; ///< The textual content associated with this object. + QVector m_imageList; ///< A list of icons generated from image data. }; class Data : public QObject { @@ -142,17 +166,28 @@ class Data : public QObject { return instance; } - bool initializeDatabase(QString path) { - db = QSqlDatabase::addDatabase("QSQLITE"); - db.setDatabaseName(path + "/tombll.db"); - db.setConnectOptions("QSQLITE_OPEN_READONLY"); + bool initializeDatabase(const QString& path) { + bool status = false; + const QString filePath = path + "/tombll.db"; + QFileInfo fileInfo(filePath); - if (db.open()) { - return true; + // Open the file + if (!fileInfo.exists() || !fileInfo.isFile()) { + qCritical() + << "Error: The database path is not a regular file: " << path; + status = false; } else { - qDebug() << "Error opening database:" << db.lastError().text(); - return false; + db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(path + "/tombll.db"); + db.setConnectOptions("QSQLITE_OPEN_READONLY"); + if (db.open()) { // flawfinder: ignore + status = true; + } else { + qDebug() << "Error opening database:" << db.lastError().text(); + status = false; + } } + return status; } void releaseDatabase() { diff --git a/src/TombRaiderLinuxLauncher.cpp b/src/TombRaiderLinuxLauncher.cpp index 393022f..124e4a1 100644 --- a/src/TombRaiderLinuxLauncher.cpp +++ b/src/TombRaiderLinuxLauncher.cpp @@ -449,7 +449,7 @@ void TombRaiderLinuxLauncher::infoClicked() { int id = selectedItem->data(Qt::UserRole).toInt(); if (id) { InfoData info = controller.getInfo(id); - ui->infoWebEngineView->setHtml(info.body); + ui->infoWebEngineView->setHtml(info.m_body); ui->infoListWidget->setViewMode(QListView::IconMode); ui->infoListWidget->setIconSize(QSize(502, 377)); ui->infoListWidget->setDragEnabled(false); @@ -458,8 +458,8 @@ void TombRaiderLinuxLauncher::infoClicked() { ui->infoListWidget->setDefaultDropAction(Qt::IgnoreAction); ui->infoListWidget->setSelectionMode(QAbstractItemView::NoSelection); ui->infoListWidget->clear(); - for (int i = 0; i < info.imageList.size(); ++i) { - const QIcon &icon = info.imageList.at(i); + for (int i = 0; i < info.m_imageList.size(); ++i) { + const QIcon &icon = info.m_imageList.at(i); QListWidgetItem *item = new QListWidgetItem(icon, ""); ui->infoListWidget->addItem(item); } diff --git a/src/binary.hpp b/src/binary.hpp index 47b7ff2..8930ef3 100644 --- a/src/binary.hpp +++ b/src/binary.hpp @@ -70,17 +70,17 @@ qint64 findReplacePattern(QFile* const file) { * @return error qint64. */ qint64 widescreen_set(const QString& path) { - qint64 status = 0; // Initialize the status variable + qint64 status = 0; QFileInfo fileInfo(path); QFile file(path); // Open the file if (!fileInfo.exists() || !fileInfo.isFile()) { qCritical() << "Error: The exe path is not a regular file: " << path; - return 1; // Invalid file path + status = 1; // Invalid file path } else if (!file.open(QIODevice::ReadOnly)) { // flawfinder: ignore qCritical() << "Error opening file for reading!"; - return 2; // File open error + status = 2; // File open error } else { // Perform the pattern replacement and propagate the status status = findReplacePattern(&file); diff --git a/src/main.cpp b/src/main.cpp index 168af79..2b2331b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,7 +24,7 @@ #ifdef TEST /** - * + * The main function used for console tests */ int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); @@ -56,7 +56,8 @@ int main(int argc, char *argv[]) { } #else /** - * + * The main function used for the regular qt app. + * Takes care of command line arguments and create the window. */ int main(int argc, char *argv[]) { QApplication a(argc, argv);