MapView sends always the center point from the users view
[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 Returns the point which is considered by user as the visible center point
178     *
179     * Differs from view's center point when panel is open and view center point is shifted.
180     *
181     * @returns Center point
182     */
183     QPointF center();
184
185     /**
186     * @brief Set new view scale
187     *
188     * @param viewScale New scaling factor
189     */
190     void setViewScale(qreal viewScale);
191
192     /**
193     * @brief Set new view shifting
194     *
195     * @param viewShift New shifting amount
196     */
197     void setViewShift(qreal viewShift);
198
199     /**
200     * @brief Update center shifting value
201     */
202     void updateCenterShift();
203
204     /**
205     * @brief Get current view scale
206     *
207     * @return Current view scaling factor
208     */
209     qreal viewScale() const;
210
211     /**
212     * @brief Get current view shifting
213     *
214     * @return Current view shifting amount
215     */
216     qreal viewShift() const;
217
218 /*******************************************************************************
219  * SIGNALS
220  ******************************************************************************/
221 signals:
222     /**
223     * @brief Emitted when map center point shiftin is changed
224     *
225     * @param shifting New shifting value
226     */
227     void horizontalShiftingChanged(int shifting);
228
229     /**
230     * @brief Signal for view resize events.
231     *
232     * Signal is emitted when view has been resized.
233     * @param size view size
234     */
235     void viewResized(const QSize &size);
236
237     /**
238     * @brief Signal for view scroll events
239     *
240     * Signal is emitted when view is scrolled.
241     * @param coordinate Scene coordinates of the new center point of the view
242     * @param isUserDragAction True if caused by user dragging action
243     */
244     void viewScrolled(const SceneCoordinate &coordinate, bool isUserDragAction);
245
246     /**
247     * @brief Signal for informing that zooming animation is finished
248     */
249     void viewZoomFinished();
250
251     /**
252     * @brief Signal for informing that double click zoom is finished
253     */
254     void zoomIn();
255
256 /*******************************************************************************
257  * DATA MEMBERS
258  ******************************************************************************/
259 private:
260     bool m_doubleTapZoomRunning;                ///< Double tap zoom running flag
261
262     int m_dragTime[VALUES];                     ///< Table of mouse event durations
263     int m_index;                                ///< Current index of mouse event values table
264     int m_zoomLevel;                            ///< Current zoom level
265
266     qreal m_centerHorizontalShiftViewPixels;    ///< Center point horizontal shift in the view
267     qreal m_kineticMaxViewDistance;             ///< Maximum kinetic scroll distance in view pixels
268
269     QParallelAnimationGroup *m_scrollAndZoomAnimation;  ///< Double click zoom animation
270
271     QPoint m_dragMovement[VALUES];              ///< Table of mouse event distances
272     QPoint m_internalScenePosition;             ///< New center position (used for dragging)
273     QPoint m_lastMouseEventScenePosition;       ///< Previous mouse event position in the scene
274     QPoint m_lastMouseEventViewPosition;        ///< Previous mouse event position in the view
275     QPoint m_viewCenterPoint;                   ///< Center point of the MapView
276
277     QPointF m_centerHorizontalShiftPoint;       ///< Current amount of center point shifting
278
279     QPropertyAnimation *m_zoomAnimation;        ///< Zoom animation
280
281     QTime m_time;                               ///< Elapsed time between mouse events
282
283     QPropertyAnimation *m_centerShiftAnimation; ///< Animation for shifting the center point
284
285     MapScroller *m_scroller;                    ///< Kinetic scroller
286     SceneCoordinate m_lastSetScenePosition;     ///< Last center point coordinate set by MapEngine
287 };
288
289 #endif // MAPVIEW_H