1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/smolbote.hg
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "settingsdialog.h"
#include <QApplication>
#include <QFile>
#include <QMenuBar>
#include <QStatusBar>
#include <QToolButton>
#include <QVBoxLayout>
#include <QFileDialog>
SettingsDialog::SettingsDialog(Configuration *config, QWidget *parent)
: QMainWindow(parent)
{
resize(800, 600);
// main menu
settingsMenu.setTitle(tr("Settings"));
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);
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->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() + '*');
}
}
}
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);
}
}
|