Fixed map jumping when starting a drag
[situare] / src / map / mapview.cpp
index 895f644..8cdcf29 100644 (file)
 
 #include <QDebug>
 #include <QMouseEvent>
+#include <QParallelAnimationGroup>
 
+#include "coordinates/scenecoordinate.h"
 #include "mapcommon.h"
+#include "mapscroller.h"
+
 #include "mapview.h"
 
+const qreal MS_PER_S = 1000;
+
+// const values for tuning the kinetic scroll effect
+const int KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS = 30;
+const int KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS = 100;
+const qreal KINETIC_MAX_VIEW_DISTANCE_FACTOR = 0.8;
+const int KINETIC_SCROLL_TIME_MS = 750;
+const qreal KINETIC_SPEED_TO_DISTANCE_FACTOR = 0.15 * sqrt(KINETIC_SCROLL_TIME_MS / MS_PER_S);
+
+const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
+
 MapView::MapView(QWidget *parent)
-    : QGraphicsView(parent)
+    : QGraphicsView(parent),
+      m_doubleTapZoomRunning(false),
+      m_panelIsOpen(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -39,78 +56,186 @@ MapView::MapView(QWidget *parent)
     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
     connect(m_zoomAnimation, SIGNAL(finished()),
         this, SIGNAL(viewZoomFinished()));
-    connect(m_zoomAnimation, SIGNAL(finished()),
-            this, SLOT(disableAnchor()));
-
     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
+
+    m_scroller = &MapScroller::getInstance();
+
+    m_scrollAndZoomAnimation = new QParallelAnimationGroup();
+    m_scrollAndZoomAnimation->addAnimation(m_scroller);
+    m_scrollAndZoomAnimation->addAnimation(m_zoomAnimation);
+    connect(m_scrollAndZoomAnimation, SIGNAL(finished()),
+            this, SLOT(doubleTapZoomFinished()));
 }
 
-void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
+void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool isUserDragAction)
 {
-    qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
+    qDebug() << __PRETTY_FUNCTION__ << "coordinate" << coordinate;
 
-    centerOn(sceneCoordinate);
+    QPointF target = coordinate.toPointF();
+
+    if (!isUserDragAction) {
+        target += m_centerHorizontalShift;
+    }
+
+    centerOn(target);
 }
 
-void MapView::mouseDoubleClickEvent(QMouseEvent *event)
+void MapView::disableCenterShift()
 {
-    qWarning() << __PRETTY_FUNCTION__ << event->pos().x() << event->pos().y();
-    qWarning() << __PRETTY_FUNCTION__ << mapToScene(event->pos()).toPoint().x() <<
-            mapToScene(event->pos()).toPoint().y();
+    qWarning() << __PRETTY_FUNCTION__;
+
+    m_panelIsOpen = false;
+    updateCenterShift();
+
+    ///< @todo Update center position
+}
 
-    QPoint centerPosition = mapToScene(QPoint(width() / 2, height() / 2)).toPoint();
-    QPoint pressPosition = mapToScene(event->pos()).toPoint();
-    QPoint scenePosition = centerPosition - ((centerPosition - pressPosition) / 2);
+void MapView::doubleTapZoomFinished()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
+    m_doubleTapZoomRunning = false;
     emit zoomIn();
-//    translate(event->pos().x() / 2, event->pos().y() / 2);
-//    emit viewScrolled(scenePosition);
-//    setTransformationAnchor(QGraphicsView::AnchorViewCenter);
 }
 
