aboutsummaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-03-21 13:29:41 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-03-21 13:29:41 +0100
commitbb5574831aef42b2f81979c1df8f706d3956bdca (patch)
tree7ad31ba13d86e4501631391a70698e146848e268 /config
parentUpdate manpage (diff)
downloadsmolbote-bb5574831aef42b2f81979c1df8f706d3956bdca.tar.xz
poi-config saving config
Diffstat (limited to 'config')
-rw-r--r--config/main.cpp7
-rw-r--r--config/settingsdialog.cpp76
-rw-r--r--config/settingsdialog.h10
3 files changed, 83 insertions, 10 deletions
diff --git a/config/main.cpp b/config/main.cpp
index 25751e6..16ff6db 100644
--- a/config/main.cpp
+++ b/config/main.cpp
@@ -14,7 +14,12 @@ int main(int argc, char **argv)
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(true);
- SettingsDialog dlg;
+ Configuration config;
+ config.read(Configuration::defaultUserConfigLocation());
+
+ SettingsDialog dlg(&config);
+ dlg.configPath = Configuration::defaultUserConfigLocation();
+ dlg.setWindowTitle("poi-config: " + Configuration::defaultUserConfigLocation());
dlg.show();
return app.exec();
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);
+ }
+}
diff --git a/config/settingsdialog.h b/config/settingsdialog.h
index dc3cc08..d456bc3 100644
--- a/config/settingsdialog.h
+++ b/config/settingsdialog.h
@@ -19,13 +19,19 @@ class SettingsDialog : public QMainWindow
Q_OBJECT
public:
- explicit SettingsDialog(QWidget *parent = nullptr);
+ explicit SettingsDialog(Configuration *config, QWidget *parent = nullptr);
~SettingsDialog() override;
+ QString configPath;
+
+public slots:
+ void editItem(QTreeWidgetItem *item, int column);
+ void write(const QString &path);
+
private:
QMenu settingsMenu;
QTreeWidget treeWidget;
- Configuration config;
+ bool unsavedChanges = false;
};
#endif // SMOLBOTE_SETTINGSDIALOG_H