Animation for enabling and disabling the center point shifting
authorSami Rämö <sami.ramo@ixonos.com>
Mon, 23 Aug 2010 12:07:37 +0000 (15:07 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Mon, 23 Aug 2010 12:07:37 +0000 (15:07 +0300)
src/map/mapview.cpp
src/map/mapview.h

index 4801d60..4fbcb0c 100644 (file)
@@ -45,7 +45,6 @@ const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
 
 MapView::MapView(QWidget *parent)
     : QGraphicsView(parent),
-      m_centerShiftEnabled(false),
       m_doubleTapZoomRunning(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -65,6 +64,13 @@ MapView::MapView(QWidget *parent)
     m_scrollAndZoomAnimation->addAnimation(m_zoomAnimation);
     connect(m_scrollAndZoomAnimation, SIGNAL(finished()),
             this, SLOT(doubleTapZoomFinished()));
+
+    m_centerShiftAnimation = new QPropertyAnimation(this, "viewShift", this);
+    if (m_centerShiftAnimation) {
+        m_centerShiftAnimation->setStartValue(0.0);
+        m_centerShiftAnimation->setDuration(KINETIC_SCROLL_TIME_MS);
+    }
+
 }
 
 MapView::~MapView()
@@ -83,7 +89,7 @@ void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool i
     m_lastSetScenePosition = coordinate;
 
     if (!isUserDragAction) {
-        target += m_centerHorizontalShift;
+        target += m_centerHorizontalShiftPoint;
     }
 
     centerOn(target);
@@ -91,9 +97,15 @@ void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool i
 
 void MapView::disableCenterShift()
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__;
 
-    toggleCenterShift(false);
+//    toggleCenterShift(false);
+
+    if (m_centerShiftAnimation) {
+        m_centerShiftAnimation->setDirection(QAbstractAnimation::Backward);
+        qWarning() << __PRETTY_FUNCTION__ << "starting animation";
+        m_centerShiftAnimation->start();
+    }
 }
 
 void MapView::doubleTapZoomFinished()
@@ -106,9 +118,15 @@ void MapView::doubleTapZoomFinished()
 
 void MapView::enableCenterShift()
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__;
+
+//    toggleCenterShift(true);
 
-    toggleCenterShift(true);
+    if (m_centerShiftAnimation) {
+        m_centerShiftAnimation->setDirection(QAbstractAnimation::Forward);
+        qWarning() << __PRETTY_FUNCTION__ << "starting animation";
+        m_centerShiftAnimation->start();
+    }
 }
 
 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
@@ -241,6 +259,12 @@ void MapView::resizeEvent(QResizeEvent *event)
 
     emit viewResized(event->size());
 
+    if (m_centerShiftAnimation) {
+        m_centerShiftAnimation->setEndValue(event->size().width() / 4);
+    }
+
+    qWarning() << __PRETTY_FUNCTION__ << "animation end value:" << m_centerShiftAnimation->endValue().toReal();
+
     updateCenterShift();
 }
 
@@ -251,6 +275,17 @@ void MapView::setViewScale(qreal viewScale)
     QTransform transform;
     transform.scale(viewScale, viewScale);
     setTransform(transform);
+
+    updateCenterShift();
+}
+
+void MapView::setViewShift(qreal viewShift)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_centerHorizontalShiftViewPixels = viewShift;
+
+    updateCenterShift();
 }
 
 void MapView::setZoomLevel(int zoomLevel)
@@ -268,35 +303,28 @@ void MapView::setZoomLevel(int zoomLevel)
 
         m_zoomAnimation->start();
     }
-
-    updateCenterShift();
 }
 
-void MapView::toggleCenterShift(bool enabled)
+void MapView::updateCenterShift()
 {
-    qWarning() << __PRETTY_FUNCTION__ << enabled;
+    qWarning() << __PRETTY_FUNCTION__;
 
-    m_centerShiftEnabled = enabled;
-    updateCenterShift();
+    m_centerHorizontalShiftPoint = QPointF(m_centerHorizontalShiftViewPixels * (1.0 / viewScale()),
+                                           0);
 
     centerToSceneCoordinates(m_lastSetScenePosition);
 }
 
-void MapView::updateCenterShift()
+qreal MapView::viewScale() const
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    int shift = 0;
-
-    if (m_centerShiftEnabled)
-        shift = 200;
-
-    m_centerHorizontalShift = QPointF(shift * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel)), 0);
+    return transform().m11();
 }
 
-qreal MapView::viewScale()
+qreal MapView::viewShift() const
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    return transform().m11();
+    return m_centerHorizontalShiftViewPixels;
 }
index ca35767..aab39f3 100644 (file)
@@ -28,8 +28,8 @@
 
 #include "coordinates/scenecoordinate.h"
 
-class QPropertyAnimation;
 class QParallelAnimationGroup;
+class QPropertyAnimation;
 
 class MapScroller;
 
@@ -52,6 +52,13 @@ class MapView : public QGraphicsView
     */
     Q_PROPERTY(qreal viewScale READ viewScale WRITE setViewScale)
 
+    /**
+    * @brief View shifting
+    *
+    * @property viewShift
+    */
+    Q_PROPERTY(qreal viewShift READ viewShift WRITE setViewShift)
+
 public:
     /**
     * @brief Constructor
@@ -174,6 +181,13 @@ private:
     void setViewScale(qreal viewScale);
 
     /**
+    * @brief Set new view shifting
+    *
+    * @param viewShift New shifting amount
+    */
+    void setViewShift(qreal viewShift);
+
+    /**
     * @brief Toggles the shifting of the center point
     *
     * @param enabled True if shifting is enabled
@@ -190,7 +204,14 @@ private:
     *
     * @return Current view scaling factor
     */
-    qreal viewScale();
+    qreal viewScale() const;
+
+    /**
+    * @brief Get current view shifting
+    *
+    * @return Current view shifting amount
+    */
+    qreal viewShift() const;
 
 /*******************************************************************************
  * SIGNALS
@@ -227,27 +248,31 @@ signals:
  * DATA MEMBERS
  ******************************************************************************/
 private:
-    bool m_centerShiftEnabled;            ///< Is the center point shifting enabled?
     bool m_doubleTapZoomRunning;          ///< Double tap zoom running flag
 
     int m_dragTime[VALUES];               ///< Table of mouse event durations
     int m_index;                          ///< Index of mouse event values tables
     int m_zoomLevel;                      ///< Current zoom level
 
+    qreal m_centerHorizontalShiftViewPixels;
     qreal m_kineticMaxViewDistance;       ///< Maximum kinetic scroll distance in view pixels
 
+    QParallelAnimationGroup *m_scrollAndZoomAnimation;  ///< Double click zoom animation
+
     QPoint m_dragMovement[VALUES];        ///< Table of mouse event distances
     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;      ///< Current amount of center point shifting
+    QPointF m_centerHorizontalShiftPoint;      ///< Current amount of center point shifting
+    QPointF m_centerHorizontalShiftEndValue;
 
-    QParallelAnimationGroup *m_scrollAndZoomAnimation;  ///< Double click zoom animation
     QPropertyAnimation *m_zoomAnimation;  ///< Zoom animation
 
     QTime m_time;                         ///< Elapsed times in mouse events
 
+    QPropertyAnimation *m_centerShiftAnimation; ///< Animation for shifting the center point
+
     MapScroller *m_scroller;                    ///< Kinetic scroller
     SceneCoordinate m_lastSetScenePosition;     ///< Last center point coordinate set by MapEngine
 };