diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2018-08-01 14:14:30 +0300 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2018-08-06 18:08:02 +0300 |
commit | 66f5be386432e4b9b4755c54c1e5a51138b7acd1 (patch) | |
tree | 80277b287284763448f58245a199630242488fc8 /platform/qt | |
parent | 99a365d7b7459a4092dfa9e0bdec60280aecf57a (diff) | |
download | qtlocation-mapboxgl-66f5be386432e4b9b4755c54c1e5a51138b7acd1.tar.gz |
[Qt] Check if property value is a valid JSON string
Diffstat (limited to 'platform/qt')
-rw-r--r-- | platform/qt/app/mapwindow.cpp | 16 | ||||
-rw-r--r-- | platform/qt/qt.cmake | 1 | ||||
-rw-r--r-- | platform/qt/src/qmapboxgl.cpp | 84 | ||||
-rw-r--r-- | platform/qt/src/qmapboxgl_p.hpp | 3 |
4 files changed, 71 insertions, 33 deletions
diff --git a/platform/qt/app/mapwindow.cpp b/platform/qt/app/mapwindow.cpp index 390d89915a..536731929e 100644 --- a/platform/qt/app/mapwindow.cpp +++ b/platform/qt/app/mapwindow.cpp @@ -229,7 +229,21 @@ void MapWindow::keyPressEvent(QKeyEvent *ev) m_map->setFilter("3d-buildings", buildingsFilterExpression); - m_map->setPaintProperty("3d-buildings", "fill-extrusion-color", "#aaa"); + QString fillExtrusionColorJSON = R"JSON( + [ + "interpolate", + ["linear"], + ["get", "height"], + 0.0, "blue", + 20.0, "royalblue", + 40.0, "cyan", + 60.0, "lime", + 80.0, "yellow", + 100.0, "red" + ] + )JSON"; + + m_map->setPaintProperty("3d-buildings", "fill-extrusion-color", fillExtrusionColorJSON); m_map->setPaintProperty("3d-buildings", "fill-extrusion-opacity", .6); QVariantMap extrusionHeight; diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake index fafbb895dc..ce43177daa 100644 --- a/platform/qt/qt.cmake +++ b/platform/qt/qt.cmake @@ -79,6 +79,7 @@ add_library(qmapboxgl SHARED target_include_directories(qmapboxgl PUBLIC platform/qt/include + PRIVATE src ) target_compile_definitions(qmapboxgl diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 05cb5b7f93..459eacba4b 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -27,6 +27,7 @@ #include <mbgl/style/layers/line_layer.hpp> #include <mbgl/style/layers/raster_layer.hpp> #include <mbgl/style/layers/symbol_layer.hpp> +#include <mbgl/style/rapidjson_conversion.hpp> #include <mbgl/style/sources/geojson_source.hpp> #include <mbgl/style/transition_options.hpp> #include <mbgl/style/image.hpp> @@ -38,6 +39,7 @@ #include <mbgl/util/geo.hpp> #include <mbgl/util/geometry.hpp> #include <mbgl/util/projection.hpp> +#include <mbgl/util/rapidjson.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/util/shared_thread_pool.hpp> #include <mbgl/util/traits.hpp> @@ -60,7 +62,9 @@ #include <QVariantMap> #include <QColor> +#include <functional> #include <memory> +#include <sstream> using namespace QMapbox; @@ -977,6 +981,10 @@ void QMapboxGL::removeAnnotation(QMapbox::AnnotationID id) as defined by the \l {https://www.mapbox.com/mapbox-gl-style-spec/} {Mapbox style specification} for layout properties. Returns true if the operation succeeds, and false otherwise. + The implementation attempts to treat \a value as a JSON string, if the + QVariant inner type is a string. If not a valid JSON string, then it'll + proceed with the mapping described below. + This example hides the layer \c route: \code @@ -1007,23 +1015,9 @@ void QMapboxGL::removeAnnotation(QMapbox::AnnotationID id) \li QVariantList \endtable */ -bool QMapboxGL::setLayoutProperty(const QString& layer, const QString& property_, const QVariant& value) +bool QMapboxGL::setLayoutProperty(const QString& layer, const QString& propertyName, const QVariant& value) { - using namespace mbgl::style; - - Layer* layer_ = d_ptr->mapObj->getStyle().getLayer(layer.toStdString()); - if (!layer_) { - qWarning() << "Layer not found:" << layer; - return false; - } - - auto result = conversion::setLayoutProperty(*layer_, property_.toStdString(), value); - if (result) { - qWarning() << "Error setting layout property" << property_ << "on layer" << layer << ":" << QString::fromStdString(result->message); - return false; - } - - return true; + return d_ptr->setProperty(&mbgl::style::conversion::setLayoutProperty, layer, propertyName, value); } /*! @@ -1031,6 +1025,10 @@ bool QMapboxGL::setLayoutProperty(const QString& layer, const QString& property_ as defined by the \l {https://www.mapbox.com/mapbox-gl-style-spec/} {Mapbox style specification} for paint properties. Returns true if the operation succeeds, and false otherwise. + The implementation attempts to treat \a value as a JSON string, if the + QVariant inner type is a string. If not a valid JSON string, then it'll + proceed with the mapping described below. + For paint properties that take a color as \a value, such as \c fill-color, a string such as \c blue can be passed or a QColor. @@ -1076,23 +1074,10 @@ bool QMapboxGL::setLayoutProperty(const QString& layer, const QString& property_ map->setPaintProperty("route","line-dasharray", lineDashArray); \endcode */ -bool QMapboxGL::setPaintProperty(const QString& layer, const QString& property_, const QVariant& value) -{ - using namespace mbgl::style; - - Layer* layer_ = d_ptr->mapObj->getStyle().getLayer(layer.toStdString()); - if (!layer_) { - qWarning() << "Layer not found:" << layer; - return false; - } - auto result = conversion::setPaintProperty(*layer_, property_.toStdString(), value); - if (result) { - qWarning() << "Error setting paint property" << property_ << "on layer" << layer << ":" << QString::fromStdString(result->message); - return false; - } - - return true; +bool QMapboxGL::setPaintProperty(const QString& layer, const QString& propertyName, const QVariant& value) +{ + return d_ptr->setProperty(&mbgl::style::conversion::setPaintProperty, layer, propertyName, value); } /*! @@ -1917,3 +1902,38 @@ void QMapboxGLPrivate::requestRendering() emit needsRendering(); } } + +bool QMapboxGLPrivate::setProperty(const PropertySetter& setter, const QString& layer, const QString& name, const QVariant& value) { + using namespace mbgl::style; + + Layer* layerObject = mapObj->getStyle().getLayer(layer.toStdString()); + if (!layerObject) { + qWarning() << "Layer not found:" << layer; + return false; + } + + const std::string& propertyString = name.toStdString(); + + mbgl::optional<conversion::Error> result; + + if (value.type() == QVariant::String) { + mbgl::JSDocument document; + document.Parse<0>(value.toString().toStdString()); + if (!document.HasParseError()) { + // Treat value as a valid JSON. + const mbgl::JSValue* jsonValue = &document; + result = setter(*layerObject, propertyString, jsonValue); + } else { + result = setter(*layerObject, propertyString, value); + } + } else { + result = setter(*layerObject, propertyString, value); + } + + if (result) { + qWarning() << "Error setting paint property" << name << "on layer" << layer << ":" << QString::fromStdString(result->message); + return false; + } + + return true; +} diff --git a/platform/qt/src/qmapboxgl_p.hpp b/platform/qt/src/qmapboxgl_p.hpp index 51c7cb8fc4..80789ac8f1 100644 --- a/platform/qt/src/qmapboxgl_p.hpp +++ b/platform/qt/src/qmapboxgl_p.hpp @@ -37,6 +37,9 @@ public: void render(); void setFramebufferObject(quint32 fbo, const QSize& size); + using PropertySetter = std::function<mbgl::optional<mbgl::style::conversion::Error>(mbgl::style::Layer&, const std::string&, const mbgl::style::conversion::Convertible&)>; + bool setProperty(const PropertySetter& setter, const QString& layer, const QString& name, const QVariant& value); + mbgl::EdgeInsets margins; std::unique_ptr<mbgl::Map> mapObj; |