-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.cpp
324 lines (296 loc) · 11.7 KB
/
options.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2022-present Contributors to the jobman project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/jobman
#include "options.h"
#include "question.h"
#include <QComboBox>
#include <QCheckBox>
#include <QFileDialog>
#include <QLineEdit>
#include <QPointer>
#include <QSlider>
#include <QSettings>
#include <QStandardPaths>
#include <QToolButton>
#include <QUuid>
#include <QDebug>
// generated files
#include "ui_options.h"
class OptionsPrivate : public QObject
{
Q_OBJECT
public:
OptionsPrivate();
void init();
void update();
public Q_SLOTS:
void valueChanged(const QString& name, const QVariant& value);
void close();
void defaults();
public:
QMap<QString, QWidget*> optionwidgets;
QSharedPointer<Preset> preset;
QPointer<Options> dialog;
QScopedPointer<Ui_Options> ui;
};
OptionsPrivate::OptionsPrivate()
{
}
void
OptionsPrivate::init()
{
// ui
ui.reset(new Ui_Options());
ui->setupUi(dialog);
// event filter
dialog->installEventFilter(this);
// preset
preset.reset(new Preset());
// connect
connect(ui->close, &QPushButton::pressed, this, &OptionsPrivate::close);
connect(ui->defaults, &QPushButton::pressed, this, &OptionsPrivate::defaults);
}
void
OptionsPrivate::update() {
ui->name->setText(preset->name());
if (ui->scrollarea->widget()) {
delete ui->scrollarea->widget();
}
optionwidgets.clear();
QWidget* containerwidget = new QWidget();
containerwidget->setObjectName("optionswidget");
QGridLayout* layout = new QGridLayout(containerwidget);
int row = 0;
for (Option* option : preset->options()) {
QLabel* label = new QLabel(option->name, containerwidget);
QFont font;
font.setPointSize(10);
label->setFont(font);
layout->addWidget(label, row, 0);
if (option->type.toLower() == "checkbox") {
QCheckBox* checkbox = new QCheckBox(containerwidget);
checkbox->setChecked(option->value.toBool());
connect(checkbox, &QCheckBox::toggled, this, [this, option](bool checked) {
valueChanged(option->id, checked);
});
optionwidgets[option->id] = checkbox;
layout->addWidget(checkbox, row, 1);
}
else if (option->type.toLower() == "double") {
QLineEdit* lineedit = new QLineEdit(option->value.toString(), containerwidget);
QDoubleValidator* validator = new QDoubleValidator(option->minimum.toDouble(), option->maximum.toDouble(), 3, lineedit);
validator->setNotation(QDoubleValidator::StandardNotation);
validator->setLocale(QLocale::C); // use '.' instead of ','
lineedit->setValidator(validator);
lineedit->setFont(font);
connect(lineedit, &QLineEdit::textEdited, this, [lineedit, option]() {
QString text = lineedit->text();
if (text.contains(',')) { // needed, will still allow ','
text.replace(',', '.');
lineedit->setText(text);
}
bool valid;
double value = text.toDouble(&valid);
if (valid) {
double minimum = option->minimum.toDouble();
double maximum = option->maximum.toDouble();
if (value < minimum) {
value = minimum;
}
else if (value > maximum) {
value = maximum;
}
lineedit->setText(QString::number(value));
}
});
connect(lineedit, &QLineEdit::textChanged, this, [this, option](const QString &text) {
bool valid;
double value = text.toDouble(&valid);
if (valid) {
valueChanged(option->id, value);
}
});
optionwidgets[option->id] = lineedit;
layout->addWidget(lineedit, row, 1);
}
else if (option->type.toLower() == "dropdown") {
QComboBox* combobox = new QComboBox(containerwidget);
int currentindex = -1;
for (int i = 0; i < option->options.size(); ++i) {
const auto& optPair = option->options[i];
combobox->addItem(optPair.first, optPair.second);
if (optPair.second == option->value) {
currentindex = i;
}
}
if (currentindex != -1) {
combobox->setCurrentIndex(currentindex);
}
connect(combobox, &QComboBox::currentIndexChanged, this, [this, option, combobox](int index) {
valueChanged(option->id, combobox->itemData(index));
});
optionwidgets[option->id] = combobox;
layout->addWidget(combobox, row, 1);
}
else if (option->type.toLower() == "doubleslider") {
QHBoxLayout* sliderlayout = new QHBoxLayout();
QSlider* slider = new QSlider(Qt::Horizontal, containerwidget);
double minValue = option->minimum.toDouble();
double maxValue = option->maximum.toDouble();
int precision = 10;
slider->setRange(minValue * precision, maxValue * precision);
slider->setValue(option->value.toDouble() * precision);
QLabel* sliderValueLabel = new QLabel(QString::number(slider->value() / (double)precision, 'f', 1), containerwidget);
sliderValueLabel->setFont(font);
sliderValueLabel->setFixedWidth(40);
sliderValueLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
connect(slider, &QSlider::valueChanged, this, [this, option, sliderValueLabel, precision](int value) {
double doubleValue = value / (double)precision;
sliderValueLabel->setText(QString::number(doubleValue, 'f', 1));
valueChanged(option->id, doubleValue);
});
optionwidgets[option->id] = slider;
sliderlayout->addWidget(slider);
sliderlayout->addWidget(sliderValueLabel);
layout->addLayout(sliderlayout, row, 1);
}
else if (option->type.toLower() == "file") {
QWidget* filewidget = new QWidget(containerwidget);
QHBoxLayout* filelayout = new QHBoxLayout(filewidget);
filelayout->setContentsMargins(0, 0, 0, 0);
QLineEdit* lineedit = new QLineEdit(option->value.toString(), filewidget);
lineedit->setReadOnly(true);
filelayout->addWidget(lineedit);
QIcon icon;
icon.addFile(QString::fromUtf8(":/icons/resources/Folder.png"), QSize(), QIcon::Normal, QIcon::Off);
QToolButton* button = new QToolButton(filewidget);
button->setIcon(icon);
filelayout->addWidget(button);
connect(button, &QToolButton::clicked, this, [this, lineedit, option]() {
QString filepath = QFileDialog::getOpenFileName(nullptr, tr("Select File"));
if (!filepath.isEmpty()) {
lineedit->setText(filepath);
valueChanged(option->id, filepath);
}
});
connect(lineedit, &QLineEdit::textChanged, this, [this, option](const QString &text) {
valueChanged(option->id, text);
});
optionwidgets[option->id] = lineedit;
layout->addWidget(filewidget, row, 1);
}
else if (option->type.toLower() == "int") {
QLineEdit* lineedit = new QLineEdit(option->value.toString(), containerwidget);
QIntValidator* validator = new QIntValidator(option->minimum.toInt(), option->maximum.toInt(), lineedit);
lineedit->setValidator(validator);
lineedit->setFont(font);
connect(lineedit, &QLineEdit::textEdited, this, [lineedit, option]() {
QString text = lineedit->text();
bool valid;
double value = text.toInt(&valid);
if (valid) {
double minimum = option->minimum.toInt();
double maximum = option->maximum.toInt();
if (value < minimum) {
value = minimum;
}
else if (value > maximum) {
value = maximum;
}
lineedit->setText(QString::number(value));
}
});
connect(lineedit, &QLineEdit::textChanged, this, [this, option](const QString& text) {
bool valid;
int value = text.toInt(&valid);
if (valid) {
valueChanged(option->id, value);
}
});
optionwidgets[option->id] = lineedit;
layout->addWidget(lineedit, row, 1);
}
else if (option->type.toLower() == "intslider") {
QHBoxLayout* sliderlayout = new QHBoxLayout();
QSlider* slider = new QSlider(Qt::Horizontal, containerwidget);
slider->setRange(option->minimum.toInt(), option->maximum.toInt());
slider->setValue(option->value.toInt());
QLabel* sliderValueLabel = new QLabel(QString::number(slider->value()), containerwidget);
sliderValueLabel->setFont(font);
sliderValueLabel->setFixedWidth(20);
sliderValueLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
connect(slider, &QSlider::valueChanged, this, [this, option, sliderValueLabel](int value) {
sliderValueLabel->setText(QString::number(value));
valueChanged(option->id, value);
});
optionwidgets[option->id] = slider;
sliderlayout->addWidget(slider);
sliderlayout->addWidget(sliderValueLabel);
layout->addLayout(sliderlayout, row, 1);
}
else if (option->type.toLower() == "text") {
QLineEdit* lineedit = new QLineEdit(option->value.toString(), containerwidget);
lineedit->setFont(font);
connect(lineedit, &QLineEdit::textChanged, this, [this, option](const QString &text) {
valueChanged(option->id, text);
});
optionwidgets[option->id] = lineedit;
layout->addWidget(lineedit, row, 1);
}
row++;
}
layout->setColumnStretch(0, 1);
layout->setColumnStretch(1, 3);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), row, 0, 1, 2);
containerwidget->setLayout(layout);
ui->scrollarea->setWidget(containerwidget);
ui->scrollarea->setWidgetResizable(true);
}
void
OptionsPrivate::valueChanged(const QString& id, const QVariant& value)
{
QList<Option*> options = preset->options();
for (Option* option : options) {
if (option->id == id) {
option->value = value;
break;
}
}
}
void
OptionsPrivate::close()
{
dialog->close();
}
void
OptionsPrivate::defaults()
{
if (Question::askQuestion(dialog.data(),
"All values will be reset to their default settings.\n"
"Do you want to continue?"
)) {
for (Option* option : preset->options()) {
option->value = option->defaultvalue;
}
update();
}
}
#include "options.moc"
Options::Options(QWidget* parent)
: QDialog(parent)
, p(new OptionsPrivate())
{
p->dialog = this;
p->init();
}
Options::~Options()
{
}
void
Options::update(QSharedPointer<Preset> preset)
{
if (p->preset->uuid() != preset->uuid()) {
p->preset = preset;
p->update();
}
}