diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-09-22 18:36:52 +0300 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-09-26 11:47:34 +0200 |
commit | 3e42a0de9fd79196c3bc1bb3e843f6b242660953 (patch) | |
tree | 1000d39871f53499d037bdfe3cfa344708b72e20 /platform | |
parent | 478241a90f9ab995640e630bdf689060fe026fa5 (diff) | |
download | qtlocation-mapboxgl-3e42a0de9fd79196c3bc1bb3e843f6b242660953.tar.gz |
[Qt] s/QQuickMapboxGL{Style,StyleProperty}/QQuickMapboxGLMapParameter
Diffstat (limited to 'platform')
-rw-r--r-- | platform/qt/include/QQuickMapboxGLStyle | 1 | ||||
-rw-r--r-- | platform/qt/include/QQuickMapboxGLStyleProperty | 1 | ||||
-rw-r--r-- | platform/qt/include/qquickmapboxgl.hpp | 41 | ||||
-rw-r--r-- | platform/qt/include/qquickmapboxglmapparameter.hpp | 1 | ||||
-rw-r--r-- | platform/qt/include/qquickmapboxglstyle.hpp | 39 | ||||
-rw-r--r-- | platform/qt/include/qquickmapboxglstyleproperty.hpp | 69 | ||||
-rw-r--r-- | platform/qt/qmlapp/main.cpp | 7 | ||||
-rw-r--r-- | platform/qt/qmlapp/main.qml | 94 | ||||
-rw-r--r-- | platform/qt/qmlapp/qml.qrc | 1 | ||||
-rw-r--r-- | platform/qt/qmlapp/source.geojson | 253 | ||||
-rw-r--r-- | platform/qt/qt5.cmake | 4 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxgl.cpp | 240 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglmapparameter.cpp | 1 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglrenderer.cpp | 32 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglrenderer.hpp | 1 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglstyle.cpp | 59 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglstyleproperty.cpp | 97 |
17 files changed, 538 insertions, 403 deletions
diff --git a/platform/qt/include/QQuickMapboxGLStyle b/platform/qt/include/QQuickMapboxGLStyle deleted file mode 100644 index 24fc0c04e3..0000000000 --- a/platform/qt/include/QQuickMapboxGLStyle +++ /dev/null @@ -1 +0,0 @@ -#include "qquickmapboxglstyle.hpp" diff --git a/platform/qt/include/QQuickMapboxGLStyleProperty b/platform/qt/include/QQuickMapboxGLStyleProperty deleted file mode 100644 index 8e61ec1d4a..0000000000 --- a/platform/qt/include/QQuickMapboxGLStyleProperty +++ /dev/null @@ -1 +0,0 @@ -#include "qquickmapboxglstyleproperty.hpp" diff --git a/platform/qt/include/qquickmapboxgl.hpp b/platform/qt/include/qquickmapboxgl.hpp index 64e779b40c..25f3cf6b32 100644 --- a/platform/qt/include/qquickmapboxgl.hpp +++ b/platform/qt/include/qquickmapboxgl.hpp @@ -10,7 +10,6 @@ #include <QQmlListProperty> #include <QMapbox> -#include <QQuickMapboxGLStyle> #include <QQuickMapboxGLMapParameter> class QDeclarativeGeoServiceProvider; @@ -35,7 +34,6 @@ class Q_DECL_EXPORT QQuickMapboxGL : public QQuickFramebufferObject Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) // MapboxGL QML Type interface. - Q_PROPERTY(QQuickMapboxGLStyle *style READ style WRITE setStyle NOTIFY styleChanged) Q_PROPERTY(qreal bearing READ bearing WRITE setBearing NOTIFY bearingChanged) Q_PROPERTY(qreal pitch READ pitch WRITE setPitch NOTIFY pitchChanged) @@ -80,9 +78,6 @@ public: Q_INVOKABLE void pan(int dx, int dy); // MapboxGL QML Type interface. - void setStyle(QQuickMapboxGLStyle *); - QQuickMapboxGLStyle* style() const; - void setBearing(qreal bearing); qreal bearing() const; @@ -93,8 +88,8 @@ public: QQmlListProperty<QQuickMapboxGLMapParameter> parameters(); protected: - // QQuickItem implementation. - virtual void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value); + // QQmlParserStatus implementation + void componentComplete() override; signals: // Map QML Type signals. @@ -111,7 +106,6 @@ signals: void copyrightsVisibleChanged(bool visible); // Mapbox-specific signals. - void styleChanged(); void bearingChanged(qreal angle); void pitchChanged(qreal angle); @@ -120,8 +114,7 @@ public slots: private slots: void onMapChanged(QMapbox::MapChange); - void onStyleChanged(); - void onStylePropertyUpdated(const QVariantMap ¶ms); + void onParameterPropertyUpdated(const QString &name); private: static void appendParameter(QQmlListProperty<QQuickMapboxGLMapParameter> *prop, QQuickMapboxGLMapParameter *mapObject); @@ -139,6 +132,26 @@ private: PitchNeedsSync = 1 << 5, }; + struct StyleProperty { + enum Type { + Paint = 0, + Layout + }; + + Type type; + QString layer; + QString property; + QVariant value; + QString klass; + }; + + void processMapParameter(QQuickMapboxGLMapParameter *); + bool parseStyle(QQuickMapboxGLMapParameter *); + bool parseStyleProperty(QQuickMapboxGLMapParameter *, const QString &name); + bool parseStyleLayer(QQuickMapboxGLMapParameter *); + bool parseStyleSource(QQuickMapboxGLMapParameter *); + bool parseStyleFilter(QQuickMapboxGLMapParameter *); + qreal m_minimumZoomLevel = 0; qreal m_maximumZoomLevel = 20; qreal m_zoomLevel = 20; @@ -148,11 +161,13 @@ private: QGeoCoordinate m_center; QGeoShape m_visibleRegion; QColor m_color; - QList<QVariantMap> m_layoutChanges; - QList<QVariantMap> m_paintChanges; + QString m_styleUrl; + QList<StyleProperty> m_stylePropertyChanges; + QList<QVariantMap> m_layerChanges; + QList<QVariantMap> m_sourceChanges; + QList<QVariantMap> m_filterChanges; QList<QQuickMapboxGLMapParameter*> m_parameters; - QQuickMapboxGLStyle *m_style = 0; qreal m_bearing = 0; qreal m_pitch = 0; diff --git a/platform/qt/include/qquickmapboxglmapparameter.hpp b/platform/qt/include/qquickmapboxglmapparameter.hpp index 9a6e3a2ff0..914b8e6790 100644 --- a/platform/qt/include/qquickmapboxglmapparameter.hpp +++ b/platform/qt/include/qquickmapboxglmapparameter.hpp @@ -18,7 +18,6 @@ public: int propertyOffset() const { return m_metaPropertyOffset; } signals: - void propertiesReady(); void propertyUpdated(const QString &name); protected: diff --git a/platform/qt/include/qquickmapboxglstyle.hpp b/platform/qt/include/qquickmapboxglstyle.hpp deleted file mode 100644 index 6539841312..0000000000 --- a/platform/qt/include/qquickmapboxglstyle.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef QQUICKMAPBOXGLSTYLE_H -#define QQUICKMAPBOXGLSTYLE_H - -#include <QQuickItem> - -class Q_DECL_EXPORT QQuickMapboxGLStyle : public QQuickItem -{ - Q_OBJECT - - Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged) - Q_PROPERTY(QString styleClass READ styleClass WRITE setStyleClass NOTIFY classChanged) - -public: - QQuickMapboxGLStyle(QQuickItem *parent = Q_NULLPTR); - virtual ~QQuickMapboxGLStyle() {} - - // QQuickItem implementation - virtual void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &); - - void setUrl(const QString &); - QString url() const; - - void setStyleClass(const QString &); - QString styleClass() const; - -signals: - void urlChanged(const QString &); - void classChanged(const QString &); - - void propertyUpdated(const QVariantMap &); - -private: - QString m_url; - QString m_class; -}; - -QML_DECLARE_TYPE(QQuickMapboxGLStyle) - -#endif // QQUICKMAPBOXGLSTYLE_H diff --git a/platform/qt/include/qquickmapboxglstyleproperty.hpp b/platform/qt/include/qquickmapboxglstyleproperty.hpp deleted file mode 100644 index f49e879871..0000000000 --- a/platform/qt/include/qquickmapboxglstyleproperty.hpp +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef QQUICKMAPBOXGLSTYLEPROPERTY_H -#define QQUICKMAPBOXGLSTYLEPROPERTY_H - -#include <QQuickItem> - -class Q_DECL_EXPORT QQuickMapboxGLStyleProperty : public QQuickItem -{ - Q_OBJECT - - Q_PROPERTY(QString layer READ layer WRITE setLayer NOTIFY layerChanged) - Q_PROPERTY(QString property READ property WRITE setProperty NOTIFY propertyChanged) - Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged) - -public: - enum Type { - LayoutType = 0, - PaintType, - }; - - virtual ~QQuickMapboxGLStyleProperty() {} - - void setLayer(const QString &); - QString layer() const; - - void setProperty(const QString &); - QString property() const; - - void setValue(const QVariant &); - QVariant value() const; - -public slots: - void checkUpdated(); - -signals: - void layerChanged(const QString &); - void propertyChanged(const QString &); - void valueChanged(const QVariant &); - void updated(const QVariantMap& params); - -protected: - QQuickMapboxGLStyleProperty(QQuickItem *parent, Type); - - QVariantMap m_map; -}; - -class Q_DECL_EXPORT QQuickMapboxGLLayoutStyleProperty : public QQuickMapboxGLStyleProperty -{ -public: - QQuickMapboxGLLayoutStyleProperty(QQuickItem *parent = 0); - virtual ~QQuickMapboxGLLayoutStyleProperty() {} -}; - -class Q_DECL_EXPORT QQuickMapboxGLPaintStyleProperty : public QQuickMapboxGLStyleProperty -{ - Q_OBJECT - Q_PROPERTY(QString styleClass READ styleClass WRITE setStyleClass NOTIFY classChanged) - -public: - QQuickMapboxGLPaintStyleProperty(QQuickItem *parent = 0); - virtual ~QQuickMapboxGLPaintStyleProperty() {} - - void setStyleClass(const QString &); - QString styleClass() const; - -signals: - void classChanged(const QString &); -}; - -#endif // QQUICKMAPBOXGLSTYLEPROPERTY_H diff --git a/platform/qt/qmlapp/main.cpp b/platform/qt/qmlapp/main.cpp index d5f7dc767d..030f6cc4bd 100644 --- a/platform/qt/qmlapp/main.cpp +++ b/platform/qt/qmlapp/main.cpp @@ -4,8 +4,7 @@ #include <qqml.h> #include <QQuickMapboxGL> -#include <QQuickMapboxGLStyle> -#include <QQuickMapboxGLStyleProperty> +#include <QQuickMapboxGLMapParameter> int main(int argc, char *argv[]) { @@ -16,9 +15,7 @@ int main(int argc, char *argv[]) #endif qmlRegisterType<QQuickMapboxGL>("QQuickMapboxGL", 1, 0, "MapboxMap"); - qmlRegisterType<QQuickMapboxGLStyle>("QQuickMapboxGL", 1, 0, "MapboxStyle"); - qmlRegisterType<QQuickMapboxGLLayoutStyleProperty>("QQuickMapboxGL", 1, 0, "MapboxLayoutStyleProperty"); - qmlRegisterType<QQuickMapboxGLPaintStyleProperty>("QQuickMapboxGL", 1, 0, "MapboxPaintStyleProperty"); + qmlRegisterType<QQuickMapboxGLMapParameter>("QQuickMapboxGL", 1, 0, "MapParameter"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); diff --git a/platform/qt/qmlapp/main.qml b/platform/qt/qmlapp/main.qml index dcd0afa776..c0882ffa03 100644 --- a/platform/qt/qmlapp/main.qml +++ b/platform/qt/qmlapp/main.qml @@ -16,34 +16,13 @@ ApplicationWindow { ColorDialog { id: landColorDialog title: "Land color" - onCurrentColorChanged: { mapFront.color = currentColor } + color: "#e0ded8" } ColorDialog { id: waterColorDialog title: "Water color" - onCurrentColorChanged: { waterColor.value = currentColor } - } - - MapboxLayoutStyleProperty { - parent: styleStreets - layer: "road-label-large" - property: "visibility" - value: roadLabel.checked ? "visible" : "none" - } - - MapboxLayoutStyleProperty { - parent: styleStreets - layer: "road-label-medium" - property: "visibility" - value: roadLabel.checked ? "visible" : "none" - } - - MapboxLayoutStyleProperty { - parent: styleStreets - layer: "road-label-small" - property: "visibility" - value: roadLabel.checked ? "visible" : "none" + color: "#63c5ee" } RowLayout { @@ -75,10 +54,55 @@ ApplicationWindow { anchors.fill: parent visible: false - style: MapboxStyle { - id: styleStreets - url: "mapbox://styles/mapbox/streets-v9" - } + parameters: [ + MapParameter { + id: styleStreets + property var type: "style" + property var url: "mapbox://styles/mapbox/streets-v9" + }, + MapParameter { + property var type: "source" + property var id: "testSource" + property var sourceType: "geojson" + property var data: ":source.geojson" + }, + MapParameter { + property var type: "layer" + property var id: "testLayer" + property var layerType: "fill" + property var source: "testSource" + }, + MapParameter { + property var type: "paint" + property var layer: "testLayer" + property var fillColor: Qt.rgba(0, 0, 1, 1) + }, + MapParameter { + property var type: "paint" + property var layer: "water" + property var fillColor: waterColorDialog.color + }, + MapParameter { + property var type: "layout" + property var layer: "road-label-large" + property var visibility: roadLabel.checked ? "visible" : "none" + }, + MapParameter { + property var type: "layout" + property var layer: "road-label-medium" + property var visibility: roadLabel.checked ? "visible" : "none" + }, + MapParameter { + property var type: "layout" + property var layer: "road-label-small" + property var visibility: roadLabel.checked ? "visible" : "none" + }, + MapParameter { + property var type: "filter" + property var layer: "road-label-small" + property var filter: [ "==", "$type", "Point" ] + } + ] center: QtPositioning.coordinate(60.170448, 24.942046) // Helsinki zoomLevel: 14 @@ -91,12 +115,6 @@ ApplicationWindow { color: landColorDialog.color copyrightsVisible: true - MapboxPaintStyleProperty { - id: waterColor - layer: "water" - property: "fill-color" - } - Image { id: logo @@ -162,10 +180,12 @@ ApplicationWindow { anchors.fill: parent visible: false - style: MapboxStyle { - id: styleSatellite - url: "mapbox://styles/mapbox/satellite-streets-v9" - } + parameters: [ + MapParameter { + property var type: "style" + property var url: "mapbox://styles/mapbox/satellite-streets-v9" + } + ] center: mapFront.center zoomLevel: mapFront.zoomLevel diff --git a/platform/qt/qmlapp/qml.qrc b/platform/qt/qmlapp/qml.qrc index 5f6483ac33..a0b2f0f5ad 100644 --- a/platform/qt/qmlapp/qml.qrc +++ b/platform/qt/qmlapp/qml.qrc @@ -1,5 +1,6 @@ <RCC> <qresource prefix="/"> <file>main.qml</file> + <file>source.geojson</file> </qresource> </RCC> diff --git a/platform/qt/qmlapp/source.geojson b/platform/qt/qmlapp/source.geojson new file mode 100644 index 0000000000..f639c1913e --- /dev/null +++ b/platform/qt/qmlapp/source.geojson @@ -0,0 +1,253 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 69.2578125, + -40.17887331434695 + ], + [ + 69.2578125, + -10.141931686131018 + ], + [ + 110.74218749999999, + -10.141931686131018 + ], + [ + 110.74218749999999, + -40.17887331434695 + ], + [ + 69.2578125, + -40.17887331434695 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -71.71875, + -33.43144133557529 + ], + [ + -71.71875, + 20.3034175184893 + ], + [ + -0.703125, + 20.3034175184893 + ], + [ + -0.703125, + -33.43144133557529 + ], + [ + -71.71875, + -33.43144133557529 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 97.3828125, + 38.548165423046584 + ], + [ + 97.3828125, + 65.2198939361321 + ], + [ + 155.0390625, + 65.2198939361321 + ], + [ + 155.0390625, + 38.548165423046584 + ], + [ + 97.3828125, + 38.548165423046584 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 24.93617534637451, + 60.17058143435276 + ], + [ + 24.93617534637451, + 60.17149933938796 + ], + [ + 24.93808507919311, + 60.17149933938796 + ], + [ + 24.93808507919311, + 60.17058143435276 + ], + [ + 24.93617534637451, + 60.17058143435276 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 24.94398593902588, + 60.170346617317186 + ], + [ + 24.94398593902588, + 60.171456646699184 + ], + [ + 24.947032928466797, + 60.171456646699184 + ], + [ + 24.947032928466797, + 60.170346617317186 + ], + [ + 24.94398593902588, + 60.170346617317186 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 24.938321113586426, + 60.16800902895082 + ], + [ + 24.938321113586426, + 60.169204528711674 + ], + [ + 24.941797256469727, + 60.169204528711674 + ], + [ + 24.941797256469727, + 60.16800902895082 + ], + [ + 24.938321113586426, + 60.16800902895082 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 24.939115047454834, + 60.17280143974311 + ], + [ + 24.939115047454834, + 60.17387936902026 + ], + [ + 24.94175434112549, + 60.17387936902026 + ], + [ + 24.94175434112549, + 60.17280143974311 + ], + [ + 24.939115047454834, + 60.17280143974311 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 24.944372177124023, + 60.16034398818875 + ], + [ + 24.944372177124023, + 60.16837195311943 + ], + [ + 24.963769912719727, + 60.16837195311943 + ], + [ + 24.963769912719727, + 60.16034398818875 + ], + [ + 24.944372177124023, + 60.16034398818875 + ] + ] + ] + } + } + ] +}
\ No newline at end of file diff --git a/platform/qt/qt5.cmake b/platform/qt/qt5.cmake index 72d93da4e5..3223398a94 100644 --- a/platform/qt/qt5.cmake +++ b/platform/qt/qt5.cmake @@ -18,14 +18,10 @@ set(MBGL_QT_LIBRARIES add_library(qmapboxgl SHARED platform/qt/include/qquickmapboxgl.hpp platform/qt/include/qquickmapboxglmapparameter.hpp - platform/qt/include/qquickmapboxglstyle.hpp - platform/qt/include/qquickmapboxglstyleproperty.hpp platform/qt/src/qquickmapboxgl.cpp platform/qt/src/qquickmapboxglmapparameter.cpp platform/qt/src/qquickmapboxglrenderer.cpp platform/qt/src/qquickmapboxglrenderer.hpp - platform/qt/src/qquickmapboxglstyle.cpp - platform/qt/src/qquickmapboxglstyleproperty.cpp ) target_link_libraries(qmapboxgl diff --git a/platform/qt/src/qquickmapboxgl.cpp b/platform/qt/src/qquickmapboxgl.cpp index a0eb2a6b7c..a368ba56fe 100644 --- a/platform/qt/src/qquickmapboxgl.cpp +++ b/platform/qt/src/qquickmapboxgl.cpp @@ -4,7 +4,6 @@ #include <QMapbox> #include <QQuickMapboxGL> -#include <QQuickMapboxGLStyleProperty> #include <QQuickMapboxGLMapParameter> #include <QDebug> @@ -13,8 +12,15 @@ #include <QString> #include <QtGlobal> #include <QQmlListProperty> +#include <QJSValue> -#include <cmath> + +namespace { + +static const QRegularExpression s_camelCase {"([a-z0-9])([A-Z])"}; +static const QStringList s_parameterTypes = QStringList() << "style" << "paint" << "layout" << "layer" << "source" << "filter"; + +} // namespace QQuickMapboxGL::QQuickMapboxGL(QQuickItem *parent_) : QQuickFramebufferObject(parent_) @@ -41,6 +47,15 @@ QDeclarativeGeoServiceProvider *QQuickMapboxGL::plugin() const return nullptr; } +void QQuickMapboxGL::componentComplete() +{ + QQuickFramebufferObject::componentComplete(); + + for (const auto& param : m_parameters) { + processMapParameter(param); + } +} + void QQuickMapboxGL::setMinimumZoomLevel(qreal zoom) { zoom = qMax(mbgl::util::MIN_ZOOM, zoom); @@ -161,12 +176,14 @@ void QQuickMapboxGL::setColor(const QColor &color) m_color = color; - QVariantMap paintProperty; - paintProperty["type"] = QQuickMapboxGLLayoutStyleProperty::PaintType; - paintProperty["layer"] = "background"; - paintProperty["property"] = "background-color"; - paintProperty["value"] = color; - onStylePropertyUpdated(paintProperty); + StyleProperty change; + change.type = StyleProperty::Paint; + change.layer = "background"; + change.property = "background-color"; + change.value = color; + m_stylePropertyChanges << change; + + update(); emit colorChanged(m_color); } @@ -184,34 +201,6 @@ void QQuickMapboxGL::pan(int dx, int dy) update(); } -void QQuickMapboxGL::setStyle(QQuickMapboxGLStyle *style) -{ - if (style == m_style) { - return; - } - - disconnect(style, SIGNAL(urlChanged(QString)), this, SLOT(onStyleChanged())); - disconnect(style, SIGNAL(propertyUpdated(QVariantMap)), this, SLOT(onStylePropertyUpdated(QVariantMap))); - delete m_style; - m_style = style; - if (style) { - style->setParentItem(this); - connect(style, SIGNAL(urlChanged(QString)), this, SLOT(onStyleChanged())); - connect(style, SIGNAL(propertyUpdated(QVariantMap)), this, SLOT(onStylePropertyUpdated(QVariantMap))); - } - - m_syncState |= StyleNeedsSync; - m_styleLoaded = false; - update(); - - emit styleChanged(); -} - -QQuickMapboxGLStyle *QQuickMapboxGL::style() const -{ - return m_style; -} - void QQuickMapboxGL::setBearing(qreal angle) { angle = std::fmod(angle, 360.); @@ -254,61 +243,177 @@ qreal QQuickMapboxGL::pitch() const return m_pitch; } -void QQuickMapboxGL::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) +void QQuickMapboxGL::onMapChanged(QMapbox::MapChange change) { - QQuickFramebufferObject::itemChange(change, value); + if (change == QMapbox::MapChangeDidFinishLoadingStyle) { + m_styleLoaded = true; + update(); + } +} - switch (change) { - case QQuickItem::ItemChildAddedChange: - if (QQuickMapboxGLStyleProperty *property = qobject_cast<QQuickMapboxGLStyleProperty *>(value.item)) { - connect(property, SIGNAL(updated(QVariantMap)), this, SLOT(onStylePropertyUpdated(QVariantMap))); - connect(this, SIGNAL(styleChanged()), property, SLOT(checkUpdated())); - } - break; - case QQuickItem::ItemChildRemovedChange: - if (QQuickMapboxGLStyleProperty *property = qobject_cast<QQuickMapboxGLStyleProperty *>(value.item)) { - disconnect(property, SIGNAL(updated(QVariantMap)), this, SLOT(onStylePropertyUpdated(QVariantMap))); - disconnect(this, SIGNAL(styleChanged()), property, SLOT(checkUpdated())); +bool QQuickMapboxGL::parseStyle(QQuickMapboxGLMapParameter *param) +{ + QString url = param->property("url").toString(); + if (m_styleUrl == url) { + return false; + } + + // Reload parameters if switching style URLs. + if (!m_styleUrl.isEmpty()) { + for (const auto& param_ : m_parameters) { + if (param_->property("type").toString() == "style") continue; + processMapParameter(param_); } - default: - break; } + + m_styleUrl = url; + m_styleLoaded = false; + m_syncState |= StyleNeedsSync; + + return true; +} + +bool QQuickMapboxGL::parseStyleProperty(QQuickMapboxGLMapParameter *param, const QString &name) +{ + // Ignore meta-properties "type", "layer" and "class". + if (name == "type" || name == "layer" || name == "class") { + return false; + } + + QString formattedName(name); + formattedName = formattedName.replace(s_camelCase, "\\1-\\2").toLower(); + + QVariant value = param->property(name.toLatin1()); + if (value.canConvert<QJSValue>()) { + value = value.value<QJSValue>().toVariant(); + } + + m_stylePropertyChanges << QQuickMapboxGL::StyleProperty { + param->property("type").toString().at(0) == 'p' ? StyleProperty::Paint : StyleProperty::Layout, + param->property("layer").toString(), + formattedName, + value, + param->property("class").toString() + }; + return true; } -void QQuickMapboxGL::onMapChanged(QMapbox::MapChange change) +bool QQuickMapboxGL::parseStyleLayer(QQuickMapboxGLMapParameter *param) { - if (change == QMapbox::MapChangeDidFinishLoadingStyle) { - m_styleLoaded = true; - update(); + QVariantMap layer; + layer["id"] = param->property("name"); + layer["source"] = param->property("source"); + layer["type"] = param->property("layerType"); + if (param->property("sourceLayer").isValid()) { + layer["source-layer"] = param->property("sourceLayer"); + } + m_layerChanges << layer; + return true; +} + +bool QQuickMapboxGL::parseStyleSource(QQuickMapboxGLMapParameter *param) +{ + QString sourceType = param->property("sourceType").toString(); + + QVariantMap source; + source["id"] = param->property("name"); + source["type"] = sourceType; + if (sourceType == "vector" || sourceType == "raster") { + source["url"] = param->property("url"); + m_sourceChanges << source; + } else if (sourceType == "geojson") { + QFile geojson(param->property("data").toString()); + geojson.open(QIODevice::ReadOnly); + source["data"] = geojson.readAll(); + m_sourceChanges << source; + } else { + qWarning() << "Error: Unsupported source type '" << sourceType << "'."; + return false; } + return true; } -void QQuickMapboxGL::onStylePropertyUpdated(const QVariantMap ¶ms) +bool QQuickMapboxGL::parseStyleFilter(QQuickMapboxGLMapParameter *param) { - switch (params.value("type").toInt()) { - case QQuickMapboxGLStyleProperty::LayoutType: - m_layoutChanges << params; + QVariantMap filter; + filter["layer"] = param->property("layer"); + filter["filter"] = param->property("filter"); + m_filterChanges << filter; + return true; +} + +void QQuickMapboxGL::processMapParameter(QQuickMapboxGLMapParameter *param) +{ + bool needsUpdate = false; + switch (s_parameterTypes.indexOf(param->property("type").toString())) { + case -1: + qWarning() << "Error: Invalid value for property 'type'."; break; - case QQuickMapboxGLStyleProperty::PaintType: - m_paintChanges << params; + case 0: // style + needsUpdate |= parseStyle(param); + break; + case 1: // paint + case 2: // layout + for (int i = param->propertyOffset(); i < param->metaObject()->propertyCount(); ++i) { + needsUpdate |= parseStyleProperty(param, QString(param->metaObject()->property(i).name())); + } + break; + case 3: // layer + needsUpdate |= parseStyleLayer(param); + break; + case 4: // source + needsUpdate |= parseStyleSource(param); + break; + case 5: // filter + needsUpdate |= parseStyleFilter(param); break; } - - update(); + if (needsUpdate) update(); } -void QQuickMapboxGL::onStyleChanged() +void QQuickMapboxGL::onParameterPropertyUpdated(const QString &propertyName) { - m_syncState |= StyleNeedsSync; - update(); + QQuickMapboxGLMapParameter *param = qobject_cast<QQuickMapboxGLMapParameter *>(sender()); + if (!param) { + qWarning() << "Warning: Sender for QQuickMapboxGL::onParameterPropertyUpdated is not a QQuickMapboxGLMapParameter."; + return; + } - emit styleChanged(); + if (propertyName == "type") { + qWarning() << "Error: Cannot modify property 'type'."; + return; + } + + bool needsUpdate = false; + switch (s_parameterTypes.indexOf(param->property("type").toString())) { + case -1: + qWarning() << "Error: Invalid value for property 'type'."; + break; + case 0: // style + needsUpdate |= parseStyle(param); + break; + case 1: // paint + case 2: // layout + needsUpdate |= parseStyleProperty(param, propertyName); + break; + case 3: // layer + needsUpdate |= parseStyleLayer(param); + break; + case 4: // source + needsUpdate |= parseStyleSource(param); + break; + case 5: // filter + needsUpdate |= parseStyleFilter(param); + break; + } + if (needsUpdate) update(); } void QQuickMapboxGL::appendParameter(QQmlListProperty<QQuickMapboxGLMapParameter> *prop, QQuickMapboxGLMapParameter *param) { QQuickMapboxGL *map = static_cast<QQuickMapboxGL *>(prop->object); map->m_parameters.append(param); + QObject::connect(param, SIGNAL(propertyUpdated(QString)), map, SLOT(onParameterPropertyUpdated(QString))); } int QQuickMapboxGL::countParameters(QQmlListProperty<QQuickMapboxGLMapParameter> *prop) @@ -327,6 +432,7 @@ void QQuickMapboxGL::clearParameter(QQmlListProperty<QQuickMapboxGLMapParameter> { QQuickMapboxGL *map = static_cast<QQuickMapboxGL *>(prop->object); for (auto param : map->m_parameters) { + QObject::disconnect(param, SIGNAL(propertyUpdated(QString)), map, SLOT(onParameterPropertyUpdated(QString))); } map->m_parameters.clear(); } diff --git a/platform/qt/src/qquickmapboxglmapparameter.cpp b/platform/qt/src/qquickmapboxglmapparameter.cpp index bd5cfdd089..68fe8fac85 100644 --- a/platform/qt/src/qquickmapboxglmapparameter.cpp +++ b/platform/qt/src/qquickmapboxglmapparameter.cpp @@ -28,7 +28,6 @@ void QQuickMapboxGLMapParameter::componentComplete() QObject::connect(this, signalName, mapper, SLOT(map())); QObject::connect(mapper, SIGNAL(mapped(int)), this, SLOT(onPropertyUpdated(int))); } - emit propertiesReady(); } void QQuickMapboxGLMapParameter::onPropertyUpdated(int index) diff --git a/platform/qt/src/qquickmapboxglrenderer.cpp b/platform/qt/src/qquickmapboxglrenderer.cpp index 0401c7231d..4dc46a7737 100644 --- a/platform/qt/src/qquickmapboxglrenderer.cpp +++ b/platform/qt/src/qquickmapboxglrenderer.cpp @@ -2,12 +2,12 @@ #include <QMapboxGL> #include <QQuickMapboxGL> -#include <QQuickMapboxGLStyle> #include <QSize> #include <QOpenGLFramebufferObject> #include <QOpenGLFramebufferObjectFormat> #include <QQuickWindow> +#include <QThread> QQuickMapboxGLRenderer::QQuickMapboxGLRenderer() { @@ -59,8 +59,8 @@ void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item) m_map->setCoordinateZoom({ center.latitude(), center.longitude() }, quickMap->zoomLevel()); } - if (syncStatus & QQuickMapboxGL::StyleNeedsSync && quickMap->style()) { - m_map->setStyleUrl(quickMap->style()->url()); + if (syncStatus & QQuickMapboxGL::StyleNeedsSync && !quickMap->m_styleUrl.isEmpty()) { + m_map->setStyleUrl(quickMap->m_styleUrl); } if (syncStatus & QQuickMapboxGL::PanNeedsSync) { @@ -81,13 +81,27 @@ void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item) return; } - for (const auto& change: quickMap->m_layoutChanges) { - m_map->setLayoutProperty(change.value("layer").toString(), change.value("property").toString(), change.value("value")); + for (const auto& change : quickMap->m_sourceChanges) { + m_map->addSource(change.value("id").toString(), change); } - quickMap->m_layoutChanges.clear(); + quickMap->m_sourceChanges.clear(); - for (const auto& change: quickMap->m_paintChanges) { - m_map->setPaintProperty(change.value("layer").toString(), change.value("property").toString(), change.value("value"), change.value("class").toString()); + for (const auto& change : quickMap->m_layerChanges) { + m_map->addLayer(change); } - quickMap->m_paintChanges.clear(); + quickMap->m_layerChanges.clear(); + + for (const auto& change : quickMap->m_filterChanges) { + m_map->setFilter(change.value("layer").toString(), change.value("filter")); + } + quickMap->m_filterChanges.clear(); + + for (const auto& change : quickMap->m_stylePropertyChanges) { + if (change.type == QQuickMapboxGL::StyleProperty::Paint) { + m_map->setPaintProperty(change.layer, change.property, change.value, change.klass); + } else { + m_map->setLayoutProperty(change.layer, change.property, change.value); + } + } + quickMap->m_stylePropertyChanges.clear(); } diff --git a/platform/qt/src/qquickmapboxglrenderer.hpp b/platform/qt/src/qquickmapboxglrenderer.hpp index 0181cbd611..fa9b81ec7f 100644 --- a/platform/qt/src/qquickmapboxglrenderer.hpp +++ b/platform/qt/src/qquickmapboxglrenderer.hpp @@ -4,6 +4,7 @@ #include <QQuickFramebufferObject> #include <QScopedPointer> +#include <QMapbox> #include <QMapboxGL> class QGeoCoordinate; diff --git a/platform/qt/src/qquickmapboxglstyle.cpp b/platform/qt/src/qquickmapboxglstyle.cpp deleted file mode 100644 index 1a8640928b..0000000000 --- a/platform/qt/src/qquickmapboxglstyle.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include <QQuickMapboxGLStyle> -#include <QQuickMapboxGLStyleProperty> - -QQuickMapboxGLStyle::QQuickMapboxGLStyle(QQuickItem *parent) - : QQuickItem(parent) -{ -} - -void QQuickMapboxGLStyle::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) -{ - QQuickItem::itemChange(change, value); - - switch (change) { - case QQuickItem::ItemChildAddedChange: - if (QQuickMapboxGLStyleProperty *property = qobject_cast<QQuickMapboxGLStyleProperty *>(value.item)) { - connect(property, SIGNAL(updated(QVariantMap)), this, SIGNAL(propertyUpdated(QVariantMap))); - connect(this, SIGNAL(urlChanged(QString)), property, SLOT(checkUpdated())); - } - break; - case QQuickItem::ItemChildRemovedChange: - if (QQuickMapboxGLStyleProperty *property = qobject_cast<QQuickMapboxGLStyleProperty *>(value.item)) { - disconnect(property, SIGNAL(updated(QVariantMap)), this, SIGNAL(propertyUpdated(QVariantMap))); - disconnect(this, SIGNAL(urlChanged(QString)), property, SLOT(checkUpdated())); - } - break; - default: - break; - } -} - -void QQuickMapboxGLStyle::setUrl(const QString &url) -{ - if (url == m_url) { - return; - } - - m_url = url; - emit urlChanged(url); -} - -QString QQuickMapboxGLStyle::url() const -{ - return m_url; -} - -void QQuickMapboxGLStyle::setStyleClass(const QString &styleClass) -{ - if (styleClass == m_class) { - return; - } - - m_class = styleClass; - emit classChanged(styleClass); -} - -QString QQuickMapboxGLStyle::styleClass() const -{ - return m_class; -} diff --git a/platform/qt/src/qquickmapboxglstyleproperty.cpp b/platform/qt/src/qquickmapboxglstyleproperty.cpp deleted file mode 100644 index b8603d00ef..0000000000 --- a/platform/qt/src/qquickmapboxglstyleproperty.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include <QQuickMapboxGLStyleProperty> - -// QQuickMapboxGLStyleProperty - -QQuickMapboxGLStyleProperty::QQuickMapboxGLStyleProperty(QQuickItem *parent_, Type type) - : QQuickItem(parent_) -{ - m_map["type"] = type; -} - -void QQuickMapboxGLStyleProperty::checkUpdated() -{ - if (m_map.value("type").isValid() - && m_map.value("layer").isValid() - && m_map.value("property").isValid() - && m_map.value("value").isValid()) { - emit updated(m_map); - } -} - -void QQuickMapboxGLStyleProperty::setLayer(const QString &layer) -{ - if (m_map.value("layer").toString() == layer) { - return; - } - - m_map["layer"] = layer; - emit layerChanged(layer); - checkUpdated(); -} - -QString QQuickMapboxGLStyleProperty::layer() const -{ - return m_map.value("layer").toString(); -} - -void QQuickMapboxGLStyleProperty::setProperty(const QString &property) -{ - if (m_map.value("property").toString() == property) { - return; - } - - m_map["property"] = property; - emit propertyChanged(property); - checkUpdated(); -} - -QString QQuickMapboxGLStyleProperty::property() const -{ - return m_map.value("property").toString(); -} - -void QQuickMapboxGLStyleProperty::setValue(const QVariant &value) -{ - if (m_map.value("value") == value) { - return; - } - - m_map["value"] = value; - emit valueChanged(value); - checkUpdated(); -} - -QVariant QQuickMapboxGLStyleProperty::value() const -{ - return m_map.value("value"); -} - -void QQuickMapboxGLPaintStyleProperty::setStyleClass(const QString &styleClass) -{ - if (m_map.value("class").toString() == styleClass) { - return; - } - - m_map["class"] = styleClass; - emit classChanged(styleClass); - checkUpdated(); -} - -QString QQuickMapboxGLPaintStyleProperty::styleClass() const -{ - return m_map.value("class").toString(); -} - -// QQuickMapboxGLLayoutStyleProperty - -QQuickMapboxGLLayoutStyleProperty::QQuickMapboxGLLayoutStyleProperty(QQuickItem *parent_) - : QQuickMapboxGLStyleProperty(parent_, LayoutType) -{ -} - -// QQuickMapboxGLPaintStyleProperty - -QQuickMapboxGLPaintStyleProperty::QQuickMapboxGLPaintStyleProperty(QQuickItem *parent_) - : QQuickMapboxGLStyleProperty(parent_, PaintType) -{ -} |