-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp
455 lines (409 loc) · 12.8 KB
/
settings.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "global.h"
#include "settings.h"
#include "runtime.h"
#include "presets.h"
extern "C"
{
#include "external/siphash/siphash.h"
}
void TagEntry::replaceParameterMacros()
{
// Check the parameter values for macros and replace with corresponding values
replaceMacro(SET_MACRO_PROJECT_NAME, RTI->settings.projectName);
replaceMacro(SET_MACRO_PROJECT_OWNER, RTI->settings.projectOwner);
replaceMacro(SET_MACRO_PROJECT_PREFIX, RTI->settings.projectPrefix);
replaceMacro(SET_MACRO_PROCESS_DATE, RTI->dateString);
replaceMacro(SET_MACRO_PROCESS_TIME, RTI->timeString);
replaceMacro(SET_MACRO_RANDOM_UID, RTI->randomUID);
replaceMacro(SET_MACRO_FAKE_MRN, RTI->fakeMRN);
replaceMacro(SET_MACRO_FAKE_ACC, RTI->fakeACC);
replaceMacro(SET_MACRO_HASH_ACC, RTI->hashACC);
replaceMacro(SET_MACRO_FAKE_NAME, RTI->fakeName);
}
void TagEntry::replaceMacro(QString macro, QString value)
{
// Replace all occurrances of the macro in the parameter string
while (parameter.indexOf(macro)>=0)
{
parameter.replace(parameter.indexOf(macro), macro.size(), value);
}
}
bool ModuleSettings::calculateHashACC()
{
if (RTI->accUsedForHash.isEmpty()) {
OUT("WARNING: No ACC given in DICOMs. Unable to calculate HashACC.")
}
if (projectOwner.isEmpty()) {
OUT("WARNING: No Project Owner provided. Unable to calculate HashACC.")
}
if (projectName.isEmpty()) {
OUT("WARNING: No Project Name provided. HashACC might not be unique.")
}
QString key = projectOwner.toLower().leftJustified(16, ' ');
QString value = projectName.toLower() + "-" + RTI->accUsedForHash.toLower();
int hashlen = 8;
uint8_t output_buffer[hashlen] = {};
uint8_t* output_pointer = output_buffer;
siphash(value.toStdString().c_str(), value.length(), key.toStdString().c_str(), output_pointer, 8);
RTI->hashACC="";
for (int i = 0; i < hashlen; i++) {
RTI->hashACC+=QString::number(output_buffer[hashlen-1-i], 16).rightJustified(2, '0', true);
}
RTI->hashACC=RTI->hashACC.toUpper();
OUT("-- Hash ACC is " + RTI->hashACC.toStdString())
return true;
}
bool ModuleSettings::prepareSettings(QString projectID)
{
OUT("")
OUT("-- Receiving AET is " << projectID.toStdString())
QString cleanProjectID = projectID;
if (cleanProjectID.startsWith(AET_PREFIX))
{
// Chop the prefix from the front of the AET
cleanProjectID.remove(0,QString(AET_PREFIX).length());
}
QJsonObject generalSettings = RTI->settingsJson.value("general").toObject();
tags.clear();
projectOwner = "Unknown";
projectName = "Undefined";
projectPrefix = "";
selectedPreset = DEFAULT;
removeSafeTags = false;
removeUnknownTags = true;
removeCurves = true;
removeOverlays = true;
printAssignment = false;
// Read general settings
if (generalSettings.contains("name"))
{
projectName = generalSettings.value("name").toString();
}
if (generalSettings.contains("owner"))
{
projectOwner = generalSettings.value("owner").toString();
}
if (generalSettings.contains("prefix"))
{
projectOwner = generalSettings.value("prefix").toString();
}
if (generalSettings.contains("preset"))
{
selectedPreset=DEFAULT;
if (generalSettings.value("preset").toString().toLower()==PRESET_NONE)
{
selectedPreset = NONE;
// If the preset is set to none, then don't remove unknown tags, as
// this would remove all tags. Also keep overlays and curves, unless
// explicitly specified
removeSafeTags = false;
removeUnknownTags = false;
removeCurves = false;
removeOverlays = false;
}
}
if (generalSettings.contains("remove_unknown_tags"))
{
if (generalSettings.value("remove_unknown_tags").toString().toLower()=="false")
{
removeUnknownTags=false;
}
else
{
removeUnknownTags=true;
}
}
if (generalSettings.contains("remove_safe_tags"))
{
if (generalSettings.value("remove_safe_tags").toString().toLower()=="false")
{
removeSafeTags=false;
}
else
{
removeSafeTags=true;
}
}
if (generalSettings.contains("remove_curves"))
{
if (generalSettings.value("remove_curves").toString().toLower()=="false")
{
removeCurves=false;
}
else
{
removeCurves=true;
}
}
if (generalSettings.contains("remove_overlays"))
{
if (generalSettings.value("remove_overlays").toString().toLower()=="false")
{
removeOverlays=false;
}
else
{
removeOverlays=true;
}
}
if (generalSettings.contains("print_assignment"))
{
if (generalSettings.value("print_assignment").toString().toLower()=="true")
{
printAssignment=true;
}
}
// Read project specific settings
if ((!cleanProjectID.isEmpty()) && (RTI->settingsJson.contains(cleanProjectID)))
{
OUT("-- Reading specific settings for project " << cleanProjectID.toStdString())
QJsonObject projectSettings = RTI->settingsJson.value(cleanProjectID).toObject();
if (projectSettings.contains("name"))
{
projectName = projectSettings.value("name").toString();
}
if (projectSettings.contains("owner"))
{
projectOwner = projectSettings.value("owner").toString();
}
if (projectSettings.contains("prefix"))
{
projectPrefix = projectSettings.value("prefix").toString();
}
if (projectSettings.contains("preset"))
{
selectedPreset=DEFAULT;
if (projectSettings.value("preset").toString().toLower()==PRESET_NONE)
{
selectedPreset=NONE;
// If the preset is set to none, then don't remove unknown tags, as
// this would remove all tags. Also keep overlays and curves, unless
// explicitly specified
removeSafeTags = false;
removeUnknownTags = false;
removeCurves = false;
removeOverlays = false;
}
}
if (projectSettings.contains("remove_unknown_tags"))
{
if (projectSettings.value("remove_unknown_tags").toString().toLower()=="false")
{
removeUnknownTags=false;
}
else
{
removeUnknownTags=true;
}
}
if (projectSettings.contains("remove_safe_tags"))
{
if (projectSettings.value("remove_safe_tags").toString().toLower()=="false")
{
removeSafeTags=false;
}
else
{
removeSafeTags=true;
}
}
if (projectSettings.contains("remove_curves"))
{
if (projectSettings.value("remove_curves").toString().toLower()=="false")
{
removeCurves=false;
}
else
{
removeCurves=true;
}
}
if (projectSettings.contains("remove_overlays"))
{
if (projectSettings.value("remove_overlays").toString().toLower()=="false")
{
removeOverlays=false;
}
else
{
removeOverlays=true;
}
}
}
else
{
OUT("-- No project-specific settings found")
}
// Add preset assignments
if (!Presets::addAssignments())
{
OUT("ERROR: Invalid preset settings. Aborting.")
return false;
}
// Add general assignments
foreach(const QString& key, generalSettings.keys())
{
QJsonValue value = generalSettings.value(key);
if (!addTag(key, value.toString(), "general"))
{
OUT("ERROR: Invalid settings found. Aborting.")
return false;
}
//qDebug() << "Key = " << key << ", Value = " << value.toString();
}
// Add project specific assignments
if ((!cleanProjectID.isEmpty()) && (RTI->settingsJson.contains(cleanProjectID)))
{
QJsonObject projectAssignments = RTI->settingsJson.value(cleanProjectID).toObject();
foreach(const QString& key, projectAssignments.keys())
{
QJsonValue value = projectAssignments.value(key);
if (!addTag(key, value.toString(), "project"))
{
OUT("ERROR: Invalid assignment found. Aborting.")
return false;
}
//qDebug() << "Key = " << key << ", Value = " << value.toString();
}
}
calculateHashACC();
replaceAllParameterMacros();
if (removeSafeTags)
{
OUT("-- Removing tags flagged as SAFE")
QMapIterator<QString, TagEntry> i(tags);
while (i.hasNext())
{
i.next();
if (i.value().command==TagEntry::SAFE)
{
tags[i.key()].command = TagEntry::REMOVE;
}
}
}
if (removeUnknownTags)
{
OUT("-- Removing all unknown tags")
}
if (removeCurves)
{
OUT("-- Removing embedded curves")
}
if (removeOverlays)
{
OUT("-- Removing embedded overlays")
}
if (printAssignment)
{
printTags();
}
isPrepared=true;
return true;
}
bool ModuleSettings::addTag(QString key, QString value, QString source)
{
Uint16 group = 0;
Uint16 element = 0;
QString keyID = key;
if ((!keyID.startsWith("(")) || (!keyID.endsWith(")")))
{
// Entry is not a tag assignment. Ignore
return true;
}
keyID.chop(1);
keyID.remove(0,1);
QStringList items = keyID.split(",");
if (items.length()<2)
{
OUT("ERROR: Invalid key format " << key.toStdString())
return false;
}
bool okGroup=false;
group = items.at(0).toUInt(&okGroup, 16);
bool okElement=false;
element = items.at(1).toUInt(&okElement, 16);
if ((!okGroup) || (!okElement))
{
OUT("ERROR: Invalid key format " << key.toStdString())
return false;
}
// Interpret the command and safe the parameter if needed
TagEntry::Command command = TagEntry::KEEP;
QString parameter = "";
if (value.startsWith(COMMAND_ID_KEEP))
{
command = TagEntry::KEEP;
}
else if (value.startsWith(COMMAND_ID_SAFE))
{
command = TagEntry::SAFE;
}
else if (value.startsWith(COMMAND_ID_REMOVE))
{
command = TagEntry::REMOVE;
}
else if (value.startsWith(COMMAND_ID_CLEAR))
{
command = TagEntry::CLEAR;
}
else if (value.startsWith(QString(COMMAND_ID_SET)+"("))
{
command = TagEntry::SET;
if (!value.endsWith(")"))
{
OUT("ERROR: Invalid command found " << value.toStdString())
return false;
}
parameter = value;
parameter.chop(1);
parameter.remove(0,parameter.indexOf("(")+1);
}
else if (value.startsWith(COMMAND_ID_TRUNCDATE))
{
command = TagEntry::TRUNCDATE;
}
else
{
OUT("ERROR: Invalid command found " << value.toStdString())
return false;
}
// Overwrite the tag assignment if already existing, otherwise create new entry
if (tags.contains(key))
{
tags[key].set(group, element, command, parameter, source);
}
else
{
tags.insert(key, TagEntry(group, element, command, parameter, source));
}
return true;
}
void ModuleSettings::replaceAllParameterMacros()
{
QMapIterator<QString, TagEntry> i(tags);
while (i.hasNext())
{
i.next();
if (!i.value().parameter.isEmpty())
{
tags[i.key()].replaceParameterMacros();
}
}
}
void ModuleSettings::printTags()
{
OUT("")
OUT("-- TAG ASSIGNMENT --------------------------------------------------------------------")
QMapIterator<QString, TagEntry> i(tags);
while (i.hasNext())
{
i.next();
// Print all assignments (except tags marked as SAFE, which would make the list hard to read)
if (i.value().command != TagEntry::SAFE)
{
OUT(" " << i.key().toStdString() << ": CMD=" << i.value().getCommandName().toStdString() << ", PARAM=\"" << i.value().parameter.toStdString() << "\", SOURCE=" << i.value().source.toStdString() << ", GROUP=" << i.value().group << ", ELEMENT=" << i.value().element)
}
}
OUT("--------------------------------------------------------------------------------------")
OUT("")
}