4423
|
1 |
#include <QGraphicsSceneMouseEvent>
|
4426
|
2 |
#include <QGraphicsPathItem>
|
4427
|
3 |
#include <QtEndian>
|
4423
|
4 |
|
|
5 |
#include "drawmapscene.h"
|
|
6 |
|
4434
|
7 |
template <class T> T sqr(const T & x)
|
|
8 |
{
|
|
9 |
return x*x;
|
|
10 |
}
|
|
11 |
|
4423
|
12 |
DrawMapScene::DrawMapScene(QObject *parent) :
|
4424
|
13 |
QGraphicsScene(parent),
|
4426
|
14 |
m_pen(Qt::yellow),
|
|
15 |
m_brush(Qt::yellow)
|
4423
|
16 |
{
|
|
17 |
setSceneRect(0, 0, 4096, 2048);
|
4424
|
18 |
|
|
19 |
QLinearGradient gradient(0, 0, 0, 2048);
|
4426
|
20 |
gradient.setColorAt(0, QColor(60, 60, 155));
|
|
21 |
gradient.setColorAt(1, QColor(155, 155, 60));
|
4424
|
22 |
setBackgroundBrush(QBrush(gradient));
|
|
23 |
|
4426
|
24 |
m_pen.setWidth(67);
|
|
25 |
m_pen.setJoinStyle(Qt::RoundJoin);
|
|
26 |
m_pen.setCapStyle(Qt::RoundCap);
|
|
27 |
m_currPath = 0;
|
4423
|
28 |
}
|
|
29 |
|
|
30 |
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
|
|
31 |
{
|
4426
|
32 |
if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
|
|
33 |
{
|
|
34 |
QPainterPath path = m_currPath->path();
|
|
35 |
path.lineTo(mouseEvent->scenePos());
|
4439
|
36 |
paths.first().append(mouseEvent->scenePos().toPoint());
|
4426
|
37 |
m_currPath->setPath(path);
|
4427
|
38 |
|
|
39 |
emit pathChanged();
|
4426
|
40 |
}
|
4423
|
41 |
}
|
|
42 |
|
|
43 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
|
|
44 |
{
|
4426
|
45 |
m_currPath = addPath(QPainterPath(), m_pen);
|
|
46 |
|
|
47 |
QPainterPath path = m_currPath->path();
|
|
48 |
QPointF p = mouseEvent->scenePos();
|
|
49 |
p += QPointF(0.01, 0.01);
|
|
50 |
path.moveTo(p);
|
|
51 |
path.lineTo(mouseEvent->scenePos());
|
4439
|
52 |
paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
|
4426
|
53 |
m_currPath->setPath(path);
|
|
54 |
|
4427
|
55 |
emit pathChanged();
|
4423
|
56 |
}
|
|
57 |
|
|
58 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
|
|
59 |
{
|
4439
|
60 |
simplifyLast();
|
|
61 |
|
4426
|
62 |
m_currPath = 0;
|
4423
|
63 |
}
|
4424
|
64 |
|
4426
|
65 |
void DrawMapScene::undo()
|
4424
|
66 |
{
|
4426
|
67 |
if(items().size())
|
4427
|
68 |
{
|
4426
|
69 |
removeItem(items().first());
|
4439
|
70 |
paths.removeFirst();
|
4427
|
71 |
|
|
72 |
emit pathChanged();
|
|
73 |
}
|
4424
|
74 |
}
|
4427
|
75 |
|
|
76 |
QByteArray DrawMapScene::encode()
|
|
77 |
{
|
|
78 |
QByteArray b;
|
|
79 |
|
|
80 |
foreach(QList<QPoint> points, paths)
|
|
81 |
{
|
|
82 |
int cnt = 0;
|
|
83 |
foreach(QPoint point, points)
|
|
84 |
{
|
|
85 |
qint16 px = qToBigEndian((qint16)point.x());
|
|
86 |
qint16 py = qToBigEndian((qint16)point.y());
|
|
87 |
quint8 flags = 2;
|
4442
|
88 |
if(!cnt) flags |= 0x80;
|
4427
|
89 |
b.append((const char *)&px, 2);
|
|
90 |
b.append((const char *)&py, 2);
|
4457
|
91 |
b.append((const char *)&flags, 1);
|
4427
|
92 |
|
|
93 |
++cnt;
|
|
94 |
}
|
|
95 |
|
|
96 |
}
|
|
97 |
|
|
98 |
return b;
|
|
99 |
}
|
4434
|
100 |
|
4442
|
101 |
void DrawMapScene::decode(QByteArray data)
|
|
102 |
{
|
|
103 |
clear();
|
|
104 |
paths.clear();
|
|
105 |
|
|
106 |
QList<QPoint> points;
|
|
107 |
|
|
108 |
while(data.size() >= 5)
|
|
109 |
{
|
|
110 |
qint16 px = qFromBigEndian(*(qint16 *)data.data());
|
|
111 |
data.remove(0, 2);
|
|
112 |
qint16 py = qFromBigEndian(*(qint16 *)data.data());
|
|
113 |
data.remove(0, 2);
|
4457
|
114 |
quint8 flags = *(quint8 *)data.data();
|
|
115 |
data.remove(0, 1);
|
4442
|
116 |
|
|
117 |
//last chunk or first point
|
|
118 |
if((data.size() < 5) || (flags & 0x80))
|
|
119 |
{
|
|
120 |
if(points.size())
|
|
121 |
{
|
|
122 |
addPath(pointsToPath(points), m_pen);
|
|
123 |
paths.prepend(points);
|
|
124 |
|
|
125 |
points.clear();
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
points.append(QPoint(px, py));
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
4439
|
133 |
void DrawMapScene::simplifyLast()
|
4434
|
134 |
{
|
4439
|
135 |
QList<QPoint> points = paths[0];
|
|
136 |
|
|
137 |
QPoint prevPoint = points.first();
|
|
138 |
int i = 1;
|
|
139 |
while(i < points.size())
|
4434
|
140 |
{
|
4471
|
141 |
if( (i != points.size() - 1)
|
|
142 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
|
|
143 |
)
|
4439
|
144 |
points.removeAt(i);
|
|
145 |
else
|
|
146 |
{
|
|
147 |
prevPoint = points[i];
|
|
148 |
++i;
|
|
149 |
}
|
|
150 |
}
|
4434
|
151 |
|
4439
|
152 |
paths[0] = points;
|
|
153 |
|
|
154 |
|
|
155 |
// redraw path
|
|
156 |
{
|
|
157 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
|
4442
|
158 |
pathItem->setPath(pointsToPath(paths[0]));
|
4434
|
159 |
}
|
|
160 |
|
|
161 |
emit pathChanged();
|
|
162 |
}
|
4442
|
163 |
|
|
164 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points)
|
|
165 |
{
|
|
166 |
QPainterPath path;
|
|
167 |
|
|
168 |
if(points.size())
|
|
169 |
{
|
|
170 |
QPointF p = points[0] + QPointF(0.01, 0.01);
|
|
171 |
path.moveTo(p);
|
|
172 |
|
|
173 |
foreach(QPoint p, points)
|
|
174 |
path.lineTo(p);
|
|
175 |
}
|
|
176 |
|
|
177 |
return path;
|
|
178 |
}
|