summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <tmpsantos@gmail.com>2019-09-17 23:44:31 +0300
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2019-09-18 15:20:22 +0300
commited76d8fa7dd7aa9785e3f08172a908dafdf7adcc (patch)
tree95e39dfd8f6606f6b770a9e028440e942d6891da
parentfc7d2938dd7352055ed0562db5235c97e5a01745 (diff)
downloadqtlocation-mapboxgl-upstream/tmpsantos-qt_touch.tar.gz
[qt] Add touch support to the Qt appupstream/tmpsantos-qt_touch
-rw-r--r--platform/qt/app/mapwindow.cpp70
-rw-r--r--platform/qt/app/mapwindow.hpp7
2 files changed, 77 insertions, 0 deletions
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 <QMouseEvent>
#include <QString>
+#include <cmath>
+
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<QTouchEvent::TouchPoint> 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<QTouchEvent *>(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 <QOpenGLWidget>
#include <QPropertyAnimation>
#include <QScopedPointer>
+#include <QTouchEvent>
#include <QtGlobal>
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;
};