Review & fixing some small defects
[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 #include "coordinates/scenecoordinate.h"
30
31 class QParallelAnimationGroup;
32 class QPropertyAnimation;
33
34 class MapScroller;
35
36 #define VALUES 4
37
38 /**
39 * @brief Map view widget
40 *
41 * @author Sami Rämö - sami.ramo (at) ixonos.com
42 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
43 */
44 class MapView : public QGraphicsView
45 {
46     Q_OBJECT
47
48     /**
49     * @brief View scaling
50     *
51     * @property viewScale
52     */
53     Q_PROPERTY(qreal viewScale READ viewScale WRITE setViewScale)
54
55     /**
56     * @brief View shifting
57     *
58     * @property viewShift
59     */
60     Q_PROPERTY(qreal viewShift READ viewShift WRITE setViewShift)
61
62 public:
63     /**
64     * @brief Constructor
65     *
66     * @param parent Parent
67     */
68     MapView(QWidget *parent = 0);
69
70     /**
71     * @brief Destructor.
72     *
73     * Takes MapScroller animation from double click zoom animation group and
74     * deletes animation group.
75     */
76     ~MapView();
77
78 /*******************************************************************************
79  * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
80  ******************************************************************************/
81 protected:
82     /**
83     * @brief Called when view is resized.
84     *
85     * @param event resize event
86     */
87     void resizeEvent(QResizeEvent *event);
88
89 private:
90     /**
91     * @brief Event handler for mouse double click event
92     *
93     * Emits zoomIn signal.
94     * @param event QMouseEvent
95     */
96     void mouseDoubleClickEvent(QMouseEvent *event);
97
98     /**
99     * @brief Event handler for mouse move events
100     *
101     * Does calculate mouse movement delta from last event position and new view center
102     * based on that delta. Saves current event position for next round. Emits viewScrolled
103     * signal and doesn't actually scroll the view.
104     *
105     * Saves mouse movement deltas and durations for last few move events to be used for
106     * calculating the kinetic scrolling speed.
107     *
108     * @param event Mouse event
109     */
110     void mouseMoveEvent(QMouseEvent *event);
111
112     /**
113     * @brief Event handler for mouse press events
114     *
115     * Saves inial values for mouse and scene location for dragging the view. Does stop currently
116     * running kinetic scroll effect.
117     *
118     * @param event Mouse event
119     */
120     void mousePressEvent(QMouseEvent *event);
121
122     /**
123     * @brief Event handler for mouse release events
124     *
125     * Set up and start kinetic scrolling effect if time elapsed from last mouseMoveEvent is below
126     * the limit and drag length is over the limit.
127     *
128     * Kinetic scroll distance is calculated based on mouse movement event values saved in
129     * mouseMoveEvent(). The distance is also limited so that map doesn't run too far.
130     *
131     * @param event Mouse event
132     */
133     void mouseReleaseEvent(QMouseEvent *event);
134
135 /*******************************************************************************
136  * MEMBER FUNCTIONS AND SLOTS
137  ******************************************************************************/
138 public slots:
139     /**
140     * @brief Slot for centering view to new location
141     *
142     * Does also shift the center point horizontally, if required.
143     *
144     * @param coordinate Scene coordinates of the new center point
145     * @param isUserDragAction True if caused by user dragging action. Does not shift the center
146     *                         point if true.
147     */
148     void centerToSceneCoordinates(const SceneCoordinate &coordinate, bool isUserDragAction = false);
149
150     /**
151     * @brief Set zoom level of the view
152     *
153     * @param zoomLevel Zoom level
154     */
155     void setZoomLevel(int zoomLevel);
156
157 private slots:
158     /**
159     * @brief Disables shifting of the center point
160     */
161     void disableCenterShift();
162
163     /**
164     * @brief Double tap zoom finished.
165     *
166     * Disables double tap zoom flag and emits zoomIn signal.
167     */
168     void doubleTapZoomFinished();
169
170     /**
171     * @brief Enables shifting of the center point
172     */
173     void enableCenterShift();
174
175 private:
176     /**
177     * @brief Set new view scale
178     *
179     * @param viewScale New scaling factor
180     */
181     void setViewScale(qreal viewScale);
182
183     /**
184     * @brief Set new view shifting
185     *
186     * @param viewShift New shifting amount
187     */
188     void setViewShift(qreal viewShift);
189
190     /**
191     * @brief Update center shifting value
192     */
193     void updateCenterShift();
194
195     /**
196     * @brief Get current view scale
197     *
198     * @return Current view scaling factor
199     */
200     qreal viewScale() const;
201
202     /**
203     * @brief Get current view shifting
204     *
205     * @return Current view shifting amount
206     */
207     qreal viewShift() const;
208
209 /*******************************************************************************
210  * SIGNALS
211  ******************************************************************************/
212 signals:
213     /**
214     * @brief Emitted when map center point shiftin is changed
215     *
216     * @param shifting New shifting value
217     */
218     void horizontalShiftingChanged(int shifting);
219
220     /**
221     * @brief Signal for view resize events.
222     *
223     * Signal is emitted when view has been resized.
224     * @param size view size
225     */
226     void viewResized(const QSize &size);
227
228     /**
229     * @brief Signal for view scroll events
230     *
231     * Signal is emitted when view is scrolled.
232     * @param coordinate Scene coordinates of the new center point of the view
233     * @param isUserDragAction True if caused by user dragging action
234     */
235     void viewScrolled(const SceneCoordinate &coordinate, bool isUserDragAction);
236
237     /**
238     * @brief Signal for informing that zooming animation is finished
239     */
240     void viewZoomFinished();
241
242     /**
243     * @brief Signal for informing that double click zoom is finished
244     */
245     void zoomIn();
246
247 /*******************************************************************************
248  * DATA MEMBERS
249  ******************************************************************************/
250 private:
251     bool m_doubleTapZoomRunning;                ///< Double tap zoom running flag
252
253     int m_dragTime[VALUES];                     ///< Table of mouse event durations
254     int m_index;                                ///< Current index of mouse event values table
255     int m_zoomLevel;                            ///< Current zoom level
256
257     qreal m_centerHorizontalShiftViewPixels;    ///< Center point horizontal shift in the view
258     qreal m_kineticMaxViewDistance;             ///< Maximum kinetic scroll distance in view pixels
259
260     QParallelAnimationGroup *m_scrollAndZoomAnimation;  ///< Double click zoom animation
261
262     QPoint m_dragMovement[VALUES];              ///< Table of mouse event distances
263     QPoint m_internalScenePosition;             ///< New center position (used for dragging)
264     QPoint m_lastMouseEventScenePosition;       ///< Previous mouse event position in the scene
265     QPoint m_lastMouseEventViewPosition;        ///< Previous mouse event position in the view
266
267     QPointF m_centerHorizontalShiftPoint;       ///< Current amount of center point shifting
268
269     QPropertyAnimation *m_zoomAnimation;        ///< Zoom animation
270
271     QTime m_time;                               ///< Elapsed time between mouse events
272
273     QPropertyAnimation *m_centerShiftAnimation; ///< Animation for shifting the center point
274
275     MapScroller *m_scroller;                    ///< Kinetic scroller
276     SceneCoordinate m_lastSetScenePosition;     ///< Last center point coordinate set by MapEngine
277 };
278
279 #endif // MAPVIEW_H