diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-07-28 16:16:04 +0300 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-07-29 19:14:41 +0300 |
commit | 472ab7442f566e6d2e8f1b67282d8394d0394de6 (patch) | |
tree | 286365e2ec2a4519df82fc2e6fc9ac8a04670624 | |
parent | 3c2fce18a1c667b9d1edaf08e3f2d5dfe20cc52f (diff) | |
download | qtlocation-mapboxgl-472ab7442f566e6d2e8f1b67282d8394d0394de6.tar.gz |
[qt] Added QQuickMapboxGL{Layout,Paint}StyleProperty
-rw-r--r-- | platform/qt/include/QQuickMapboxGLStyleProperty | 1 | ||||
-rw-r--r-- | platform/qt/include/qquickmapboxgl.hpp | 24 | ||||
-rw-r--r-- | platform/qt/include/qquickmapboxglstyleproperty.hpp | 75 | ||||
-rw-r--r-- | platform/qt/platform.gyp | 2 | ||||
-rw-r--r-- | platform/qt/qmlapp/main.cpp | 5 | ||||
-rw-r--r-- | platform/qt/qmlapp/main.qml | 68 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxgl.cpp | 17 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglrenderer.cpp | 26 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglrenderer.hpp | 7 | ||||
-rw-r--r-- | platform/qt/src/qquickmapboxglstyleproperty.cpp | 118 |
10 files changed, 324 insertions, 19 deletions
diff --git a/platform/qt/include/QQuickMapboxGLStyleProperty b/platform/qt/include/QQuickMapboxGLStyleProperty new file mode 100644 index 0000000000..8e61ec1d4a --- /dev/null +++ b/platform/qt/include/QQuickMapboxGLStyleProperty @@ -0,0 +1 @@ +#include "qquickmapboxglstyleproperty.hpp" diff --git a/platform/qt/include/qquickmapboxgl.hpp b/platform/qt/include/qquickmapboxgl.hpp index de783a1f62..c651afef00 100644 --- a/platform/qt/include/qquickmapboxgl.hpp +++ b/platform/qt/include/qquickmapboxgl.hpp @@ -34,6 +34,19 @@ class Q_DECL_EXPORT QQuickMapboxGL : public QQuickFramebufferObject Q_PROPERTY(qreal pitch READ pitch WRITE setPitch NOTIFY pitchChanged) public: + struct LayoutPropertyChange { + QString layer; + QString property; + QVariant value; + }; + + struct PaintPropertyChange { + QString layer; + QString property; + QVariant value; + QString klass; + }; + QQuickMapboxGL(QQuickItem *parent = 0); virtual ~QQuickMapboxGL(); @@ -69,6 +82,9 @@ public: Q_INVOKABLE void pan(int dx, int dy); + QList<LayoutPropertyChange>& layoutPropertyChanges() { return m_layoutChanges; } + QList<PaintPropertyChange>& paintPropertyChanges() { return m_paintChanges; } + // MapboxGL QML Type interface. void setStyle(const QString &style); QString style() const; @@ -81,6 +97,9 @@ public: QPointF swapPan(); + void setLayoutProperty(const QString &layer, const QString &property, const QVariant &value); + void setPaintProperty(const QString &layer, const QString &property, const QVariant &value, const QString &klass = QString()); + enum SyncState { NothingNeedsSync = 0, ZoomNeedsSync = 1 << 0, @@ -89,7 +108,6 @@ public: PanNeedsSync = 1 << 3, BearingNeedsSync = 1 << 4, PitchNeedsSync = 1 << 5, - ColorNeedsSync = 1 << 6, }; int swapSyncState(); @@ -123,7 +141,9 @@ private: QGeoCoordinate m_center; QGeoShape m_visibleRegion; - QColor m_color = Qt::black; + QColor m_color; + QList<LayoutPropertyChange> m_layoutChanges; + QList<PaintPropertyChange> m_paintChanges; QString m_style; qreal m_bearing = 0; diff --git a/platform/qt/include/qquickmapboxglstyleproperty.hpp b/platform/qt/include/qquickmapboxglstyleproperty.hpp new file mode 100644 index 0000000000..99832eed85 --- /dev/null +++ b/platform/qt/include/qquickmapboxglstyleproperty.hpp @@ -0,0 +1,75 @@ +#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: + virtual ~QQuickMapboxGLStyleProperty() {} + + // QQuickItem implementation + virtual void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &); + + void setLayer(const QString &); + QString layer() const; + + void setProperty(const QString &); + QString property() const; + + void setValue(const QVariant &); + QVariant value() const; + +signals: + void layerChanged(const QString &); + void propertyChanged(const QString &); + void valueChanged(const QVariant &); + +protected: + QQuickMapboxGLStyleProperty(QQuickItem *parent); + virtual void updateParent() = 0; + + QVariant m_layer; + QVariant m_property; + QVariant m_value; +}; + +class Q_DECL_EXPORT QQuickMapboxGLLayoutStyleProperty : public QQuickMapboxGLStyleProperty +{ +public: + QQuickMapboxGLLayoutStyleProperty(QQuickItem *parent = 0); + virtual ~QQuickMapboxGLLayoutStyleProperty() {} + +protected: + virtual void updateParent(); +}; + +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 &); + +protected: + virtual void updateParent(); + +private: + QVariant m_class; +}; + +#endif // QQUICKMAPBOXGLSTYLEPROPERTY_H diff --git a/platform/qt/platform.gyp b/platform/qt/platform.gyp index b948898e2a..cb46aab07e 100644 --- a/platform/qt/platform.gyp +++ b/platform/qt/platform.gyp @@ -49,6 +49,7 @@ 'include/qmapbox.hpp', 'include/qmapboxgl.hpp', 'include/qquickmapboxgl.hpp', + 'include/qquickmapboxglstyleproperty.hpp', 'qmapbox.qrc', 'src/async_task.cpp', 'src/async_task_impl.hpp', @@ -61,6 +62,7 @@ 'src/qmapboxgl.cpp', 'src/qmapboxgl_p.hpp', 'src/qquickmapboxgl.cpp', + 'src/qquickmapboxglstyleproperty.cpp', 'src/qquickmapboxglrenderer.cpp', 'src/qquickmapboxglrenderer.hpp', 'src/run_loop.cpp', diff --git a/platform/qt/qmlapp/main.cpp b/platform/qt/qmlapp/main.cpp index 1b07c699cc..0a376c6bdd 100644 --- a/platform/qt/qmlapp/main.cpp +++ b/platform/qt/qmlapp/main.cpp @@ -4,6 +4,7 @@ #include <qqml.h> #include <QQuickMapboxGL> +#include <QQuickMapboxGLStyleProperty> int main(int argc, char *argv[]) { @@ -13,7 +14,9 @@ int main(int argc, char *argv[]) app.setWindowIcon(QIcon(":icon.png")); #endif - qmlRegisterType<QQuickMapboxGL>("QQuickMapboxGL", 1, 0, "QQuickMapboxGL"); + qmlRegisterType<QQuickMapboxGL>("QQuickMapboxGL", 1, 0, "MapboxMap"); + qmlRegisterType<QQuickMapboxGLLayoutStyleProperty>("QQuickMapboxGL", 1, 0, "MapboxLayoutStyleProperty"); + qmlRegisterType<QQuickMapboxGLPaintStyleProperty>("QQuickMapboxGL", 1, 0, "MapboxPaintStyleProperty"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); diff --git a/platform/qt/qmlapp/main.qml b/platform/qt/qmlapp/main.qml index a5e51e9801..6edfd7490e 100644 --- a/platform/qt/qmlapp/main.qml +++ b/platform/qt/qmlapp/main.qml @@ -14,10 +14,36 @@ ApplicationWindow { visible: true ColorDialog { - id: colorDialog - title: "Background color" - visible: false - color: "black" + id: landColorDialog + title: "Land color" + onCurrentColorChanged: { mapStreets.color = currentColor } + } + + ColorDialog { + id: waterColorDialog + title: "Water color" + onCurrentColorChanged: { waterColor.value = currentColor } + } + + MapboxLayoutStyleProperty { + parent: mapStreets + layer: "road-label-large" + property: "visibility" + value: roadLabel.checked ? "visible" : "none" + } + + MapboxLayoutStyleProperty { + parent: mapStreets + layer: "road-label-medium" + property: "visibility" + value: roadLabel.checked ? "visible" : "none" + } + + MapboxLayoutStyleProperty { + parent: mapStreets + layer: "road-label-small" + property: "visibility" + value: roadLabel.checked ? "visible" : "none" } RowLayout { @@ -43,7 +69,7 @@ ApplicationWindow { front: Rectangle { anchors.fill: parent - QQuickMapboxGL { + MapboxMap { id: mapStreets anchors.fill: parent @@ -59,9 +85,15 @@ ApplicationWindow { bearing: bearingSlider.value pitch: pitchSlider.value - color: colorDialog.currentColor + color: landColorDialog.color copyrightsVisible: true + MapboxPaintStyleProperty { + id: waterColor + layer: "water" + property: "fill-color" + } + Image { id: logo @@ -121,7 +153,7 @@ ApplicationWindow { back: Rectangle { anchors.fill: parent - QQuickMapboxGL { + MapboxMap { id: mapSatellite anchors.fill: parent @@ -232,9 +264,25 @@ ApplicationWindow { } Button { - id: colorChangeButton - text: "Change background color" - onClicked: colorDialog.open() + anchors.left: parent.left + anchors.right: parent.right + text: "Select land color" + onClicked: landColorDialog.open() + } + + Button { + anchors.left: parent.left + anchors.right: parent.right + text: "Select water color" + onClicked: waterColorDialog.open() + } + + CheckBox { + id: roadLabel + anchors.left: parent.left + anchors.right: parent.right + text: "Toggle road label" + checked: true } } } diff --git a/platform/qt/src/qquickmapboxgl.cpp b/platform/qt/src/qquickmapboxgl.cpp index 69878e0776..d8492c5218 100644 --- a/platform/qt/src/qquickmapboxgl.cpp +++ b/platform/qt/src/qquickmapboxgl.cpp @@ -150,14 +150,13 @@ bool QQuickMapboxGL::copyrightsVisible() const void QQuickMapboxGL::setColor(const QColor &color) { - if (color == m_color) { + if (m_color == color) { return; } m_color = color; - m_syncState |= ColorNeedsSync; - update(); + setPaintProperty("background", "background-color", color); emit colorChanged(m_color); } @@ -175,6 +174,18 @@ void QQuickMapboxGL::pan(int dx, int dy) update(); } +void QQuickMapboxGL::setLayoutProperty(const QString &layer, const QString &property, const QVariant &value) +{ + m_layoutChanges.append(LayoutPropertyChange { layer, property, value }); + update(); +} + +void QQuickMapboxGL::setPaintProperty(const QString &layer, const QString &property, const QVariant &value, const QString &klass) +{ + m_paintChanges.append(PaintPropertyChange { layer, property, value, klass }); + update(); +} + void QQuickMapboxGL::setStyle(const QString &styleUrl) { if (m_style == styleUrl) { diff --git a/platform/qt/src/qquickmapboxglrenderer.cpp b/platform/qt/src/qquickmapboxglrenderer.cpp index 330b5dc7e5..326eafa928 100644 --- a/platform/qt/src/qquickmapboxglrenderer.cpp +++ b/platform/qt/src/qquickmapboxglrenderer.cpp @@ -19,12 +19,21 @@ QQuickMapboxGLRenderer::QQuickMapboxGLRenderer() settings.setViewportMode(QMapboxGLSettings::FlippedYViewport); m_map.reset(new QMapboxGL(nullptr, settings)); + connect(m_map.data(), SIGNAL(mapChanged(QMapboxGL::MapChange)), this, SLOT(onMapChanged(QMapboxGL::MapChange))); } QQuickMapboxGLRenderer::~QQuickMapboxGLRenderer() { } +void QQuickMapboxGLRenderer::onMapChanged(QMapboxGL::MapChange change) +{ + if (change == QMapboxGL::MapChangeDidFinishLoadingMap) { + m_styleLoaded = true; + update(); + } +} + QOpenGLFramebufferObject* QQuickMapboxGLRenderer::createFramebufferObject(const QSize &size) { m_map->resize(size); @@ -61,6 +70,7 @@ void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item) if (syncStatus & QQuickMapboxGL::StyleNeedsSync) { m_map->setStyleURL(quickMap->style()); + m_styleLoaded = false; } if (syncStatus & QQuickMapboxGL::PanNeedsSync) { @@ -76,7 +86,19 @@ void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item) m_map->setPitch(quickMap->pitch()); } - if (syncStatus & QQuickMapboxGL::ColorNeedsSync && m_map->isFullyLoaded()) { - m_map->setPaintProperty("background", "background-color", quickMap->color()); + if (m_styleLoaded) { + if (!quickMap->layoutPropertyChanges().empty()) { + for (const auto& change: quickMap->layoutPropertyChanges()) { + m_map->setLayoutProperty(change.layer, change.property, change.value); + } + quickMap->layoutPropertyChanges().clear(); + } + + if (!quickMap->paintPropertyChanges().empty()) { + for (const auto& change: quickMap->paintPropertyChanges()) { + m_map->setPaintProperty(change.layer, change.property, change.value, change.klass); + } + quickMap->paintPropertyChanges().clear(); + } } } diff --git a/platform/qt/src/qquickmapboxglrenderer.hpp b/platform/qt/src/qquickmapboxglrenderer.hpp index d367e17f78..d7ca7430ab 100644 --- a/platform/qt/src/qquickmapboxglrenderer.hpp +++ b/platform/qt/src/qquickmapboxglrenderer.hpp @@ -4,8 +4,9 @@ #include <QQuickFramebufferObject> #include <QScopedPointer> +#include <QMapboxGL> + class QGeoCoordinate; -class QMapboxGL; class QOpenGLFramebufferObject; class QSize; @@ -25,8 +26,12 @@ public: signals: void centerChanged(const QGeoCoordinate &); +public slots: + void onMapChanged(QMapboxGL::MapChange); + private: bool m_initialized = false; + bool m_styleLoaded = false; QScopedPointer<QMapboxGL> m_map; }; diff --git a/platform/qt/src/qquickmapboxglstyleproperty.cpp b/platform/qt/src/qquickmapboxglstyleproperty.cpp new file mode 100644 index 0000000000..77e6a00583 --- /dev/null +++ b/platform/qt/src/qquickmapboxglstyleproperty.cpp @@ -0,0 +1,118 @@ +#include <QQuickMapboxGLStyleProperty> +#include <QQuickMapboxGL> + +QQuickMapboxGLStyleProperty::QQuickMapboxGLStyleProperty(QQuickItem *parent_) + : QQuickItem(parent_) +{ +} + +QQuickMapboxGLLayoutStyleProperty::QQuickMapboxGLLayoutStyleProperty(QQuickItem *parent_) + : QQuickMapboxGLStyleProperty(parent_) +{ +} + +QQuickMapboxGLPaintStyleProperty::QQuickMapboxGLPaintStyleProperty(QQuickItem *parent_) + : QQuickMapboxGLStyleProperty(parent_) +{ +} + +void QQuickMapboxGLLayoutStyleProperty::updateParent() +{ + if (m_layer.isNull() || m_property.isNull() || m_value.isNull()) { + return; + } + + QQuickMapboxGL *map = qobject_cast<QQuickMapboxGL *>(parentItem()); + if (map) { + map->setLayoutProperty(layer(), property(), m_value); + } else { + qWarning() << "Style property requires QQuickMapboxGL as parent item."; + } +} + +void QQuickMapboxGLPaintStyleProperty::updateParent() +{ + if (m_layer.isNull() || m_property.isNull() || m_value.isNull()) { + return; + } + + QQuickMapboxGL *map = qobject_cast<QQuickMapboxGL *>(parentItem()); + if (map) { + map->setPaintProperty(layer(), property(), m_value, styleClass()); + } else { + qWarning() << "Style property requires QQuickMapboxGL as parent item."; + } +} + +void QQuickMapboxGLStyleProperty::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) +{ + QQuickItem::itemChange(change, data); + + if (change == QQuickItem::ItemParentHasChanged) { + updateParent(); + } +} + +void QQuickMapboxGLStyleProperty::setLayer(const QString &layer) +{ + if (layer == m_layer.toString()) { + return; + } + + m_layer = layer; + emit layerChanged(layer); + updateParent(); +} + +QString QQuickMapboxGLStyleProperty::layer() const +{ + return m_layer.toString(); +} + +void QQuickMapboxGLStyleProperty::setProperty(const QString &property) +{ + if (property == m_property.toString()) { + return; + } + + m_property = property; + emit propertyChanged(property); + updateParent(); +} + +QString QQuickMapboxGLStyleProperty::property() const +{ + return m_property.toString(); +} + +void QQuickMapboxGLStyleProperty::setValue(const QVariant &value) +{ + if (value == m_value) { + return; + } + + m_value = value; + emit valueChanged(value); + updateParent(); +} + +QVariant QQuickMapboxGLStyleProperty::value() const +{ + return m_value; +} + +void QQuickMapboxGLPaintStyleProperty::setStyleClass(const QString &styleClass) +{ + if (styleClass == m_class.toString()) { + return; + } + + m_class = styleClass; + emit classChanged(styleClass); + updateParent(); +} + +QString QQuickMapboxGLPaintStyleProperty::styleClass() const +{ + return m_class.toString(); +} |