-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQGLWidgetMapWindow.cpp
167 lines (129 loc) · 3.52 KB
/
QGLWidgetMapWindow.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
#include "QGLWidgetMapWindow.h"
#include <QColor>
#include <QDebug>
#include <QFile>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QString>
int kAnimationDuration = 10000;
MapWindow::MapWindow(QMapboxGLSettings settings, QObject *parent)
: m_map(parent, settings)
, m_bearingAnimation(&m_map, "bearing")
, m_zoomAnimation(&m_map, "zoom")
{
connect(&m_map, SIGNAL(needsRendering()), this, SLOT(updateGL()));
// Set default location to 31 Anaconda Drive, North Haven.
m_map.setCoordinateZoom(QMapbox::Coordinate(-34.7958563, 138.4881648), 10);
QFile lightStyle(":/resources/mapbox/light.json");
if (!lightStyle.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray style = lightStyle.readAll();
m_map.setStyleJson(QString(style));
connect(&m_zoomAnimation, SIGNAL(finished()), this, SLOT(animationFinished()));
connect(&m_zoomAnimation, SIGNAL(valueChanged(const QVariant&)), this, SLOT(animationValueChanged()));
}
void MapWindow :: addRoute()
{
// The data source for the route line and markers
QFile geojson(":/resources/mapbox/testRoute3.json");
if (!geojson.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray data = geojson.readAll();
QVariantMap routeSource;
routeSource["type"] = "geojson";
routeSource["data"] = QString(data);
m_map.addSource("routeSource", routeSource);
// The route, painted on top of the route case
QVariantMap route;
route["id"] = "route";
route["type"] = "line";
route["source"] = "routeSource";
m_map.addLayer(route);
m_map.setPaintProperty("route", "line-color", QColor(255,0,0));
m_map.setPaintProperty("route", "line-width", 8.0);
m_map.setLayoutProperty("route", "line-join", "round");
m_map.setLayoutProperty("route", "line-cap", "round");
}
void MapWindow::animationFinished()
{
}
void MapWindow::animationValueChanged()
{
m_animationTicks++;
}
void MapWindow::keyPressEvent(QKeyEvent *ev)
{
}
void MapWindow::mousePressEvent(QMouseEvent *ev)
{
#if QT_VERSION < 0x050000
m_lastPos = ev->posF();
#else
m_lastPos = ev->localPos();
#endif
// if (ev->type() == QEvent::MouseButtonPress) {
// if (ev->buttons() == (Qt::LeftButton | Qt::RightButton)) {
// }
// }
if (ev->type() == QEvent::MouseButtonDblClick) {
if (ev->buttons() == Qt::LeftButton) {
m_map.scaleBy(2.0, m_lastPos);
} else if (ev->buttons() == Qt::RightButton) {
m_map.scaleBy(0.5, m_lastPos);
}
}
ev->accept();
}
void MapWindow::mouseMoveEvent(QMouseEvent *ev)
{
#if QT_VERSION < 0x050000
QPointF delta = ev->posF() - m_lastPos;
#else
QPointF delta = ev->localPos() - m_lastPos;
#endif
if (!delta.isNull()) {
if (ev->buttons() == Qt::LeftButton && ev->modifiers() & Qt::ShiftModifier) {
m_map.setPitch(m_map.pitch() - delta.y());
} else if (ev->buttons() == Qt::LeftButton) {
m_map.moveBy(delta);
} else if (ev->buttons() == Qt::RightButton) {
#if QT_VERSION < 0x050000
m_map.rotateBy(m_lastPos, ev->posF());
#else
m_map.rotateBy(m_lastPos, ev->localPos());
#endif
}
}
#if QT_VERSION < 0x050000
m_lastPos = ev->posF();
#else
m_lastPos = ev->localPos();
#endif
ev->accept();
}
void MapWindow::wheelEvent(QWheelEvent *ev)
{
if (ev->orientation() == Qt::Horizontal) {
return;
}
float factor = ev->delta() / 1200.;
if (ev->delta() < 0) {
factor = factor > -1 ? factor : 1 / factor;
}
m_map.scaleBy(1 + factor, ev->pos());
ev->accept();
}
void MapWindow::initializeGL()
{
QMapbox::initializeGLExtensions();
}
void MapWindow::resizeGL(int w, int h)
{
QSize size(w, h);
m_map.resize(size);
}
void MapWindow::paintGL()
{
m_frameDraws++;
m_map.render();
}