-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinteractivemap.cpp
264 lines (230 loc) · 6.96 KB
/
interactivemap.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "interactivemap.h"
#include "mapellipseitem.h"
#include "mappolygonitem.h"
#include "maprouteitem.h"
#include "mapobjectitem.h"
#include "maprangeringitem.h"
#include "maptrailitem.h"
#include "maplineitem.h"
#include <QDebug>
InteractiveMap::InteractiveMap(QWidget *parent) : GraphicsMap(parent),
m_centerObj(nullptr),
m_scaleable(true)
{
}
bool InteractiveMap::pushOperator(MapOperator *op)
{
if(!op)
return false;
else if(m_operators.contains(op))
return false;
// process end funtion with previos operator
if(!m_operators.isEmpty()) {
auto pre = m_operators.top();
if(pre->mode() == MapOperator::EditOnly)
popOperator();
else
pre->end();
}
m_operators.push(op);
//
op->setScene(this->scene());
op->setMap(this);
if(op->mode() == MapOperator::EditOnly)
connect(op, &MapOperator::completed, this, &InteractiveMap::popOperator, Qt::ConnectionType(Qt::AutoConnection|Qt::UniqueConnection));
connect(op, &MapOperator::modeChanged, this, &InteractiveMap::onOperatorModeChanged, Qt::ConnectionType(Qt::AutoConnection|Qt::UniqueConnection));
// process ready funtion with newlly operator
op->ready();
return true;
}
bool InteractiveMap::popOperator()
{
if(m_operators.isEmpty())
return false;
// unset current
auto op = m_operators.pop();
disconnect(op, &MapOperator::completed, this, &InteractiveMap::popOperator);
op->end();
// set new current
// todo: ignore event for newOp at first
if(!m_operators.isEmpty()) {
auto newOp = m_operators.top();
newOp->ready();
}
return true;
}
MapOperator *InteractiveMap::topOperator() const
{
if(m_operators.isEmpty())
return nullptr;
return m_operators.top();
}
void InteractiveMap::clearOperator()
{
while (!m_operators.isEmpty()) {
popOperator();
}
}
void InteractiveMap::setCenter(const MapObjectItem *obj)
{
if(m_centerObj)
disconnect(obj, &MapObjectItem::coordinateChanged, this, qOverload<const QGeoCoordinate&>(&GraphicsMap::centerOn));
// only case that we center on object at first that we should to save drag mode and anchor mode
else {
m_dragMode = this->dragMode();
m_anchor = this->transformationAnchor();
}
//
m_centerObj = obj;
if(m_centerObj) {
// We should not to drag map when object is always center on map
connect(obj, &MapObjectItem::coordinateChanged, this, qOverload<const QGeoCoordinate&>(&GraphicsMap::centerOn), Qt::ConnectionType(Qt::AutoConnection|Qt::UniqueConnection));
this->setDragMode(QGraphicsView::NoDrag);
this->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
centerOn(obj->coordinate());
}
// restore previous drag mode if centerOn is unset
else {
this->setDragMode(m_dragMode);
this->setTransformationAnchor(m_anchor);
}
}
void InteractiveMap::setZoomable(bool on)
{
m_scaleable = on;
}
void InteractiveMap::wheelEvent(QWheelEvent *e)
{
if(!m_scaleable) {
e->accept();
return;
}
bool increase = e->angleDelta().y() > 0;
if(increase)
this->setZoomLevel(zoomLevel() + 0.2);
else
this->setZoomLevel(zoomLevel() - 0.2);
e->accept();
}
void InteractiveMap::keyPressEvent(QKeyEvent *event)
{
auto op = m_operators.isEmpty() ? nullptr : m_operators.top();
if(!op || !op->handleKeyPressEvent(event))
GraphicsMap::keyPressEvent(event);
}
void InteractiveMap::keyReleaseEvent(QKeyEvent *event)
{
auto op = m_operators.isEmpty() ? nullptr : m_operators.top();
if(!op || !op->handleKeyReleaseEvent(event))
GraphicsMap::keyReleaseEvent(event);
}
void InteractiveMap::mouseMoveEvent(QMouseEvent *event)
{
auto op = m_operators.isEmpty() ? nullptr : m_operators.top();
// TODO: move event will be generated whether press event is triggerd or not
if( !op || !op->handleMouseMoveEvent(event))
GraphicsMap::mouseMoveEvent(event);
}
void InteractiveMap::mousePressEvent(QMouseEvent *event)
{
auto op = m_operators.isEmpty() ? nullptr : m_operators.top();
if(!op || !op->handleMousePressEvent(event))
GraphicsMap::mousePressEvent(event);
viewport()->setCursor(Qt::ArrowCursor);
}
void InteractiveMap::mouseReleaseEvent(QMouseEvent *event)
{
auto op = m_operators.isEmpty() ? nullptr : m_operators.top();
if(!op || !op->handleMouseReleaseEvent(event))
GraphicsMap::mouseReleaseEvent(event);
viewport()->setCursor(Qt::ArrowCursor);
}
void InteractiveMap::mouseDoubleClickEvent(QMouseEvent *event)
{
auto op = m_operators.isEmpty() ? nullptr : m_operators.top();
if(!op || !op->handleMouseDoubleClickEvent(event))
GraphicsMap::mouseDoubleClickEvent(event);
}
void InteractiveMap::onOperatorModeChanged()
{
auto op = dynamic_cast<MapOperator*>(sender());
if(op->mode() == MapOperator::EditOnly) {
connect(op, &MapOperator::completed, this, &InteractiveMap::popOperator, Qt::ConnectionType(Qt::AutoConnection|Qt::UniqueConnection));
}
else {
disconnect(op, &MapOperator::completed, this, &InteractiveMap::popOperator);
}
}
MapOperator::MapOperator(QObject *parent) : QObject(parent)
{
}
void MapOperator::setMode(OperatorMode mode)
{
if(mode == m_mode)
return;
m_mode = mode;
emit modeChanged(mode);
}
void MapOperator::ready()
{
}
void MapOperator::end()
{
// waitting press event to active @enable
m_keyEventEnable = false;
m_mouseEventEnable = false;
}
bool MapOperator::handleKeyPressEvent(QKeyEvent *event)
{
m_keyEventEnable = true;
return keyPressEvent(event);
}
bool MapOperator::handleKeyReleaseEvent(QKeyEvent *event)
{
if(m_skipOnceKeyEvent) {
m_skipOnceKeyEvent = false;
return false;
}
else if(m_keyEventEnable)
return keyReleaseEvent(event);
return false;
}
bool MapOperator::handleMousePressEvent(QMouseEvent *event)
{
// press event means a new event round
m_mouseEventEnable = true;
m_mouseMoveEventEnable = true;
return mousePressEvent(event);
}
bool MapOperator::handleMouseMoveEvent(QMouseEvent *event)
{
// we should propagate event when we in case of mouseTracking
if(m_mouseMoveTrackingEnable) {
return mouseMoveEvent(event);
}
else if(m_mouseEventEnable && m_mouseMoveEventEnable && !m_skipOnceMouseEvent)
return mouseMoveEvent(event);
return false;
}
bool MapOperator::handleMouseReleaseEvent(QMouseEvent *event)
{
m_mouseMoveEventEnable = false;
if(m_skipOnceMouseEvent) {
m_skipOnceMouseEvent = false;
return false;
}
else if(m_mouseEventEnable) {
return mouseReleaseEvent(event);
}
return false;
}
bool MapOperator::handleMouseDoubleClickEvent(QMouseEvent *event)
{
m_mouseMoveEventEnable = true;
if(m_skipOnceMouseEvent) {
return false;
}
else if(m_mouseEventEnable)
return mouseDoubleClickEvent(event);
return false;
}