Files
MapLibreParticleLab/platform/qt/src/qquickmapboxglrenderer.cpp
Bruno de Oliveira Abinader 8843bebee2 [Qt] Added QQuickMapboxGLStyle
A QQuickMapboxGLStyleProperty being a child of QQuickMapboxGLStyle
assures that only properties associated with that particular style will
reflect on the map - and that happens only when the style is the current
set via style property.
2016-08-17 15:21:52 +03:00

121 lines
3.8 KiB
C++

#include "qquickmapboxglrenderer.hpp"
#include <QMapboxGL>
#include <QQuickMapboxGL>
#include <QQuickMapboxGLStyle>
#include <QSize>
#include <QOpenGLFramebufferObject>
#include <QOpenGLFramebufferObjectFormat>
#include <QQuickWindow>
QQuickMapboxGLRenderer::QQuickMapboxGLRenderer()
{
QMapbox::initializeGLExtensions();
QMapboxGLSettings settings;
settings.setAccessToken(qgetenv("MAPBOX_ACCESS_TOKEN"));
settings.setCacheDatabasePath("/tmp/mbgl-cache.db");
settings.setCacheDatabaseMaximumSize(20 * 1024 * 1024);
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)
{
auto onMapChangeWillStartLoadingMap = [&]() {
m_styleLoaded = false;
};
auto onMapChangeDidFinishLoadingMap = [&]() {
m_styleLoaded = true;
emit styleChanged();
};
switch (change) {
case QMapboxGL::MapChangeWillStartLoadingMap:
onMapChangeWillStartLoadingMap();
break;
case QMapboxGL::MapChangeDidFinishLoadingMap:
onMapChangeDidFinishLoadingMap();
break;
default:
break;
}
}
QOpenGLFramebufferObject* QQuickMapboxGLRenderer::createFramebufferObject(const QSize &size)
{
m_map->resize(size);
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
return new QOpenGLFramebufferObject(size, format);
}
void QQuickMapboxGLRenderer::render()
{
m_map->render();
}
void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item)
{
if (!m_initialized) {
auto qquickMapbox = static_cast<QQuickMapboxGL*>(item);
QObject::connect(m_map.data(), &QMapboxGL::needsRendering, qquickMapbox, &QQuickMapboxGL::update);
QObject::connect(this, &QQuickMapboxGLRenderer::centerChanged, qquickMapbox, &QQuickMapboxGL::setCenter);
m_initialized = true;
}
auto quickMap = static_cast<QQuickMapboxGL*>(item);
auto syncStatus = quickMap->swapSyncState();
if (syncStatus & QQuickMapboxGL::CenterNeedsSync || syncStatus & QQuickMapboxGL::ZoomNeedsSync) {
const auto& center = quickMap->center();
m_map->setCoordinateZoom({ center.latitude(), center.longitude() }, quickMap->zoomLevel());
}
if (syncStatus & QQuickMapboxGL::StyleNeedsSync && quickMap->style()) {
m_map->setStyleURL(quickMap->style()->url());
m_styleLoaded = false;
}
if (syncStatus & QQuickMapboxGL::PanNeedsSync) {
m_map->moveBy(quickMap->swapPan());
emit centerChanged(QGeoCoordinate(m_map->latitude(), m_map->longitude()));
}
if (syncStatus & QQuickMapboxGL::BearingNeedsSync) {
m_map->setBearing(quickMap->bearing());
}
if (syncStatus & QQuickMapboxGL::PitchNeedsSync) {
m_map->setPitch(quickMap->pitch());
}
if (m_styleLoaded) {
if (!quickMap->layoutPropertyChanges().empty()) {
for (const auto& change: quickMap->layoutPropertyChanges()) {
m_map->setLayoutProperty(change.value("layer").toString(), change.value("property").toString(), change.value("value"));
}
quickMap->layoutPropertyChanges().clear();
}
if (!quickMap->paintPropertyChanges().empty()) {
for (const auto& change: quickMap->paintPropertyChanges()) {
m_map->setPaintProperty(change.value("layer").toString(), change.value("property").toString(), change.value("value"), change.value("class").toString());
}
quickMap->paintPropertyChanges().clear();
}
}
}