Updates the view when center shifting is enabled/disabled
authorSami Rämö <sami.ramo@ixonos.com>
Mon, 23 Aug 2010 10:41:32 +0000 (13:41 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Mon, 23 Aug 2010 10:41:32 +0000 (13:41 +0300)
 - Does not use animation

 - Also made some variable renaming

src/map/mapview.cpp
src/map/mapview.h

index 8cdcf29..4801d60 100644 (file)
@@ -45,8 +45,8 @@ const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
 
 MapView::MapView(QWidget *parent)
     : QGraphicsView(parent),
-      m_doubleTapZoomRunning(false),
-      m_panelIsOpen(false)
+      m_centerShiftEnabled(false),
+      m_doubleTapZoomRunning(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -67,11 +67,20 @@ MapView::MapView(QWidget *parent)
             this, SLOT(doubleTapZoomFinished()));
 }
 
+MapView::~MapView()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_scrollAndZoomAnimation->removeAnimation(m_scroller);
+    delete m_scrollAndZoomAnimation;
+}
+
 void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool isUserDragAction)
 {
     qDebug() << __PRETTY_FUNCTION__ << "coordinate" << coordinate;
 
     QPointF target = coordinate.toPointF();
+    m_lastSetScenePosition = coordinate;
 
     if (!isUserDragAction) {
         target += m_centerHorizontalShift;
@@ -82,12 +91,9 @@ void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool i
 
 void MapView::disableCenterShift()
 {
-    qWarning() << __PRETTY_FUNCTION__;
-
-    m_panelIsOpen = false;
-    updateCenterShift();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    ///< @todo Update center position
+    toggleCenterShift(false);
 }
 
 void MapView::doubleTapZoomFinished()
@@ -100,12 +106,9 @@ void MapView::doubleTapZoomFinished()
 
 void MapView::enableCenterShift()
 {
-    qWarning() << __PRETTY_FUNCTION__;
-
-    m_panelIsOpen = true;
-    updateCenterShift();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    ///< @todo Update center position
+    toggleCenterShift(true);
 }
 
 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
@@ -122,7 +125,8 @@ void MapView::mouseDoubleClickEvent(QMouseEvent *event)
 
         m_scroller->setEasingCurve(QEasingCurve::Linear);
         m_scroller->setDuration(ZOOM_TIME_MS);
-        m_scroller->setStartValue(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()));
+        m_scroller->setStartValue(SceneCoordinate(m_internalScenePosition.x(),
+                                                  m_internalScenePosition.y()));
         m_scroller->setEndValue(SceneCoordinate(zoomPosition.x(), zoomPosition.y()));
 
         m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
@@ -141,20 +145,21 @@ void MapView::mouseMoveEvent(QMouseEvent *event)
     if (m_doubleTapZoomRunning)
         return;
 
-    m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
+    m_internalScenePosition += m_lastMouseEventScenePosition - mapToScene(event->pos()).toPoint();
 
     if (m_index >= VALUES)
         m_index = 0;
 
-    m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
+    m_dragMovement[m_index] = m_lastMouseEventViewPosition - event->pos();
     m_dragTime[m_index] = m_time.elapsed();
     m_time.start();
     m_index++;
 
-    emit viewScrolled(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()), true);
+    emit viewScrolled(SceneCoordinate(m_internalScenePosition.x(), m_internalScenePosition.y()),
+                      true);
 
-    m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
-    m_mouseLastViewPosition = event->pos();
+    m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
+    m_lastMouseEventViewPosition = event->pos();
 }
 
 void MapView::mousePressEvent(QMouseEvent *event)
@@ -170,9 +175,9 @@ void MapView::mousePressEvent(QMouseEvent *event)
 
     QGraphicsView::mousePressEvent(event);
 
-    m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
-    m_mouseLastViewPosition = event->pos();
-    m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
+    m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
+    m_lastMouseEventViewPosition = event->pos();
+    m_internalScenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
 
     for (int i = 0; i < VALUES; i++) {
         m_dragMovement[i] = QPoint();
@@ -219,8 +224,9 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
 
             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->setStartValue(SceneCoordinate(m_internalScenePosition.x(),
+                                                      m_internalScenePosition.y()));
+            QPointF endValue = QPointF(m_internalScenePosition) + effectSceneDistance;
             m_scroller->setEndValue(SceneCoordinate(endValue.x(), endValue.y()));
             m_scroller->start();
         }
@@ -266,13 +272,23 @@ void MapView::setZoomLevel(int zoomLevel)
     updateCenterShift();
 }
 
+void MapView::toggleCenterShift(bool enabled)
+{
+    qWarning() << __PRETTY_FUNCTION__ << enabled;
+
+    m_centerShiftEnabled = enabled;
+    updateCenterShift();
+
+    centerToSceneCoordinates(m_lastSetScenePosition);
+}
+
 void MapView::updateCenterShift()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
     int shift = 0;
 
-    if (m_panelIsOpen)
+    if (m_centerShiftEnabled)
         shift = 200;
 
     m_centerHorizontalShift = QPointF(shift * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel)), 0);
@@ -284,11 +300,3 @@ qreal MapView::viewScale()
 
     return transform().m11();
 }
-
-MapView::~MapView()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_scrollAndZoomAnimation->removeAnimation(m_scroller);
-    delete m_scrollAndZoomAnimation;
-}
index 37fb7c7..ae1168b 100644 (file)
 #include <QGraphicsView>
 #include <QTime>
 
+#include "coordinates/scenecoordinate.h"
+
 class QPropertyAnimation;
 class QParallelAnimationGroup;
 
 class MapScroller;
 
-class SceneCoordinate;
-
 #define VALUES 4
 
 /**
@@ -163,6 +163,8 @@ private:
     */
     void setViewScale(qreal viewScale);
 
+    void toggleCenterShift(bool enabled);
+
     void updateCenterShift();
 
     /**
@@ -206,8 +208,8 @@ signals:
  * DATA MEMBERS
  ******************************************************************************/
 private:
+    bool m_centerShiftEnabled;
     bool m_doubleTapZoomRunning;         ///< Double tap zoom running flag
-    bool m_panelIsOpen;
 
     int m_dragTime[VALUES];               ///< Table of mouse event durations
     int m_index;                          ///< Index of mouse event values tables
@@ -216,9 +218,9 @@ private:
     qreal m_kineticMaxViewDistance;       ///< Maximum kinetic scroll distance in view pixels
 
     QPoint m_dragMovement[VALUES];        ///< Table of mouse event distances
-    QPoint m_mouseLastScenePosition;      ///< Previous mouse event position in the scene
-    QPoint m_mouseLastViewPosition;       ///< Previous mouse event position in the view
-    QPoint m_scenePosition;               ///< New center position
+    QPoint m_internalScenePosition;       ///< New center position
+    QPoint m_lastMouseEventScenePosition; ///< Previous mouse event position in the scene
+    QPoint m_lastMouseEventViewPosition;  ///< Previous mouse event position in the view
 
     QPointF m_centerHorizontalShift;
 
@@ -228,6 +230,7 @@ private:
     QTime m_time;                         ///< Elapsed times in mouse events
 
     MapScroller *m_scroller;              ///< Kinetic scroller
+    SceneCoordinate m_lastSetScenePosition;
 };
 
 #endif // MAPVIEW_H