Added 'Reset to Default' button in GUI

This commit is contained in:
kalaposfos13 2024-11-27 13:04:15 +01:00
parent 09b66800da
commit 608601d9b7
2 changed files with 36 additions and 3 deletions

View File

@ -4,6 +4,7 @@
#include "kbm_config_dialog.h" #include "kbm_config_dialog.h"
#include "kbm_help_dialog.h" #include "kbm_help_dialog.h"
#include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "common/config.h" #include "common/config.h"
@ -53,6 +54,7 @@ EditorDialog::EditorDialog(QWidget* parent) : QDialog(parent) {
QPushButton* saveButton = new QPushButton("Save", this); QPushButton* saveButton = new QPushButton("Save", this);
QPushButton* cancelButton = new QPushButton("Cancel", this); QPushButton* cancelButton = new QPushButton("Cancel", this);
QPushButton* helpButton = new QPushButton("Help", this); QPushButton* helpButton = new QPushButton("Help", this);
QPushButton* defaultButton = new QPushButton("Default", this);
// Layout for the game selection and buttons // Layout for the game selection and buttons
QHBoxLayout* topLayout = new QHBoxLayout(); QHBoxLayout* topLayout = new QHBoxLayout();
@ -60,6 +62,7 @@ EditorDialog::EditorDialog(QWidget* parent) : QDialog(parent) {
topLayout->addStretch(); topLayout->addStretch();
topLayout->addWidget(saveButton); topLayout->addWidget(saveButton);
topLayout->addWidget(cancelButton); topLayout->addWidget(cancelButton);
topLayout->addWidget(defaultButton);
topLayout->addWidget(helpButton); topLayout->addWidget(helpButton);
// Main layout with editor and buttons // Main layout with editor and buttons
@ -74,6 +77,7 @@ EditorDialog::EditorDialog(QWidget* parent) : QDialog(parent) {
connect(saveButton, &QPushButton::clicked, this, &EditorDialog::onSaveClicked); connect(saveButton, &QPushButton::clicked, this, &EditorDialog::onSaveClicked);
connect(cancelButton, &QPushButton::clicked, this, &EditorDialog::onCancelClicked); connect(cancelButton, &QPushButton::clicked, this, &EditorDialog::onCancelClicked);
connect(helpButton, &QPushButton::clicked, this, &EditorDialog::onHelpClicked); connect(helpButton, &QPushButton::clicked, this, &EditorDialog::onHelpClicked);
connect(defaultButton, &QPushButton::clicked, this, &EditorDialog::onResetToDefaultClicked);
connect(gameComboBox, &QComboBox::currentTextChanged, this, connect(gameComboBox, &QComboBox::currentTextChanged, this,
&EditorDialog::onGameSelectionChanged); &EditorDialog::onGameSelectionChanged);
} }
@ -179,6 +183,34 @@ void EditorDialog::onHelpClicked() {
} }
} }
void EditorDialog::onResetToDefaultClicked() {
bool default_default = gameComboBox->currentText() == "default";
QString prompt =
default_default
? "Do you want to reset your custom default config to the original default config?"
: "Do you want to reset this config to your custom default config?";
QMessageBox::StandardButton reply =
QMessageBox::question(this, "Reset to Default", prompt, QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
if (default_default) {
const auto default_file = Config::GetFoolproofKbmConfigFile("default");
std::filesystem::remove(default_file);
}
const auto config_file = Config::GetFoolproofKbmConfigFile("default");
QFile file(config_file);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
editor->setPlainText(in.readAll());
file.close();
} else {
QMessageBox::warning(this, "Error", "Could not open the file for reading");
}
// saveFile(gameComboBox->currentText());
}
}
bool EditorDialog::hasUnsavedChanges() { bool EditorDialog::hasUnsavedChanges() {
// Compare the current content with the original content to check if there are unsaved changes // Compare the current content with the original content to check if there are unsaved changes
return editor->toPlainText() != originalConfig; return editor->toPlainText() != originalConfig;

View File

@ -30,8 +30,9 @@ private:
bool hasUnsavedChanges(); // Checks for unsaved changes bool hasUnsavedChanges(); // Checks for unsaved changes
private slots: private slots:
void onSaveClicked(); // Save button slot void onSaveClicked(); // Save button slot
void onCancelClicked(); // Slot for handling cancel button void onCancelClicked(); // Slot for handling cancel button
void onHelpClicked(); // Slot for handling help button void onHelpClicked(); // Slot for handling help button
void onResetToDefaultClicked();
void onGameSelectionChanged(const QString& game); // Slot for game selection changes void onGameSelectionChanged(const QString& game); // Slot for game selection changes
}; };