[Qt] Allow setting the style via QML

This commit is contained in:
Thiago Marcos P. Santos
2016-04-28 19:43:24 +03:00
parent c8d6540bc1
commit da653ec537
4 changed files with 38 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ class Q_DECL_EXPORT QQuickMapboxGL : public QQuickFramebufferObject
{
Q_OBJECT
// Map QML Type interface implementation.
Q_ENUMS(QGeoServiceProvider::Error)
Q_PROPERTY(QDeclarativeGeoServiceProvider *plugin READ plugin WRITE setPlugin NOTIFY pluginChanged)
Q_PROPERTY(qreal minimumZoomLevel READ minimumZoomLevel WRITE setMinimumZoomLevel NOTIFY minimumZoomLevelChanged)
@@ -25,6 +26,9 @@ class Q_DECL_EXPORT QQuickMapboxGL : public QQuickFramebufferObject
Q_PROPERTY(bool copyrightsVisible READ copyrightsVisible WRITE setCopyrightsVisible NOTIFY copyrightsVisibleChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
// MapboxGL QML Type interface.
Q_PROPERTY(QString style READ style WRITE setStyle NOTIFY styleChanged)
public:
QQuickMapboxGL(QQuickItem *parent = 0);
virtual ~QQuickMapboxGL();
@@ -59,10 +63,14 @@ public:
void setColor(const QColor &color);
QColor color() const;
void setStyle(const QString &style);
QString style() const;
enum SyncState {
NothingNeedsSync = 0x00,
ZoomNeedsSync = 0x01,
CenterNeedsSync = 0x02,
StyleNeedsSync = 0x04,
};
int swapSyncState();
@@ -80,6 +88,8 @@ signals:
void copyrightsVisibleChanged(bool visible);
void colorChanged(const QColor &color);
void styleChanged();
private:
qreal m_minimumZoomLevel = 0;
qreal m_maximumZoomLevel = 20;
@@ -88,6 +98,8 @@ private:
QGeoCoordinate m_center;
QGeoShape m_visibleRegion;
QString m_style;
int m_syncState = NothingNeedsSync;
};

View File

@@ -21,6 +21,8 @@ ApplicationWindow {
id: map
anchors.fill: parent
style: "mapbox://styles/mapbox/streets-v8"
center: QtPositioning.coordinate(60.170448, 24.942046)
zoomLevel: 14
minimumZoomLevel: 8

View File

@@ -3,6 +3,7 @@
#include <QQuickMapboxGL>
#include <QQuickItem>
#include <QString>
#include <QtGlobal>
QQuickMapboxGL::QQuickMapboxGL(QQuickItem *parent_)
@@ -156,6 +157,25 @@ QColor QQuickMapboxGL::color() const
return QColor();
}
void QQuickMapboxGL::setStyle(const QString &styleUrl)
{
if (m_style == styleUrl) {
return;
}
m_style = styleUrl;
m_syncState |= StyleNeedsSync;
update();
emit styleChanged();
}
QString QQuickMapboxGL::style() const
{
return m_style;
}
int QQuickMapboxGL::swapSyncState()
{
int oldState = m_syncState;

View File

@@ -42,7 +42,6 @@ void QQuickMapboxGLRenderer::render()
void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item)
{
if (!m_initialized) {
m_map->setStyleURL(QMapbox::defaultStyles()[0].first);
QObject::connect(m_map.data(), SIGNAL(needsRendering()), item, SLOT(update()));
m_initialized = true;
}
@@ -58,4 +57,8 @@ void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item)
const auto& center = quickMap->center();
m_map->setCoordinate(QMapbox::Coordinate(center.latitude(), center.longitude()));
}
if (syncStatus & QQuickMapboxGL::StyleNeedsSync) {
m_map->setStyleURL(quickMap->style());
}
}