-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlshapesreader.cpp
281 lines (251 loc) · 7.58 KB
/
xmlshapesreader.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
#include "xmlshapesreader.h"
#include <QString>
#include "shape.h"
#include "box.h"
#include "Cone.h"
#include "Ellipsoid.h"
#include "mainwindow.h"
XmlShapesReader::XmlShapesReader(std::list <Shape*> *linkedlist):
mLinkedList{linkedlist}
{
// mLinkedList.push_back(linkedlist);
//connect(this,SIGNAL(XmlShapesReader.on_likedList_changed(Shape *editShape)),MainWindow,SLOT(MainWindow))
}
QString XmlShapesReader::errorString() const
{
return QObject::tr("%1\nLine %2, column %3")
.arg(mReader.errorString())
.arg(mReader.lineNumber())
.arg(mReader.columnNumber());
}
bool XmlShapesReader::read(QIODevice *device)
{
mReader.setDevice(device);
if (mReader.readNextStartElement()) {
if (mReader.name() == "shapes")
read_shapes();
else
mReader.raiseError(QObject::tr("Not a Shapes file"));
}
return !mReader.error();
}
void XmlShapesReader::read_shapes()
{
int written_shapes{0};
while(mReader.readNextStartElement())
{
if(mReader.name() == "shape")
{
written_shapes++;
read_shape();
}
else
mReader.raiseError(QString("No Shape!!")); //mReader.skipCurrentElement();
}
}
void XmlShapesReader::read_shape()
{
int id{-1};
Vector3 color;
Vector3 translation;
Vector3 rotation;
Vector3 scale;
Shape* shape{nullptr};
while (mReader.readNextStartElement())
{
//Check for any type of listed properties in the xml file.
if (mReader.name() == "id")
read_id(id);
else if(mReader.name() == "color")
read_color(color);
else if (mReader.name() == "translation")
read_xyz(translation);
else if (mReader.name() == "rotation")
read_xyz(rotation);
else if (mReader.name() == "scale")
read_xyz(scale);
else if (mReader.name() == "box" && shape==nullptr)
read_box(id,shape);
else if (mReader.name()=="cone" && shape==nullptr)
read_cone(id,shape);
else if (mReader.name()=="ellipsoid" && shape==nullptr)
read_ellipsoid(id,shape);
else
mReader.skipCurrentElement();
}
//Set the aquired properties into the shape class, then add the shape to the linked-list.
if(shape!=nullptr)
{
//int invalid{-1};
bool valid_values{true};
if(shape->get_id() >=0)
{
if (shape->check_values(color.mX,color.mY,color.mZ))//valid_values,invalid);
shape->set_color(color.mX,color.mY,color.mZ);
else
{
mReader.raiseError(QObject::tr("Corruption in file.\n Color value missing."));
valid_values = false;
}
if (shape->check_values(translation.mX,translation.mY,translation.mZ))
shape->set_translation(translation.mX,translation.mY,translation.mZ);
else
{
mReader.raiseError(QObject::tr("Corruption in file.\n Translation value missing."));
valid_values = false;
}
if (shape->check_values(rotation.mX,rotation.mY,rotation.mZ))
shape->set_rotation(rotation.mX,rotation.mY,rotation.mZ);
else
{
mReader.raiseError(QObject::tr("Corruption in file. Rotation value missing."));
valid_values = false;
}
if (shape->check_values(scale.mX,scale.mY,scale.mZ))
shape->set_scale(scale.mX,scale.mY,scale.mZ);
else
{
mReader.raiseError(QObject::tr("Corruption in file. Scale value missing."));
valid_values = false;
}
if(valid_values)
{
mLinkedList->push_back(shape);
//FIND WAY TO ADD ITEM TO THE LISTWIDGET!!!!!!
// MainWindow *window = MainWindow::;
// window->add_shape_to_UIlist(shape);
}
}
else
mReader.raiseError(QObject::tr("Corruption in file. \nMissing ID"));
}
}
//Read the id from the XML file.
void XmlShapesReader::read_id(int &id)
{
if (mReader.name() == "id")
id=mReader.readElementText().toInt();
}
void XmlShapesReader::read_color(Vector3 &color)
{
while(mReader.readNextStartElement())
{
if (mReader.name() == "red")
{
color.mX = mReader.readElementText().toDouble();
}
else if(mReader.name() == "green")
{
color.mY = mReader.readElementText().toDouble();
}
else if(mReader.name() == "blue")
{
color.mZ = mReader.readElementText().toDouble();
}
else
mReader.skipCurrentElement();
}
}
void XmlShapesReader::read_xyz(Vector3 &vec)
{
vec.mX=-1;
vec.mY=-1;
vec.mZ=-1;
while(mReader.readNextStartElement())
{
if(mReader.name() == "x")
{
vec.mX = mReader.readElementText().toDouble();
}
else if(mReader.name() == "y")
{
vec.mY = mReader.readElementText().toDouble();
}
else if(mReader.name() == "z")
{
vec.mZ = mReader.readElementText().toDouble();
}
else
mReader.skipCurrentElement();
}
}
void XmlShapesReader::read_box(int id,Shape *&shape)
{
//Create new box with initialized properties, then fill in with values from the file.
Box* box = new Box(id);
double height{-1};
double width{-1};
double depth{-1};
while(mReader.readNextStartElement())
{
if(mReader.name() == "height")
{
height = mReader.readElementText().toDouble();
}
else if(mReader.name() == "width")
{
width = mReader.readElementText().toDouble();
}
else if(mReader.name() == "depth")
{
depth = mReader.readElementText().toDouble();
}
else
mReader.skipCurrentElement();
}
box->set_size(height,width,depth);
shape=box;
}
void XmlShapesReader::read_cone(int id, Shape *&shape)
{
Cone *cone = new Cone(id);
double height{-1}, rx{-1}, ry{-1};
//Read through file and get basic properties
while (mReader.readNextStartElement())
{
if(mReader.name() == "height")
{
height = mReader.readElementText().toDouble();
}
else if (mReader.name() == "radiusX")
{
rx = mReader.readElementText().toDouble();
}
else if (mReader.name() == "radiusY")
{
ry = mReader.readElementText().toDouble();
}
else
mReader.skipCurrentElement();
}
//Set properties of new cone
cone->set_size(height,rx,ry);
shape = cone;
}
void XmlShapesReader::read_ellipsoid(int id, Shape *&shape)
{
Ellipsoid *ellipse = new Ellipsoid(id);
double rx{-1}, ry{-1}, rz{-1};
Vector3 xyz_ellipsoid;
mReader.readNextStartElement();
while(mReader.readNextStartElement())//ERROR HERE I NEED TO GO DOWN ONE MORE LEVEL TO READ
{
if(mReader.name()=="x")
{
rx = mReader.readElementText().toDouble();
}
else if(mReader.name() =="y")
{
ry = mReader.readElementText().toDouble();
}
else if (mReader.name() == "z")
{
rz = mReader.readElementText().toDouble();
}
else
mReader.skipCurrentElement();
}
//Set properties of the new Ellipsoid
ellipse->set_size(rx,ry,rz);
shape = ellipse;
}