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