Review, updating functional tests
[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 "coordinates/scenecoordinate.h"
30 #include "mapcommon.h"
31 #include "mapscroller.h"
32 #include "ui/panelcommon.h"
33
34 #include "mapview.h"
35
36 const qreal MS_PER_S = 1000;
37
38 // const values for tuning the kinetic scroll effect
39 const int KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS = 30;
40 const int KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS = 100;
41 const qreal KINETIC_MAX_VIEW_DISTANCE_FACTOR = 0.8;
42 const int KINETIC_SCROLL_TIME_MS = 750;
43 const qreal KINETIC_SPEED_TO_DISTANCE_FACTOR = 0.15 * sqrt(KINETIC_SCROLL_TIME_MS / MS_PER_S);
44
45 const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
46
47 MapView::MapView(QWidget *parent)
48     : QGraphicsView(parent),
49       m_doubleTapZoomRunning(false)
50 {
51     qDebug() << __PRETTY_FUNCTION__;
52
53     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55
56     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
57     connect(m_zoomAnimation, SIGNAL(finished()),
58         this, SIGNAL(viewZoomFinished()));
59     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
60
61     m_scroller = &MapScroller::getInstance();
62
63     m_scrollAndZoomAnimation = new QParallelAnimationGroup();
64     m_scrollAndZoomAnimation->addAnimation(m_scroller);
65     m_scrollAndZoomAnimation->addAnimation(m_zoomAnimation);
66     connect(m_scrollAndZoomAnimation, SIGNAL(finished()),
67             this, SLOT(doubleTapZoomFinished()));
68
69     m_centerShiftAnimation = new QPropertyAnimation(this, "viewShift", this);
70     if (m_centerShiftAnimation) {
71         m_centerShiftAnimation->setStartValue(0.0);
72         m_centerShiftAnimation->setDuration(ZOOM_TIME_MS);
73     }
74 }
75
76 MapView::~MapView()
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79
80     m_scrollAndZoomAnimation->removeAnimation(m_scroller);
81     delete m_scrollAndZoomAnimation;
82 }
83
84 QPointF MapView::center() const
85 {
86     return mapToScene(m_viewCenterPoint) - m_centerHorizontalShiftPoint;
87 }
88
89 void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate)
90 {
91     qDebug() << __PRETTY_FUNCTION__ << "coordinate" << coordinate;
92
93     QPointF target = coordinate.toPointF();
94     m_lastSetScenePosition = coordinate;
95
96     target += m_centerHorizontalShiftPoint;
97
98     centerOn(target);
99 }
100
101 void MapView::disableCenterShift()
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     if (m_centerShiftAnimation) {
106         m_centerShiftAnimation->setDirection(QAbstractAnimation::Backward);
107         m_centerShiftAnimation->start();
108     }
109 }
110
111 void MapView::doubleTapZoomFinished()
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     m_doubleTapZoomRunning = false;
116     emit zoomIn();
117 }
118
119 void MapView::enableCenterShift()
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     if (m_centerShiftAnimation) {
124         m_centerShiftAnimation->setDirection(QAbstractAnimation::Forward);
125         m_centerShiftAnimation->start();
126     }
127 }
128
129 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
130 {
131     qDebug() << __PRETTY_FUNCTION__;
132
133     if (m_zoomLevel + 1 <= OSM_MAX_ZOOM_LEVEL) {
134         QPoint pressPosition = mapToScene(event->pos()).toPoint();
135         QPoint viewCenterPosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
136         QPoint zoomPosition = viewCenterPosition - ((viewCenterPosition - pressPosition) / 2)
137                               - m_centerHorizontalShiftPoint.toPoint() / 2;
138
139         m_scrollAndZoomAnimation->stop();
140         m_doubleTapZoomRunning = true;
141
142         m_scroller->setEasingCurve(QEasingCurve::Linear);
143         m_scroller->setDuration(ZOOM_TIME_MS);
144         QPointF centerPoint = center();
145         m_scroller->setStartValue(SceneCoordinate(centerPoint.x(), centerPoint.y()));
146         m_scroller->setEndValue(SceneCoordinate(zoomPosition.x(), zoomPosition.y()));
147
148         m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
149         m_zoomAnimation->setDuration(ZOOM_TIME_MS);
150         m_zoomAnimation->setStartValue(viewScale());
151         m_zoomAnimation->setEndValue(pow(2, m_zoomLevel + 1 - OSM_MAX_ZOOM_LEVEL));
152
153         m_scrollAndZoomAnimation->start();
154     }
155 }
156
157 void MapView::mouseMoveEvent(QMouseEvent *event)
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     if (m_doubleTapZoomRunning)
162         return;
163
164     m_internalScenePosition += m_lastMouseEventScenePosition - mapToScene(event->pos()).toPoint();
165
166     if (m_index >= VALUES)
167         m_index = 0;
168
169     m_dragMovement[m_index] = m_lastMouseEventViewPosition - event->pos();
170     m_dragTime[m_index] = m_time.elapsed();
171     m_time.start();
172     m_index++;
173
174     QPointF viewCenterPoint = m_internalScenePosition - m_centerHorizontalShiftPoint;
175
176     emit viewScrolled(SceneCoordinate(viewCenterPoint.x(), viewCenterPoint.y()));
177
178     m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
179     m_lastMouseEventViewPosition = event->pos();
180 }
181
182 void MapView::mousePressEvent(QMouseEvent *event)
183 {
184     qDebug() << __PRETTY_FUNCTION__;
185
186     if (m_doubleTapZoomRunning)
187         return;
188
189     m_time.start();
190
191     m_scroller->stop();
192
193     QGraphicsView::mousePressEvent(event);
194
195     m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
196     m_lastMouseEventViewPosition = event->pos();
197     m_internalScenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
198
199     for (int i = 0; i < VALUES; i++) {
200         m_dragMovement[i] = QPoint();
201         m_dragTime[i] = 0;
202     }
203     m_index = 0;
204 }
205
206 void MapView::mouseReleaseEvent(QMouseEvent *event)
207 {
208     qDebug() << __PRETTY_FUNCTION__;
209
210     if (m_doubleTapZoomRunning)
211         return;
212
213     int elapsed = m_time.elapsed();
214
215     QGraphicsView::mouseReleaseEvent(event);
216
217     // start kinetic scroll only if there isn't too much time elapsed from the last mouse move event
218     if (elapsed <= KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS) {
219         QPointF dragViewSpeed;
220         int dragLength = 0;
221         int values = 0;
222         for (int i = 0; i < VALUES; i++) {
223             if (m_dragTime[i] > 0) {
224                 dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
225                 dragLength += m_dragMovement[i].manhattanLength();
226                 values++;
227             }
228         }
229
230         if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
231             dragViewSpeed /= values;
232             QPointF effectViewDistance = dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
233
234             // limit the scroll distance in screen pixels
235             qreal biggerDistance = qMax(abs(effectViewDistance.x()), abs(effectViewDistance.y()));
236             if (biggerDistance > m_kineticMaxViewDistance)
237                 effectViewDistance /= biggerDistance / m_kineticMaxViewDistance;
238
239             QPointF effectSceneDistance = effectViewDistance
240                                           * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel));
241
242             m_scroller->setEasingCurve(QEasingCurve::OutCirc);
243             m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
244             QPointF centerPoint = center();
245             m_scroller->setStartValue(SceneCoordinate(centerPoint.x(), centerPoint.y()));
246             QPointF endValue = centerPoint + effectSceneDistance;
247             m_scroller->setEndValue(SceneCoordinate(endValue.x(), endValue.y()));
248             m_scroller->start();
249         }
250     }
251 }
252
253 void MapView::resizeEvent(QResizeEvent *event)
254 {
255     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
256
257     m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
258
259     m_viewCenterPoint.setX(event->size().width() / 2);
260     m_viewCenterPoint.setY(event->size().height() / 2);
261
262     emit viewResized(event->size());
263
264     if (m_centerShiftAnimation) {
265         int mapVisibleWidth = event->size().width() - PANEL_WIDTH - PANEL_BAR_WIDTH;
266         int shiftFromMiddle = m_viewCenterPoint.x() - (mapVisibleWidth / 2);
267         m_centerShiftAnimation->setEndValue(shiftFromMiddle);
268         updateCenterShift();
269     }
270 }
271
272 void MapView::setViewScale(qreal viewScale)
273 {
274     qDebug() << __PRETTY_FUNCTION__;
275
276     QTransform transform;
277     transform.scale(viewScale, viewScale);
278     setTransform(transform);
279
280     updateCenterShift();
281 }
282
283 void MapView::setViewShift(qreal viewShift)
284 {
285     qDebug() << __PRETTY_FUNCTION__;
286
287     m_centerHorizontalShiftViewPixels = viewShift;
288     emit horizontalShiftingChanged(m_centerHorizontalShiftViewPixels);
289
290     updateCenterShift();
291 }
292
293 void MapView::setZoomLevel(int zoomLevel)
294 {
295     qDebug() << __PRETTY_FUNCTION__;
296
297     m_zoomLevel = zoomLevel;
298
299     if (m_zoomAnimation) {
300         m_zoomAnimation->stop();
301         m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
302         m_zoomAnimation->setDuration(ZOOM_TIME_MS);
303         m_zoomAnimation->setStartValue(viewScale());
304         m_zoomAnimation->setEndValue(pow(2, zoomLevel - OSM_MAX_ZOOM_LEVEL));
305
306         m_zoomAnimation->start();
307     }
308 }
309
310 void MapView::updateCenterShift()
311 {
312     qDebug() << __PRETTY_FUNCTION__;
313
314     m_centerHorizontalShiftPoint = QPointF(m_centerHorizontalShiftViewPixels * (1.0 / viewScale()),
315                                            0);
316
317     centerToSceneCoordinates(m_lastSetScenePosition);
318 }
319
320 qreal MapView::viewScale() const
321 {
322     qDebug() << __PRETTY_FUNCTION__;
323
324     return transform().m11();
325 }
326
327 qreal MapView::viewShift() const
328 {
329     qDebug() << __PRETTY_FUNCTION__;
330
331     return m_centerHorizontalShiftViewPixels;
332 }