diff --git a/platform/qt/app/mapwindow.cpp b/platform/qt/app/mapwindow.cpp index 0a143280d..58f1619c8 100644 --- a/platform/qt/app/mapwindow.cpp +++ b/platform/qt/app/mapwindow.cpp @@ -61,7 +61,7 @@ void MapWindow::changeStyle() { static uint8_t currentStyleIndex; - auto& styles = QMapbox::defaultStyles(); + auto& styles = m_map->defaultStyles(); m_map->setStyleUrl(styles[currentStyleIndex].first); setWindowTitle(QString("Mapbox GL: ") + styles[currentStyleIndex].second); @@ -467,14 +467,14 @@ void MapWindow::initializeGL() connect(m_map.data(), SIGNAL(needsRendering()), this, SLOT(update())); // Set default location to Helsinki. - m_map->setCoordinateZoom(QMapbox::Coordinate(60.170448, 24.942046), 14); + m_map->setCoordinateZoom(QMapbox::Coordinate(60.170448, 24.942046), 5); - QString styleUrl = qgetenv("MAPBOX_STYLE_URL"); + QString styleUrl = qgetenv("MGL_STYLE_URL"); if (styleUrl.isEmpty()) { changeStyle(); } else { m_map->setStyleUrl(styleUrl); - setWindowTitle(QString("Mapbox GL: ") + styleUrl); + setWindowTitle(QString("MapLibre GL: ") + styleUrl); } m_bearingAnimation = new QPropertyAnimation(m_map.data(), "bearing"); diff --git a/platform/qt/include/qmapbox.hpp b/platform/qt/include/qmapbox.hpp index 3acc9d55e..e9642343b 100644 --- a/platform/qt/include/qmapbox.hpp +++ b/platform/qt/include/qmapbox.hpp @@ -102,8 +102,6 @@ enum NetworkMode { Offline, }; -Q_MAPBOXGL_EXPORT QVector >& defaultStyles(); - Q_MAPBOXGL_EXPORT NetworkMode networkMode(); Q_MAPBOXGL_EXPORT void setNetworkMode(NetworkMode); diff --git a/platform/qt/include/qmapboxgl.hpp b/platform/qt/include/qmapboxgl.hpp index ce4124d1b..743f068ed 100644 --- a/platform/qt/include/qmapboxgl.hpp +++ b/platform/qt/include/qmapboxgl.hpp @@ -16,6 +16,12 @@ class QMapboxGLPrivate; // This header follows the Qt coding style: https://wiki.qt.io/Qt_Coding_Style +// TODO: this will be wrapped at some point +namespace mbgl +{ + class TileServerOptions; +} + class Q_MAPBOXGL_EXPORT QMapboxGLSettings { public: @@ -63,8 +69,8 @@ public: QString assetPath() const; void setAssetPath(const QString &); - QString accessToken() const; - void setAccessToken(const QString &); + QString apiKey() const; + void setApiKey(const QString &); QString apiBaseUrl() const; void setApiBaseUrl(const QString &); @@ -75,6 +81,8 @@ public: std::function resourceTransform() const; void setResourceTransform(const std::function &); + mbgl::TileServerOptions *tileServerOptionsInternal() const; + private: GLContextMode m_contextMode; MapMode m_mapMode; @@ -84,10 +92,11 @@ private: unsigned m_cacheMaximumSize; QString m_cacheDatabasePath; QString m_assetPath; - QString m_accessToken; - QString m_apiBaseUrl; + QString m_apiKey; QString m_localFontFamily; std::function m_resourceTransform; + + mbgl::TileServerOptions *m_tileServerOptionsInternal{}; }; struct Q_MAPBOXGL_EXPORT QMapboxGLCameraOptions { @@ -252,6 +261,8 @@ public: void destroyRenderer(); void setFramebufferObject(quint32 fbo, const QSize &size); + const QVector> &defaultStyles() const; + public slots: void render(); void connectionEstablished(); diff --git a/platform/qt/src/http_file_source.cpp b/platform/qt/src/http_file_source.cpp index e4d89e3d6..0e7167f06 100644 --- a/platform/qt/src/http_file_source.cpp +++ b/platform/qt/src/http_file_source.cpp @@ -11,7 +11,9 @@ namespace mbgl { -HTTPFileSource::Impl::Impl() : m_manager(new QNetworkAccessManager(this)) +HTTPFileSource::Impl::Impl(const ResourceOptions& options) + : m_manager(new QNetworkAccessManager(this)), + m_resourceOptions(options.clone()) { QNetworkProxyFactory::setUseSystemConfiguration(true); } @@ -87,8 +89,18 @@ void HTTPFileSource::Impl::onReplyFinished() reply->deleteLater(); } -HTTPFileSource::HTTPFileSource() - : impl(std::make_unique()) { +void HTTPFileSource::Impl::setResourceOptions(ResourceOptions options) +{ + m_resourceOptions = options; +} + +ResourceOptions HTTPFileSource::Impl::getResourceOptions() +{ + return m_resourceOptions.clone(); +} + +HTTPFileSource::HTTPFileSource(const ResourceOptions& options) + : impl(std::make_unique(options)) { } HTTPFileSource::~HTTPFileSource() = default; @@ -98,4 +110,12 @@ std::unique_ptr HTTPFileSource::request(const Resource& resource, return std::make_unique(impl.get(), resource, callback); } +void HTTPFileSource::setResourceOptions(ResourceOptions options) { + impl->setResourceOptions(options.clone()); +} + +ResourceOptions HTTPFileSource::getResourceOptions() { + return impl->getResourceOptions(); +} + } // namespace mbgl diff --git a/platform/qt/src/http_file_source.hpp b/platform/qt/src/http_file_source.hpp index e9c67a7fc..f3dc64a25 100644 --- a/platform/qt/src/http_file_source.hpp +++ b/platform/qt/src/http_file_source.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -21,18 +22,22 @@ class HTTPFileSource::Impl : public QObject Q_OBJECT public: - Impl(); + Impl(const ResourceOptions& options); virtual ~Impl() = default; void request(HTTPRequest *); void cancel(HTTPRequest *); + void setResourceOptions(ResourceOptions options); + ResourceOptions getResourceOptions(); + public slots: void onReplyFinished(); private: QMap, QVector>> m_pending; QNetworkAccessManager *m_manager; + ResourceOptions m_resourceOptions; }; } // namespace mbgl diff --git a/platform/qt/src/qmapbox.cpp b/platform/qt/src/qmapbox.cpp index 34cb5e86b..ad0b79036 100644 --- a/platform/qt/src/qmapbox.cpp +++ b/platform/qt/src/qmapbox.cpp @@ -1,7 +1,6 @@ #include "qmapbox.hpp" #include -#include #include #include #include @@ -203,26 +202,6 @@ void setNetworkMode(NetworkMode mode) mbgl::NetworkStatus::Set(static_cast(mode)); } -/*! - \fn QVector >& QMapbox::defaultStyles() - - Returns a list containing a pair of string objects, representing the style - URL and name, respectively. -*/ -QVector >& defaultStyles() -{ - static QVector> styles; - - if (styles.isEmpty()) { - for (auto style : mbgl::util::default_styles::orderedStyles) { - styles.append(QPair( - QString::fromStdString(style.url), QString::fromStdString(style.name))); - } - } - - return styles; -} - /*! Returns the amount of meters per pixel from a given \a latitude and \a zoom. */ diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index b379dfd66..5c4735cd6 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -218,8 +219,8 @@ QMapboxGLSettings::QMapboxGLSettings() , m_cacheMaximumSize(mbgl::util::DEFAULT_MAX_CACHE_SIZE) , m_cacheDatabasePath(":memory:") , m_assetPath(QCoreApplication::applicationDirPath()) - , m_accessToken(qgetenv("MAPBOX_ACCESS_TOKEN")) - , m_apiBaseUrl(mbgl::util::API_BASE_URL) + , m_apiKey(qgetenv("MGL_API_KEY")) + , m_tileServerOptionsInternal(new mbgl::TileServerOptions(mbgl::TileServerOptions::DefaultConfiguration())) { } @@ -373,28 +374,24 @@ void QMapboxGLSettings::setAssetPath(const QString &path) } /*! - Returns the access token. + Returns the API key. - By default, it is taken from the environment variable \c MAPBOX_ACCESS_TOKEN + By default, it is taken from the environment variable \c MGL_API_KEY or empty if the variable is not set. */ -QString QMapboxGLSettings::accessToken() const { - return m_accessToken; +QString QMapboxGLSettings::apiKey() const { + return m_apiKey; } /*! - Sets the access \a token. + Sets the API key. - Mapbox-hosted vector tiles and styles require an API - \l {https://www.mapbox.com/help/define-access-token/}{access token}, which you - can obtain from the \l {https://www.mapbox.com/studio/account/tokens/} - {Mapbox account page}. Access tokens associate requests to Mapbox's vector tile - and style APIs with your Mapbox account. They also deter other developers from - using your styles without your permission. + MapTiler-hosted and Mapbox-hosted vector tiles and styles require an API + key or access token. */ -void QMapboxGLSettings::setAccessToken(const QString &token) +void QMapboxGLSettings::setApiKey(const QString &key) { - m_accessToken = token; + m_apiKey = key; } /*! @@ -402,7 +399,7 @@ void QMapboxGLSettings::setAccessToken(const QString &token) */ QString QMapboxGLSettings::apiBaseUrl() const { - return m_apiBaseUrl; + return QString::fromStdString(m_tileServerOptionsInternal->baseURL()); } /*! @@ -414,7 +411,7 @@ QString QMapboxGLSettings::apiBaseUrl() const */ void QMapboxGLSettings::setApiBaseUrl(const QString& url) { - m_apiBaseUrl = url; + m_tileServerOptionsInternal = &m_tileServerOptionsInternal->withBaseURL(url.toStdString()); } /*! @@ -457,6 +454,10 @@ void QMapboxGLSettings::setResourceTransform(const std::function> &QMapboxGL::defaultStyles() const +{ + return d_ptr->defaultStyles; +} + /*! \fn void QMapboxGL::needsRendering() @@ -1710,9 +1720,9 @@ mbgl::MapOptions mapOptionsFromQMapboxGLSettings(const QMapboxGLSettings &settin mbgl::ResourceOptions resourceOptionsFromQMapboxGLSettings(const QMapboxGLSettings &settings) { return std::move(mbgl::ResourceOptions() - .withAccessToken(settings.accessToken().toStdString()) + .withApiKey(settings.apiKey().toStdString()) .withAssetPath(settings.assetPath().toStdString()) - .withBaseURL(settings.apiBaseUrl().toStdString()) + .withTileServerOptions(*settings.tileServerOptionsInternal()) .withCachePath(settings.cacheDatabasePath().toStdString()) .withMaximumCacheSize(settings.cacheDatabaseMaximumSize())); } @@ -1733,6 +1743,10 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin connect(m_mapObserver.get(), SIGNAL(copyrightsChanged(QString)), q, SIGNAL(copyrightsChanged(QString))); auto resourceOptions = resourceOptionsFromQMapboxGLSettings(settings); + for (auto style : resourceOptions.tileServerOptions().defaultStyles()) { + defaultStyles.append(QPair( + QString::fromStdString(style.getUrl()), QString::fromStdString(style.getName()))); + } // Setup the Map object. mapObj = std::make_unique(*this, *m_mapObserver, diff --git a/platform/qt/src/qmapboxgl_p.hpp b/platform/qt/src/qmapboxgl_p.hpp index ec45d89c7..19dcfbe9b 100644 --- a/platform/qt/src/qmapboxgl_p.hpp +++ b/platform/qt/src/qmapboxgl_p.hpp @@ -40,6 +40,7 @@ public: mbgl::EdgeInsets margins; std::unique_ptr mapObj; + QVector> defaultStyles; public slots: void requestRendering(); diff --git a/src/mbgl/storage/http_file_source.hpp b/src/mbgl/storage/http_file_source.hpp index ca15f998d..c4188621f 100644 --- a/src/mbgl/storage/http_file_source.hpp +++ b/src/mbgl/storage/http_file_source.hpp @@ -21,8 +21,9 @@ public: void setResourceOptions(ResourceOptions) override; ResourceOptions getResourceOptions() override; -private: class Impl; + +private: const std::unique_ptr impl; };