91 foreach(QPoint point, points) |
91 foreach(QPoint point, points) |
92 { |
92 { |
93 qint16 px = qToBigEndian((qint16)point.x()); |
93 qint16 px = qToBigEndian((qint16)point.x()); |
94 qint16 py = qToBigEndian((qint16)point.y()); |
94 qint16 py = qToBigEndian((qint16)point.y()); |
95 quint8 flags = 2; |
95 quint8 flags = 2; |
96 if(cnt) flags |= 0x80; |
96 if(!cnt) flags |= 0x80; |
97 b.append((const char *)&flags, 1); |
97 b.append((const char *)&flags, 1); |
98 b.append((const char *)&px, 2); |
98 b.append((const char *)&px, 2); |
99 b.append((const char *)&py, 2); |
99 b.append((const char *)&py, 2); |
100 |
100 |
101 ++cnt; |
101 ++cnt; |
102 } |
102 } |
103 |
103 |
104 } |
104 } |
105 |
105 |
106 return b; |
106 return b; |
|
107 } |
|
108 |
|
109 void DrawMapScene::decode(QByteArray data) |
|
110 { |
|
111 clear(); |
|
112 paths.clear(); |
|
113 |
|
114 QList<QPoint> points; |
|
115 |
|
116 while(data.size() >= 5) |
|
117 { |
|
118 quint8 flags = *(quint8 *)data.data(); |
|
119 data.remove(0, 1); |
|
120 qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
121 data.remove(0, 2); |
|
122 qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
123 data.remove(0, 2); |
|
124 |
|
125 //last chunk or first point |
|
126 if((data.size() < 5) || (flags & 0x80)) |
|
127 { |
|
128 if(points.size()) |
|
129 { |
|
130 qDebug() << points; |
|
131 addPath(pointsToPath(points), m_pen); |
|
132 paths.prepend(points); |
|
133 |
|
134 points.clear(); |
|
135 } |
|
136 } |
|
137 |
|
138 points.append(QPoint(px, py)); |
|
139 } |
107 } |
140 } |
108 |
141 |
109 void DrawMapScene::simplifyLast() |
142 void DrawMapScene::simplifyLast() |
110 { |
143 { |
111 QList<QPoint> points = paths[0]; |
144 QList<QPoint> points = paths[0]; |
126 paths[0] = points; |
159 paths[0] = points; |
127 |
160 |
128 |
161 |
129 // redraw path |
162 // redraw path |
130 { |
163 { |
131 |
|
132 QPainterPath path; |
|
133 QPointF p = paths[0][0] + QPointF(0.01, 0.01); |
|
134 path.moveTo(p); |
|
135 |
|
136 foreach(QPoint p, paths[0]) |
|
137 path.lineTo(p); |
|
138 |
|
139 QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]); |
164 QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]); |
140 pathItem->setPath(path); |
165 pathItem->setPath(pointsToPath(paths[0])); |
141 |
|
142 ++i; |
|
143 } |
166 } |
144 |
167 |
145 emit pathChanged(); |
168 emit pathChanged(); |
146 } |
169 } |
|
170 |
|
171 QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
|
172 { |
|
173 QPainterPath path; |
|
174 |
|
175 if(points.size()) |
|
176 { |
|
177 QPointF p = points[0] + QPointF(0.01, 0.01); |
|
178 path.moveTo(p); |
|
179 |
|
180 foreach(QPoint p, points) |
|
181 path.lineTo(p); |
|
182 } |
|
183 |
|
184 return path; |
|
185 } |