-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotections.cpp
More file actions
367 lines (321 loc) · 10.4 KB
/
Copy pathprotections.cpp
File metadata and controls
367 lines (321 loc) · 10.4 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
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
/*************************************************************************
Protections - description
-------------------
authors : Laurent Francioli
copyright : Institut universitaire romand de Sante au Travail
University of Geneva (UniGe)
*************************************************************************/
#include "protections.h"
//Initialize static maps to 0
map<string,int>* Protections::materials = 0;
map<string,vector<string>*>* Protections::clothes = 0;
vector<Protection>* Protections::protections = 0;
void Protections::loadProtectionsLib(const char* filename, bool replace){
//Delete existing protections if any
if(replace){
resetProtectionsLibrary();
}
//Initialize the maps
if(!materials){
materials = new map<string,int>;
}
if(!clothes){
clothes = new map<string,vector<string>*>;
}
QDomDocument doc;
//********************************
// Read the DOM tree form file
//********************************
QFile f(filename);
f.open(QIODevice::ReadOnly);
doc.setContent(&f);
f.close();
//********************************
// Parse the DOM tree
//********************************
QDomElement root=doc.documentElement();
// We traverse the protections
QDomElement child=root.firstChildElement();
while(!child.isNull())
{
if (child.tagName() == "Clothes")
{
parseClothes(child);
}
else if(child.tagName() == "Materials"){
parseMaterials(child);
}
child = child.nextSiblingElement();
}
}
void Protections::loadProtections(const char* filename, bool replace){
//Delete existing protections if any
if(replace){
resetProtections();
}
protections = new vector<Protection>;
QDomDocument doc;
//********************************
// Read the DOM tree form file
//********************************
QFile f(filename);
f.open(QIODevice::ReadOnly);
doc.setContent(&f);
f.close();
//********************************
// Parse the DOM tree
//********************************
QDomElement root=doc.documentElement();
// We traverse the protections
QDomElement child=root.firstChild().toElement();
while(!child.isNull())
{
if (child.tagName() == "Protection")
{
// We traverse clothing and zones
QDomElement grandChild=child.firstChild().toElement();
while(!grandChild.isNull())
{
if (grandChild.tagName() == "Clothing" || grandChild.tagName() == "Zone")
{
Protection p;
p.name = grandChild.attribute("name").toStdString();
if(grandChild.tagName() == "Clothing"){
p.zoneType = Protections::CLOTHINGTYPE;
}
else{
p.zoneType = Protections::ZONETYPE;
}
if(child.hasAttribute("material")){
p.material = child.attribute("material").toStdString();
if(materials && materials->find(p.material)!=materials->end()){
p.IP = (*materials)[p.material];
}
else{
p.IP = 1;
}
}
else{
p.IP = child.attribute("IP").toInt();
}
if(child.attribute("active") == "1"){
p.active = true;
}
else{
p.active = false;
}
protections->push_back(p);
}
grandChild = grandChild.nextSibling().toElement();
}
}
child = child.nextSibling().toElement();
}
}
void Protections::applyProtections(vector<Zone*>* zones){
//Check that the zones exist and are not empty
if(!zones || zones->size() < 1){
return;
}
//Check that the protections list exists
if(!protections){
return;
}
//Apply all protections
for(vector<Protection>::iterator proIter = protections->begin(); proIter != protections->end(); proIter++){
//Check that the protection is active and that the IP is > 1
if(proIter->active && proIter->IP > 1){
//If it's a clothing, check that the clothing is loaded from a protection lib
//and apply the IP to all the zones covered
if(proIter->zoneType == Protections::CLOTHINGTYPE){
vector<string>* protectedZones = (*clothes)[string(proIter->name)];
if(protectedZones){
vector<string>::iterator zoneIter;
for(zoneIter = protectedZones->begin(); zoneIter != protectedZones->end(); ++zoneIter){
applyProtection(zones, *zoneIter, proIter->IP);
}
}
}
//If it's a simple zone, just apply the IP
else{
applyProtection(zones,proIter->name, proIter->IP);
}
}
}
}
void Protections::resetProtectionsLibrary(){
if(materials){
delete materials;
materials = 0;
}
if(clothes){
for(map<string,vector<string>*>::iterator i = clothes->begin(); i != clothes->end(); i++){
delete i->second;
}
clothes->clear();
delete clothes;
clothes = 0;
}
//Update the protections (reset all materials to IP=1)
if(protections){
vector<Protection>::iterator protIter;
for(protIter = protections->begin(); protIter != protections->end(); protIter++){
if(!protIter->material.empty()){
protIter->IP = 1;
}
}
}
}
void Protections::saveProtectionsLibraryAsXML(QTreeWidget* clothesTree, QTreeWidget* materialsTree, string filename){
//Create root XML element
QDomDocument doc("Protections");
QDomElement rootNode = doc.createElement("Protections");
doc.appendChild(rootNode);
//Create the Clothes part
QDomElement clothesNode = doc.createElement("Clothes");
rootNode.appendChild(clothesNode);
//Loop over all the clothes and add them
for(int i = 0; i < clothesTree->topLevelItemCount(); i++){
QDomElement clothingNode = createClothingElement(clothesTree->topLevelItem(i),doc);
clothesNode.appendChild(clothingNode);
}
//Create the Materials part
QDomElement materialsNode = doc.createElement("Materials");
rootNode.appendChild(materialsNode);
//Loop over all the clothes and add them
for(int i = 0; i < materialsTree->topLevelItemCount(); i++){
QDomElement materialNode = doc.createElement("Material");
materialNode.setAttribute("name",materialsTree->topLevelItem(i)->text(0));
materialNode.setAttribute("IP", materialsTree->topLevelItem(i)->text(1));
materialsNode.appendChild(materialNode);
}
//Write the XML file
std::ofstream file(filename.c_str());
if (file.is_open())
{
file << doc.toString().toStdString();
}
else{
// MessageBox(NULL, L"Failed to open file.", L"Error", 0x10000);//MODIFY
}
}
void Protections::resetProtections(){
if(protections){
delete protections;
protections = 0;
}
}
void Protections::saveProtectionsAsXML(QTreeWidget* protections, string filename){
//Create root XML element
QDomDocument doc("Protections");
QDomElement rootNode = doc.createElement("Protections");
doc.appendChild(rootNode);
//Loop over all the clothes and add them
QTreeWidgetItem *currentProtection;
for(int i = 0; i < protections->topLevelItemCount(); i++){
currentProtection = protections->topLevelItem(i);
//Create the main protection node
QDomElement protectionNode = doc.createElement("Protection");
if(currentProtection->text(1).size() > 0){
protectionNode.setAttribute("material",currentProtection->text(1));
}
else{
protectionNode.setAttribute("IP",currentProtection->text(2));
}
if(currentProtection->checkState(0) == Qt::Checked){
protectionNode.setAttribute("active","1");
}
else{
protectionNode.setAttribute("active","0");
}
//Create the typed subnode
QDomElement subNode = doc.createElement(currentProtection->text(3));
subNode.setAttribute("name",currentProtection->text(0));
//Append the nodes
protectionNode.appendChild(subNode);
rootNode.appendChild(protectionNode);
}
//Write the XML file
std::ofstream file(filename.c_str());
if (file.is_open())
{
file << doc.toString().toStdString();
}
else{
//MessageBox(NULL, L"Failed to open file.", L"Error", 0x10000);//MODIFY
}
}
QDomElement Protections::createClothingElement(QTreeWidgetItem* item, QDomDocument doc){
QDomElement clothingNode = doc.createElement("Clothing");
clothingNode.setAttribute("name",item->text(0));
for(int i=0; i<item->childCount();i++){
QDomElement zoneNode = doc.createElement("Zone");
zoneNode.setAttribute("name",item->child(i)->text(0));
clothingNode.appendChild(zoneNode);
}
return clothingNode;
}
void Protections::parseClothes(QDomElement clothesElement){
//Parse and store each of the clothes
QDomElement clothing = clothesElement.firstChildElement();
while(!clothing.isNull()){
if(clothing.tagName() == "Clothing" && clothing.hasAttribute("name")){
//Parse the name of the zones protected by the clothing
vector<string> *zones = new vector<string>;
QDomElement zone = clothing.firstChildElement();
while(!zone.isNull()){
if(zone.tagName() == "Zone" && zone.hasAttribute("name")){
zones->push_back(zone.attribute("name").toStdString());
}
zone = zone.nextSiblingElement();
}
//Add the clothing to the clothes lib
(*clothes)[clothing.attribute("name").toStdString()] = zones;
}
clothing = clothing.nextSiblingElement();
}
}
void Protections::parseMaterials(QDomElement materialsElement){
//Parse and store each of the materials
QDomElement material = materialsElement.firstChildElement();
while(!material.isNull()){
if(material.tagName() == "Material" && material.hasAttribute("name")){
//Check that the IP is a valid number
bool ok;
int IP = material.attribute("IP").toInt(&ok);
if(ok && IP > 0){
//Add the material to the clothes lib
string name = material.attribute("name").toStdString();
(*materials)[name] = IP;
//Update the protections where the material is present
if(protections){
vector<Protection>::iterator protIter;
for(protIter = protections->begin(); protIter != protections->end(); protIter++){
if(protIter->material == name){
protIter->IP = IP;
}
}
}
}
}
material = material.nextSiblingElement();
}
}
bool Protections::applyProtection(vector<Zone*>* zones, string zonename, int IP){
vector<Zone*>::iterator zoneIter;
//Loop over all zones
for(zoneIter = zones->begin(); zoneIter!=zones->end();++zoneIter){
//If the zone found is the right one, apply the IP factor
if((*zoneIter)->getName() == zonename){
(*zoneIter)->setZoneIP(IP);
return true;
}
//Otherwise, if the zone contains subzones, loop over the subzones
else if((*zoneIter)->getSubZones()){
if(applyProtection((*zoneIter)->getSubZones(), zonename, IP)){
return true;
}
}
}
return false;
}