Files
MapLibreParticleLab/platform/qt/src/qquickmapboxglrenderer.cpp
Thiago Marcos P. Santos 91cdcbebf4 [Qt] Implement pan() method
2016-05-02 19:33:54 +03:00

69 lines
1.8 KiB
C++

#include "qquickmapboxglrenderer.hpp"
#include <QMapboxGL>
#include <QQuickMapboxGL>
#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);
m_map.reset(new QMapboxGL(nullptr, settings));
}
QQuickMapboxGLRenderer::~QQuickMapboxGLRenderer()
{
}
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) {
QObject::connect(m_map.data(), SIGNAL(needsRendering()), item, SLOT(update()));
m_initialized = true;
}
auto quickMap = static_cast<QQuickMapboxGL*>(item);
auto syncStatus = quickMap->swapSyncState();
if (syncStatus & QQuickMapboxGL::ZoomNeedsSync) {
m_map->setZoom(quickMap->zoomLevel());
}
if (syncStatus & QQuickMapboxGL::CenterNeedsSync) {
const auto& center = quickMap->center();
m_map->setCoordinate(QMapbox::Coordinate(center.latitude(), center.longitude()));
}
if (syncStatus & QQuickMapboxGL::StyleNeedsSync) {
m_map->setStyleURL(quickMap->style());
}
if (syncStatus & QQuickMapboxGL::PanNeedsSync) {
m_map->moveBy(quickMap->swapPan());
}
}