2024-05-25 02:56:12 +02:00
# 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>
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' \n Place the LoEDD folder in the spt-aki base directory: 'SPT-AKI/LoEDD!' \n Current 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 \n Folder Name: %2 \n Version: %3 \n Author: %4 \n akiVersion: %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 ) ;
2024-05-25 03:17:14 +02:00
// Add remaining mods that were not in the order.json
finalModList . append ( remainingMods ) ;
2024-05-25 02:56:12 +02:00
// Add the final ordered mods to the list widget
2024-05-25 03:17:14 +02:00
foreach ( QString modName , finalModList ) {
2024-05-25 02:56:12 +02:00
QString tooltipText = modTooltips . value ( modName ) ;
2024-05-25 03:17:14 +02:00
addListItem ( modName , tooltipText ) ;
2024-05-25 02:56:12 +02:00
}
initialOrder = QStringList ( ) ;
for ( int i = 0 ; i < ui - > listWidget - > count ( ) ; + + i ) {
initialOrder . append ( ui - > listWidget - > item ( i ) - > text ( ) ) ;
}
}
2024-05-25 03:17:14 +02:00
void MainWindow : : addListItem ( const QString & modName , const QString & tooltipText , int position ) {
2024-05-25 02:56:12 +02:00
QListWidgetItem * listItem = new QListWidgetItem ( modName ) ;
listItem - > setToolTip ( tooltipText ) ;
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. \n Save before closing? " , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : Yes ) ;
if ( reply = = QMessageBox : : Yes ) {
saveOrder ( ) ;
}
}
}
settings - > setValue ( " width " , width ) ;
settings - > setValue ( " height " , height ) ;
event - > accept ( ) ;
}