-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShape.cpp
92 lines (77 loc) · 2.41 KB
/
Shape.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
/*
* Shape.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Shape.h"
namespace mmp {
MShape::MShape(const QVector<QPointF>& vertices_) : _isLocked(false) {
setVertices(vertices_);
build();
}
void MShape::copyFrom(const MShape& shape)
{
// Just copy vertices.
setVertices(shape.getVertices());
}
MShape* MShape::clone() const {
MShape* copyShape = _create();
copyShape->copyFrom(*this);
return copyShape;
}
void MShape::translate(const QPointF& offset)
{
// We can feel free to translate every vertex without check by default.
for (QVector<QPointF>::iterator it = vertices.begin(); it != vertices.end(); ++it)
*it += offset;
}
void MShape::read(const QDomElement& obj)
{
// Read basic data.
Serializable::read(obj);
// Read vertices.
QDomElement verticesObj = obj.firstChildElement("vertices");
QDomNode vertexNode = verticesObj.firstChild();
QVector<QPointF> vertices;
while (!vertexNode.isNull())
{
const QDomElement& vertexElem = vertexNode.toElement();
qreal x = vertexElem.attribute("x").toDouble();
qreal y = vertexElem.attribute("y").toDouble();
vertices.append(QPointF(x, y));
vertexNode = vertexNode.nextSibling();
}
// Set the vertices.
setVertices(vertices);
// Rebuild.
build();
}
void MShape::write(QDomElement& obj)
{
// Write basic data.
Serializable::write(obj);
// Write vertices.
QDomElement verticesObj = obj.ownerDocument().createElement("vertices");
for (int i=0; i<nVertices(); i++)
{
QDomElement vertexObj = obj.ownerDocument().createElement("vertex");
vertexObj.setAttribute("x", QString::number(getVertex(i).x()));
vertexObj.setAttribute("y", QString::number(getVertex(i).y()));
verticesObj.appendChild(vertexObj);
}
obj.appendChild(verticesObj);
}
}