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 setBackgroundBrush(QBrush(gradient)); |
42 |
42 |
43 m_pen.setWidth(67); |
43 m_pen.setWidth(72); |
44 m_pen.setJoinStyle(Qt::RoundJoin); |
44 m_pen.setJoinStyle(Qt::RoundJoin); |
45 m_pen.setCapStyle(Qt::RoundCap); |
45 m_pen.setCapStyle(Qt::RoundCap); |
46 m_currPath = 0; |
46 m_currPath = 0; |
47 } |
47 } |
48 |
48 |
77 QPainterPath path = m_currPath->path(); |
77 QPainterPath path = m_currPath->path(); |
78 QPointF p = mouseEvent->scenePos(); |
78 QPointF p = mouseEvent->scenePos(); |
79 p += QPointF(0.01, 0.01); |
79 p += QPointF(0.01, 0.01); |
80 path.moveTo(p); |
80 path.moveTo(p); |
81 path.lineTo(mouseEvent->scenePos()); |
81 path.lineTo(mouseEvent->scenePos()); |
82 paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint()); |
82 paths.prepend(qMakePair(serializePenWidth(m_pen.width()), QList<QPoint>() << mouseEvent->scenePos().toPoint())); |
83 m_currPath->setPath(path); |
83 m_currPath->setPath(path); |
84 |
84 |
85 emit pathChanged(); |
85 emit pathChanged(); |
86 } |
86 } |
87 |
87 |
89 { |
89 { |
90 if (m_currPath) |
90 if (m_currPath) |
91 { |
91 { |
92 QPainterPath path = m_currPath->path(); |
92 QPainterPath path = m_currPath->path(); |
93 path.lineTo(mouseEvent->scenePos()); |
93 path.lineTo(mouseEvent->scenePos()); |
94 paths.first().append(mouseEvent->scenePos().toPoint()); |
94 paths.first().second.append(mouseEvent->scenePos().toPoint()); |
95 m_currPath->setPath(path); |
95 m_currPath->setPath(path); |
96 |
96 |
97 simplifyLast(); |
97 simplifyLast(); |
98 |
98 |
99 m_currPath = 0; |
99 m_currPath = 0; |
|
100 } |
|
101 } |
|
102 |
|
103 void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) |
|
104 { |
|
105 if(wheelEvent->delta() > 0 && m_pen.width() < 512) |
|
106 m_pen.setWidth(m_pen.width() + 10); |
|
107 else if(wheelEvent->delta() < 0 && m_pen.width() >= 12) |
|
108 m_pen.setWidth(m_pen.width() - 10); |
|
109 |
|
110 if(m_currPath) |
|
111 { |
|
112 m_currPath->setPen(m_pen); |
|
113 paths.first().first = serializePenWidth(m_pen.width()); |
100 } |
114 } |
101 } |
115 } |
102 |
116 |
103 void DrawMapScene::undo() |
117 void DrawMapScene::undo() |
104 { |
118 { |
146 QByteArray b; |
160 QByteArray b; |
147 |
161 |
148 for(int i = paths.size() - 1; i >= 0; --i) |
162 for(int i = paths.size() - 1; i >= 0; --i) |
149 { |
163 { |
150 int cnt = 0; |
164 int cnt = 0; |
151 QList<QPoint> points = paths.at(i); |
165 QPair<quint8, QList<QPoint> > points = paths.at(i); |
152 foreach(QPoint point, points) |
166 foreach(QPoint point, points.second) |
153 { |
167 { |
154 qint16 px = qToBigEndian((qint16)point.x()); |
168 qint16 px = qToBigEndian((qint16)point.x()); |
155 qint16 py = qToBigEndian((qint16)point.y()); |
169 qint16 py = qToBigEndian((qint16)point.y()); |
156 quint8 flags = 2; |
170 quint8 flags = 0; |
157 if(!cnt) flags |= 0x80; |
171 if(!cnt) flags = 0x80 + points.first; |
158 b.append((const char *)&px, 2); |
172 b.append((const char *)&px, 2); |
159 b.append((const char *)&py, 2); |
173 b.append((const char *)&py, 2); |
160 b.append((const char *)&flags, 1); |
174 b.append((const char *)&flags, 1); |
161 |
175 |
162 ++cnt; |
176 ++cnt; |
172 oldItems.clear(); |
186 oldItems.clear(); |
173 oldPaths.clear(); |
187 oldPaths.clear(); |
174 clear(); |
188 clear(); |
175 paths.clear(); |
189 paths.clear(); |
176 |
190 |
177 QList<QPoint> points; |
191 QPair<quint8, QList<QPoint> > points; |
178 |
192 |
179 while(data.size() >= 5) |
193 while(data.size() >= 5) |
180 { |
194 { |
181 qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
195 qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
182 data.remove(0, 2); |
196 data.remove(0, 2); |
183 qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
197 qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
184 data.remove(0, 2); |
198 data.remove(0, 2); |
185 quint8 flags = *(quint8 *)data.data(); |
199 quint8 flags = *(quint8 *)data.data(); |
186 data.remove(0, 1); |
200 data.remove(0, 1); |
187 |
201 |
188 if((flags & 0x80) && points.size()) |
202 if(flags & 0x80) |
189 { |
203 { |
190 addPath(pointsToPath(points), m_pen); |
204 if(points.second.size()) |
191 paths.prepend(points); |
205 { |
192 |
206 addPath(pointsToPath(points.second), m_pen); |
193 points.clear(); |
207 |
194 } |
208 paths.prepend(points); |
195 |
209 |
196 points.append(QPoint(px, py)); |
210 points.second.clear(); |
197 } |
211 } |
198 |
212 |
199 if(points.size()) |
213 quint8 penWidth = flags & 0x7f; |
200 { |
214 m_pen.setWidth(deserializePenWidth(penWidth)); |
201 addPath(pointsToPath(points), m_pen); |
215 points.first = penWidth; |
|
216 } |
|
217 |
|
218 points.second.append(QPoint(px, py)); |
|
219 } |
|
220 |
|
221 if(points.second.size()) |
|
222 { |
|
223 addPath(pointsToPath(points.second), m_pen); |
202 paths.prepend(points); |
224 paths.prepend(points); |
203 } |
225 } |
204 |
226 |
205 emit pathChanged(); |
227 emit pathChanged(); |
206 } |
228 } |
207 |
229 |
208 void DrawMapScene::simplifyLast() |
230 void DrawMapScene::simplifyLast() |
209 { |
231 { |
210 if(!paths.size()) return; |
232 if(!paths.size()) return; |
211 |
233 |
212 QList<QPoint> points = paths.at(0); |
234 QList<QPoint> points = paths.at(0).second; |
213 |
235 |
214 QPoint prevPoint = points.first(); |
236 QPoint prevPoint = points.first(); |
215 int i = 1; |
237 int i = 1; |
216 while(i < points.size()) |
238 while(i < points.size()) |
217 { |
239 { |