-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.cpp
74 lines (59 loc) · 2.67 KB
/
settings.cpp
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
#include "settings.h"
#include "application.h"
#include <QInputDialog>
#include <QPushButton>
#include <QLabel>
ConfigLayout::ConfigLayout(QVariant *value, const QString &text) : QGridLayout(), value(value), text(text) {
auto *button = new QPushButton(text);
connect(button, &QPushButton::clicked, this, &ConfigLayout::setValue);
valueLabel = new QLabel;
valueLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
valueLabel->setText(value->toString());
this->addWidget(button, 0, 0);
this->addWidget(valueLabel, 0, 1);
}
void ConfigLayout::setValue() const {
if (value->type() == QVariant::Int) {
const int intValue = QInputDialog::getInt(valueLabel, APPLICATION, text, value->toInt());
valueLabel->setText(QString("%1").arg(intValue));
*value = intValue;
}
}
ConfigCheckBox::ConfigCheckBox(QVariant *value, const QString &text) : QCheckBox(text), value(value) {
setChecked(value->toBool());
connect(this, &ConfigCheckBox::stateChanged, this, &ConfigCheckBox::setValue);
}
void ConfigCheckBox::setValue(const int state) const {
*value = static_cast<bool>(state);
}
SettingsWindow::SettingsWindow(QWidget *parent) : QWidget(parent), mainWindow((QMainWindow*)parent) {
setWindowTitle("Settings");
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
settings = new QSettings(ORGANIZATION, APPLICATION);
loadSettings();
auto *button = new QPushButton("Close");
connect(button, &QPushButton::clicked, this, &SettingsWindow::close);
auto *settingsLayout = new QVBoxLayout;
settingsLayout->addWidget(new ConfigCheckBox(&values["autoStart"], QString("Auto start ") + APPLICATION));
settingsLayout->addLayout(new ConfigLayout(&values["udpPort"], "UDP Port:"));
settingsLayout->addWidget(button);
setLayout(settingsLayout);
}
void SettingsWindow::loadSettings() {
values["mainWindowPosition"] = settings->value("mainWindowPosition", QPoint(200, 200));
values["mainWindowSize"] = settings->value("mainWindowSize", QSize(400, 400));
values["lastDir"] = settings->value("lastDir", QApplication::applicationDirPath());
values["autoStart"] = settings->value("autoStart", false);
values["udpPort"] = settings->value("udpPort", 49152).toInt();
}
void SettingsWindow::saveSettings() {
settings->setValue("mainWindowPosition", mainWindow->pos());
settings->setValue("mainWindowSize", mainWindow->size());
settings->setValue("lastDir", values["lastDir"].toString());
settings->setValue("autoStart", values["autoStart"].toBool());
settings->setValue("udpPort", values["udpPort"].toInt());
}
void SettingsWindow::closeEvent(QCloseEvent *event) {
saveSettings();
event->accept();
}