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