Review, updating functional tests
[situare] / src / map / mapview.h
index 9607c19..6715fa6 100644 (file)
@@ -3,6 +3,7 @@
    Copyright (C) 2010  Ixonos Plc. Authors:
 
        Sami Rämö - sami.ramo@ixonos.com
+       Pekka Nissinen - pekka.nissinen@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
 #define MAPVIEW_H
 
 #include <QGraphicsView>
+#include <QTime>
+
+#include "coordinates/scenecoordinate.h"
+
+class QParallelAnimationGroup;
+class QPropertyAnimation;
+
+class MapScroller;
+
+#define VALUES 4
 
 /**
 * @brief Map view widget
 *
-* QAbstractKineticScroller is enabled when built for Maemo5.
 * @author Sami Rämö - sami.ramo (at) ixonos.com
+* @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
 */
 class MapView : public QGraphicsView
 {
     Q_OBJECT
+
+    /**
+    * @brief View scaling
+    *
+    * @property viewScale
+    */
+    Q_PROPERTY(qreal viewScale READ viewScale WRITE setViewScale)
+
+    /**
+    * @brief View shifting
+    *
+    * @property viewShift
+    */
+    Q_PROPERTY(qreal viewShift READ viewShift WRITE setViewShift)
+
 public:
     /**
     * @brief Constructor
@@ -41,36 +67,83 @@ public:
     */
     MapView(QWidget *parent = 0);
 
+    /**
+    * @brief Destructor.
+    *
+    * Takes MapScroller animation from double click zoom animation group and
+    * deletes animation group.
+    */
+    ~MapView();
+
+/*******************************************************************************
+ * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
+ ******************************************************************************/
 protected:
-//    void scrollContentsBy(int dx, int dy);
+    /**
+    * @brief Called when view is resized.
+    *
+    * @param event resize event
+    */
+    void resizeEvent(QResizeEvent *event);
 
 private:
-    //
     /**
-    * @brief get current horizontal scale (vertical should be same)
+    * @brief Event handler for mouse double click event
     *
-    * @return qreal Current horizontal scale value
+    * Emits zoomIn signal.
+    * @param event QMouseEvent
     */
-    qreal currentScale();
+    void mouseDoubleClickEvent(QMouseEvent *event);
 
-signals:
     /**
-    * @brief Signal for view scroll events
+    * @brief Event handler for mouse move events
+    *
+    * Does calculate mouse movement delta from last event position and new view center
+    * based on that delta. Saves current event position for next round. Emits viewScrolled
+    * signal and doesn't actually scroll the view.
     *
-    * Signal is emitted when view scroll is requested with mouse (or finger).
-    * @param sceneCoordinate Scene coordinates of the new requested center point of the view
+    * Saves mouse movement deltas and durations for last few move events to be used for
+    * calculating the kinetic scrolling speed.
+    *
+    * @param event Mouse event
     */
-    void viewScrolled(QPointF sceneCoordinate);
+    void mouseMoveEvent(QMouseEvent *event);
 
-    void viewResized(const QSize &size);
+    /**
+    * @brief Event handler for mouse press events
+    *
+    * Saves inial values for mouse and scene location for dragging the view. Does stop currently
+    * running kinetic scroll effect.
+    *
+    * @param event Mouse event
+    */
+    void mousePressEvent(QMouseEvent *event);
 
+    /**
+    * @brief Event handler for mouse release events
+    *
+    * Set up and start kinetic scrolling effect if time elapsed from last mouseMoveEvent is below
+    * the limit and drag length is over the limit.
+    *
+    * Kinetic scroll distance is calculated based on mouse movement event values saved in
+    * mouseMoveEvent(). The distance is also limited so that map doesn't run too far.
+    *
+    * @param event Mouse event
+    */
+    void mouseReleaseEvent(QMouseEvent *event);
+
+/*******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
 public slots:
     /**
     * @brief Slot for centering view to new location
     *
-    * @param sceneCoordinate Scene coordinates of the new center point
+    * Does also shift the center point horizontally, if required.
+    *
+    * @param coordinate Scene coordinates of the new center point
     */
-    void centerToSceneCoordinates(QPointF sceneCoordinate);
+    void centerToSceneCoordinates(const SceneCoordinate &coordinate);
 
     /**
     * @brief Set zoom level of the view
@@ -79,40 +152,135 @@ public slots:
     */
     void setZoomLevel(int zoomLevel);
 
-protected:
-    void resizeEvent(QResizeEvent *event);
-
 private slots:
     /**
-    * @brief Slot for mouse move events
+    * @brief Disables shifting of the center point
+    */
+    void disableCenterShift();
+
+    /**
+    * @brief Double tap zoom finished.
     *
-    * Does calculate mouse movement delta from last event position and new view center
-    * based on that delta. Saves current event position for next round. Emits viewScrolled
-    * signal and doesn't actually scroll the view.
-    * @param event Mouse event
+    * Disables double tap zoom flag and emits zoomIn signal.
     */
-    void mouseMoveEvent(QMouseEvent *event);
+    void doubleTapZoomFinished();
 
     /**
-    * @brief Slot for mouse press events
+    * @brief Enables shifting of the center point
+    */
+    void enableCenterShift();
+
+private:
+    /**
+    * @brief Returns the point which is considered by user as the visible center point
     *
-    * Saves inial values for mouse and scene location for dragging view.
-    * @param event Mouse event
+    * Differs from view's center point when panel is open and view center point is shifted.
+    *
+    * @returns Center point
     */
-    void mousePressEvent(QMouseEvent *event);
+    QPointF center() const;
+
+    /**
+    * @brief Set new view scale
+    *
+    * @param viewScale New scaling factor
+    */
+    void setViewScale(qreal viewScale);
+
+    /**
+    * @brief Set new view shifting
+    *
+    * @param viewShift New shifting amount
+    */
+    void setViewShift(qreal viewShift);
+
+    /**
+    * @brief Update center shifting value
+    */
+    void updateCenterShift();
+
+    /**
+    * @brief Get current view scale
+    *
+    * @return Current view scaling factor
+    */
+    qreal viewScale() const;
+
+    /**
+    * @brief Get current view shifting
+    *
+    * @return Current view shifting amount
+    */
+    qreal viewShift() const;
+
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
+signals:
+    /**
+    * @brief Emitted when map center point shiftin is changed
+    *
+    * @param shifting New shifting value
+    */
+    void horizontalShiftingChanged(int shifting);
+
+    /**
+    * @brief Signal for view resize events.
+    *
+    * Signal is emitted when view has been resized.
+    * @param size view size
+    */
+    void viewResized(const QSize &size);
 
     /**
-    * @brief Timer events for smooth zoom effect
+    * @brief Signal for view scroll events
     *
-    * @param event
+    * Signal is emitted when view is scrolled.
+    * @param coordinate Scene coordinates of the new center point of the view
     */
-    void timerEvent(QTimerEvent *event);
+    void viewScrolled(const SceneCoordinate &coordinate);
 
+    /**
+    * @brief Signal for informing that zooming animation is finished
+    */
+    void viewZoomFinished();
+
+    /**
+    * @brief Signal for informing that double click zoom is finished
+    */
+    void zoomIn();
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
 private:
-    QPointF m_mousePosition; ///< Previous mouse event position
-    QPointF m_scenePosition; ///< New center position
-    qreal m_zoomTargetScale; ///< Scaling factor of the target zoom level
-    qreal m_zoomScaleDelta; ///< Scaling factor delta for smooth zoom transition effect
+    bool m_doubleTapZoomRunning;                ///< Double tap zoom running flag
+
+    int m_dragTime[VALUES];                     ///< Table of mouse event durations
+    int m_index;                                ///< Current index of mouse event values table
+    int m_zoomLevel;                            ///< Current zoom level
+
+    qreal m_centerHorizontalShiftViewPixels;    ///< Center point horizontal shift in the view
+    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 (used for dragging)
+    QPoint m_lastMouseEventScenePosition;       ///< Previous mouse event position in the scene
+    QPoint m_lastMouseEventViewPosition;        ///< Previous mouse event position in the view
+    QPoint m_viewCenterPoint;                   ///< Center point of the MapView
+
+    QPointF m_centerHorizontalShiftPoint;       ///< Current amount of center point shifting
+
+    QPropertyAnimation *m_zoomAnimation;        ///< Zoom animation
+
+    QTime m_time;                               ///< Elapsed time between 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
 };
 
 #endif // MAPVIEW_H