[qt] Added QQuickMapboxGL{Layout,Paint}StyleProperty

This commit is contained in:
Bruno de Oliveira Abinader
2016-07-28 16:16:04 +03:00
parent 3c2fce18a1
commit 472ab7442f
10 changed files with 324 additions and 19 deletions

View File

@@ -0,0 +1 @@
#include "qquickmapboxglstyleproperty.hpp"

View File

@@ -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;

View File

@@ -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

View File

@@ -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',

View File

@@ -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")));

View File

@@ -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
}
}
}

View File

@@ -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) {

View File

@@ -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();
}
}
}

View File

@@ -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;
};

View File

@@ -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();
}