36 setSceneRect(0, 0, 4096, 2048); |
36 setSceneRect(0, 0, 4096, 2048); |
37 |
37 |
38 QLinearGradient gradient(0, 0, 0, 2048); |
38 QLinearGradient gradient(0, 0, 0, 2048); |
39 gradient.setColorAt(0, QColor(60, 60, 155)); |
39 gradient.setColorAt(0, QColor(60, 60, 155)); |
40 gradient.setColorAt(1, QColor(155, 155, 60)); |
40 gradient.setColorAt(1, QColor(155, 155, 60)); |
41 setBackgroundBrush(QBrush(gradient)); |
41 |
|
42 m_eraser = QBrush(gradient); |
|
43 setBackgroundBrush(m_eraser); |
|
44 m_isErasing = false; |
42 |
45 |
43 m_pen.setWidth(76); |
46 m_pen.setWidth(76); |
44 m_pen.setJoinStyle(Qt::RoundJoin); |
47 m_pen.setJoinStyle(Qt::RoundJoin); |
45 m_pen.setCapStyle(Qt::RoundCap); |
48 m_pen.setCapStyle(Qt::RoundCap); |
46 m_currPath = 0; |
49 m_currPath = 0; |
77 QPainterPath path = m_currPath->path(); |
80 QPainterPath path = m_currPath->path(); |
78 QPointF p = mouseEvent->scenePos(); |
81 QPointF p = mouseEvent->scenePos(); |
79 p += QPointF(0.01, 0.01); |
82 p += QPointF(0.01, 0.01); |
80 path.moveTo(p); |
83 path.moveTo(p); |
81 path.lineTo(mouseEvent->scenePos()); |
84 path.lineTo(mouseEvent->scenePos()); |
82 paths.prepend(qMakePair(serializePenWidth(m_pen.width()), QList<QPoint>() << mouseEvent->scenePos().toPoint())); |
85 |
|
86 PathParams params; |
|
87 params.width = serializePenWidth(m_pen.width()); |
|
88 params.erasing = m_isErasing; |
|
89 params.points = QList<QPoint>() << mouseEvent->scenePos().toPoint(); |
|
90 paths.prepend(params); |
83 m_currPath->setPath(path); |
91 m_currPath->setPath(path); |
84 |
92 |
85 emit pathChanged(); |
93 emit pathChanged(); |
86 } |
94 } |
87 |
95 |
153 paths.clear(); |
161 paths.clear(); |
154 |
162 |
155 emit pathChanged(); |
163 emit pathChanged(); |
156 } |
164 } |
157 |
165 |
|
166 |
|
167 void DrawMapScene::setErasing(bool erasing) |
|
168 { |
|
169 m_isErasing = erasing; |
|
170 if(erasing) |
|
171 m_pen.setBrush(m_eraser); |
|
172 else |
|
173 m_pen.setBrush(m_brush); |
|
174 } |
|
175 |
158 QByteArray DrawMapScene::encode() |
176 QByteArray DrawMapScene::encode() |
159 { |
177 { |
160 QByteArray b; |
178 QByteArray b; |
161 |
179 |
162 for(int i = paths.size() - 1; i >= 0; --i) |
180 for(int i = paths.size() - 1; i >= 0; --i) |
163 { |
181 { |
164 int cnt = 0; |
182 int cnt = 0; |
165 QPair<quint8, QList<QPoint> > points = paths.at(i); |
183 PathParams params = paths.at(i); |
166 foreach(QPoint point, points.second) |
184 foreach(QPoint point, params.points) |
167 { |
185 { |
168 qint16 px = qToBigEndian((qint16)point.x()); |
186 qint16 px = qToBigEndian((qint16)point.x()); |
169 qint16 py = qToBigEndian((qint16)point.y()); |
187 qint16 py = qToBigEndian((qint16)point.y()); |
170 quint8 flags = 0; |
188 quint8 flags = 0; |
171 if(!cnt) flags = 0x80 + points.first; |
189 if(!cnt) flags = 0x80 + params.width; |
|
190 if(params.erasing) flags |= 0x40; |
172 b.append((const char *)&px, 2); |
191 b.append((const char *)&px, 2); |
173 b.append((const char *)&py, 2); |
192 b.append((const char *)&py, 2); |
174 b.append((const char *)&flags, 1); |
193 b.append((const char *)&flags, 1); |
175 |
194 |
176 ++cnt; |
195 ++cnt; |
199 quint8 flags = *(quint8 *)data.data(); |
220 quint8 flags = *(quint8 *)data.data(); |
200 data.remove(0, 1); |
221 data.remove(0, 1); |
201 |
222 |
202 if(flags & 0x80) |
223 if(flags & 0x80) |
203 { |
224 { |
204 if(points.second.size()) |
225 if(params.points.size()) |
205 { |
226 { |
206 addPath(pointsToPath(points.second), m_pen); |
227 addPath(pointsToPath(params.points), m_pen); |
207 |
228 |
208 paths.prepend(points); |
229 paths.prepend(params); |
209 |
230 |
210 points.second.clear(); |
231 params.points.clear(); |
211 } |
232 } |
212 |
233 |
213 quint8 penWidth = flags & 0x7f; |
234 quint8 penWidth = flags & 0x3f; |
214 m_pen.setWidth(deserializePenWidth(penWidth)); |
235 m_pen.setWidth(deserializePenWidth(penWidth)); |
215 points.first = penWidth; |
236 if(flags & 0x40) |
216 } |
237 m_pen.setBrush(m_eraser); |
217 |
238 else |
218 points.second.append(QPoint(px, py)); |
239 m_pen.setBrush(m_brush); |
219 } |
240 params.width = penWidth; |
220 |
241 } |
221 if(points.second.size()) |
242 |
222 { |
243 params.points.append(QPoint(px, py)); |
223 addPath(pointsToPath(points.second), m_pen); |
244 } |
224 paths.prepend(points); |
245 |
|
246 if(params.points.size()) |
|
247 { |
|
248 addPath(pointsToPath(params.points), m_pen); |
|
249 paths.prepend(params); |
225 } |
250 } |
226 |
251 |
227 emit pathChanged(); |
252 emit pathChanged(); |
|
253 |
|
254 setErasing(erasing); |
228 } |
255 } |
229 |
256 |
230 void DrawMapScene::simplifyLast() |
257 void DrawMapScene::simplifyLast() |
231 { |
258 { |
232 if(!paths.size()) return; |
259 if(!paths.size()) return; |
233 |
260 |
234 QList<QPoint> points = paths.at(0).second; |
261 QList<QPoint> points = paths.at(0).points; |
235 |
262 |
236 QPoint prevPoint = points.first(); |
263 QPoint prevPoint = points.first(); |
237 int i = 1; |
264 int i = 1; |
238 while(i < points.size()) |
265 while(i < points.size()) |
239 { |
266 { |