From ed76d8fa7dd7aa9785e3f08172a908dafdf7adcc Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Tue, 17 Sep 2019 23:44:31 +0300 Subject: [qt] Add touch support to the Qt app --- platform/qt/app/mapwindow.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++ platform/qt/app/mapwindow.hpp | 7 +++++ 2 files changed, 77 insertions(+) diff --git a/platform/qt/app/mapwindow.cpp b/platform/qt/app/mapwindow.cpp index 6171c8bf35..eb759d2504 100644 --- a/platform/qt/app/mapwindow.cpp +++ b/platform/qt/app/mapwindow.cpp @@ -9,12 +9,15 @@ #include #include +#include + int kAnimationDuration = 10000; MapWindow::MapWindow(const QMapboxGLSettings &settings) : m_settings(settings) { setWindowIcon(QIcon(":icon.png")); + setAttribute(Qt::WA_AcceptTouchEvents); } MapWindow::~MapWindow() @@ -494,3 +497,70 @@ void MapWindow::paintGL() m_map->setFramebufferObject(defaultFramebufferObject(), size() * pixelRatio()); m_map->render(); } + +void MapWindow::touchEvent(QTouchEvent* ev) { + switch (ev->type()) { + case QTouchEvent::TouchBegin: + m_accumulatedTouchAngle = 0.0; + m_touchRotation = false; + // fall through + case QTouchEvent::TouchUpdate: + case QTouchEvent::TouchEnd: { + QList tps = ev->touchPoints(); + if (tps.count() == 1) { + QPointF delta = tps.first().pos() - tps.first().lastPos(); + m_map->moveBy(delta); + } else if (tps.count() == 2) { + QLineF last(tps.first().lastPos(), tps.last().lastPos()); + QLineF end(tps.first().pos(), tps.last().pos()); + + QPointF lastCenter(0.5 * last.p1() + 0.5 * last.p2()); + QPointF endCenter(0.5 * end.p1() + 0.5 * end.p2()); + + QPointF delta = endCenter - lastCenter; + m_map->moveBy(delta); + + float s = end.length() / last.length(); + m_map->scaleBy(s, endCenter); + + if (end.length() > 75.0) { + m_accumulatedTouchAngle = std::fmod(m_accumulatedTouchAngle + last.angleTo(end), 360.0); + if (m_accumulatedTouchAngle >= 180.0) { + m_accumulatedTouchAngle -= 360.0; + } + } + if (m_touchRotation || m_accumulatedTouchAngle > 10.0 || m_accumulatedTouchAngle < -10.0) { + m_touchRotation = true; + + QMapboxGLCameraOptions camera; + camera.anchor = endCenter; + camera.bearing = m_map->bearing() + last.angleTo(end); + + m_map->jumpTo(camera); + + } + } else if (tps.count() == 3) { + float s = 100.0 * (tps.first().pos().y() - tps.first().lastPos().y()) / width(); + m_map->pitchBy(s); + } + }; break; + default: + ev->ignore(); + break; + } +} + +bool MapWindow::event(QEvent* ev) { + switch (ev->type()) { + case QEvent::TouchBegin: + case QEvent::TouchUpdate: + case QEvent::TouchEnd: { + touchEvent(static_cast(ev)); + return true; + }; break; + default: + break; + } + + return QOpenGLWidget::event(ev); +} diff --git a/platform/qt/app/mapwindow.hpp b/platform/qt/app/mapwindow.hpp index 0671c42518..37b7f1d5d5 100644 --- a/platform/qt/app/mapwindow.hpp +++ b/platform/qt/app/mapwindow.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include class QKeyEvent; @@ -31,6 +32,7 @@ private: qreal pixelRatio(); // QWidget implementation. + bool event(QEvent *e) final; void keyPressEvent(QKeyEvent *ev) final; void mousePressEvent(QMouseEvent *ev) final; void mouseMoveEvent(QMouseEvent *ev) final; @@ -40,6 +42,8 @@ private: void initializeGL() final; void paintGL() final; + void touchEvent(QTouchEvent *ev); + QPointF m_lastPos; QMapboxGLSettings m_settings; @@ -55,6 +59,9 @@ private: QVariant m_lineAnnotationId; QVariant m_fillAnnotationId; + bool m_touchRotation = false; + double m_accumulatedTouchAngle = 0.0; + bool m_sourceAdded = false; }; -- cgit v1.2.1