-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_data.cpp
More file actions
executable file
·238 lines (202 loc) · 7.74 KB
/
user_data.cpp
File metadata and controls
executable file
·238 lines (202 loc) · 7.74 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
#include "user_data.hpp"
UserData::UserData() {
jsonFilePath = QCoreApplication::applicationDirPath() + "/Resources/userData.json";
}
bool UserData::loadJson() {
QFile loadFile(jsonFilePath);
if (!loadFile.exists())
return false;
if (!loadFile.open(QIODevice::ReadOnly))
return false;
QByteArray saveData = loadFile.readAll();
QJsonParseError error;
QJsonDocument loadDoc(QJsonDocument::fromJson(saveData, &error));
if (error.error != QJsonParseError::NoError)
return false;
if (!read(loadDoc.object())) {
clear();
return false;
}
return true;
}
bool UserData::saveJson() {
QFile saveFile(jsonFilePath);
if (!saveFile.open(QIODevice::WriteOnly))
return false;
QJsonObject gameObject;
write(gameObject);
QJsonDocument saveDoc(gameObject);
saveFile.write(saveDoc.toJson());
return true;
}
HVarSet UserData::hiddenVariable(int currentRow) {
return hVarList.at(currentRow);
}
QList<HVarSet> UserData::hiddenVarList() {
return hVarList;
}
UPref UserData::userPreference() {
return pref;
}
void UserData::clear() {
pref.rmWithoutAsk = false;
pref.Pysource = "";
pref.Pyprogram = "";
hVarList.clear();
}
bool UserData::read(const QJsonObject &json) {
bool pass = true;
// Read User Preference
if (json.contains("UserPreference") && json["UserPreference"].isObject()) {
QJsonObject obj = json["UserPreference"].toObject();
if (obj.contains("rmWithoutAsk") && obj["rmWithoutAsk"].isBool())
pref.rmWithoutAsk = obj["rmWithoutAsk"].toBool();
else pass = false;
if (obj.contains("Pysource") && obj["Pysource"].isString())
pref.Pysource = obj["Pysource"].toString();
else pass = false;
if (obj.contains("Pyprogram") && obj["Pyprogram"].isString())
pref.Pyprogram = obj["Pyprogram"].toString();
else pass = false;
}else pass = false;
// Read Hidden Variable Template
if (json.contains("Template") && json["Template"].isObject()) {
QJsonObject obj = json["Template"].toObject();
if (!obj.contains("filename"))
return false;
QStringList keyList = obj.keys();
for (int i = 0; i < keyList.size(); i++) {
if(obj[keyList[i]].isString())
model.addModel(keyList[i], StringType);
else if (obj[keyList[i]].isDouble()){
model.addModel(keyList[i], DoubleType);
} else return false;
}
} else return false;
// Read Hidden Variables
if (json.contains("HiddenVariable") && json["HiddenVariable"].isArray()) {
QJsonArray varArray = json["HiddenVariable"].toArray();
hVarList.clear();
hVarList.reserve(varArray.size());
for (int varIndex = 0; varIndex < varArray.size(); ++varIndex) {
QJsonObject varObject = varArray[varIndex].toObject();
HVarSet set;
for(auto i : model.model){
switch (i.second) {
case DataType::StringType:
if (varObject.contains(i.first) && varObject[i.first].isString())
set.strList.push_back(varObject[i.first].toString());
else return false;
break;
case DataType::DoubleType:
if (varObject.contains(i.first) && varObject[i.first].isDouble())
set.doubleList.push_back(varObject[i.first].toDouble());
else return false;
break;
default:
qDebug() << "\nXuan: data type does not exist.\n";
break;
}
}
hVarList.append(set);
}
} else pass = false;
return pass;
}
void UserData::write(QJsonObject &json) {
QJsonObject commentObject;
commentObject["0_Overview"] = "The Json file needs contain three things: Template, HiddenVariable and UserPreference.";
commentObject["1_Template"] = "Template is the template for hidden variables. You need specify what is included in each HiddenVariable object. Note that 'filename' must be included.";
commentObject["2_HiddenVariable"] = "HiddenVariable is a JSON array. Each object in the array contains all necessary hidden variables, which are passed to python script as arguments. Note that 'filename' is the identifier for matching videos.";
commentObject["3_UserPreference"] = "UserPreference contains all user preference settings. For example, if you check 'Always apply hidden parameters if the name matches' in hidden variable tab, it is recorded for next time use.";
json["CommentThatCanbeDelete"] = commentObject;
QJsonObject templateObject;
for (auto i : model.model) {
switch (i.second) {
case DataType::StringType:
templateObject[i.first] = "";
break;
case DataType::DoubleType:
templateObject[i.first] = 0.0;
break;
default:
qDebug() << "\nXuan: data type does not exist.\n";
break;
}
}
json["Template"] = templateObject;
QJsonArray array;
for(HVarSet i : hVarList) {
QJsonObject obj;
int strIndex = 0;
int doubleIndex = 0;
for(auto it : model.model){
switch (it.second) {
case DataType::StringType:
obj[it.first] = i.strList.at(strIndex);
strIndex++;
break;
case DataType::DoubleType:
obj[it.first] = i.doubleList.at(doubleIndex);
doubleIndex++;
break;
default:
qDebug() << "\nXuan: data type does not exist-.\n";
break;
}
}
array.append(obj);
}
json["HiddenVariable"] = array;
QJsonObject prefObject;
prefObject["rmWithoutAsk"] = pref.rmWithoutAsk;
prefObject["Pysource"] = pref.Pysource;
prefObject["Pyprogram"] = pref.Pyprogram;
json["UserPreference"] = prefObject;
}
void UserData::setHiddenVariable(int currentRow, int index, double value) {
auto set = hVarList.at(currentRow);
set.doubleList.replace(index, value);
hVarList.replace(currentRow, set);
}
void UserData::setHiddenVariableStr(int currentRow, int index, QString value) {
auto set = hVarList.at(currentRow);
set.strList.replace(index, value);
hVarList.replace(currentRow, set);
}
void UserData::setRmWithoutAsk(bool i) {
pref.rmWithoutAsk = i;
}
void UserData::setPysource(QString str) {
pref.Pysource = str;
}
void UserData::setPyprogram(QString str) {
pref.Pyprogram = str;
}
void UserData::addHiddenVariable(QString filename) {
HVarSet set;
for(auto i : model.model){
switch (i.second) {
case DataType::StringType:
set.strList.push_back("");
break;
case DataType::DoubleType:
set.doubleList.push_back(0.0);
break;
default:
qDebug() << "\nXuan: data type does not exist--.\n";
break;
}
}
set.strList.replace(0, filename);
hVarList.append(set);
}
bool UserData::removeHiddenVariable(int currentRow) {
if (currentRow >= hVarList.size())
return false;
hVarList.removeAt(currentRow);
return true;
}
HidVarModel UserData::hidenVarModel() const{
return model;
}