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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Copyright (C) 2017 Mapbox, Inc.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qgeomapmapboxgl.h"
#include "qgeomapmapboxgl_p.h"
#include "qsgmapboxglnode.h"
#include "qmapboxglstylechange_p.h"
#include <QtCore/QByteArray>
#include <QtCore/QCoreApplication>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFramebufferObject>
#include <QtLocation/private/qdeclarativecirclemapitem_p.h>
#include <QtLocation/private/qdeclarativegeomapitembase_p.h>
#include <QtLocation/private/qdeclarativepolygonmapitem_p.h>
#include <QtLocation/private/qdeclarativepolylinemapitem_p.h>
#include <QtLocation/private/qdeclarativerectanglemapitem_p.h>
#include <QtLocation/private/qgeomapparameter_p.h>
#include <QtLocation/private/qgeoprojection_p.h>
#include <QtQuick/QQuickWindow>
#include <QtQuick/QSGImageNode>
#include <QtQuick/private/qsgtexture_p.h>
#include <QMapboxGL>
#include <cmath>
// FIXME: Expose from Mapbox GL constants
#define MBGL_TILE_SIZE 512.0
namespace {
// WARNING! The development token is subject to Mapbox Terms of Services
// and must not be used in production.
static char developmentToken[] =
"pk.eyJ1IjoicXRzZGsiLCJhIjoiY2l5azV5MHh5MDAwdTMybzBybjUzZnhxYSJ9.9rfbeqPjX2BusLRDXHCOBA";
static const double invLog2 = 1.0 / std::log(2.0);
static double zoomLevelFrom256(double zoomLevelFor256, double tileSize)
{
return std::log(std::pow(2.0, zoomLevelFor256) * 256.0 / tileSize) * invLog2;
}
} // namespace
QGeoMapMapboxGLPrivate::QGeoMapMapboxGLPrivate(QGeoMappingManagerEngineMapboxGL *engine)
: QGeoMapPrivate(engine, new QGeoProjectionWebMercator)
{
}
QGeoMapMapboxGLPrivate::~QGeoMapMapboxGLPrivate()
{
}
QSGNode *QGeoMapMapboxGLPrivate::updateSceneGraph(QSGNode *node, QQuickWindow *window)
{
Q_Q(QGeoMapMapboxGL);
if (m_viewportSize.isEmpty()) {
delete node;
return 0;
}
QMapboxGL *map = 0;
if (!node) {
QOpenGLContext *currentCtx = QOpenGLContext::currentContext();
if (!currentCtx) {
qWarning("QOpenGLContext is NULL!");
return node;
}
if (m_useFBO) {
QSGMapboxGLTextureNode *mbglNode = new QSGMapboxGLTextureNode(m_settings, m_viewportSize, window->devicePixelRatio(), q);
QObject::connect(mbglNode->map(), &QMapboxGL::mapChanged, q, &QGeoMapMapboxGL::onMapChanged);
m_syncState = MapTypeSync | CameraDataSync | ViewportSync;
node = mbglNode;
} else {
QSGMapboxGLRenderNode *mbglNode = new QSGMapboxGLRenderNode(m_settings, m_viewportSize, window->devicePixelRatio(), q);
QObject::connect(mbglNode->map(), &QMapboxGL::mapChanged, q, &QGeoMapMapboxGL::onMapChanged);
m_syncState = MapTypeSync | CameraDataSync | ViewportSync;
node = mbglNode;
}
}
map = (m_useFBO) ? static_cast<QSGMapboxGLTextureNode *>(node)->map()
: static_cast<QSGMapboxGLRenderNode *>(node)->map();
if (m_syncState & MapTypeSync) {
m_developmentMode = m_activeMapType.name().startsWith("mapbox://")
&& m_settings.accessToken() == developmentToken;
map->setStyleUrl(m_activeMapType.name());
}
if (m_syncState & CameraDataSync) {
map->setZoom(zoomLevelFrom256(m_cameraData.zoomLevel() , MBGL_TILE_SIZE));
map->setBearing(m_cameraData.bearing());
map->setPitch(m_cameraData.tilt());
QGeoCoordinate coordinate = m_cameraData.center();
map->setCoordinate(QMapbox::Coordinate(coordinate.latitude(), coordinate.longitude()));
}
if (m_syncState & ViewportSync) {
if (m_useFBO) {
static_cast<QSGMapboxGLTextureNode *>(node)->resize(m_viewportSize, window->devicePixelRatio());
} else {
map->resize(m_viewportSize, m_viewportSize * window->devicePixelRatio());
}
}
if (m_styleLoaded) {
syncStyleChanges(map);
}
if (m_useFBO) {
static_cast<QSGMapboxGLTextureNode *>(node)->render(window);
}
threadedRenderingHack(window, map);
m_syncState = NoSync;
return node;
}
void QGeoMapMapboxGLPrivate::addParameter(QGeoMapParameter *param)
{
Q_Q(QGeoMapMapboxGL);
QObject::connect(param, &QGeoMapParameter::propertyUpdated, q,
&QGeoMapMapboxGL::onParameterPropertyUpdated);
if (m_styleLoaded) {
m_styleChanges << QMapboxGLStyleChange::addMapParameter(param);
emit q->sgNodeChanged();
}
}
void QGeoMapMapboxGLPrivate::removeParameter(QGeoMapParameter *param)
{
Q_Q(QGeoMapMapboxGL);
q->disconnect(param);
}
QGeoMap::ItemTypes QGeoMapMapboxGLPrivate::supportedMapItemTypes() const
{
return QGeoMap::MapRectangle | QGeoMap::MapCircle | QGeoMap::MapPolygon | QGeoMap::MapPolyline;
}
void QGeoMapMapboxGLPrivate::addMapItem(QDeclarativeGeoMapItemBase *item)
{
Q_Q(QGeoMapMapboxGL);
switch (item->itemType()) {
case QGeoMap::NoItem:
case QGeoMap::MapQuickItem:
case QGeoMap::CustomMapItem:
return;
case QGeoMap::MapRectangle: {
QDeclarativeRectangleMapItem *mapItem = static_cast<QDeclarativeRectangleMapItem *>(item);
QObject::connect(mapItem, &QQuickItem::visibleChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativeGeoMapItemBase::mapItemOpacityChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativeRectangleMapItem::bottomRightChanged, q, &QGeoMapMapboxGL::onMapItemGeometryChanged);
QObject::connect(mapItem, &QDeclarativeRectangleMapItem::topLeftChanged, q, &QGeoMapMapboxGL::onMapItemGeometryChanged);
QObject::connect(mapItem, &QDeclarativeRectangleMapItem::colorChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem->border(), &QDeclarativeMapLineProperties::colorChanged, q, &QGeoMapMapboxGL::onMapItemSubPropertyChanged);
QObject::connect(mapItem->border(), &QDeclarativeMapLineProperties::widthChanged, q, &QGeoMapMapboxGL::onMapItemUnsupportedPropertyChanged);
} break;
case QGeoMap::MapCircle: {
QDeclarativeCircleMapItem *mapItem = static_cast<QDeclarativeCircleMapItem *>(item);
QObject::connect(mapItem, &QQuickItem::visibleChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativeGeoMapItemBase::mapItemOpacityChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativeCircleMapItem::centerChanged, q, &QGeoMapMapboxGL::onMapItemGeometryChanged);
QObject::connect(mapItem, &QDeclarativeCircleMapItem::radiusChanged, q, &QGeoMapMapboxGL::onMapItemGeometryChanged);
QObject::connect(mapItem, &QDeclarativeCircleMapItem::colorChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem->border(), &QDeclarativeMapLineProperties::colorChanged, q, &QGeoMapMapboxGL::onMapItemSubPropertyChanged);
QObject::connect(mapItem->border(), &QDeclarativeMapLineProperties::widthChanged, q, &QGeoMapMapboxGL::onMapItemUnsupportedPropertyChanged);
} break;
case QGeoMap::MapPolygon: {
QDeclarativePolygonMapItem *mapItem = static_cast<QDeclarativePolygonMapItem *>(item);
QObject::connect(mapItem, &QQuickItem::visibleChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativeGeoMapItemBase::mapItemOpacityChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativePolygonMapItem::pathChanged, q, &QGeoMapMapboxGL::onMapItemGeometryChanged);
QObject::connect(mapItem, &QDeclarativePolygonMapItem::colorChanged, q, &QGeoMapMapboxGL::onMapItemGeometryChanged);
QObject::connect(mapItem->border(), &QDeclarativeMapLineProperties::colorChanged, q, &QGeoMapMapboxGL::onMapItemSubPropertyChanged);
QObject::connect(mapItem->border(), &QDeclarativeMapLineProperties::widthChanged, q, &QGeoMapMapboxGL::onMapItemUnsupportedPropertyChanged);
} break;
case QGeoMap::MapPolyline: {
QDeclarativePolylineMapItem *mapItem = static_cast<QDeclarativePolylineMapItem *>(item);
QObject::connect(mapItem, &QQuickItem::visibleChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativeGeoMapItemBase::mapItemOpacityChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
QObject::connect(mapItem, &QDeclarativePolylineMapItem::pathChanged, q, &QGeoMapMapboxGL::onMapItemGeometryChanged);
QObject::connect(mapItem->line(), &QDeclarativeMapLineProperties::colorChanged, q, &QGeoMapMapboxGL::onMapItemSubPropertyChanged);
QObject::connect(mapItem->line(), &QDeclarativeMapLineProperties::widthChanged, q, &QGeoMapMapboxGL::onMapItemSubPropertyChanged);
} break;
}
QObject::connect(item, &QDeclarativeGeoMapItemBase::mapItemOpacityChanged, q, &QGeoMapMapboxGL::onMapItemPropertyChanged);
m_styleChanges << QMapboxGLStyleChange::addMapItem(item, m_mapItemsBefore);
emit q->sgNodeChanged();
}
void QGeoMapMapboxGLPrivate::removeMapItem(QDeclarativeGeoMapItemBase *item)
{
Q_Q(QGeoMapMapboxGL);
switch (item->itemType()) {
case QGeoMap::NoItem:
case QGeoMap::MapQuickItem:
case QGeoMap::CustomMapItem:
return;
case QGeoMap::MapRectangle:
q->disconnect(static_cast<QDeclarativeRectangleMapItem *>(item)->border());
break;
case QGeoMap::MapCircle:
q->disconnect(static_cast<QDeclarativeCircleMapItem *>(item)->border());
break;
case QGeoMap::MapPolygon:
q->disconnect(static_cast<QDeclarativePolygonMapItem *>(item)->border());
break;
case QGeoMap::MapPolyline:
q->disconnect(static_cast<QDeclarativePolylineMapItem *>(item)->line());
break;
}
q->disconnect(item);
m_styleChanges << QMapboxGLStyleChange::removeMapItem(item);
emit q->sgNodeChanged();
}
void QGeoMapMapboxGLPrivate::changeViewportSize(const QSize &)
{
Q_Q(QGeoMapMapboxGL);
m_syncState = m_syncState | ViewportSync;
emit q->sgNodeChanged();
}
void QGeoMapMapboxGLPrivate::changeCameraData(const QGeoCameraData &)
{
Q_Q(QGeoMapMapboxGL);
m_syncState = m_syncState | CameraDataSync;
emit q->sgNodeChanged();
}
void QGeoMapMapboxGLPrivate::changeActiveMapType(const QGeoMapType)
{
Q_Q(QGeoMapMapboxGL);
m_syncState = m_syncState | MapTypeSync;
emit q->sgNodeChanged();
}
void QGeoMapMapboxGLPrivate::syncStyleChanges(QMapboxGL *map)
{
for (const auto& change : m_styleChanges) {
change->apply(map);
}
m_styleChanges.clear();
}
void QGeoMapMapboxGLPrivate::threadedRenderingHack(QQuickWindow *window, QMapboxGL *map)
{
// FIXME: Optimal support for threaded rendering needs core changes
// in Mapbox GL Native. Meanwhile we need to set a timer to update
// the map until all the resources are loaded, which is not exactly
// battery friendly, because might trigger more paints than we need.
if (!m_warned) {
m_threadedRendering = window->openglContext()->thread() != QCoreApplication::instance()->thread();
if (m_threadedRendering) {
qWarning() << "Threaded rendering is not optimal in the Mapbox GL plugin.";
}
m_warned = true;
}
if (m_threadedRendering) {
if (!map->isFullyLoaded()) {
QMetaObject::invokeMethod(&m_refresh, "start", Qt::QueuedConnection);
} else {
QMetaObject::invokeMethod(&m_refresh, "stop", Qt::QueuedConnection);
}
}
}
/*
* QGeoMapMapboxGL implementation
*/
QGeoMapMapboxGL::QGeoMapMapboxGL(QGeoMappingManagerEngineMapboxGL *engine, QObject *parent)
: QGeoMap(*new QGeoMapMapboxGLPrivate(engine), parent), m_engine(engine)
{
Q_D(QGeoMapMapboxGL);
connect(&d->m_refresh, &QTimer::timeout, this, &QGeoMap::sgNodeChanged);
d->m_refresh.setInterval(250);
}
QGeoMapMapboxGL::~QGeoMapMapboxGL()
{
}
QString QGeoMapMapboxGL::copyrightsStyleSheet() const
{
return QStringLiteral("* { vertical-align: middle; font-weight: normal }");
}
void QGeoMapMapboxGL::setMapboxGLSettings(const QMapboxGLSettings& settings)
{
Q_D(QGeoMapMapboxGL);
d->m_settings = settings;
// If the access token is not set, use the development access token.
// This will only affect mapbox:// styles.
if (d->m_settings.accessToken().isEmpty()) {
d->m_settings.setAccessToken(developmentToken);
}
}
void QGeoMapMapboxGL::setUseFBO(bool useFBO)
{
Q_D(QGeoMapMapboxGL);
d->m_useFBO = useFBO;
}
void QGeoMapMapboxGL::setMapItemsBefore(const QString &before)
{
Q_D(QGeoMapMapboxGL);
d->m_mapItemsBefore = before;
}
QSGNode *QGeoMapMapboxGL::updateSceneGraph(QSGNode *oldNode, QQuickWindow *window)
{
Q_D(QGeoMapMapboxGL);
return d->updateSceneGraph(oldNode, window);
}
void QGeoMapMapboxGL::onMapChanged(QMapboxGL::MapChange change)
{
Q_D(QGeoMapMapboxGL);
if (change == QMapboxGL::MapChangeDidFinishLoadingStyle || change == QMapboxGL::MapChangeDidFailLoadingMap) {
d->m_styleLoaded = true;
} else if (change == QMapboxGL::MapChangeWillStartLoadingMap) {
d->m_styleLoaded = false;
d->m_styleChanges.clear();
for (QDeclarativeGeoMapItemBase *item : d->m_mapItems)
d->m_styleChanges << QMapboxGLStyleChange::addMapItem(item, d->m_mapItemsBefore);
for (QGeoMapParameter *param : d->m_mapParameters)
d->m_styleChanges << QMapboxGLStyleChange::addMapParameter(param);
}
}
void QGeoMapMapboxGL::onMapItemPropertyChanged()
{
Q_D(QGeoMapMapboxGL);
QDeclarativeGeoMapItemBase *item = static_cast<QDeclarativeGeoMapItemBase *>(sender());
d->m_styleChanges << QMapboxGLStyleSetPaintProperty::fromMapItem(item);
d->m_styleChanges << QMapboxGLStyleSetLayoutProperty::fromMapItem(item);
emit sgNodeChanged();
}
void QGeoMapMapboxGL::onMapItemSubPropertyChanged()
{
Q_D(QGeoMapMapboxGL);
QDeclarativeGeoMapItemBase *item = static_cast<QDeclarativeGeoMapItemBase *>(sender()->parent());
d->m_styleChanges << QMapboxGLStyleSetPaintProperty::fromMapItem(item);
emit sgNodeChanged();
}
void QGeoMapMapboxGL::onMapItemUnsupportedPropertyChanged()
{
// TODO https://bugreports.qt.io/browse/QTBUG-58872
qWarning() << "Unsupported property for managed Map item";
}
void QGeoMapMapboxGL::onMapItemGeometryChanged()
{
Q_D(QGeoMapMapboxGL);
QDeclarativeGeoMapItemBase *item = static_cast<QDeclarativeGeoMapItemBase *>(sender());
d->m_styleChanges << QMapboxGLStyleAddSource::fromMapItem(item);
emit sgNodeChanged();
}
void QGeoMapMapboxGL::onParameterPropertyUpdated(QGeoMapParameter *param, const char *)
{
Q_D(QGeoMapMapboxGL);
d->m_styleChanges.append(QMapboxGLStyleChange::addMapParameter(param));
emit sgNodeChanged();
}
void QGeoMapMapboxGL::copyrightsChanged(const QString ©rightsHtml)
{
Q_D(QGeoMapMapboxGL);
QString copyrightsHtmlFinal = copyrightsHtml;
if (d->m_developmentMode) {
copyrightsHtmlFinal.prepend("<a href='https://www.mapbox.com/pricing'>"
+ tr("Development access token, do not use in production.") + "</a> - ");
}
if (d->m_activeMapType.name().startsWith("mapbox://")) {
copyrightsHtmlFinal = "<table><tr><th><img src='qrc:/mapboxgl/logo.png'/></th><th>"
+ copyrightsHtmlFinal + "</th></tr></table>";
}
QGeoMap::copyrightsChanged(copyrightsHtmlFinal);
}
|