-void MapView::keyPressEvent(QKeyEvent *event)
+void MapView::enableCenterShift()
 {
-    if (event->key() == Qt::Key_Space)
-        setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
+    qWarning() << __PRETTY_FUNCTION__;
+
+    m_panelIsOpen = true;
+    updateCenterShift();
+
+    ///< @todo Update center position
+}
+
+void MapView::mouseDoubleClickEvent(QMouseEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_zoomLevel + 1 <= OSM_MAX_ZOOM_LEVEL) {
+        QPoint pressPosition = mapToScene(event->pos()).toPoint();
+        QPoint viewCenterPosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
+        QPoint zoomPosition = viewCenterPosition - ((viewCenterPosition - pressPosition) / 2);
+
+        m_scrollAndZoomAnimation->stop();
+        m_doubleTapZoomRunning = true;
+
+        m_scroller->setEasingCurve(QEasingCurve::Linear);
+        m_scroller->setDuration(ZOOM_TIME_MS);
+        m_scroller->setStartValue(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()));
+        m_scroller->setEndValue(SceneCoordinate(zoomPosition.x(), zoomPosition.y()));
+
+        m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
+        m_zoomAnimation->setDuration(ZOOM_TIME_MS);
+        m_zoomAnimation->setStartValue(viewScale());
+        m_zoomAnimation->setEndValue(pow(2, m_zoomLevel+1 - OSM_MAX_ZOOM_LEVEL));
+
+        m_scrollAndZoomAnimation->start();
+    }
 }
 
 void MapView::mouseMoveEvent(QMouseEvent *event)
 {
-    m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    qWarning() << __PRETTY_FUNCTION__;
+    if (m_doubleTapZoomRunning)
+        return;
+
+    m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
 
-    qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
+    if (m_index >= VALUES)
+        m_index = 0;
 
-    emit viewScrolled(m_scenePosition);
+    m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
+    m_dragTime[m_index] = m_time.elapsed();
+    m_time.start();
+    m_index++;
 
-    m_mousePosition = mapToScene(event->pos()).toPoint();
+    emit viewScrolled(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()), true);
+
+    m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
+    m_mouseLastViewPosition = event->pos();
 }
 
 void MapView::mousePressEvent(QMouseEvent *event)
 {
-    qWarning() << __PRETTY_FUNCTION__;
     qDebug() << __PRETTY_FUNCTION__;
 
+    if (m_doubleTapZoomRunning)
+        return;
+
+    m_time.start();
+
+    m_scroller->stop();
+
     QGraphicsView::mousePressEvent(event);
 
-    m_mousePosition = mapToScene(event->pos()).toPoint();
+    m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
+    m_mouseLastViewPosition = event->pos();
     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
+
+    for (int i = 0; i < VALUES; i++) {
+        m_dragMovement[i] = QPoint();
+        m_dragTime[i] = 0;
+    }
+    m_index = 0;
 }
 
 void MapView::mouseReleaseEvent(QMouseEvent *event)
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_doubleTapZoomRunning)
+        return;
+
+    int elapsed = m_time.elapsed();
 
     QGraphicsView::mouseReleaseEvent(event);
+
+    // start kinetic scroll only if there isn't too much time elapsed from the last mouse move event
+    if (elapsed <= KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS) {
+        QPointF dragViewSpeed;
+        int dragLength = 0;
+        int values = 0;
+        for (int i = 0; i < VALUES; i++) {
+            if (m_dragTime[i] > 0) {
+                dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
+                dragLength += m_dragMovement[i].manhattanLength();
+                values++;
+            }
+        }
+
+        if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
+            dragViewSpeed /= values;
+            QPointF effectViewDistance = dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
+
+            // limit the scroll distance in screen pixels
+            qreal biggerDistance = qMax(abs(effectViewDistance.x()), abs(effectViewDistance.y()));
+            if (biggerDistance > m_kineticMaxViewDistance)
+                effectViewDistance /= biggerDistance / m_kineticMaxViewDistance;
+
+            QPointF effectSceneDistance = effectViewDistance
+                                          * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel));
+
+            m_scroller->setEasingCurve(QEasingCurve::OutCirc);
+            m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
+            m_scroller->setStartValue(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()));
+            QPointF endValue = QPointF(m_scenePosition) + effectSceneDistance;
+            m_scroller->setEndValue(SceneCoordinate(endValue.x(), endValue.y()));
+            m_scroller->start();
+        }
+    }
 }
 
 void MapView::resizeEvent(QResizeEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
 
+    m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
+
     emit viewResized(event->size());
+
+    updateCenterShift();
 }
 
 void MapView::setViewScale(qreal viewScale)
@@ -126,14 +251,31 @@ void MapView::setZoomLevel(int zoomLevel)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    m_zoomLevel = zoomLevel;
+
     if (m_zoomAnimation) {
         m_zoomAnimation->stop();
-        m_zoomAnimation->setDuration(ZOOM_TIME);
+        m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
+        m_zoomAnimation->setDuration(ZOOM_TIME_MS);
         m_zoomAnimation->setStartValue(viewScale());
-        m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
+        m_zoomAnimation->setEndValue(pow(2, zoomLevel - OSM_MAX_ZOOM_LEVEL));
 
         m_zoomAnimation->start();
     }
+
+    updateCenterShift();
+}
+
+void MapView::updateCenterShift()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    int shift = 0;
+
+    if (m_panelIsOpen)
+        shift = 200;
+
+    m_centerHorizontalShift = QPointF(shift * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel)), 0);
 }
 
 qreal MapView::viewScale()
@@ -143,15 +285,10 @@ qreal MapView::viewScale()
     return transform().m11();
 }
 
-void MapView::disableAnchor()
+MapView::~MapView()
 {
-    qWarning() << __PRETTY_FUNCTION__;
-
-//    QCoreApplication::postEvent(this, new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
-//                                                      Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
+    qDebug() << __PRETTY_FUNCTION__;
 
-//    QGraphicsView::mouseReleaseEvent(new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
-//                                                     Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
-//    centerOn(0, 0);
-    releaseMouse();
+    m_scrollAndZoomAnimation->removeAnimation(m_scroller);
+    delete m_scrollAndZoomAnimation;
 }