-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhidden_var_view.cpp
More file actions
executable file
·307 lines (267 loc) · 11.2 KB
/
hidden_var_view.cpp
File metadata and controls
executable file
·307 lines (267 loc) · 11.2 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
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
#include "hidden_var_view.hpp"
HiddenVarView::HiddenVarView(UserData *d, LogView * l): strList(), doubleList(){
data = d;
log = l;
list = new QListWidget();
QObject::connect(list, &QListWidget::currentRowChanged, this, &HiddenVarView::updateParameter);
addButton = new QPushButton("Add");
QObject::connect(addButton, &QPushButton::clicked, this, &HiddenVarView::addButtonClicked);
removeButton = new QPushButton("Remove");
QObject::connect(removeButton, &QPushButton::clicked, this, &HiddenVarView::removeButtonClicked);
showInFolderBT = new QPushButton("Show userData.json in Folder");
QObject::connect(showInFolderBT, &QPushButton::clicked, this, &HiddenVarView::showInFolderClicked);
discardAllBT = new QPushButton("Discard All Changes");
QObject::connect(discardAllBT, &QPushButton::clicked, this, &HiddenVarView::discardAllBTClicked);
// add hidden variable to widget
int xCoordinate = 0;
HidVarModel model = data->hidenVarModel();
QGridLayout *lay = new QGridLayout();
StrVarItem *itt;
DoubleVarItem *ittt;
int count = 0;
for(auto i : model.model){
switch (i.second) {
case DataType::StringType:
itt = new StrVarItem(i.first, "");
strList.push_back(itt);
QObject::connect(itt, &StrVarItem::valueChanged, this, &HiddenVarView::strParameterChanged);
lay->addWidget(itt->nameLabel, xCoordinate, (count / 10) * 3, Qt::AlignRight);
lay->addWidget(itt->valueBox, xCoordinate, (count / 10) * 3 + 1, 1, 2, Qt::AlignLeft);
xCoordinate++;
xCoordinate %= 10;
break;
case DataType::DoubleType:
ittt = new DoubleVarItem(i.first, 0.0);
doubleList.push_back(ittt);
QObject::connect(doubleList.last(), &DoubleVarItem::valueChanged, this, &HiddenVarView::doubleParameterChanged);
lay->addWidget(doubleList.last()->nameLabel, xCoordinate, (count / 10) * 3, Qt::AlignRight);
lay->addWidget(doubleList.last()->valueBox, xCoordinate, (count / 10) * 3 + 1, 1, 2, Qt::AlignLeft);
xCoordinate++;
xCoordinate %= 10;
break;
default:
log->write("Error: DataType does not exist.");
break;
}
count++;
}
group = new QGroupBox();
group->setTitle("Hidden Parameters");
group->setLayout(lay);
auto title = new QLabel("Video Name:");
auto it = title->font();
it.setPointSize(11);
title->setFont(it);
rmWithoutAskCheckBox = new QCheckBox("Do not ask verification for remove operation");
QObject::connect(rmWithoutAskCheckBox, &QCheckBox::stateChanged, this, &HiddenVarView::rmWithoutAskClicked);
QGridLayout *layout = new QGridLayout();
layout->addWidget(title, 0, 0, Qt::AlignLeft);
layout->addWidget(group, 1, 4, 15, 5);
layout->addWidget(list, 1, 0, 18, 2);
layout->addWidget(addButton, 19, 0);
layout->addWidget(removeButton, 19, 1);
layout->addWidget(discardAllBT, 20, 0, 1, 2);
layout->addWidget(showInFolderBT, 20, 8);
layout->addWidget(rmWithoutAskCheckBox, 20, 4, 1, 4);
setLayout(layout);
// Load presetting parameters to the screen
loadParameters();
}
QStringList HiddenVarView::getArguments(QString &filename) {
QStringList arguments;
int row = -1;
for (int i = 0; i < list->count(); i++) {
if (list->item(i)->text() == filename)
row = i;
}
if (row == -1) {
for (int i = 1; i < strList.size(); i++) {
arguments << "";
}
for (int i = 0; i < doubleList.size(); i++) {
arguments << "0";
}
return arguments;
}
HVarSet set = data->hiddenVariable(row);
for (int i = 1; i < strList.size(); i++) {
arguments << set.strList.at(i);
}
for (int i = 0; i < doubleList.size(); i++) {
arguments << QString::number(set.doubleList.at(i), 'g', 15);
}
return arguments;
}
QStringList HiddenVarView::getPrintArguments(QString &filename) {
QStringList arguments;
int row = -1;
for (int i = 0; i < list->count(); i++) {
if (list->item(i)->text() == filename)
row = i;
}
if (row == -1) {
for (int i = 1; i < strList.size(); i++) {
arguments << " " + strList.at(i)->nameLabel->text();
}
for (int i = 0; i < doubleList.size(); i++) {
arguments << " " + doubleList.at(i)->nameLabel->text() + " 0";
}
return arguments;
}
HVarSet set = data->hiddenVariable(row);
for (int i = 1; i < strList.size(); i++) {
arguments << " " + strList.at(i)->nameLabel->text() + " " + set.strList.at(i);
}
for (int i = 0; i < doubleList.size(); i++) {
arguments << " " + doubleList.at(i)->nameLabel->text() + " " + QString::number(set.doubleList.at(i), 'g', 15);
}
return arguments;
}
void HiddenVarView::updateParameter(int currentRow) {
group->setDisabled(false);
HVarSet set = data->hiddenVariable(currentRow);
for (int i = 0; i < strList.size(); i++) {
strList.at(i)->valueBox->setText(set.strList.at(i));
}
for (int i = 0; i < doubleList.size(); i++) {
doubleList.at(i)->valueBox->setText(QString::number(set.doubleList.at(i), 'g', 15));
}
}
void HiddenVarView::doubleParameterChanged(DoubleVarItem *param) {
if (list->currentItem() == nullptr)
return;
data->setHiddenVariable(list->currentRow(), doubleList.indexOf(param), param->valueBox->text().toDouble());
}
void HiddenVarView::strParameterChanged(StrVarItem *param) {
if (list->currentItem() == nullptr)
return;
data->setHiddenVariableStr(list->currentRow(), strList.indexOf(param), param->valueBox->text());
// If file name is changed, update name in the list
if (strList.indexOf(param) == 0) {
list->currentItem()->setText(param->valueBox->text());
emit reloadParam();
}
}
/*###########################################
# Event Handle Functions #
###########################################*/
void HiddenVarView::addButtonClicked() {
QString newFileName = "untitled_" + QString::number(list->count());
data->addHiddenVariable(newFileName);
addItem(newFileName);
list->setCurrentRow(list->count() - 1);
emit reloadParam();
}
void HiddenVarView::removeButtonClicked() {
if (list->count() == 0) return;
int ret = QMessageBox::Yes;
if (!rmWithoutAskCheckBox->isChecked()) {
QMessageBox msgBox;
msgBox.setText("Remove Operation Detected.");
msgBox.setInformativeText("Do you want to remove the current parameter set from the list?");
msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes);
msgBox.setDefaultButton(QMessageBox::NoButton);
msgBox.setIcon(QMessageBox::Warning);
ret = msgBox.exec();
}
if (ret == QMessageBox::Yes) {
if (!data->removeHiddenVariable(list->currentRow())) {
log->write("Error: Fail to remove item, since it does not exist in the list");
QMessageBox msg;
msg.setText("Error: Fail to remove item, since it does not exist in the list");
msg.setIcon(QMessageBox::Critical);
msg.exec();
return;
}
QObject::disconnect(list, &QListWidget::currentRowChanged, this, &HiddenVarView::updateParameter);
int row = list->currentRow();
list->takeItem(row);
if (list->count() == 0) {
group->setDisabled(true);
for (auto i : strList) {
QObject::disconnect(i, &StrVarItem::valueChanged, this, &HiddenVarView::strParameterChanged);
i->valueBox->setText("");
QObject::connect(i, &StrVarItem::valueChanged, this, &HiddenVarView::strParameterChanged);
}
for (auto i : doubleList) {
QObject::disconnect(i, &DoubleVarItem::valueChanged, this, &HiddenVarView::doubleParameterChanged);
i->valueBox->setText("");
QObject::connect(i, &DoubleVarItem::valueChanged, this, &HiddenVarView::doubleParameterChanged);
}
QObject::connect(list, &QListWidget::currentRowChanged, this, &HiddenVarView::updateParameter);
return;
}
if (row == list->count())
list->setCurrentRow(row - 1);
updateParameter(list->currentRow());
QObject::connect(list, &QListWidget::currentRowChanged, this, &HiddenVarView::updateParameter);
emit reloadParam();
}
}
void HiddenVarView::showInFolderClicked() {
showFileInFolder(QCoreApplication::applicationDirPath() + "/Resources/userData.json");
}
void HiddenVarView::discardAllBTClicked() {
data->clear();
QObject::disconnect(list, &QListWidget::currentRowChanged, this, &HiddenVarView::updateParameter);
list->clear();
data->loadJson();
loadParameters();
if (list->count() == 0) {
group->setDisabled(true);
for (auto i : strList) {
QObject::disconnect(i, &StrVarItem::valueChanged, this, &HiddenVarView::strParameterChanged);
i->valueBox->setText("");
QObject::connect(i, &StrVarItem::valueChanged, this, &HiddenVarView::strParameterChanged);
}
for (auto i : doubleList) {
QObject::disconnect(i, &DoubleVarItem::valueChanged, this, &HiddenVarView::doubleParameterChanged);
i->valueBox->setText("");
QObject::connect(i, &DoubleVarItem::valueChanged, this, &HiddenVarView::doubleParameterChanged);
}
QObject::connect(list, &QListWidget::currentRowChanged, this, &HiddenVarView::updateParameter);
}
QObject::connect(list, &QListWidget::currentRowChanged, this, &HiddenVarView::updateParameter);
if (list->count() != 0)
updateParameter(0);
}
void HiddenVarView::rmWithoutAskClicked(int state) {
data->setRmWithoutAsk(rmWithoutAskCheckBox->isChecked());
}
/*###########################################
# Helper Functions #
###########################################*/
void HiddenVarView::addItem(QString name) {
QListWidgetItem *newItem = new QListWidgetItem();
newItem->setText(name);
newItem->setTextAlignment(Qt::AlignCenter);
// Set font size
auto it = newItem->font();
#if defined(_WIN32) || defined(_WIN64) //Code for Windows
it.setPointSize(10);
#elif defined(__APPLE__) //Code for Mac
it.setPointSize(14);
#endif
newItem->setFont(it);
// Add to the list
list->addItem(newItem);
}
void HiddenVarView::loadParameters() {
rmWithoutAskCheckBox->setChecked(data->userPreference().rmWithoutAsk);
if (data->hiddenVarList().size() == 0)
return group->setDisabled(true);
for (HVarSet i : data->hiddenVarList()){
addItem(i.strList.at(0));
}
list->setCurrentRow(0);
}
int HiddenVarView::getParameterNum() {
return strList.count() + doubleList.count();
}
bool HiddenVarView::isMatched(QString name) {
for (int i = 0; i < list->count(); i++) {
if (list->item(i)->text() == name)
return true;
}
return false;
}