author | koda |
Sat, 19 Feb 2011 01:38:40 +0100 | |
changeset 4948 | c3dc41ae68fa |
parent 4938 | 0985edac2ad7 |
child 4976 | 088d40d8aba2 |
permissions | -rw-r--r-- |
4423 | 1 |
#include <QGraphicsSceneMouseEvent> |
4426 | 2 |
#include <QGraphicsPathItem> |
4427 | 3 |
#include <QtEndian> |
4937 | 4 |
#include <QDebug> |
4423 | 5 |
|
6 |
#include "drawmapscene.h" |
|
7 |
||
4434 | 8 |
template <class T> T sqr(const T & x) |
9 |
{ |
|
10 |
return x*x; |
|
11 |
} |
|
12 |
||
4423 | 13 |
DrawMapScene::DrawMapScene(QObject *parent) : |
4424 | 14 |
QGraphicsScene(parent), |
4426 | 15 |
m_pen(Qt::yellow), |
16 |
m_brush(Qt::yellow) |
|
4423 | 17 |
{ |
18 |
setSceneRect(0, 0, 4096, 2048); |
|
4424 | 19 |
|
20 |
QLinearGradient gradient(0, 0, 0, 2048); |
|
4426 | 21 |
gradient.setColorAt(0, QColor(60, 60, 155)); |
22 |
gradient.setColorAt(1, QColor(155, 155, 60)); |
|
4424 | 23 |
setBackgroundBrush(QBrush(gradient)); |
24 |
||
4426 | 25 |
m_pen.setWidth(67); |
26 |
m_pen.setJoinStyle(Qt::RoundJoin); |
|
27 |
m_pen.setCapStyle(Qt::RoundCap); |
|
28 |
m_currPath = 0; |
|
4423 | 29 |
} |
30 |
||
31 |
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
32 |
{ |
|
4426 | 33 |
if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
34 |
{ |
|
35 |
QPainterPath path = m_currPath->path(); |
|
4937 | 36 |
|
4938
0985edac2ad7
use ctrl instead of alt to avoid colliding with linux window positioning
nemo
parents:
4937
diff
changeset
|
37 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
4937 | 38 |
{ |
39 |
int c = path.elementCount(); |
|
40 |
QPointF pos = mouseEvent->scenePos(); |
|
41 |
path.setElementPositionAt(c - 1, pos.x(), pos.y()); |
|
42 |
||
43 |
} else |
|
44 |
{ |
|
45 |
path.lineTo(mouseEvent->scenePos()); |
|
46 |
paths.first().append(mouseEvent->scenePos().toPoint()); |
|
47 |
} |
|
4426 | 48 |
m_currPath->setPath(path); |
4427 | 49 |
|
50 |
emit pathChanged(); |
|
4426 | 51 |
} |
4423 | 52 |
} |
53 |
||
54 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
55 |
{ |
|
4426 | 56 |
m_currPath = addPath(QPainterPath(), m_pen); |
57 |
||
58 |
QPainterPath path = m_currPath->path(); |
|
59 |
QPointF p = mouseEvent->scenePos(); |
|
60 |
p += QPointF(0.01, 0.01); |
|
61 |
path.moveTo(p); |
|
62 |
path.lineTo(mouseEvent->scenePos()); |
|
4439 | 63 |
paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint()); |
4426 | 64 |
m_currPath->setPath(path); |
65 |
||
4427 | 66 |
emit pathChanged(); |
4423 | 67 |
} |
68 |
||
69 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
70 |
{ |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
71 |
Q_UNUSED(mouseEvent); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
72 |
|
4439 | 73 |
simplifyLast(); |
74 |
||
4426 | 75 |
m_currPath = 0; |
4423 | 76 |
} |
4424 | 77 |
|
4426 | 78 |
void DrawMapScene::undo() |
4424 | 79 |
{ |
4426 | 80 |
if(items().size()) |
4427 | 81 |
{ |
4426 | 82 |
removeItem(items().first()); |
4439 | 83 |
paths.removeFirst(); |
4427 | 84 |
|
85 |
emit pathChanged(); |
|
86 |
} |
|
4424 | 87 |
} |
4427 | 88 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
89 |
void DrawMapScene::clearMap() |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
90 |
{ |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
91 |
clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
92 |
paths.clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
93 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
94 |
emit pathChanged(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
95 |
} |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
96 |
|
4427 | 97 |
QByteArray DrawMapScene::encode() |
98 |
{ |
|
99 |
QByteArray b; |
|
100 |
||
4666 | 101 |
for(int i = paths.size() - 1; i >= 0; --i) |
4427 | 102 |
{ |
103 |
int cnt = 0; |
|
4666 | 104 |
QList<QPoint> points = paths.at(i); |
4427 | 105 |
foreach(QPoint point, points) |
106 |
{ |
|
107 |
qint16 px = qToBigEndian((qint16)point.x()); |
|
108 |
qint16 py = qToBigEndian((qint16)point.y()); |
|
109 |
quint8 flags = 2; |
|
4442 | 110 |
if(!cnt) flags |= 0x80; |
4427 | 111 |
b.append((const char *)&px, 2); |
112 |
b.append((const char *)&py, 2); |
|
4457 | 113 |
b.append((const char *)&flags, 1); |
4427 | 114 |
|
115 |
++cnt; |
|
116 |
} |
|
117 |
||
118 |
} |
|
119 |
||
120 |
return b; |
|
121 |
} |
|
4434 | 122 |
|
4442 | 123 |
void DrawMapScene::decode(QByteArray data) |
124 |
{ |
|
125 |
clear(); |
|
126 |
paths.clear(); |
|
127 |
||
128 |
QList<QPoint> points; |
|
129 |
||
130 |
while(data.size() >= 5) |
|
131 |
{ |
|
132 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
133 |
data.remove(0, 2); |
|
134 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
135 |
data.remove(0, 2); |
|
4457 | 136 |
quint8 flags = *(quint8 *)data.data(); |
137 |
data.remove(0, 1); |
|
4442 | 138 |
|
4666 | 139 |
if((flags & 0x80) && points.size()) |
4442 | 140 |
{ |
4666 | 141 |
addPath(pointsToPath(points), m_pen); |
142 |
paths.prepend(points); |
|
143 |
||
144 |
points.clear(); |
|
145 |
} |
|
4442 | 146 |
|
4666 | 147 |
points.append(QPoint(px, py)); |
148 |
} |
|
149 |
||
150 |
if(points.size()) |
|
151 |
{ |
|
152 |
addPath(pointsToPath(points), m_pen); |
|
153 |
paths.prepend(points); |
|
4442 | 154 |
} |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
155 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
156 |
emit pathChanged(); |
4442 | 157 |
} |
158 |
||
4439 | 159 |
void DrawMapScene::simplifyLast() |
4434 | 160 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
161 |
if(!paths.size()) return; |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
162 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
163 |
QList<QPoint> points = paths.at(0); |
4439 | 164 |
|
165 |
QPoint prevPoint = points.first(); |
|
166 |
int i = 1; |
|
167 |
while(i < points.size()) |
|
4434 | 168 |
{ |
4471 | 169 |
if( (i != points.size() - 1) |
170 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
|
171 |
) |
|
4439 | 172 |
points.removeAt(i); |
173 |
else |
|
174 |
{ |
|
175 |
prevPoint = points[i]; |
|
176 |
++i; |
|
177 |
} |
|
178 |
} |
|
4434 | 179 |
|
4439 | 180 |
paths[0] = points; |
181 |
||
182 |
||
183 |
// redraw path |
|
184 |
{ |
|
185 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]); |
|
4442 | 186 |
pathItem->setPath(pointsToPath(paths[0])); |
4434 | 187 |
} |
188 |
||
189 |
emit pathChanged(); |
|
190 |
} |
|
4442 | 191 |
|
192 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
|
193 |
{ |
|
194 |
QPainterPath path; |
|
195 |
||
196 |
if(points.size()) |
|
197 |
{ |
|
198 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
199 |
path.moveTo(p); |
|
200 |
||
201 |
foreach(QPoint p, points) |
|
202 |
path.lineTo(p); |
|
203 |
} |
|
204 |
||
205 |
return path; |
|
206 |
} |