Map double click zoom reviewed.
[situare] / src / map / mapview.h
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Sami Rämö - sami.ramo@ixonos.com
6        Pekka Nissinen - pekka.nissinen@ixonos.com
7
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #ifndef MAPVIEW_H
24 #define MAPVIEW_H
25
26 #include <QGraphicsView>
27 #include <QTime>
28
29 class QPropertyAnimation;
30 class QParallelAnimationGroup;
31
32 class MapScroller;
33
34 #define VALUES 4
35
36 /**
37 * @brief Map view widget
38 *
39 * @author Sami Rämö - sami.ramo (at) ixonos.com
40 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
41 */
42 class MapView : public QGraphicsView
43 {
44     Q_OBJECT
45
46     /**
47     * @brief View scaling
48     *
49     * @property viewScale
50     */
51     Q_PROPERTY(qreal viewScale READ viewScale WRITE setViewScale)
52
53 public:
54     /**
55     * @brief Constructor
56     *
57     * @param parent Parent
58     */
59     MapView(QWidget *parent = 0);
60
61     /**
62     * @brief Destructor.
63     *
64     * Takes MapScroller animation from double click zoom animation group and
65     * deletes animation group.
66     */
67     ~MapView();
68
69 /*******************************************************************************
70  * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
71  ******************************************************************************/
72 protected:
73     /**
74     * @brief Called when view is resized.
75     *
76     * @param event resize event
77     */
78     void resizeEvent(QResizeEvent *event);
79
80 private:
81     /**
82     * @brief Event handler for mouse double click event
83     *
84     * Emits zoomIn signal.
85     * @param event QMouseEvent
86     */
87     void mouseDoubleClickEvent(QMouseEvent *event);
88
89     /**
90     * @brief Event handler for mouse move events
91     *
92     * Does calculate mouse movement delta from last event position and new view center
93     * based on that delta. Saves current event position for next round. Emits viewScrolled
94     * signal and doesn't actually scroll the view.
95     *
96     * Saves mouse movement deltas and durations for last few move events to be used for
97     * calculating the kinetic scrolling speed.
98     *
99     * @param event Mouse event
100     */
101     void mouseMoveEvent(QMouseEvent *event);
102
103     /**
104     * @brief Event handler for mouse press events
105     *
106     * Saves inial values for mouse and scene location for dragging the view. Does stop currently
107     * running kinetic scroll effect.
108     *
109     * @param event Mouse event
110     */
111     void mousePressEvent(QMouseEvent *event);
112
113     /**
114     * @brief Event handler for mouse release events
115     *
116     * Set up and start kinetic scrolling effect if time elapsed from last mouseMoveEvent is below
117     * the limit and drag length is over the limit.
118     *
119     * Kinetic scroll distance is calculated based on mouse movement event values saved in
120     * mouseMoveEvent().
121     *
122     * @param event Mouse event
123     */
124     void mouseReleaseEvent(QMouseEvent *event);
125
126 /*******************************************************************************
127  * MEMBER FUNCTIONS AND SLOTS
128  ******************************************************************************/
129 public slots:
130     /**
131     * @brief Slot for centering view to new location
132     *
133     * @param sceneCoordinate Scene coordinates of the new center point
134     */
135     void centerToSceneCoordinates(QPoint sceneCoordinate);
136
137     /**
138     * @brief Set zoom level of the view
139     *
140     * @param zoomLevel Zoom level
141     */
142     void setZoomLevel(int zoomLevel);
143
144 private slots:
145     /**
146     * @brief Double tap zoom finished.
147     *
148     * Disables double tap zoom flag and emits zoomIn signal.
149     */
150     void doubleTapZoomFinished();
151
152 private:
153     /**
154     * @brief Set new view scale
155     *
156     * @param viewScale New scaling factor
157     */
158     void setViewScale(qreal viewScale);
159
160     /**
161     * @brief Get current view scale
162     *
163     * @return Current view scaling factor
164     */
165     qreal viewScale();
166
167 /*******************************************************************************
168  * SIGNALS
169  ******************************************************************************/
170 signals:
171     /**
172     * @brief Signal for view resize events.
173     *
174     * Signal is emitted when view has been resized.
175     * @param size view size
176     */
177     void viewResized(const QSize &size);
178
179     /**
180     * @brief Signal for view scroll events
181     *
182     * Signal is emitted when view is scrolled.
183     * @param sceneCoordinate Scene coordinates of the new center point of the view
184     */
185     void viewScrolled(QPoint sceneCoordinate);
186
187     /**
188     * @brief Signal for informing that zooming animation is finished
189     */
190     void viewZoomFinished();
191
192     /**
193     * @brief Signal for informing that double click zoom is finished
194     */
195     void zoomIn();
196
197 /*******************************************************************************
198  * DATA MEMBERS
199  ******************************************************************************/
200 private:
201     bool m_doubleTapZoomRunning;         ///< Double tap zoom running flag
202
203     int m_dragTime[VALUES];               ///< Table of mouse event durations
204     int m_index;                          ///< Index of mouse event values tables
205     int m_zoomLevel;                      ///< Current zoom level
206
207     QPoint m_dragMovement[VALUES];        ///< Table of mouse event distances
208     QPoint m_mouseLastScenePosition;      ///< Previous mouse event position in the scene
209     QPoint m_mouseLastViewPosition;       ///< Previous mouse event position in the view
210     QPoint m_scenePosition;               ///< New center position
211
212     QParallelAnimationGroup *m_scrollAndZoomAnimation;  ///< Double click zoom animation
213     QPropertyAnimation *m_zoomAnimation;  ///< Zoom animation
214
215     QTime m_time;                         ///< Elapsed times in mouse events
216
217     MapScroller *m_scroller;              ///< Kinetic scroller
218 };
219
220 #endif // MAPVIEW_H