From 66f5be386432e4b9b4755c54c1e5a51138b7acd1 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Wed, 1 Aug 2018 14:14:30 +0300 Subject: [Qt] Check if property value is a valid JSON string --- platform/qt/src/qmapboxgl.cpp | 84 ++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 32 deletions(-) (limited to 'platform/qt/src/qmapboxgl.cpp') 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 #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) -{ - 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 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; +} -- cgit v1.2.1