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
|
#include "rectangle.h"
Rectangle::Rectangle()
: Line()
{
m_rectangle.setTopLeft(QPoint(10, 50));
m_rectangle.setTopRight(QPoint(30, 50));
m_rectangle.setBottomLeft(QPoint(10, 70));
m_rectangle.setBottomRight(QPoint(30, 70));
}
bool Rectangle::isHit(const QPoint &clickPoint)
{
return m_rectangle.contains(clickPoint);
}
void Rectangle::draw(QPainter *painter)
{
if (m_selected)
{
QPen penTemp(Qt::DotLine);
penTemp.setColor(Qt::red);
painter->setPen(penTemp);
painter->drawEllipse(m_rectangle.topLeft(), 5, 5);
painter->drawEllipse(m_rectangle.topRight(), 5, 5);
painter->drawEllipse(m_rectangle.bottomLeft(), 5, 5);
painter->drawEllipse(m_rectangle.bottomRight(), 5, 5);
}
else
{
QPen penNormal(Qt::SolidLine);
penNormal.setColor(Qt::black);
painter->setPen(penNormal);
}
painter->drawRect(m_rectangle);
}
void Rectangle::move(const QPoint &oldPoint, const QPoint &newPoint)
{
if (m_selected)
{
QPoint offset = newPoint - oldPoint;
QVector3D vecOld(oldPoint);
if (vecOld.distanceToPoint(QVector3D(m_rectangle.topLeft())) < 5)
{
m_rectangle.setTopLeft(m_rectangle.topLeft() + offset);
}
else if (vecOld.distanceToPoint(QVector3D(m_rectangle.topRight())) < 5)
{
m_rectangle.setTopRight(m_rectangle.topRight() + offset);
}
else if (vecOld.distanceToPoint(QVector3D(m_rectangle.bottomLeft())) < 5)
{
m_rectangle.setBottomLeft(m_rectangle.bottomLeft() + offset);
}
else if (vecOld.distanceToPoint(QVector3D(m_rectangle.bottomRight())) < 5)
{
m_rectangle.setBottomRight(m_rectangle.bottomRight() + offset);
}
else
{
m_rectangle.setTopLeft(m_rectangle.topLeft() + offset);
m_rectangle.setBottomRight(m_rectangle.bottomRight() + offset);
}
}
}
bool Rectangle::attributesToDom(QDomDocument *doc, QDomElement &e)
{
if (e.isNull())
{
qDebug() << className() << "::attributesToDom Error: NULL element passed.";
return false;
}
else
{
e.setAttribute("x1", m_rectangle.topLeft().rx());
e.setAttribute("y1", m_rectangle.topLeft().ry());
e.setAttribute("x2", m_rectangle.bottomRight().rx());
e.setAttribute("y2", m_rectangle.bottomRight().ry());
return true;
}
}
bool Rectangle::attributesFromDom(const QDomElement &e)
{
if (e.isNull())
{
qDebug() << className() << "::attributesFromDom Error: NULL element passed.";
return false;
}
else
{
m_rectangle.setTopLeft(QPoint(e.attribute("x1", "0").toInt(),
e.attribute("y1", "0").toInt()));
m_rectangle.setBottomRight(QPoint(e.attribute("x2", "0").toInt(),
e.attribute("y2", "0").toInt()));
return true;
}
}
QString Rectangle::className()
{
return "Rectangle";
}
|