Added speed calculation from multiple mouseMoveEvents
[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
31 class MapScroller;
32
33 #define VALUES 4
34
35 /**
36 * @brief Map view widget
37 *
38 * @author Sami Rämö - sami.ramo (at) ixonos.com
39 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
40 */
41 class MapView : public QGraphicsView
42 {
43     Q_OBJECT
44
45     /**
46     * @brief View scaling
47     *
48     * @property viewScale
49     */
50     Q_PROPERTY(qreal viewScale READ viewScale WRITE setViewScale)
51
52 public:
53     /**
54     * @brief Constructor
55     *
56     * @param parent Parent
57     */
58     MapView(QWidget *parent = 0);
59
60 /*******************************************************************************
61  * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
62  ******************************************************************************/
63 protected:
64     /**
65     * @brief Called when view is resized.
66     *
67     * @param event resize event
68     */
69     void resizeEvent(QResizeEvent *event);
70
71 private:
72     /**
73     * @brief Event handler for mouse move events
74     *
75     * Does calculate mouse movement delta from last event position and new view center
76     * based on that delta. Saves current event position for next round. Emits viewScrolled
77     * signal and doesn't actually scroll the view.
78     * @param event Mouse event
79     */
80     void mouseMoveEvent(QMouseEvent *event);
81
82     /**
83     * @brief Event handler for mouse press events
84     *
85     * Saves inial values for mouse and scene location for dragging view.
86     * @param event Mouse event
87     */
88     void mousePressEvent(QMouseEvent *event);
89
90     void mouseReleaseEvent(QMouseEvent *event);
91
92 /*******************************************************************************
93  * MEMBER FUNCTIONS AND SLOTS
94  ******************************************************************************/
95 public slots:
96     /**
97     * @brief Slot for centering view to new location
98     *
99     * @param sceneCoordinate Scene coordinates of the new center point
100     */
101     void centerToSceneCoordinates(QPoint sceneCoordinate);
102
103     /**
104     * @brief Set zoom level of the view
105     *
106     * @param zoomLevel Zoom level
107     */
108     void setZoomLevel(int zoomLevel);
109
110 private:
111     /**
112     * @brief Set new view scale
113     *
114     * @param viewScale New scaling factor
115     */
116     void setViewScale(qreal viewScale);
117
118     /**
119     * @brief Get current view scale
120     *
121     * @return Current view scaling factor
122     */
123     qreal viewScale();
124
125 /*******************************************************************************
126  * SIGNALS
127  ******************************************************************************/
128 signals:
129     /**
130     * @brief Signal for view resize events.
131     *
132     * Signal is emitted when view has been resized.
133     * @param size view size
134     */
135     void viewResized(const QSize &size);
136
137     /**
138     * @brief Signal for view scroll events
139     *
140     * Signal is emitted when view is scrolled.
141     * @param sceneCoordinate Scene coordinates of the new center point of the view
142     */
143     void viewScrolled(QPoint sceneCoordinate);
144
145     /**
146     * @brief Signal for informing that zooming animation is finished
147     */
148     void viewZoomFinished();
149
150 /*******************************************************************************
151  * DATA MEMBERS
152  ******************************************************************************/
153 private:
154     int m_dragTime[VALUES];
155     int m_index;
156     int m_zoomLevel;
157
158     QPoint m_dragMovement[VALUES];
159     QPoint m_mouseLastScenePosition;               ///< Previous mouse event position
160     QPoint m_mouseLastViewPosition;
161     QPoint m_scenePosition;               ///< New center position
162
163     QPropertyAnimation *m_zoomAnimation;  ///< Zoom animation
164
165     QTime time;
166
167     MapScroller *m_scroller;
168 };
169
170 #endif // MAPVIEW_H