-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicUI.cpp
More file actions
141 lines (121 loc) · 5.94 KB
/
dynamicUI.cpp
File metadata and controls
141 lines (121 loc) · 5.94 KB
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
#include "dynamicUI.h"
#include <float.h>
#include <string>
#include <QDebug>
#include <QFile>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
#include <QFileDialog>
#include "inifile.h"
DynamicUI::DynamicUI() {
}
DynamicUI::~DynamicUI() {
// mIniFile.Save();
mMainLayout = nullptr;
}
void DynamicUI::loadInterface(const char* filepath, QBoxLayout *layout)
{
if(mIniFile.Load(filepath)) {
qDebug() << "Loading " << filepath << " failed!";
}
mMainLayout = layout;
}
void DynamicUI::buildInterface(const char* sectionName) {
const char* section_name = sectionName;
inifile::IniSection* section = mIniFile.getSection(section_name);
for(auto it = section->begin(); it != section->end(); it++) {
// [key] = [value]
// [key] => [type]_[name]
QString key = QString::fromStdString(it->key);
QString value = QString::fromStdString(it->value);
QString type = key.split('_')[0];
QString name = key.split('_')[1];
QHBoxLayout* horizontalLayout = new QHBoxLayout();
QLabel* label = new QLabel(name);
horizontalLayout->addWidget(label);
if(type == "int") {
QSpinBox* spinBox = new QSpinBox();
spinBox->setRange(INT_MIN,INT_MAX);
spinBox->setButtonSymbols(QAbstractSpinBox::ButtonSymbols::NoButtons);
spinBox->setValue(value.toInt());
// A wise way provided by "Qt Help"(Press F1 at valueChanged).
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged), [this, key, section_name](int i) {
this->mIniFile.SetIntValue(section_name, key.toStdString(), i);
});
horizontalLayout->addWidget(spinBox);
} else if(type == "double") {
QDoubleSpinBox* doubleSpinBox = new QDoubleSpinBox();
doubleSpinBox->setRange(DBL_MIN,DBL_MAX);
doubleSpinBox->setButtonSymbols(QAbstractSpinBox::ButtonSymbols::NoButtons);
doubleSpinBox->setValue(value.toDouble());
// A wise way provided by "Qt Help"(Press F1 at valueChanged).
connect(doubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [this, key, section_name](double d) {
this->mIniFile.SetDoubleValue(section_name, key.toStdString(), d);
});
horizontalLayout->addWidget(doubleSpinBox);
} else if(type == "string") {
QLineEdit* lineEdit = new QLineEdit(value);
lineEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
// A wise way provided by "Qt Help"(Press F1 at textChanged).
connect(lineEdit, QOverload<const QString&>::of(&QLineEdit::textChanged), [this, key, section_name](const QString& text) {
this->mIniFile.SetStringValue(section_name, key.toStdString(), text.toStdString());
});
horizontalLayout->addWidget(lineEdit);
} else if(type == "bool") {
QCheckBox* checkBox = new QCheckBox();
if(value == "true") {
checkBox->setChecked(true);
} else {
checkBox->setChecked(false);
}
// A wise way provided by "Qt Help"(Press F1 at stateChanged).
connect(checkBox, QOverload<int>::of(&QCheckBox::stateChanged), [this, key, section_name](int i) {
this->mIniFile.SetBoolValue(section_name, key.toStdString(), i);
});
horizontalLayout->addWidget(checkBox);
} else if(type == "file") {
QLineEdit* lineEdit = new QLineEdit(value);
lineEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
// A wise way provided by "Qt Help"(Press F1 at textChanged).
connect(lineEdit, QOverload<const QString&>::of(&QLineEdit::textChanged), [this, key, section_name](const QString& text) {
this->mIniFile.SetStringValue(section_name, key.toStdString(), text.toStdString());
});
QPushButton* pushButton = new QPushButton("Browser");
pushButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(pushButton, QOverload<const bool>::of(&QPushButton::clicked), [lineEdit, key](const bool) {
QString filePath = QFileDialog::getOpenFileName(nullptr, "Please select a file: ", key);
if(!filePath.isEmpty()) {
lineEdit->setText(filePath);
}
});
horizontalLayout->addWidget(lineEdit);
horizontalLayout->addWidget(pushButton);
} else if(type == "folder") {
QLineEdit* lineEdit = new QLineEdit(value);
lineEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
// A wise way provided by "Qt Help"(Press F1 at textChanged).
connect(lineEdit, QOverload<const QString&>::of(&QLineEdit::textChanged), [this, key, section_name](const QString& text) {
this->mIniFile.SetStringValue(section_name, key.toStdString(), text.toStdString());
});
QPushButton* pushButton = new QPushButton("Browser");
pushButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(pushButton, QOverload<const bool>::of(&QPushButton::clicked), [lineEdit, key](const bool) {
QString filePath = QFileDialog::getExistingDirectory(nullptr, "Please select a folder: ", key, QFileDialog::ShowDirsOnly);
if(!filePath.isEmpty()) {
lineEdit->setText(filePath);
}
});
horizontalLayout->addWidget(lineEdit);
horizontalLayout->addWidget(pushButton);
}
mMainLayout->addLayout(horizontalLayout);
}
}
int DynamicUI::saveIniFile() {
return mIniFile.Save();
}