2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
6 Pekka Nissinen - pekka.nissinen@ixonos.com
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.
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.
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,
26 #include <QMouseEvent>
27 #include <QParallelAnimationGroup>
29 #include "coordinates/scenecoordinate.h"
30 #include "mapcommon.h"
31 #include "mapscroller.h"
32 #include "ui/panelcommon.h"
36 const qreal MS_PER_S = 1000;
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);
45 const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
47 MapView::MapView(QWidget *parent)
48 : QGraphicsView(parent),
49 m_doubleTapZoomRunning(false)
51 qDebug() << __PRETTY_FUNCTION__;
53 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56 m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
57 connect(m_zoomAnimation, SIGNAL(finished()),
58 this, SIGNAL(viewZoomFinished()));
59 setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
61 m_scroller = &MapScroller::getInstance();
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()));
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);
78 qDebug() << __PRETTY_FUNCTION__;
80 m_scrollAndZoomAnimation->removeAnimation(m_scroller);
81 delete m_scrollAndZoomAnimation;
84 QPointF MapView::center()
86 return mapToScene(m_viewCenterPoint) - m_centerHorizontalShiftPoint;
89 void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate, bool isUserDragAction)
91 qDebug() << __PRETTY_FUNCTION__ << "coordinate" << coordinate;
93 QPointF target = coordinate.toPointF();
94 m_lastSetScenePosition = coordinate;
96 // if (!isUserDragAction)
97 target += m_centerHorizontalShiftPoint;
102 void MapView::disableCenterShift()
104 qDebug() << __PRETTY_FUNCTION__;
106 if (m_centerShiftAnimation) {
107 m_centerShiftAnimation->setDirection(QAbstractAnimation::Backward);
108 m_centerShiftAnimation->start();
112 void MapView::doubleTapZoomFinished()
114 qDebug() << __PRETTY_FUNCTION__;
116 m_doubleTapZoomRunning = false;
120 void MapView::enableCenterShift()
122 qDebug() << __PRETTY_FUNCTION__;
124 if (m_centerShiftAnimation) {
125 m_centerShiftAnimation->setDirection(QAbstractAnimation::Forward);
126 m_centerShiftAnimation->start();
130 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
132 qDebug() << __PRETTY_FUNCTION__;
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);
139 m_scrollAndZoomAnimation->stop();
140 m_doubleTapZoomRunning = true;
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()));
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));
153 m_scrollAndZoomAnimation->start();
157 void MapView::mouseMoveEvent(QMouseEvent *event)
159 qDebug() << __PRETTY_FUNCTION__;
161 if (m_doubleTapZoomRunning)
164 m_internalScenePosition += m_lastMouseEventScenePosition - mapToScene(event->pos()).toPoint();
166 if (m_index >= VALUES)
169 m_dragMovement[m_index] = m_lastMouseEventViewPosition - event->pos();
170 m_dragTime[m_index] = m_time.elapsed();
174 QPointF viewCenterPoint = m_internalScenePosition - m_centerHorizontalShiftPoint;
176 emit viewScrolled(SceneCoordinate(viewCenterPoint.x(), viewCenterPoint.y()), true);
178 m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
179 m_lastMouseEventViewPosition = event->pos();
182 void MapView::mousePressEvent(QMouseEvent *event)
184 qDebug() << __PRETTY_FUNCTION__;
186 if (m_doubleTapZoomRunning)
193 QGraphicsView::mousePressEvent(event);
195 m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
196 m_lastMouseEventViewPosition = event->pos();
197 m_internalScenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
199 for (int i = 0; i < VALUES; i++) {
200 m_dragMovement[i] = QPoint();
206 void MapView::mouseReleaseEvent(QMouseEvent *event)
208 qDebug() << __PRETTY_FUNCTION__;
210 if (m_doubleTapZoomRunning)
213 int elapsed = m_time.elapsed();
215 QGraphicsView::mouseReleaseEvent(event);
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;
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();
230 if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
231 dragViewSpeed /= values;
232 QPointF effectViewDistance = dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
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;
239 QPointF effectSceneDistance = effectViewDistance
240 * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel));
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);
254 void MapView::resizeEvent(QResizeEvent *event)
256 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
258 m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
260 m_viewCenterPoint.setX(event->size().width() / 2);
261 m_viewCenterPoint.setY(event->size().height() / 2);
263 emit viewResized(event->size());
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);
273 void MapView::setViewScale(qreal viewScale)
275 qDebug() << __PRETTY_FUNCTION__;
277 QTransform transform;
278 transform.scale(viewScale, viewScale);
279 setTransform(transform);
284 void MapView::setViewShift(qreal viewShift)
286 qDebug() << __PRETTY_FUNCTION__;
288 m_centerHorizontalShiftViewPixels = viewShift;
289 emit horizontalShiftingChanged(m_centerHorizontalShiftViewPixels);
294 void MapView::setZoomLevel(int zoomLevel)
296 qDebug() << __PRETTY_FUNCTION__;
298 m_zoomLevel = zoomLevel;
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));
307 m_zoomAnimation->start();
311 void MapView::updateCenterShift()
313 qDebug() << __PRETTY_FUNCTION__;
315 m_centerHorizontalShiftPoint = QPointF(m_centerHorizontalShiftViewPixels * (1.0 / viewScale()),
318 centerToSceneCoordinates(m_lastSetScenePosition);
321 qreal MapView::viewScale() const
323 qDebug() << __PRETTY_FUNCTION__;
325 return transform().m11();
328 qreal MapView::viewShift() const
330 qDebug() << __PRETTY_FUNCTION__;
332 return m_centerHorizontalShiftViewPixels;