-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_widget.cpp
229 lines (192 loc) · 5.41 KB
/
main_widget.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
#include "main_widget.h"
#include "config.h"
#include <QtWidgets>
#include <thread>
class SliderCombo {
public:
QLayout *layout;
QSlider *slider;
QLineEdit *edit;
};
static QCheckBox *_AddCheckBox(
QVBoxLayout *pLayout,
const char *szLabel,
Hjson::Value& config,
const char *szConfig,
std::function<void()> pOnChecked = nullptr,
std::function<void()> pOnUnchecked = nullptr
) {
pLayout->addSpacing(10);
auto tmpCheckBox = new QCheckBox(szLabel);
QObject::connect(
tmpCheckBox,
&QCheckBox::stateChanged,
[&config, szConfig, pOnChecked, pOnUnchecked](int state) {
switch (state)
{
case Qt::Checked:
if (szConfig) {
config[szConfig] = true;
}
if (pOnChecked) {
pOnChecked();
}
break;
case Qt::Unchecked:
if (szConfig) {
config[szConfig] = false;
}
if (pOnUnchecked) {
pOnUnchecked();
}
break;
default:
break;
}
}
);
if (szConfig) {
if (!!config[szConfig]) {
tmpCheckBox->setChecked(true);
} else if (pOnUnchecked) {
pOnUnchecked();
}
}
pLayout->addWidget(tmpCheckBox);
return tmpCheckBox;
}
static SliderCombo _AddSlider(
QVBoxLayout *pLayout,
const char *szLabel,
int minVal,
int maxVal,
int tickInterval,
Hjson::Value& config,
const char *szConfig
) {
auto tmpVL = new QVBoxLayout();
tmpVL->addSpacing(20);
tmpVL->addWidget(new QLabel(szLabel));
tmpVL->addSpacing(5);
auto tmpHL = new QHBoxLayout();
auto tmpSlider = new QSlider(Qt::Orientation::Horizontal);
tmpSlider->setTickPosition(QSlider::TickPosition::TicksBelow);
tmpSlider->setMinimum(minVal);
tmpSlider->setMaximum(maxVal);
tmpSlider->setTickInterval(tickInterval);
tmpHL->addWidget(tmpSlider);
tmpHL->addSpacing(10);
auto tmpEdit = new QLineEdit();
tmpEdit->setAlignment(Qt::AlignRight);
tmpEdit->setFixedWidth(50);
tmpEdit->setValidator(new QIntValidator(tmpEdit));
tmpHL->addWidget(tmpEdit);
// Use to_int64() to always get a number even if the value is a string or
// something else.
tmpSlider->setValue(config[szConfig].to_int64());
tmpEdit->setText(QString::number(config[szConfig].to_int64()));
QObject::connect(
tmpSlider,
&QSlider::valueChanged,
[tmpEdit, &config, szConfig](int value) {
if (tmpEdit->text() != QString::number(value)) {
tmpEdit->setText(QString::number(value));
}
config[szConfig] = value;
}
);
QObject::connect(
tmpEdit,
&QLineEdit::textChanged,
[tmpSlider](const QString& str) {
int val = str.toInt();
if (val >= tmpSlider->minimum() && val <= tmpSlider->maximum() &&
tmpSlider->value() != val)
{
tmpSlider->setValue(val);
}
}
);
tmpVL->addItem(tmpHL);
pLayout->addItem(tmpVL);
return {tmpVL, tmpSlider, tmpEdit};
}
static QPushButton *_AddButton(
QVBoxLayout *pLayout,
const char *szLabel,
std::function<void()> pOnClick
) {
pLayout->addSpacing(40);
auto tmpBtn = new QPushButton();
tmpBtn->setText(szLabel);
tmpBtn->setFixedWidth(70);
tmpBtn->setFixedHeight(30);
pLayout->addWidget(tmpBtn, 0, Qt::AlignRight);
QObject::connect(tmpBtn, &QPushButton::clicked, pOnClick);
return tmpBtn;
}
static void _InitDialog(QDialog *pDialog) {
pDialog->resize(200, 150);
auto dialogLayout = new QVBoxLayout();
dialogLayout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
dialogLayout->addSpacing(35);
dialogLayout->addWidget(new QLabel("Working..."));
pDialog->setLayout(dialogLayout);
pDialog->setModal(true);
}
MainWidget::MainWidget(Hjson::Value& config)
: m_dialog(new QDialog(this, Qt::CoverWindow))
{
_InitDialog(m_dialog);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->setAlignment(Qt::AlignTop);
mainLayout->setContentsMargins(20, 20, 20, 20);
auto alphaCombo = _AddSlider(mainLayout, "Alpha", 0, 3000, 500, config,
Cfg::alpha);
auto checkbox = _AddCheckBox(
mainLayout,
"Enable alpha",
config,
Cfg::enableAlpha,
[alphaCombo]() {
alphaCombo.slider->setEnabled(true);
alphaCombo.edit->setEnabled(true);
},
[alphaCombo]() {
alphaCombo.slider->setEnabled(false);
alphaCombo.edit->setEnabled(false);
}
);
// Move down the alpha slider to below the checkbox.
mainLayout->removeItem(alphaCombo.layout);
mainLayout->addItem(alphaCombo.layout);
_AddSlider(mainLayout, "Beta", 0, 256, 32, config, Cfg::beta);
_AddSlider(mainLayout, "Gamma", 0, 10, 1, config, Cfg::gamma);
mainLayout->addSpacing(20);
// Use to_string() to always get a string even if the value is a number or
// something else.
mainLayout->addWidget(new QLabel(
config[Cfg::exampleString].to_string().c_str()));
_AddButton(
mainLayout,
"Run",
[this]() {
this->m_dialog->open();
// Shows how to do heavy work in a background thread in order to not
// block the UI.
std::thread([this]() {
// Simulate heavy work by just sleeping for three seconds.
std::this_thread::sleep_for(std::chrono::seconds(3));
// Calls the member function work_finished() from the UI thread instead
// of from the current thread.
QMetaObject::invokeMethod(this, "work_finished");
}).detach();
}
);
setLayout(mainLayout);
setWindowTitle(tr("HjsonExample"));
setMinimumSize(200, 389);
}
void MainWidget::work_finished() {
m_dialog->close();
}