Re-factored the source to use the new coordinate classes
[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 class SceneCoordinate;
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 sceneCoordinate Scene coordinates of the new center point
136     */
137     void centerToSceneCoordinates(const SceneCoordinate &sceneCoordinate);
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     /**
148     * @brief Double tap zoom finished.
149     *
150     * Disables double tap zoom flag and emits zoomIn signal.
151     */
152     void doubleTapZoomFinished();
153
154 private:
155     /**
156     * @brief Set new view scale
157     *
158     * @param viewScale New scaling factor
159     */
160     void setViewScale(qreal viewScale);
161
162     /**
163     * @brief Get current view scale
164     *
165     * @return Current view scaling factor
166     */
167     qreal viewScale();
168
169 /*******************************************************************************
170  * SIGNALS
171  ******************************************************************************/
172 signals:
173     /**
174     * @brief Signal for view resize events.
175     *
176     * Signal is emitted when view has been resized.
177     * @param size view size
178     */
179     void viewResized(const QSize &size);
180
181     /**
182     * @brief Signal for view scroll events
183     *
184     * Signal is emitted when view is scrolled.
185     * @param sceneCoordinate Scene coordinates of the new center point of the view
186     */
187     void viewScrolled(const SceneCoordinate &sceneCoordinate);
188
189     /**
190     * @brief Signal for informing that zooming animation is finished
191     */
192     void viewZoomFinished();
193
194     /**
195     * @brief Signal for informing that double click zoom is finished
196     */
197     void zoomIn();
198
199 /*******************************************************************************
200  * DATA MEMBERS
201  ******************************************************************************/
202 private:
203     bool m_doubleTapZoomRunning;         ///< Double tap zoom running flag
204
205     int m_dragTime[VALUES];               ///< Table of mouse event durations
206     int m_index;                          ///< Index of mouse event values tables
207     int m_zoomLevel;                      ///< Current zoom level
208
209     qreal m_kineticMaxViewDistance;       ///< Maximum kinetic scroll distance in view pixels
210
211     QPoint m_dragMovement[VALUES];        ///< Table of mouse event distances
212     QPoint m_mouseLastScenePosition;      ///< Previous mouse event position in the scene
213     QPoint m_mouseLastViewPosition;       ///< Previous mouse event position in the view
214     QPoint m_scenePosition;               ///< New center position
215
216     QParallelAnimationGroup *m_scrollAndZoomAnimation;  ///< Double click zoom animation
217     QPropertyAnimation *m_zoomAnimation;  ///< Zoom animation
218
219     QTime m_time;                         ///< Elapsed times in mouse events
220
221     MapScroller *m_scroller;              ///< Kinetic scroller
222 };
223
224 #endif // MAPVIEW_H