-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupbasepage.cpp
executable file
·392 lines (310 loc) · 9.95 KB
/
setupbasepage.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "setupbasepage.h"
#include "config.h"
#include "optionwidgets.h"
#define HINTTIMEOUT 500
////////////////////////////////////////////////////////////////////////////////
void setEnumValue(const char *name, const QString &value)
{
ConfigEnumPtr ce = getEnum(name);
if (!ce.isNull())
ce->setValue(value);
}
void setStringValue(const char *name, const QString &value)
{
ConfigStringPtr ce = getString(name);
if (!ce.isNull())
ce->setValue(value);
}
void setBoolValue(const char *name, bool value)
{
ConfigBoolPtr ce = getBool(name);
if (!ce.isNull())
*ce->valueRef() = value;
}
void setIntValue(const char *name, int value)
{
ConfigIntPtr ce = getInt(name);
if (!ce.isNull())
*ce->valueRef() = value;
}
ConfigEnumPtr getEnum(const char *name)
{
return Config::instance()->get(name).staticCast<ConfigEnum>();
}
ConfigStringPtr getString(const char *name)
{
return Config::instance()->get(name).staticCast<ConfigString>();
}
ConfigBoolPtr getBool(const char *name)
{
return Config::instance()->get(name).staticCast<ConfigBool>();
}
ConfigIntPtr getInt(const char *name)
{
return Config::instance()->get(name).staticCast<ConfigInt>();
}
////////////////////////////////////////////////////////////////////////////////
SetupBasePage::SetupBasePage(const QPixmap &icon, const QString &title, QWidget *parent)
: QFrame(parent),
myGroup(0),
myLastAdded(0)
{
init(icon, title, parent);
}
SetupBasePage::SetupBasePage(const QString &title, QWidget *parent)
: QFrame(parent),
myGroup(0),
myLastAdded(0)
{
init(QPixmap(), title, parent);
}
void SetupBasePage::init(const QPixmap &icon, const QString &title, QWidget *)
{
setFrameStyle(QFrame::StyledPanel);
QVBoxLayout *vbl = new QVBoxLayout();
setLayout(vbl);
QLabel *titleLabel = new QLabel(title, this);
titleLabel->setFrameStyle(QFrame::StyledPanel);
titleLabel->setProperty("kind", "header1");
vbl->addWidget(titleLabel);
QHBoxLayout *hbl = new QHBoxLayout();
hbl->setMargin(0);
hbl->setSpacing(0);
titleLabel->setLayout(hbl);
if (!icon.isNull()) {
QLabel *iconLabel = new QLabel(titleLabel);
iconLabel->setPixmap(icon);
iconLabel->setFixedSize(QSize(icon.width(),icon.height()));
hbl->addStretch();
hbl->addWidget(iconLabel);
}
myMainLayout = new QHBoxLayout();
vbl->addLayout(myMainLayout);
myLayout = new QVBoxLayout();
myMainLayout->addLayout(myLayout);
myMainLayout->setStretchFactor(myLayout,1);
myTip = new QLabel(this);
myTip->hide();
myTip->setFrameStyle(QFrame::StyledPanel);
myTip->setAlignment(Qt::AlignTop);
myTip->setWordWrap(true);
myTip->setProperty("kind", "helplabel");
myHintTimer = new QTimer(this);
connect(myHintTimer, SIGNAL(timeout()), this, SLOT(hintTimeout()));
}
SetupBasePage::~SetupBasePage()
{
}
void SetupBasePage::switchLayout(int l)
{
QList<QWidget*> list = l ? myCol2 : myCol1;
QList<QWidget*> tempList;
// remove old layout
while (myMainLayout->count()) {
QLayoutItem *li = myMainLayout->takeAt(0);
myLayout = (QVBoxLayout*)li->layout();
if (myLayout)
for (int i = 0; i < myLayout->count(); i++) {
QWidget *w = myLayout->itemAt(i)->widget();
if (w)
tempList.append(w);
}
delete li;
}
// set new layout
myLayout = new QVBoxLayout();
myMainLayout->addLayout(myLayout);
myMainLayout->setStretchFactor(myLayout,1);
for (int i = 0; i < tempList.count(); i++) {
QWidget *w = tempList.at(i);
myLayout->addWidget(w);
if (list.count())
if (w == list.first()) {
list.takeFirst();
myLayout->addStretch();
myLayout = new QVBoxLayout();
myMainLayout->addSpacing(16);
myMainLayout->addLayout(myLayout);
myMainLayout->setStretchFactor(myLayout,1);
}
}
myLayout->addStretch();
}
void SetupBasePage::addColumn()
{
finishGroup();
if (myLayout)
myLayout->addStretch();
myLayout = new QVBoxLayout();
myMainLayout->addSpacing(16);
myMainLayout->addLayout(myLayout);
myMainLayout->setStretchFactor(myLayout,1);
myCol1.append(myLastAdded);
}
void SetupBasePage::addColumn2()
{
myCol2.append(myLastAdded);
}
void SetupBasePage::addGroup(const QString &title)
{
finishGroup();
myGroup = new QGroupBox(title, this);
myLayout->addWidget(myGroup);
myGroupLayout = new QVBoxLayout();
//myGroupLayout->setSpacing(4);
//myGroupLayout->setMargin(6);
myGroup->setLayout(myGroupLayout);
myLastAdded = myGroup;
}
void SetupBasePage::finishGroup()
{
if (myGroup) {
myGroup = 0;
}
}
void SetupBasePage::finishPage()
{
finishGroup();
if (myLayout)
myLayout->addStretch();
((QVBoxLayout*)layout())->addStretch();
// fit all the option text
int baseWidth = myWidgets.first()->titleTextWidth();
int minWidth = 0;
foreach (OptionBase *ob, myWidgets) {
minWidth = qMax(minWidth, ob->titleTextWidth());
}
if (minWidth > baseWidth)
foreach (OptionBase *ob, myWidgets) {
ob->setTitleTextWidth(minWidth+10);
}
}
void SetupBasePage::addSection(const QString &title)
{
finishGroup();
QWidget *w = new QWidget(this);
myLayout->addWidget(w);
myLastAdded = w;
QHBoxLayout *hbl = new QHBoxLayout;
w->setLayout(hbl);
hbl->setMargin(0);
// myLayout->addLayout(hbl);
QFrame *line = new QFrame(this);
line->setFrameShape(QFrame::HLine);
line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
hbl->addWidget(line);
QLabel *label = new QLabel(title, this);
label->setProperty("kind", "header2");
label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
hbl->addWidget(label);
}
void SetupBasePage::addOption(const QString &title, const char *id)
{
ConfigOptionPtr option = Config::instance()->get(id);
if (!option)
return;
switch(option->kind()) {
case ConfigOption::O_String:
addOptionWidget(new OptionString(title, option.staticCast<ConfigString>(), this));
break;
case ConfigOption::O_Enum:
addOptionWidget(new OptionEnum(title, option.staticCast<ConfigEnum>(), this));
break;
case ConfigOption::O_List:
addOptionWidget(new OptionList(title, option.staticCast<ConfigList>(), this));
break;
case ConfigOption::O_Bool: {
OptionBool *ob = new OptionBool(title, option.staticCast<ConfigBool>(), this);
connect(ob, SIGNAL(checked(ConfigBoolPtr, Qt::CheckState)),
this, SIGNAL(optionChecked(ConfigBoolPtr, Qt::CheckState)));
addOptionWidget(ob);
}
break;
case ConfigOption::O_Int:
addOptionWidget(new OptionInt(title, option.staticCast<ConfigInt>(), this));
break;
case ConfigOption::O_Info:
case ConfigOption::O_Obsolete:
default:
break;
}
}
void SetupBasePage::addOptionWidget(OptionBase *ob)
{
if (myGroup)
myGroupLayout->addWidget(ob);
else {
myLayout->addWidget(ob);
myLastAdded = ob;
}
// if (ob->option())
// connect(ob, SIGNAL(highlighted(ConfigOptionPtr)),
// this, SIGNAL(optionHighlighted(ConfigOptionPtr)));
// else
// connect(ob, SIGNAL(highlighted(const QString&, const QString&)),
// this, SIGNAL(optionsHighlighted(const QString&, const QString&)));
myWidgets.append(ob);
if (ob->option()) {
connect(ob, SIGNAL(highlighted(OptionBase*)),
this, SLOT(onOptionHighlighted(OptionBase*)));
myWidgetMap[ob->option()] = ob;
}
else {
connect(ob, SIGNAL(highlighted(const QString&, const QString&)),
this, SLOT(onOptionHighlighted(const QString&, const QString&)));
}
connect(ob, SIGNAL(offlighted(OptionBase*)),
this, SLOT(onOptionOfflighted(OptionBase*)));
}
void SetupBasePage::storeValues()
{
foreach(OptionBase *ob, myWidgets) {
ob->storeValue();
}
}
void SetupBasePage::reloadWidget(const char *id)
{
ConfigOptionPtr co = Config::instance()->get(id);
if (co) {
if (myWidgetMap.contains(co)) {
OptionBase *ob = myWidgetMap[co];
ob->readValue();
}
}
}
void SetupBasePage::hintTimeout()
{
myHintTimer->stop();
QPoint pos = mapFromGlobal(QCursor::pos());
int lm = 11;
int x = pos.x() > width()/2 ? lm : width()/2;
// QToolTip::showText(mapToGlobal(QPoint(x,0)), txt, this, rect());
// QToolTip::showText(QCursor::pos(), txt);
myTip->setGeometry(x,40,width()/2-lm,height()-40-8);
myTip->setText(myHintText);
myTip->raise();
myTip->show();
}
void SetupBasePage::onOptionHighlighted(OptionBase *option)
{
QString txt = QString("Corresponds to Tag: <b>%1</b><hr><p>%2")
.arg(option->option()->nameString(), option->option()->docString());
onOptionHighlighted(option->title(), txt);
}
void SetupBasePage::onOptionHighlighted(const QString &title, const QString &text)
{
myHintText = QString("<p><b>%1</b><img src=':/gui/icon16_help.png' align=right><hr><p>%2").arg(title, text);
myHintTimer->start(HINTTIMEOUT);
}
void SetupBasePage::onOptionOfflighted(OptionBase*)
{
myTip->hide();
myHintTimer->stop();
}
void SetupBasePage::onOptionChecked(ConfigBoolPtr option, Qt::CheckState check)
{
OptionBool *ob = (OptionBool*) myWidgetMap[option];
if (!ob)
return;
ob->setCheckState(check);
}