c674ba7027266fb2ff019a00bb23a5ac7e82b1a2
[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 #define VALUES 4
35
36 /**
37 * @brief Map view widget
38 *
39 * @author Sami Rämö - sami.ramo (at) ixonos.com
40 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
41 */
42 class MapView : public QGraphicsView
43 {
44     Q_OBJECT
45
46     /**
47     * @brief View scaling
48     *
49     * @property viewScale
50     */
51     Q_PROPERTY(qreal viewScale READ viewScale WRITE setViewScale)
52
53 public:
54     /**
55     * @brief Constructor
56     *
57     * @param parent Parent
58     */
59     MapView(QWidget *parent = 0);
60
61 /*******************************************************************************
62  * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
63  ******************************************************************************/
64 protected:
65     /**
66     * @brief Event handler for mouse double click event
67     *
68     * Emits zoomIn signal.
69     * @param event QMouseEvent
70     */
71     void mouseDoubleClickEvent(QMouseEvent *event);
72
73     /**
74     * @brief Called when view is resized.
75     *
76     * @param event resize event
77     */
78     void resizeEvent(QResizeEvent *event);
79
80 private:
81     void mouseDoubleClickEvent(QMouseEvent *event);
82
83     /**
84     * @brief Event handler for mouse move events
85     *
86     * Does calculate mouse movement delta from last event position and new view center
87     * based on that delta. Saves current event position for next round. Emits viewScrolled
88     * signal and doesn't actually scroll the view.
89     *
90     * Saves mouse movement deltas and durations for last few move events to be used for
91     * calculating the kinetic scrolling speed.
92     *
93     * @param event Mouse event
94     */
95     void mouseMoveEvent(QMouseEvent *event);
96
97     /**
98     * @brief Event handler for mouse press events
99     *
100     * Saves inial values for mouse and scene location for dragging the view. Does stop currently
101     * running kinetic scroll effect.
102     *
103     * @param event Mouse event
104     */
105     void mousePressEvent(QMouseEvent *event);
106
107     /**
108     * @brief Event handler for mouse release events
109     *
110     * Set up and start kinetic scrolling effect if time elapsed from last mouseMoveEvent is below
111     * the limit and drag length is over the limit.
112     *
113     * Kinetic scroll distance is calculated based on mouse movement event values saved in
114     * mouseMoveEvent().
115     *
116     * @param event Mouse event
117     */
118     void mouseReleaseEvent(QMouseEvent *event);
119
120 /*******************************************************************************
121  * MEMBER FUNCTIONS AND SLOTS
122  ******************************************************************************/
123 public slots:
124     /**
125     * @brief Slot for centering view to new location
126     *
127     * @param sceneCoordinate Scene coordinates of the new center point
128     */
129     void centerToSceneCoordinates(QPoint sceneCoordinate);
130
131     /**
132     * @brief Set zoom level of the view
133     *
134     * @param zoomLevel Zoom level
135     */
136     void setZoomLevel(int zoomLevel);
137
138 private:
139     /**
140     * @brief Set new view scale
141     *
142     * @param viewScale New scaling factor
143     */
144     void setViewScale(qreal viewScale);
145
146     /**
147     * @brief Get current view scale
148     *
149     * @return Current view scaling factor
150     */
151     qreal viewScale();
152
153 /*******************************************************************************
154  * SIGNALS
155  ******************************************************************************/
156 signals:
157     /**
158     * @brief Signal for view resize events.
159     *
160     * Signal is emitted when view has been resized.
161     * @param size view size
162     */
163     void viewResized(const QSize &size);
164
165     /**
166     * @brief Signal for view scroll events
167     *
168     * Signal is emitted when view is scrolled.
169     * @param sceneCoordinate Scene coordinates of the new center point of the view
170     */
171     void viewScrolled(QPoint sceneCoordinate);
172
173     /**
174     * @brief Signal for informing that zooming animation is finished
175     */
176     void viewZoomFinished();
177
178     void zoomIn();
179
180 /*******************************************************************************
181  * DATA MEMBERS
182  ******************************************************************************/
183 private:
184     int m_dragTime[VALUES];               ///< Table of mouse event durations
185     int m_index;                          ///< Index of mouse event values tables
186     int m_zoomLevel;                      ///< Current zoom level
187
188     QPoint m_dragMovement[VALUES];        ///< Table of mouse event distances
189     QPoint m_mouseLastScenePosition;      ///< Previous mouse event position in the scene
190     QPoint m_mouseLastViewPosition;       ///< Previous mouse event position in the view
191     QPoint m_scenePosition;               ///< New center position
192
193     QPropertyAnimation *m_zoomAnimation;  ///< Zoom animation
194     QParallelAnimationGroup *m_groupAnimation;
195
196     QTime m_time;                         ///< Elapsed times in mouse events
197
198     MapScroller *m_scroller;              ///< Kinetic scroller
199
200     QParallelAnimationGroup *m_group;
201 };
202
203 #endif // MAPVIEW_H