forked from FredTingaud/FSM-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSMView.cpp
More file actions
117 lines (103 loc) · 2.07 KB
/
Copy pathFSMView.cpp
File metadata and controls
117 lines (103 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <fsm-editor/FSMView.h>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>
FSMView::FSMView(QGraphicsScene* scene, QWidget* parent)
: super(scene, parent)
{
setDragMode(QGraphicsView::RubberBandDrag);
}
void FSMView::keyPressEvent(QKeyEvent *event)
{
super::keyPressEvent(event);
if (event->key() == Qt::Key_Space && !event->isAutoRepeat())
{
setScrollMode(true);
}
}
void FSMView::keyReleaseEvent(QKeyEvent *event)
{
super::keyReleaseEvent(event);
if (event->key() == Qt::Key_Space && !event->isAutoRepeat())
{
setScrollMode(false);
}
}
void FSMView::zoomIn()
{
scale(1.2, 1.2);
}
void FSMView::zoomOut()
{
scale(0.8, 0.8);
}
qreal FSMView::currentZoom() const
{
return transform().m11();
}
void FSMView::setZoom(qreal zoom)
{
scale(zoom, zoom);
}
void FSMView::zoomView(int delta)
{
if (delta > 0)
{
zoomIn();
}
else
{
zoomOut();
}
}
void FSMView::drawBackground(QPainter * painter, const QRectF & rect)
{
super::drawBackground(painter, rect);
if (scene()->items().empty())
{
QRectF rect = mapToScene(visibleRegion().boundingRect()).boundingRect();
painter->setPen(Qt::white);
painter->drawText(rect, Qt::AlignCenter, tr("Double click to\ncreate a new state"));
}
}
void FSMView::mousePressEvent(QMouseEvent* e)
{
if (e->button() == Qt::MidButton)
{
setScrollMode(true);
QMouseEvent* left = copyMouseEventLeft(e);
super::mousePressEvent(left);
}
else
{
super::mousePressEvent(e);
}
}
QMouseEvent* FSMView::copyMouseEventLeft(QMouseEvent* e)
{
return new QMouseEvent(e->type(), e->localPos(), Qt::LeftButton, e->buttons() & Qt::LeftButton, e->modifiers());
}
void FSMView::setScrollMode(bool on)
{
if (on)
{
setDragMode(QGraphicsView::ScrollHandDrag);
}
else
{
setDragMode(QGraphicsView::RubberBandDrag);
}
}
void FSMView::mouseReleaseEvent(QMouseEvent* e)
{
if (e->button() == Qt::MidButton)
{
setScrollMode(false);
QMouseEvent* left = copyMouseEventLeft(e);
super::mousePressEvent(left);
}
else
{
super::mouseReleaseEvent(e);
}
}