4423
|
1 |
#include <QDebug>
|
|
2 |
#include <QGraphicsSceneMouseEvent>
|
4426
|
3 |
#include <QGraphicsPathItem>
|
4427
|
4 |
#include <QtEndian>
|
4423
|
5 |
|
|
6 |
#include "drawmapscene.h"
|
|
7 |
|
|
8 |
DrawMapScene::DrawMapScene(QObject *parent) :
|
4424
|
9 |
QGraphicsScene(parent),
|
4426
|
10 |
m_pen(Qt::yellow),
|
|
11 |
m_brush(Qt::yellow)
|
4423
|
12 |
{
|
|
13 |
setSceneRect(0, 0, 4096, 2048);
|
4424
|
14 |
|
|
15 |
QLinearGradient gradient(0, 0, 0, 2048);
|
4426
|
16 |
gradient.setColorAt(0, QColor(60, 60, 155));
|
|
17 |
gradient.setColorAt(1, QColor(155, 155, 60));
|
4424
|
18 |
setBackgroundBrush(QBrush(gradient));
|
|
19 |
|
4426
|
20 |
m_pen.setWidth(67);
|
|
21 |
m_pen.setJoinStyle(Qt::RoundJoin);
|
|
22 |
m_pen.setCapStyle(Qt::RoundCap);
|
|
23 |
m_currPath = 0;
|
4423
|
24 |
}
|
|
25 |
|
|
26 |
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
|
|
27 |
{
|
|
28 |
|
|
29 |
qDebug() << "move" << mouseEvent->scenePos();
|
4424
|
30 |
|
4426
|
31 |
if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
|
|
32 |
{
|
|
33 |
QPainterPath path = m_currPath->path();
|
|
34 |
path.lineTo(mouseEvent->scenePos());
|
4427
|
35 |
paths.last().append(mouseEvent->scenePos().toPoint());
|
4426
|
36 |
m_currPath->setPath(path);
|
4427
|
37 |
|
|
38 |
emit pathChanged();
|
4426
|
39 |
}
|
4423
|
40 |
}
|
|
41 |
|
|
42 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
|
|
43 |
{
|
|
44 |
qDebug() << "press" << mouseEvent->scenePos();
|
4424
|
45 |
|
4426
|
46 |
m_currPath = addPath(QPainterPath(), m_pen);
|
|
47 |
|
|
48 |
QPainterPath path = m_currPath->path();
|
|
49 |
QPointF p = mouseEvent->scenePos();
|
|
50 |
p += QPointF(0.01, 0.01);
|
|
51 |
path.moveTo(p);
|
|
52 |
path.lineTo(mouseEvent->scenePos());
|
4427
|
53 |
paths.append(QList<QPoint>() << mouseEvent->scenePos().toPoint());
|
4426
|
54 |
m_currPath->setPath(path);
|
|
55 |
|
4427
|
56 |
emit pathChanged();
|
4423
|
57 |
}
|
|
58 |
|
|
59 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
|
|
60 |
{
|
|
61 |
qDebug() << "release" << mouseEvent->scenePos();
|
4426
|
62 |
|
|
63 |
m_currPath = 0;
|
4423
|
64 |
}
|
4424
|
65 |
|
4426
|
66 |
void DrawMapScene::undo()
|
4424
|
67 |
{
|
4426
|
68 |
if(items().size())
|
4427
|
69 |
{
|
4426
|
70 |
removeItem(items().first());
|
4427
|
71 |
paths.removeLast();
|
|
72 |
|
|
73 |
emit pathChanged();
|
|
74 |
}
|
4424
|
75 |
}
|
4427
|
76 |
|
|
77 |
QByteArray DrawMapScene::encode()
|
|
78 |
{
|
|
79 |
QByteArray b;
|
|
80 |
|
|
81 |
foreach(QList<QPoint> points, paths)
|
|
82 |
{
|
|
83 |
int cnt = 0;
|
|
84 |
foreach(QPoint point, points)
|
|
85 |
{
|
|
86 |
qint16 px = qToBigEndian((qint16)point.x());
|
|
87 |
qint16 py = qToBigEndian((qint16)point.y());
|
|
88 |
quint8 flags = 2;
|
|
89 |
if(cnt) flags |= 0x80;
|
|
90 |
b.append((const char *)&flags, 1);
|
|
91 |
b.append((const char *)&px, 2);
|
|
92 |
b.append((const char *)&py, 2);
|
|
93 |
|
|
94 |
++cnt;
|
|
95 |
}
|
|
96 |
|
|
97 |
}
|
|
98 |
|
|
99 |
return b;
|
|
100 |
}
|