diff options
Diffstat (limited to 'config/settingsdialog.cpp')
-rw-r--r-- | config/settingsdialog.cpp | 76 |
1 files changed, 69 insertions, 7 deletions
diff --git a/config/settingsdialog.cpp b/config/settingsdialog.cpp index ef21a47..cc8cb8e 100644 --- a/config/settingsdialog.cpp +++ b/config/settingsdialog.cpp @@ -8,33 +8,95 @@ #include "settingsdialog.h" #include <QApplication> +#include <QFile> #include <QMenuBar> +#include <QStatusBar> #include <QToolButton> #include <QVBoxLayout> +#include <QFileDialog> -SettingsDialog::SettingsDialog(QWidget *parent) +SettingsDialog::SettingsDialog(Configuration *config, QWidget *parent) : QMainWindow(parent) { resize(800, 600); // main menu settingsMenu.setTitle(tr("Settings")); - settingsMenu.addAction(tr("Quit"), qApp, &QApplication::quit, QKeySequence("Ctrl+Q")); + + auto *saveAction = settingsMenu.addAction(tr("Save")); + saveAction->setShortcut(QKeySequence::Save); + + auto *saveAsAction = settingsMenu.addAction(tr("Save As")); + saveAsAction->setShortcut(QKeySequence::SaveAs); + + settingsMenu.addAction(tr("Quit"), qApp, &QApplication::quit, QKeySequence::Quit); + menuBar()->addMenu(&settingsMenu); setCentralWidget(&treeWidget); treeWidget.setColumnCount(2); treeWidget.setHeaderLabels({ tr("setting"), tr("value") }); + treeWidget.setEditTriggers(QTreeWidget::NoEditTriggers); + connect(&treeWidget, &QTreeWidget::itemDoubleClicked, this, &SettingsDialog::editItem); - //config.read() - - for(const auto &option : config.options()) { + for(const auto &option : config->options()) { auto *item = new QTreeWidgetItem(&treeWidget); item->setText(0, QString::fromStdString(option->long_name())); - item->setText(1, QString::fromStdString(config.value<std::string>(option->long_name().c_str()).value_or(std::string()))); + item->setText(1, QString::fromStdString(config->value<std::string>(option->long_name().c_str()).value_or(std::string()))); + item->setFlags(item->flags() | Qt::ItemIsEditable); } treeWidget.resizeColumnToContents(0); + + // connect signals + connect(saveAction, &QAction::triggered, this, [this]() { + write(configPath); + statusBar()->showMessage(tr("Configuration saved to: ") + configPath, 3000); + }); + + connect(saveAsAction, &QAction::triggered, this, [this]() { + QString path = QFileDialog::getSaveFileName(this, tr("Save Configuration"), configPath, tr("smolbote config (smolbote.cfg)")); + if(!path.isEmpty()) { + write(path); + statusBar()->showMessage(tr("Configuration saved to: ") + path, 3000); + } + }); +} + +SettingsDialog::~SettingsDialog() +{ + if(unsavedChanges) + write(configPath); +} + +void SettingsDialog::editItem(QTreeWidgetItem *item, int column) +{ + if(column == 1) { + treeWidget.editItem(item, column); + unsavedChanges = true; + + if(!windowTitle().endsWith('*')) { + setWindowTitle(windowTitle() + '*'); + } + } } -SettingsDialog::~SettingsDialog() = default; +void SettingsDialog::write(const QString &path) +{ + QFile output(path); + output.open(QIODevice::WriteOnly); + + for(int i = 0; i < treeWidget.topLevelItemCount(); ++i) { + auto *item = treeWidget.topLevelItem(i); + output.write(QString(item->text(0) + " = " + item->text(1) + "\n").toUtf8()); + } + + output.close(); + + unsavedChanges = false; + if(windowTitle().endsWith('*')) { + QString title = windowTitle(); + title.chop(1); + setWindowTitle(title); + } +} |