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