|
1 #include <QGraphicsSceneMouseEvent> |
|
2 #include <QGraphicsPathItem> |
|
3 #include <QtEndian> |
|
4 |
|
5 #include "drawmapscene.h" |
|
6 |
|
7 template <class T> T sqr(const T & x) |
|
8 { |
|
9 return x*x; |
|
10 } |
|
11 |
|
12 DrawMapScene::DrawMapScene(QObject *parent) : |
|
13 QGraphicsScene(parent), |
|
14 m_pen(Qt::yellow), |
|
15 m_brush(Qt::yellow) |
|
16 { |
|
17 setSceneRect(0, 0, 4096, 2048); |
|
18 |
|
19 QLinearGradient gradient(0, 0, 0, 2048); |
|
20 gradient.setColorAt(0, QColor(60, 60, 155)); |
|
21 gradient.setColorAt(1, QColor(155, 155, 60)); |
|
22 setBackgroundBrush(QBrush(gradient)); |
|
23 |
|
24 m_pen.setWidth(67); |
|
25 m_pen.setJoinStyle(Qt::RoundJoin); |
|
26 m_pen.setCapStyle(Qt::RoundCap); |
|
27 m_currPath = 0; |
|
28 } |
|
29 |
|
30 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
31 { |
|
32 if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
|
33 { |
|
34 QPainterPath path = m_currPath->path(); |
|
35 path.lineTo(mouseEvent->scenePos()); |
|
36 paths.first().append(mouseEvent->scenePos().toPoint()); |
|
37 m_currPath->setPath(path); |
|
38 |
|
39 emit pathChanged(); |
|
40 } |
|
41 } |
|
42 |
|
43 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
44 { |
|
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()); |
|
52 paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint()); |
|
53 m_currPath->setPath(path); |
|
54 |
|
55 emit pathChanged(); |
|
56 } |
|
57 |
|
58 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
59 { |
|
60 simplifyLast(); |
|
61 |
|
62 m_currPath = 0; |
|
63 } |
|
64 |
|
65 void DrawMapScene::undo() |
|
66 { |
|
67 if(items().size()) |
|
68 { |
|
69 removeItem(items().first()); |
|
70 paths.removeFirst(); |
|
71 |
|
72 emit pathChanged(); |
|
73 } |
|
74 } |
|
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; |
|
88 if(!cnt) flags |= 0x80; |
|
89 b.append((const char *)&px, 2); |
|
90 b.append((const char *)&py, 2); |
|
91 b.append((const char *)&flags, 1); |
|
92 |
|
93 ++cnt; |
|
94 } |
|
95 |
|
96 } |
|
97 |
|
98 return b; |
|
99 } |
|
100 |
|
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); |
|
114 quint8 flags = *(quint8 *)data.data(); |
|
115 data.remove(0, 1); |
|
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 |
|
133 void DrawMapScene::simplifyLast() |
|
134 { |
|
135 QList<QPoint> points = paths[0]; |
|
136 |
|
137 QPoint prevPoint = points.first(); |
|
138 int i = 1; |
|
139 while(i < points.size()) |
|
140 { |
|
141 if( (i != points.size() - 1) |
|
142 && (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
|
143 ) |
|
144 points.removeAt(i); |
|
145 else |
|
146 { |
|
147 prevPoint = points[i]; |
|
148 ++i; |
|
149 } |
|
150 } |
|
151 |
|
152 paths[0] = points; |
|
153 |
|
154 |
|
155 // redraw path |
|
156 { |
|
157 QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]); |
|
158 pathItem->setPath(pointsToPath(paths[0])); |
|
159 } |
|
160 |
|
161 emit pathChanged(); |
|
162 } |
|
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 } |