1 #include <QDebug> |
1 #include <QDebug> |
2 #include <QGraphicsSceneMouseEvent> |
2 #include <QGraphicsSceneMouseEvent> |
|
3 #include <QGraphicsPathItem> |
3 |
4 |
4 #include "drawmapscene.h" |
5 #include "drawmapscene.h" |
5 |
6 |
6 DrawMapScene::DrawMapScene(QObject *parent) : |
7 DrawMapScene::DrawMapScene(QObject *parent) : |
7 QGraphicsScene(parent), |
8 QGraphicsScene(parent), |
8 m_pen(Qt::black), |
9 m_pen(Qt::yellow), |
9 m_brush(Qt::black) |
10 m_brush(Qt::yellow) |
10 { |
11 { |
11 setSceneRect(0, 0, 4096, 2048); |
12 setSceneRect(0, 0, 4096, 2048); |
12 |
13 |
13 QLinearGradient gradient(0, 0, 0, 2048); |
14 QLinearGradient gradient(0, 0, 0, 2048); |
14 gradient.setColorAt(0, QColor(160, 160, 255)); |
15 gradient.setColorAt(0, QColor(60, 60, 155)); |
15 gradient.setColorAt(1, QColor(255, 255, 160)); |
16 gradient.setColorAt(1, QColor(155, 155, 60)); |
16 setBackgroundBrush(QBrush(gradient)); |
17 setBackgroundBrush(QBrush(gradient)); |
17 |
18 |
18 m_halfWidth = 67; |
19 m_pen.setWidth(67); |
|
20 m_pen.setJoinStyle(Qt::RoundJoin); |
|
21 m_pen.setCapStyle(Qt::RoundCap); |
|
22 m_currPath = 0; |
19 } |
23 } |
20 |
24 |
21 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
25 void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
22 { |
26 { |
23 |
27 |
24 qDebug() << "move" << mouseEvent->scenePos(); |
28 qDebug() << "move" << mouseEvent->scenePos(); |
25 |
29 |
26 if(mouseEvent->buttons() && Qt::LeftButton) |
30 if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
27 drawFigure(mouseEvent->scenePos()); |
31 { |
|
32 QPainterPath path = m_currPath->path(); |
|
33 path.lineTo(mouseEvent->scenePos()); |
|
34 m_currPath->setPath(path); |
|
35 //drawFigure(mouseEvent->scenePos()); |
|
36 } |
28 } |
37 } |
29 |
38 |
30 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
39 void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
31 { |
40 { |
32 qDebug() << "press" << mouseEvent->scenePos(); |
41 qDebug() << "press" << mouseEvent->scenePos(); |
33 |
42 |
34 drawFigure(mouseEvent->scenePos()); |
43 m_currPath = addPath(QPainterPath(), m_pen); |
|
44 |
|
45 QPainterPath path = m_currPath->path(); |
|
46 QPointF p = mouseEvent->scenePos(); |
|
47 p += QPointF(0.01, 0.01); |
|
48 path.moveTo(p); |
|
49 path.lineTo(mouseEvent->scenePos()); |
|
50 m_currPath->setPath(path); |
|
51 |
|
52 //drawFigure(mouseEvent->scenePos()); |
35 } |
53 } |
36 |
54 |
37 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
55 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
38 { |
56 { |
39 qDebug() << "release" << mouseEvent->scenePos(); |
57 qDebug() << "release" << mouseEvent->scenePos(); |
|
58 |
|
59 m_currPath = 0; |
40 } |
60 } |
41 |
61 |
42 void DrawMapScene::drawFigure(const QPointF & point) |
62 void DrawMapScene::undo() |
43 { |
63 { |
44 addEllipse( |
64 if(items().size()) |
45 point.x() - m_halfWidth, |
65 removeItem(items().first()); |
46 point.y() - m_halfWidth, |
|
47 m_halfWidth * 2, |
|
48 m_halfWidth * 2, |
|
49 m_pen, |
|
50 m_brush |
|
51 ); |
|
52 } |
66 } |