Skip to content

Commit

Permalink
improve InfoData struct
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Dec 15, 2024
1 parent af0c5f6 commit a959461
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
83 changes: 59 additions & 24 deletions src/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
#ifndef SRC_DATA_HPP_
#define SRC_DATA_HPP_

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include <QFileInfo>
#include <QIcon>
#include <QObject>
#include <QPixmap>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

/**
* @struct FolderNames
Expand Down Expand Up @@ -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<QByteArray> 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<QByteArray>` 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<QByteArray>& 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<QIcon> imageList;

QString m_body; ///< The textual content associated with this object.
QVector<QIcon> m_imageList; ///< A list of icons generated from image data.
};

class Data : public QObject {
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/TombRaiderLinuxLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#ifdef TEST
/**
*
* The main function used for console tests
*/
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a959461

Please sign in to comment.