1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Copyright (C) 2017 Mapbox, Inc. |
5 | ** Contact: http://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtLocation module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL3$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see http://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at http://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or later as published by the Free |
29 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
30 | ** the packaging of this file. Please review the following information to |
31 | ** ensure the GNU General Public License version 2.0 requirements will be |
32 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
33 | ** |
34 | ** $QT_END_LICENSE$ |
35 | ** |
36 | ****************************************************************************/ |
37 | |
38 | #include "qgeomappingmanagerenginemapboxgl.h" |
39 | #include "qgeomapmapboxgl.h" |
40 | |
41 | #include <QtCore/qstandardpaths.h> |
42 | #include <QtLocation/private/qabstractgeotilecache_p.h> |
43 | #include <QtLocation/private/qgeocameracapabilities_p.h> |
44 | #include <QtLocation/private/qgeomaptype_p.h> |
45 | |
46 | #include <QDir> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | QGeoMappingManagerEngineMapboxGL::QGeoMappingManagerEngineMapboxGL(const QVariantMap ¶meters, QGeoServiceProvider::Error *error, QString *errorString) |
51 | : QGeoMappingManagerEngine() |
52 | { |
53 | *error = QGeoServiceProvider::NoError; |
54 | errorString->clear(); |
55 | |
56 | QGeoCameraCapabilities cameraCaps; |
57 | cameraCaps.setMinimumZoomLevel(0.0); |
58 | cameraCaps.setMaximumZoomLevel(20.0); |
59 | cameraCaps.setTileSize(512); |
60 | cameraCaps.setSupportsBearing(true); |
61 | cameraCaps.setSupportsTilting(true); |
62 | cameraCaps.setMinimumTilt(0); |
63 | cameraCaps.setMaximumTilt(60); |
64 | cameraCaps.setMinimumFieldOfView(36.87); |
65 | cameraCaps.setMaximumFieldOfView(36.87); |
66 | setCameraCapabilities(cameraCaps); |
67 | |
68 | QList<QGeoMapType> mapTypes; |
69 | int mapId = 0; |
70 | const QByteArray pluginName = "mapboxgl" ; |
71 | |
72 | if (parameters.contains(QStringLiteral("mapboxgl.china" ))) { |
73 | m_useChinaEndpoint = parameters.value(QStringLiteral("mapboxgl.china" )).toBool(); |
74 | } |
75 | |
76 | if (parameters.contains(QStringLiteral("mapboxgl.api_base_url" ))) { |
77 | const QString apiBaseUrl = parameters.value(QStringLiteral("mapboxgl.api_base_url" )).toString(); |
78 | m_settings.setApiBaseUrl(apiBaseUrl); |
79 | } |
80 | |
81 | QVariantMap metadata; |
82 | metadata["isHTTPS" ] = true; |
83 | |
84 | if (m_useChinaEndpoint) { |
85 | m_settings.setApiBaseUrl(QStringLiteral("https://api.mapbox.cn" )); |
86 | |
87 | mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox://styles/mapbox/streets-zh-v1" ), |
88 | tr(s: "China Streets" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
89 | mapTypes << QGeoMapType(QGeoMapType::GrayStreetMap, QStringLiteral("mapbox://styles/mapbox/light-zh-v1" ), |
90 | tr(s: "China Light" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
91 | mapTypes << QGeoMapType(QGeoMapType::GrayStreetMap, QStringLiteral("mapbox://styles/mapbox/dark-zh-v1" ), |
92 | tr(s: "China Dark" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
93 | } else { |
94 | mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox://styles/mapbox/streets-v10" ), |
95 | tr(s: "Streets" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
96 | mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox://styles/mapbox/basic-v9" ), |
97 | tr(s: "Basic" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
98 | mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox://styles/mapbox/bright-v9" ), |
99 | tr(s: "Bright" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
100 | mapTypes << QGeoMapType(QGeoMapType::TerrainMap, QStringLiteral("mapbox://styles/mapbox/outdoors-v10" ), |
101 | tr(s: "Outdoors" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
102 | mapTypes << QGeoMapType(QGeoMapType::SatelliteMapDay, QStringLiteral("mapbox://styles/mapbox/satellite-v9" ), |
103 | tr(s: "Satellite" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
104 | mapTypes << QGeoMapType(QGeoMapType::HybridMap, QStringLiteral("mapbox://styles/mapbox/satellite-streets-v10" ), |
105 | tr(s: "Satellite Streets" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
106 | mapTypes << QGeoMapType(QGeoMapType::GrayStreetMap, QStringLiteral("mapbox://styles/mapbox/light-v9" ), |
107 | tr(s: "Light" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
108 | mapTypes << QGeoMapType(QGeoMapType::GrayStreetMap, QStringLiteral("mapbox://styles/mapbox/dark-v9" ), |
109 | tr(s: "Dark" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
110 | mapTypes << QGeoMapType(QGeoMapType::TransitMap, QStringLiteral("mapbox://styles/mapbox/navigation-preview-day-v2" ), |
111 | tr(s: "Navigation Preview Day" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
112 | mapTypes << QGeoMapType(QGeoMapType::TransitMap, QStringLiteral("mapbox://styles/mapbox/navigation-preview-night-v2" ), |
113 | tr(s: "Navigation Preview Night" ), false, true, ++mapId, pluginName, cameraCaps, metadata); |
114 | mapTypes << QGeoMapType(QGeoMapType::CarNavigationMap, QStringLiteral("mapbox://styles/mapbox/navigation-guidance-day-v2" ), |
115 | tr(s: "Navigation Guidance Day" ), false, false, ++mapId, pluginName, cameraCaps, metadata); |
116 | mapTypes << QGeoMapType(QGeoMapType::CarNavigationMap, QStringLiteral("mapbox://styles/mapbox/navigation-guidance-night-v2" ), |
117 | tr(s: "Navigation Guidance Night" ), false, true, ++mapId, pluginName, cameraCaps, metadata); |
118 | } |
119 | |
120 | if (parameters.contains(QStringLiteral("mapboxgl.mapping.additional_style_urls" ))) { |
121 | const QString ids = parameters.value(QStringLiteral("mapboxgl.mapping.additional_style_urls" )).toString(); |
122 | const QStringList idList = ids.split(sep: ',', behavior: Qt::SkipEmptyParts); |
123 | |
124 | for (auto it = idList.crbegin(), end = idList.crend(); it != end; ++it) { |
125 | if ((*it).isEmpty()) |
126 | continue; |
127 | if ((*it).startsWith(QStringLiteral("http:" ))) |
128 | metadata["isHTTPS" ] = false; |
129 | else |
130 | metadata["isHTTPS" ] = true; |
131 | |
132 | mapTypes.prepend(t: QGeoMapType(QGeoMapType::CustomMap, *it, |
133 | tr(s: "User provided style" ), false, false, ++mapId, pluginName, cameraCaps, metadata)); |
134 | } |
135 | } |
136 | |
137 | setSupportedMapTypes(mapTypes); |
138 | |
139 | if (parameters.contains(QStringLiteral("mapboxgl.access_token" ))) { |
140 | m_settings.setAccessToken(parameters.value(QStringLiteral("mapboxgl.access_token" )).toString()); |
141 | } |
142 | |
143 | bool memoryCache = false; |
144 | if (parameters.contains(QStringLiteral("mapboxgl.mapping.cache.memory" ))) { |
145 | memoryCache = parameters.value(QStringLiteral("mapboxgl.mapping.cache.memory" )).toBool(); |
146 | m_settings.setCacheDatabasePath(QStringLiteral(":memory:" )); |
147 | } |
148 | |
149 | QString cacheDirectory; |
150 | if (parameters.contains(QStringLiteral("mapboxgl.mapping.cache.directory" ))) { |
151 | cacheDirectory = parameters.value(QStringLiteral("mapboxgl.mapping.cache.directory" )).toString(); |
152 | } else { |
153 | cacheDirectory = QAbstractGeoTileCache::baseLocationCacheDirectory() + QStringLiteral("mapboxgl/" ); |
154 | } |
155 | |
156 | if (!memoryCache && QDir::root().mkpath(dirPath: cacheDirectory)) { |
157 | m_settings.setCacheDatabasePath(cacheDirectory + "/mapboxgl.db" ); |
158 | } |
159 | |
160 | if (parameters.contains(QStringLiteral("mapboxgl.mapping.cache.size" ))) { |
161 | bool ok = false; |
162 | int cacheSize = parameters.value(QStringLiteral("mapboxgl.mapping.cache.size" )).toString().toInt(ok: &ok); |
163 | |
164 | if (ok) |
165 | m_settings.setCacheDatabaseMaximumSize(cacheSize); |
166 | } |
167 | |
168 | if (parameters.contains(QStringLiteral("mapboxgl.mapping.use_fbo" ))) { |
169 | m_useFBO = parameters.value(QStringLiteral("mapboxgl.mapping.use_fbo" )).toBool(); |
170 | } |
171 | |
172 | if (parameters.contains(QStringLiteral("mapboxgl.mapping.items.insert_before" ))) { |
173 | m_mapItemsBefore = parameters.value(QStringLiteral("mapboxgl.mapping.items.insert_before" )).toString(); |
174 | } |
175 | |
176 | engineInitialized(); |
177 | } |
178 | |
179 | QGeoMappingManagerEngineMapboxGL::~QGeoMappingManagerEngineMapboxGL() |
180 | { |
181 | } |
182 | |
183 | QGeoMap *QGeoMappingManagerEngineMapboxGL::createMap() |
184 | { |
185 | QGeoMapMapboxGL* map = new QGeoMapMapboxGL(this, 0); |
186 | map->setMapboxGLSettings(m_settings, useChinaEndpoint: m_useChinaEndpoint); |
187 | map->setUseFBO(m_useFBO); |
188 | map->setMapItemsBefore(m_mapItemsBefore); |
189 | |
190 | return map; |
191 | } |
192 | |
193 | QT_END_NAMESPACE |
194 | |