0fbaa9023f70a775f99304b143039c891dc1c4a4
[situare] / src / map / mapview.cpp
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 #include <cmath>
24
25 #include <QDebug>
26 #include <QMouseEvent>
27 #include <QParallelAnimationGroup>
28
29 #include "mapcommon.h"
30 #include "mapscroller.h"
31
32 #include "mapview.h"
33
34 const qreal MS_PER_S = 1000;
35
36 // const values for tuning the kinetic scroll effect
37 const int KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS = 30;
38 const int KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS = 100;
39 const qreal KINETIC_MAX_VIEW_DISTANCE_FACTOR = 0.8;
40 const int KINETIC_SCROLL_TIME_MS = 750;
41 const qreal KINETIC_SPEED_TO_DISTANCE_FACTOR = 0.15 * sqrt(KINETIC_SCROLL_TIME_MS / MS_PER_S);
42
43 const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
44
45 MapView::MapView(QWidget *parent)
46     : QGraphicsView(parent),
47       m_doubleTapZoomRunning(false)
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50
51     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53
54     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
55     connect(m_zoomAnimation, SIGNAL(finished()),
56         this, SIGNAL(viewZoomFinished()));
57     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
58
59     m_scroller = &MapScroller::getInstance();
60
61     m_scrollAndZoomAnimation = new QParallelAnimationGroup();
62     m_scrollAndZoomAnimation->addAnimation(m_scroller);
63     m_scrollAndZoomAnimation->addAnimation(m_zoomAnimation);
64     connect(m_scrollAndZoomAnimation, SIGNAL(finished()),
65             this, SLOT(doubleTapZoomFinished()));
66 }
67
68 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
69 {
70     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
71
72     centerOn(sceneCoordinate);
73 }
74
75 void MapView::doubleTapZoomFinished()
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78
79     m_doubleTapZoomRunning = false;
80     emit zoomIn();
81 }
82
83 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
84 {
85     qDebug() << __PRETTY_FUNCTION__;
86
87     if (m_zoomLevel + 1 <= MAX_MAP_ZOOM_LEVEL) {
88         QPoint pressPosition = mapToScene(event->pos()).toPoint();
89         QPoint viewCenterPosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
90         QPoint zoomPosition = viewCenterPosition - ((viewCenterPosition - pressPosition) / 2);
91
92         m_scrollAndZoomAnimation->stop();
93         m_doubleTapZoomRunning = true;
94
95         m_scroller->setEasingCurve(QEasingCurve::Linear);
96         m_scroller->setDuration(ZOOM_TIME_MS);
97         m_scroller->setStartValue(m_scenePosition);
98         m_scroller->setEndValue(zoomPosition);
99
100         m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
101         m_zoomAnimation->setDuration(ZOOM_TIME_MS);
102         m_zoomAnimation->setStartValue(viewScale());
103         m_zoomAnimation->setEndValue(pow(2, m_zoomLevel+1 - MAX_MAP_ZOOM_LEVEL));
104
105         m_scrollAndZoomAnimation->start();
106     }
107 }
108
109 void MapView::mouseMoveEvent(QMouseEvent *event)
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     if (m_doubleTapZoomRunning)
114         return;
115
116     m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
117
118     if (m_index >= VALUES)
119         m_index = 0;
120
121     m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
122     m_dragTime[m_index] = m_time.elapsed();
123     m_time.start();
124     m_index++;
125
126     emit viewScrolled(m_scenePosition);
127
128     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
129     m_mouseLastViewPosition = event->pos();
130 }
131
132 void MapView::mousePressEvent(QMouseEvent *event)
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     if (m_doubleTapZoomRunning)
137         return;
138
139     m_time.start();
140
141     m_scroller->stop();
142
143     QGraphicsView::mousePressEvent(event);
144
145     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
146     m_mouseLastViewPosition = event->pos();
147     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
148
149     for (int i = 0; i < VALUES; i++) {
150         m_dragMovement[i] = QPoint();
151         m_dragTime[i] = 0;
152     }
153     m_index = 0;
154 }
155
156 void MapView::mouseReleaseEvent(QMouseEvent *event)
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     if (m_doubleTapZoomRunning)
161         return;
162
163     int elapsed = m_time.elapsed();
164
165     QGraphicsView::mouseReleaseEvent(event);
166
167     // start kinetic scroll only if there isn't too much time elapsed from the last mouse move event
168     if (elapsed <= KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS) {
169         QPointF dragViewSpeed;
170         int dragLength = 0;
171         int values = 0;
172         for (int i = 0; i < VALUES; i++) {
173             if (m_dragTime[i] > 0) {
174                 dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
175                 dragLength += m_dragMovement[i].manhattanLength();
176                 values++;
177             }
178         }
179
180         if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
181             dragViewSpeed /= values;
182             QPointF effectViewDistance = dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
183
184             // limit the scroll distance in screen pixels
185             qreal biggerDistance = qMax(abs(effectViewDistance.x()), abs(effectViewDistance.y()));
186             if (biggerDistance > m_kineticMaxViewDistance)
187                 effectViewDistance /= biggerDistance / m_kineticMaxViewDistance;
188
189             QPointF effectSceneDistance = effectViewDistance
190                                           * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
191
192             m_scroller->setEasingCurve(QEasingCurve::OutCirc);
193             m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
194             m_scroller->setStartValue(m_scenePosition);
195             m_scroller->setEndValue(m_scenePosition + effectSceneDistance.toPoint());
196             m_scroller->start();
197         }
198     }
199 }
200
201 void MapView::resizeEvent(QResizeEvent *event)
202 {
203     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
204
205     m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
206
207     emit viewResized(event->size());
208 }
209
210 void MapView::setViewScale(qreal viewScale)
211 {
212     qDebug() << __PRETTY_FUNCTION__;
213
214     QTransform transform;
215     transform.scale(viewScale, viewScale);
216     setTransform(transform);
217 }
218
219 void MapView::setZoomLevel(int zoomLevel)
220 {
221     qDebug() << __PRETTY_FUNCTION__;
222
223     m_zoomLevel = zoomLevel;
224
225     if (m_zoomAnimation) {
226         m_zoomAnimation->stop();
227         m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
228         m_zoomAnimation->setDuration(ZOOM_TIME_MS);
229         m_zoomAnimation->setStartValue(viewScale());
230         m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
231
232         m_zoomAnimation->start();
233     }
234 }
235
236 qreal MapView::viewScale()
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239
240     return transform().m11();
241 }
242
243 MapView::~MapView()
244 {
245     qDebug() << __PRETTY_FUNCTION__;
246
247     m_scrollAndZoomAnimation->removeAnimation(m_scroller);
248     delete m_scrollAndZoomAnimation;
249 }