228 lines
7.4 KiB
C++
228 lines
7.4 KiB
C++
![]() |
#include "mainwindow.h"
|
||
|
#include "ui_mainwindow.h"
|
||
|
|
||
|
#include <QFile>
|
||
|
#include <QJsonDocument>
|
||
|
#include <QJsonObject>
|
||
|
#include <QJsonArray>
|
||
|
#include <QDir>
|
||
|
#include <QResizeEvent>
|
||
|
#include <QCloseEvent>
|
||
|
#include <QSettings>
|
||
|
#include <QDebug>
|
||
|
#include <QBrush>
|
||
|
|
||
|
MainWindow::MainWindow(QWidget *parent)
|
||
|
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
||
|
ui->setupUi(this);
|
||
|
initWindowSize();
|
||
|
initUI();
|
||
|
}
|
||
|
|
||
|
MainWindow::~MainWindow() {
|
||
|
delete ui;
|
||
|
}
|
||
|
|
||
|
void MainWindow::initWindowSize() {
|
||
|
settings = new QSettings("config.ini", QSettings::IniFormat);
|
||
|
width = settings->value("width", 400).toInt();
|
||
|
height = settings->value("height", 300).toInt();
|
||
|
resize(width, height);
|
||
|
}
|
||
|
|
||
|
void MainWindow::initUI() {
|
||
|
loadConfig();
|
||
|
|
||
|
if (!checkModsDirectory())
|
||
|
return;
|
||
|
|
||
|
initialOrder.clear();
|
||
|
loadData(modDir);
|
||
|
|
||
|
ui->listWidget->setDragDropMode(QListWidget::InternalMove);
|
||
|
ui->listWidget->setAlternatingRowColors(true);
|
||
|
|
||
|
connect(ui->saveButton, &QPushButton::clicked, this, &MainWindow::saveOrder);
|
||
|
}
|
||
|
|
||
|
void MainWindow::loadConfig() {
|
||
|
sptBaseDir = settings->value("sptBaseDir", "").toString();
|
||
|
addNewItemsTo = settings->value("addNewItemsTo", "end").toString();
|
||
|
reminder = settings->value("reminder", true).toBool();
|
||
|
}
|
||
|
|
||
|
bool MainWindow::checkModsDirectory() {
|
||
|
modDir = QDir::currentPath();
|
||
|
QDir modsDir(modDir + "/../user/mods");
|
||
|
if (!modsDir.exists()) {
|
||
|
QMessageBox::critical(this, "Error", "Could not find path '../user/mods'\nPlace the LoEDD folder in the spt-aki base directory: 'SPT-AKI/LoEDD!'\nCurrent Directory: " + modDir);
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void MainWindow::loadData(const QString &modDir) {
|
||
|
QFile orderFile(modDir + "/../user/mods/order.json");
|
||
|
QStringList orderedMods;
|
||
|
|
||
|
if (orderFile.exists()) {
|
||
|
orderFile.open(QIODevice::ReadOnly | QIODevice::Text);
|
||
|
QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll());
|
||
|
QJsonObject obj = doc.object();
|
||
|
orderedMods = obj["order"].toVariant().toStringList();
|
||
|
orderFile.close();
|
||
|
}
|
||
|
|
||
|
QDir modsDir(modDir + "/../user/mods");
|
||
|
QStringList modFolders;
|
||
|
QStringList validMods;
|
||
|
QHash<QString, QString> modTooltips;
|
||
|
|
||
|
// Clear the list widget before populating it
|
||
|
ui->listWidget->clear();
|
||
|
|
||
|
// List all folders in the mods directory and verify they have a package.json
|
||
|
foreach (QFileInfo entry, modsDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
|
||
|
QString folderName = entry.fileName();
|
||
|
QString packageFilePath = entry.filePath() + "/package.json";
|
||
|
QFile packageFile(packageFilePath);
|
||
|
|
||
|
if (packageFile.exists()) {
|
||
|
qDebug() << "Found package.json for mod:" << folderName;
|
||
|
packageFile.open(QIODevice::ReadOnly | QIODevice::Text);
|
||
|
QJsonDocument doc = QJsonDocument::fromJson(packageFile.readAll());
|
||
|
QJsonObject obj = doc.object();
|
||
|
QString modName = obj["name"].toString();
|
||
|
QString version = obj["version"].toString();
|
||
|
QString author = obj["author"].toString();
|
||
|
QString akiVersion = obj["akiVersion"].toString();
|
||
|
QString tooltipText = QString("Name: %1\nFolder Name: %2\nVersion: %3\nAuthor: %4\nakiVersion: %5")
|
||
|
.arg(modName).arg(folderName).arg(version).arg(author).arg(akiVersion);
|
||
|
qDebug() << "Mod Info:" << modName << version << author << akiVersion;
|
||
|
|
||
|
modFolders.append(folderName);
|
||
|
validMods.append(folderName);
|
||
|
modTooltips.insert(folderName, tooltipText);
|
||
|
packageFile.close();
|
||
|
} else {
|
||
|
qDebug() << "No package.json found for mod folder:" << folderName;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create a list to keep track of mods that have been added to the final list
|
||
|
QStringList modsAdded;
|
||
|
|
||
|
// Temporary list to hold the reordered mods
|
||
|
QStringList finalModList;
|
||
|
|
||
|
// Reorder listWidget items based on order.json
|
||
|
foreach (QString modName, orderedMods) {
|
||
|
if (validMods.contains(modName)) {
|
||
|
finalModList.append(modName);
|
||
|
modsAdded.append(modName);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Sort remaining mods that were not in the order.json alphabetically
|
||
|
QStringList remainingMods;
|
||
|
foreach (QString modName, validMods) {
|
||
|
if (!modsAdded.contains(modName)) {
|
||
|
remainingMods.append(modName);
|
||
|
}
|
||
|
}
|
||
|
remainingMods.sort(Qt::CaseInsensitive);
|
||
|
|
||
|
// Add remaining mods that were not in the order.json to the beginning of the list
|
||
|
finalModList = remainingMods + finalModList;
|
||
|
|
||
|
// Add the final ordered mods to the list widget
|
||
|
for (int i = 0; i < finalModList.size(); ++i) {
|
||
|
QString modName = finalModList[i];
|
||
|
QString tooltipText = modTooltips.value(modName);
|
||
|
bool isNew = remainingMods.contains(modName);
|
||
|
addListItem(modName, tooltipText, -1, isNew);
|
||
|
}
|
||
|
|
||
|
initialOrder = QStringList();
|
||
|
for (int i = 0; i < ui->listWidget->count(); ++i) {
|
||
|
initialOrder.append(ui->listWidget->item(i)->text());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MainWindow::addListItem(const QString &modName, const QString &tooltipText, int position, bool isNew) {
|
||
|
QListWidgetItem *listItem = new QListWidgetItem(modName);
|
||
|
listItem->setToolTip(tooltipText);
|
||
|
|
||
|
// Set background color for new items
|
||
|
if (isNew) {
|
||
|
listItem->setBackground(QBrush(QColor(255, 255, 200))); // Light yellow
|
||
|
}
|
||
|
|
||
|
// Set position indicator
|
||
|
if (position == -1) {
|
||
|
position = ui->listWidget->count();
|
||
|
}
|
||
|
QString itemText = QString("%1. %2").arg(position + 1).arg(modName);
|
||
|
listItem->setText(itemText);
|
||
|
|
||
|
if (position == -1) {
|
||
|
ui->listWidget->addItem(listItem);
|
||
|
} else {
|
||
|
ui->listWidget->insertItem(position, listItem);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MainWindow::saveOrder() {
|
||
|
QStringList newOrder;
|
||
|
for (int i = 0; i < ui->listWidget->count(); ++i) {
|
||
|
newOrder.append(ui->listWidget->item(i)->text());
|
||
|
}
|
||
|
|
||
|
QString orderFilePath = modDir + "/../user/mods/order.json";
|
||
|
QString backupFilePath = modDir + "/../user/mods/order.bkp";
|
||
|
|
||
|
if (QFile::exists(orderFilePath)) {
|
||
|
QFile::copy(orderFilePath, backupFilePath);
|
||
|
}
|
||
|
|
||
|
QFile orderFile(orderFilePath);
|
||
|
if (orderFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||
|
QJsonObject obj;
|
||
|
obj["order"] = QJsonArray::fromStringList(newOrder);
|
||
|
QJsonDocument doc(obj);
|
||
|
orderFile.write(doc.toJson());
|
||
|
orderFile.close();
|
||
|
}
|
||
|
|
||
|
initialOrder = newOrder;
|
||
|
|
||
|
settings->setValue("width", width);
|
||
|
settings->setValue("height", height);
|
||
|
}
|
||
|
|
||
|
void MainWindow::resizeEvent(QResizeEvent *event) {
|
||
|
QMainWindow::resizeEvent(event);
|
||
|
width = size().width();
|
||
|
height = size().height();
|
||
|
}
|
||
|
|
||
|
void MainWindow::closeEvent(QCloseEvent *event) {
|
||
|
if (reminder && ui->listWidget->count() > 0) {
|
||
|
QStringList currentOrder;
|
||
|
for (int i = 0; i < ui->listWidget->count(); ++i) {
|
||
|
currentOrder.append(ui->listWidget->item(i)->text());
|
||
|
}
|
||
|
if (currentOrder != initialOrder) {
|
||
|
QMessageBox::StandardButton reply;
|
||
|
reply = QMessageBox::question(this, "Reminder", "You have unsaved changes.\nSave before closing?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
|
||
|
if (reply == QMessageBox::Yes) {
|
||
|
saveOrder();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
settings->setValue("width", width);
|
||
|
settings->setValue("height", height);
|
||
|
event->accept();
|
||
|
}
|