From 66f5be386432e4b9b4755c54c1e5a51138b7acd1 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Wed, 1 Aug 2018 14:14:30 +0300 Subject: [PATCH] [Qt] Check if property value is a valid JSON string --- platform/qt/app/mapwindow.cpp | 16 ++++++- platform/qt/qt.cmake | 1 + platform/qt/src/qmapboxgl.cpp | 84 ++++++++++++++++++++------------- 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 390d89915..536731929 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 fafbb895d..ce43177da 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 05cb5b7f9..459eacba4 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -60,7 +62,9 @@ #include #include +#include #include +#include 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) + +bool QMapboxGL::setPaintProperty(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::setPaintProperty(*layer_, property_.toStdString(), value); - if (result) { - qWarning() << "Error setting paint property" << property_ << "on layer" << layer << ":" << QString::fromStdString(result->message); - return false; - } - - return true; + 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 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 51c7cb8fc..80789ac8f 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::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 mapObj;