diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2015-07-22 14:16:58 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2016-04-20 20:55:51 +0300 |
commit | 7a44b95627f4875756b2701b65eb7e6a33ab7c7f (patch) | |
tree | 1223c8700d48a7c035651ad099920745e1a9a19f | |
parent | 854b3bc39b2a84656ca2c120865bf926d7a4b47d (diff) | |
download | qtlocation-mapboxgl-7a44b95627f4875756b2701b65eb7e6a33ab7c7f.tar.gz |
[Qt] Add a Qt app example
Should build and run with `make run-qt`
Signed-off-by: Bruno Abinader <bruno@mapbox.com>
Signed-off-by: Thiago Marcos P. Santos <thiago@mapbox.com>
-rw-r--r-- | platform/qt/app/main.cpp | 20 | ||||
-rw-r--r-- | platform/qt/app/mapwindow.cpp | 122 | ||||
-rw-r--r-- | platform/qt/app/mapwindow.hpp | 32 |
3 files changed, 174 insertions, 0 deletions
diff --git a/platform/qt/app/main.cpp b/platform/qt/app/main.cpp new file mode 100644 index 0000000000..0f43f30e02 --- /dev/null +++ b/platform/qt/app/main.cpp @@ -0,0 +1,20 @@ +#include "mapwindow.hpp" + +#include <QApplication> + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QMapboxGLSettings settings; + settings.setAccessToken(qgetenv("MAPBOX_ACCESS_TOKEN")); + settings.setCacheDatabasePath("/tmp/mbgl-cache.db"); + settings.setCacheDatabaseMaximumSize(20 * 1024 * 1024); + + MapWindow window(settings); + + window.resize(800, 600); + window.show(); + + return app.exec(); +} diff --git a/platform/qt/app/mapwindow.cpp b/platform/qt/app/mapwindow.cpp new file mode 100644 index 0000000000..9758ef5afa --- /dev/null +++ b/platform/qt/app/mapwindow.cpp @@ -0,0 +1,122 @@ +#include "mapwindow.hpp" + +#include <mbgl/util/default_styles.hpp> + +#include <QKeyEvent> +#include <QMouseEvent> +#include <QString> + +MapWindow::MapWindow(const QMapboxGLSettings &settings) : m_map(nullptr, settings) +{ + connect(&m_map, SIGNAL(needsRendering()), this, SLOT(updateGL())); + + // Set default location to Helsinki. + m_map.setCoordinateZoom(QMapboxGL::Coordinate(60.170448, 24.942046), 14); + + changeStyle(); +} + +void MapWindow::changeStyle() +{ + static uint8_t currentStyleIndex; + + mbgl::util::default_styles::DefaultStyle newStyle = + mbgl::util::default_styles::orderedStyles[currentStyleIndex]; + + QString url(newStyle.url); + m_map.setStyleURL(url); + + QString name(newStyle.name); + setWindowTitle(QString("Mapbox GL: ") + name); + + if (++currentStyleIndex == mbgl::util::default_styles::numOrderedStyles) { + currentStyleIndex = 0; + } +} + +void MapWindow::keyPressEvent(QKeyEvent *ev) +{ + if (ev->key() == Qt::Key_S) { + changeStyle(); + } + + ev->accept(); +} + +void MapWindow::mousePressEvent(QMouseEvent *ev) +{ +#if QT_VERSION < 0x050000 + m_lastPos = ev->posF(); +#else + m_lastPos = ev->localPos(); +#endif + + if (ev->type() == QEvent::MouseButtonPress) { + if (ev->buttons() == (Qt::LeftButton | Qt::RightButton)) { + changeStyle(); + } + } + + if (ev->type() == QEvent::MouseButtonDblClick) { + if (ev->buttons() == Qt::LeftButton) { + m_map.scaleBy(2.0, m_lastPos, 500); + } else if (ev->buttons() == Qt::RightButton) { + m_map.scaleBy(0.5, m_lastPos, 500); + } + } + + ev->accept(); +} + +void MapWindow::mouseMoveEvent(QMouseEvent *ev) +{ +#if QT_VERSION < 0x050000 + QPointF delta = ev->posF() - m_lastPos; +#else + QPointF delta = ev->localPos() - m_lastPos; +#endif + + if (!delta.isNull()) { + if (ev->buttons() == Qt::LeftButton) { + m_map.moveBy(delta); + } else if (ev->buttons() == Qt::RightButton) { +#if QT_VERSION < 0x050000 + m_map.rotateBy(m_lastPos, ev->posF()); +#else + m_map.rotateBy(m_lastPos, ev->localPos()); +#endif + } + } + +#if QT_VERSION < 0x050000 + m_lastPos = ev->posF(); +#else + m_lastPos = ev->localPos(); +#endif + ev->accept(); +} + +void MapWindow::wheelEvent(QWheelEvent *ev) +{ + if (ev->orientation() == Qt::Horizontal) { + return; + } + + float factor = ev->delta() / 1200.; + if (ev->delta() < 0) { + factor = factor > -1 ? factor : 1 / factor; + } + + m_map.scaleBy(1 + factor, ev->pos(), 50); + ev->accept(); +} + +void MapWindow::resizeGL(int w, int h) +{ + m_map.resize(QSize(w, h)); +} + +void MapWindow::paintGL() +{ + m_map.render(); +} diff --git a/platform/qt/app/mapwindow.hpp b/platform/qt/app/mapwindow.hpp new file mode 100644 index 0000000000..4bbeac5303 --- /dev/null +++ b/platform/qt/app/mapwindow.hpp @@ -0,0 +1,32 @@ +#ifndef MAPWINDOW_H +#define MAPWINDOW_H + +#include <QGLWidget> +#include <QMapboxGL> + +class QKeyEvent; +class QMouseEvent; +class QWheelEvent; + +class MapWindow : public QGLWidget +{ +public: + MapWindow(const QMapboxGLSettings &); + +private: + void changeStyle(); + + // QGLWidget implementation. + void keyPressEvent(QKeyEvent *ev) final; + void mousePressEvent(QMouseEvent *ev) final; + void mouseMoveEvent(QMouseEvent *ev) final; + void wheelEvent(QWheelEvent *ev) final; + void resizeGL(int w, int h) final; + void paintGL() final; + + QPointF m_lastPos; + + QMapboxGL m_map; +}; + +#endif |