[qt] Update for tile server configuration changes (#102)
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -102,8 +102,6 @@ enum NetworkMode {
|
||||
Offline,
|
||||
};
|
||||
|
||||
Q_MAPBOXGL_EXPORT QVector<QPair<QString, QString> >& defaultStyles();
|
||||
|
||||
Q_MAPBOXGL_EXPORT NetworkMode networkMode();
|
||||
Q_MAPBOXGL_EXPORT void setNetworkMode(NetworkMode);
|
||||
|
||||
|
||||
@@ -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<std::string(const std::string &)> resourceTransform() const;
|
||||
void setResourceTransform(const std::function<std::string(const std::string &)> &);
|
||||
|
||||
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<std::string(const std::string &)> 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<QPair<QString, QString>> &defaultStyles() const;
|
||||
|
||||
public slots:
|
||||
void render();
|
||||
void connectionEstablished();
|
||||
|
||||
@@ -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<Impl>()) {
|
||||
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<Impl>(options)) {
|
||||
}
|
||||
|
||||
HTTPFileSource::~HTTPFileSource() = default;
|
||||
@@ -98,4 +110,12 @@ std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource,
|
||||
return std::make_unique<HTTPRequest>(impl.get(), resource, callback);
|
||||
}
|
||||
|
||||
void HTTPFileSource::setResourceOptions(ResourceOptions options) {
|
||||
impl->setResourceOptions(options.clone());
|
||||
}
|
||||
|
||||
ResourceOptions HTTPFileSource::getResourceOptions() {
|
||||
return impl->getResourceOptions();
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <mbgl/storage/http_file_source.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/storage/resource_options.hpp>
|
||||
|
||||
#include <QMap>
|
||||
#include <QNetworkAccessManager>
|
||||
@@ -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<QUrl, QPair<QPointer<QNetworkReply>, QVector<HTTPRequest *>>> m_pending;
|
||||
QNetworkAccessManager *m_manager;
|
||||
ResourceOptions m_resourceOptions;
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "qmapbox.hpp"
|
||||
|
||||
#include <mbgl/storage/network_status.hpp>
|
||||
#include <mbgl/util/default_styles.hpp>
|
||||
#include <mbgl/util/geometry.hpp>
|
||||
#include <mbgl/util/traits.hpp>
|
||||
#include <mbgl/util/projection.hpp>
|
||||
@@ -203,26 +202,6 @@ void setNetworkMode(NetworkMode mode)
|
||||
mbgl::NetworkStatus::Set(static_cast<mbgl::NetworkStatus::Status>(mode));
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn QVector<QPair<QString, QString> >& QMapbox::defaultStyles()
|
||||
|
||||
Returns a list containing a pair of string objects, representing the style
|
||||
URL and name, respectively.
|
||||
*/
|
||||
QVector<QPair<QString, QString> >& defaultStyles()
|
||||
{
|
||||
static QVector<QPair<QString, QString>> styles;
|
||||
|
||||
if (styles.isEmpty()) {
|
||||
for (auto style : mbgl::util::default_styles::orderedStyles) {
|
||||
styles.append(QPair<QString, QString>(
|
||||
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.
|
||||
*/
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
#include <mbgl/util/projection.hpp>
|
||||
#include <mbgl/util/rapidjson.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/tile_server_options.hpp>
|
||||
#include <mbgl/util/traits.hpp>
|
||||
|
||||
#include <QGuiApplication>
|
||||
@@ -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<std::string(con
|
||||
m_resourceTransform = transform;
|
||||
}
|
||||
|
||||
mbgl::TileServerOptions *QMapboxGLSettings::tileServerOptionsInternal() const {
|
||||
return m_tileServerOptionsInternal;
|
||||
}
|
||||
|
||||
/*!
|
||||
\class QMapboxGL
|
||||
\brief The QMapboxGL class is a Qt wrapper for the Mapbox GL Native engine.
|
||||
@@ -1655,6 +1656,15 @@ void QMapboxGL::connectionEstablished()
|
||||
mbgl::NetworkStatus::Reachable();
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a list containing a pair of string objects, representing the style
|
||||
URL and name, respectively.
|
||||
*/
|
||||
const QVector<QPair<QString, QString>> &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, QString>(
|
||||
QString::fromStdString(style.getUrl()), QString::fromStdString(style.getName())));
|
||||
}
|
||||
|
||||
// Setup the Map object.
|
||||
mapObj = std::make_unique<mbgl::Map>(*this, *m_mapObserver,
|
||||
|
||||
@@ -40,6 +40,7 @@ public:
|
||||
|
||||
mbgl::EdgeInsets margins;
|
||||
std::unique_ptr<mbgl::Map> mapObj;
|
||||
QVector<QPair<QString, QString>> defaultStyles;
|
||||
|
||||
public slots:
|
||||
void requestRendering();
|
||||
|
||||
@@ -21,8 +21,9 @@ public:
|
||||
void setResourceOptions(ResourceOptions) override;
|
||||
ResourceOptions getResourceOptions() override;
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
|
||||
private:
|
||||
const std::unique_ptr<Impl> impl;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user