MapView sends always the center point from the users view
[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()
85 {
86     return mapToScene(m_viewCenterPoint) - m_centerHorizontalShiftPoint;
87 }
88
89 void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool isUserDragAction)
90 {
91     qDebug() << __PRETTY_FUNCTION__ << "coordinate" << coordinate;
92
93     QPointF target = coordinate.toPointF();
94     m_lastSetScenePosition = coordinate;
95
96 //    if (!isUserDragAction)
97         target += m_centerHorizontalShiftPoint;
98
99     centerOn(target);
100 }
101
102 void MapView::disableCenterShift()
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     if (m_centerShiftAnimation) {
107         m_centerShiftAnimation->setDirection(QAbstractAnimation::Backward);
108         m_centerShiftAnimation->start();
109     }
110 }
111
112 void MapView::doubleTapZoomFinished()
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     m_doubleTapZoomRunning = false;
117     emit zoomIn();
118 }
119
120 void MapView::enableCenterShift()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     if (m_centerShiftAnimation) {
125         m_centerShiftAnimation->setDirection(QAbstractAnimation::Forward);
126         m_centerShiftAnimation->start();
127     }
128 }
129
130 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     if (m_zoomLevel + 1 <= OSM_MAX_ZOOM_LEVEL) {
135         QPoint pressPosition = mapToScene(event->pos()).toPoint();
136         QPoint viewCenterPosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
137         QPoint zoomPosition = viewCenterPosition - ((viewCenterPosition - pressPosition) / 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         m_scroller->setStartValue(SceneCoordinate(m_internalScenePosition.x(),
145                                                   m_internalScenePosition.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()), true);
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->setKineticScrollFlag(true);
249             m_scroller->start();
250         }
251     }
252 }
253
254 void MapView::resizeEvent(QResizeEvent *event)
255 {
256     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
257
258     m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
259
260     m_viewCenterPoint.setX(event->size().width() / 2);
261     m_viewCenterPoint.setY(event->size().height() / 2);
262
263     emit viewResized(event->size());
264
265     if (m_centerShiftAnimation) {
266         int mapVisibleWidth = event->size().width() - PANEL_WIDTH - PANEL_BAR_WIDTH;
267         int shiftFromMiddle = m_viewCenterPoint.x() - (mapVisibleWidth / 2);
268         m_centerShiftAnimation->setEndValue(shiftFromMiddle);
269         updateCenterShift();
270     }
271 }
272
273 void MapView::setViewScale(qreal viewScale)
274 {
275     qDebug() << __PRETTY_FUNCTION__;
276
277     QTransform transform;
278     transform.scale(viewScale, viewScale);
279     setTransform(transform);
280
281     updateCenterShift();
282 }
283
284 void MapView::setViewShift(qreal viewShift)
285 {
286     qDebug() << __PRETTY_FUNCTION__;
287
288     m_centerHorizontalShiftViewPixels = viewShift;
289     emit horizontalShiftingChanged(m_centerHorizontalShiftViewPixels);
290
291     updateCenterShift();
292 }
293
294 void MapView::setZoomLevel(int zoomLevel)
295 {
296     qDebug() << __PRETTY_FUNCTION__;
297
298     m_zoomLevel = zoomLevel;
299
300     if (m_zoomAnimation) {
301         m_zoomAnimation->stop();
302         m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
303         m_zoomAnimation->setDuration(ZOOM_TIME_MS);
304         m_zoomAnimation->setStartValue(viewScale());
305         m_zoomAnimation->setEndValue(pow(2, zoomLevel - OSM_MAX_ZOOM_LEVEL));
306
307         m_zoomAnimation->start();
308     }
309 }
310
311 void MapView::updateCenterShift()
312 {
313     qDebug() << __PRETTY_FUNCTION__;
314
315     m_centerHorizontalShiftPoint = QPointF(m_centerHorizontalShiftViewPixels * (1.0 / viewScale()),
316                                            0);
317
318     centerToSceneCoordinates(m_lastSetScenePosition);
319 }
320
321 qreal MapView::viewScale() const
322 {
323     qDebug() << __PRETTY_FUNCTION__;
324
325     return transform().m11();
326 }
327
328 qreal MapView::viewShift() const
329 {
330     qDebug() << __PRETTY_FUNCTION__;
331
332     return m_centerHorizontalShiftViewPixels;
333 }