From 29b3f1eb809fd1c84dfab53e99d7f99b7bfe36cc Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 1 Jun 2018 16:24:43 +0300 Subject: Support dynamic properties in QMapboxGLStyleChange QMapboxGLStyleChange should iterate also over dynamic properties when generating the style change objects. These dynamic properties could come from generic QGeoMapParameter objects instantiated from C++ code. Task-number: QTBUG-68598 Change-Id: I01a3e0971615c61f74789446c03255185a373803 Reviewed-by: Paolo Angelelli --- .../geoservices/mapboxgl/qmapboxglstylechange.cpp | 56 ++++++++++++---------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp b/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp index 964f94f5..6c47d3ee 100644 --- a/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp +++ b/src/plugins/geoservices/mapboxgl/qmapboxglstylechange.cpp @@ -45,14 +45,14 @@ namespace { -QString formatPropertyName(QString *name) +QByteArray formatPropertyName(const QByteArray &name) { + QString nameAsString = QString::fromLatin1(name); static const QRegularExpression camelCaseRegex(QStringLiteral("([a-z0-9])([A-Z])")); - - return name->replace(camelCaseRegex, QStringLiteral("\\1-\\2")).toLower(); + return nameAsString.replace(camelCaseRegex, QStringLiteral("\\1-\\2")).toLower().toLatin1(); } -bool isImmutableProperty(const QString &name) +bool isImmutableProperty(const QByteArray &name) { return name == QStringLiteral("type") || name == QStringLiteral("layer"); } @@ -167,6 +167,16 @@ QMapbox::Feature featureFromMapItem(QDeclarativeGeoMapItemBase *item) } } +QList getAllPropertyNamesList(QObject *object) +{ + const QMetaObject *metaObject = object->metaObject(); + QList propertyNames(object->dynamicPropertyNames()); + for (int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i) { + propertyNames.append(metaObject->property(i).name()); + } + return propertyNames; +} + } // namespace @@ -261,22 +271,20 @@ QList> QMapboxGLStyleSetLayoutProperty::fro QList> changes; - // Offset objectName and type properties. - for (int i = 2; i < param->metaObject()->propertyCount(); ++i) { - QString name = param->metaObject()->property(i).name(); - - if (isImmutableProperty(name)) + QList propertyNames = getAllPropertyNamesList(param); + for (const QByteArray &propertyName : propertyNames) { + if (isImmutableProperty(propertyName)) continue; auto layout = new QMapboxGLStyleSetLayoutProperty(); - layout->m_value = param->property(name.toLatin1()); + layout->m_value = param->property(propertyName); if (layout->m_value.canConvert()) { layout->m_value = layout->m_value.value().toVariant(); } layout->m_layer = param->property("layer").toString(); - layout->m_property = formatPropertyName(&name); + layout->m_property = formatPropertyName(propertyName); changes << QSharedPointer(layout); } @@ -340,22 +348,20 @@ QList> QMapboxGLStyleSetPaintProperty::from QList> changes; - // Offset objectName and type properties. - for (int i = 2; i < param->metaObject()->propertyCount(); ++i) { - QString name = param->metaObject()->property(i).name(); - - if (isImmutableProperty(name)) + QList propertyNames = getAllPropertyNamesList(param); + for (const QByteArray &propertyName : propertyNames) { + if (isImmutableProperty(propertyName)) continue; auto paint = new QMapboxGLStyleSetPaintProperty(); - paint->m_value = param->property(name.toLatin1()); + paint->m_value = param->property(propertyName); if (paint->m_value.canConvert()) { paint->m_value = paint->m_value.value().toVariant(); } paint->m_layer = param->property("layer").toString(); - paint->m_property = formatPropertyName(&name); + paint->m_property = formatPropertyName(propertyName); changes << QSharedPointer(paint); } @@ -464,14 +470,16 @@ QSharedPointer QMapboxGLStyleAddLayer::fromMapParameter(QG static const QStringList layerProperties = QStringList() << QStringLiteral("name") << QStringLiteral("layerType") << QStringLiteral("before"); - // Offset objectName and type properties. - for (int i = 2; i < param->metaObject()->propertyCount(); ++i) { - QString name = param->metaObject()->property(i).name(); - QVariant value = param->property(name.toLatin1()); + QList propertyNames = getAllPropertyNamesList(param); + for (const QByteArray &propertyName : propertyNames) { + if (isImmutableProperty(propertyName)) + continue; + + const QVariant value = param->property(propertyName); - switch (layerProperties.indexOf(name)) { + switch (layerProperties.indexOf(propertyName)) { case -1: - layer->m_params[formatPropertyName(&name)] = value; + layer->m_params[formatPropertyName(propertyName)] = value; break; case 0: // name layer->m_params[QStringLiteral("id")] = value; -- cgit v1.2.1