Updates the view when center shifting is enabled/disabled
[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 QPropertyAnimation;
32 class QParallelAnimationGroup;
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 public:
56     /**
57     * @brief Constructor
58     *
59     * @param parent Parent
60     */
61     MapView(QWidget *parent = 0);
62
63     /**
64     * @brief Destructor.
65     *
66     * Takes MapScroller animation from double click zoom animation group and
67     * deletes animation group.
68     */
69     ~MapView();
70
71 /*******************************************************************************
72  * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
73  ******************************************************************************/
74 protected:
75     /**
76     * @brief Called when view is resized.
77     *
78     * @param event resize event
79     */
80     void resizeEvent(QResizeEvent *event);
81
82 private:
83     /**
84     * @brief Event handler for mouse double click event
85     *
86     * Emits zoomIn signal.
87     * @param event QMouseEvent
88     */
89     void mouseDoubleClickEvent(QMouseEvent *event);
90
91     /**
92     * @brief Event handler for mouse move events
93     *
94     * Does calculate mouse movement delta from last event position and new view center
95     * based on that delta. Saves current event position for next round. Emits viewScrolled
96     * signal and doesn't actually scroll the view.
97     *
98     * Saves mouse movement deltas and durations for last few move events to be used for
99     * calculating the kinetic scrolling speed.
100     *
101     * @param event Mouse event
102     */
103     void mouseMoveEvent(QMouseEvent *event);
104
105     /**
106     * @brief Event handler for mouse press events
107     *
108     * Saves inial values for mouse and scene location for dragging the view. Does stop currently
109     * running kinetic scroll effect.
110     *
111     * @param event Mouse event
112     */
113     void mousePressEvent(QMouseEvent *event);
114
115     /**
116     * @brief Event handler for mouse release events
117     *
118     * Set up and start kinetic scrolling effect if time elapsed from last mouseMoveEvent is below
119     * the limit and drag length is over the limit.
120     *
121     * Kinetic scroll distance is calculated based on mouse movement event values saved in
122     * mouseMoveEvent(). The distance is also limited so that map doesn't run too far.
123     *
124     * @param event Mouse event
125     */
126     void mouseReleaseEvent(QMouseEvent *event);
127
128 /*******************************************************************************
129  * MEMBER FUNCTIONS AND SLOTS
130  ******************************************************************************/
131 public slots:
132     /**
133     * @brief Slot for centering view to new location
134     *
135     * @param coordinate Scene coordinates of the new center point
136     */
137     void centerToSceneCoordinates(const SceneCoordinate &coordinate, bool isUserDragAction = false);
138
139     /**
140     * @brief Set zoom level of the view
141     *
142     * @param zoomLevel Zoom level
143     */
144     void setZoomLevel(int zoomLevel);
145
146 private slots:
147     void disableCenterShift();
148
149     /**
150     * @brief Double tap zoom finished.
151     *
152     * Disables double tap zoom flag and emits zoomIn signal.
153     */
154     void doubleTapZoomFinished();
155
156     void enableCenterShift();
157
158 private:
159     /**
160     * @brief Set new view scale
161     *
162     * @param viewScale New scaling factor
163     */
164     void setViewScale(qreal viewScale);
165
166     void toggleCenterShift(bool enabled);
167
168     void updateCenterShift();
169
170     /**
171     * @brief Get current view scale
172     *
173     * @return Current view scaling factor
174     */
175     qreal viewScale();
176
177 /*******************************************************************************
178  * SIGNALS
179  ******************************************************************************/
180 signals:
181     /**
182     * @brief Signal for view resize events.
183     *
184     * Signal is emitted when view has been resized.
185     * @param size view size
186     */
187     void viewResized(const QSize &size);
188
189     /**
190     * @brief Signal for view scroll events
191     *
192     * Signal is emitted when view is scrolled.
193     * @param coordinate Scene coordinates of the new center point of the view
194     */
195     void viewScrolled(const SceneCoordinate &coordinate, bool isUserDragAction);
196
197     /**
198     * @brief Signal for informing that zooming animation is finished
199     */
200     void viewZoomFinished();
201
202     /**
203     * @brief Signal for informing that double click zoom is finished
204     */
205     void zoomIn();
206
207 /*******************************************************************************
208  * DATA MEMBERS
209  ******************************************************************************/
210 private:
211     bool m_centerShiftEnabled;
212     bool m_doubleTapZoomRunning;         ///< Double tap zoom running flag
213
214     int m_dragTime[VALUES];               ///< Table of mouse event durations
215     int m_index;                          ///< Index of mouse event values tables
216     int m_zoomLevel;                      ///< Current zoom level
217
218     qreal m_kineticMaxViewDistance;       ///< Maximum kinetic scroll distance in view pixels
219
220     QPoint m_dragMovement[VALUES];        ///< Table of mouse event distances
221     QPoint m_internalScenePosition;       ///< New center position
222     QPoint m_lastMouseEventScenePosition; ///< Previous mouse event position in the scene
223     QPoint m_lastMouseEventViewPosition;  ///< Previous mouse event position in the view
224
225     QPointF m_centerHorizontalShift;
226
227     QParallelAnimationGroup *m_scrollAndZoomAnimation;  ///< Double click zoom animation
228     QPropertyAnimation *m_zoomAnimation;  ///< Zoom animation
229
230     QTime m_time;                         ///< Elapsed times in mouse events
231
232     MapScroller *m_scroller;              ///< Kinetic scroller
233     SceneCoordinate m_lastSetScenePosition;
234 };
235
236 #endif // MAPVIEW_H