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 "mapcommon.h"
30 #include "mapscroller.h"
34 const qreal MS_PER_S = 1000;
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);
43 const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
45 MapView::MapView(QWidget *parent)
46 : QGraphicsView(parent),
47 m_doubleTapZoomRunning(false)
49 qDebug() << __PRETTY_FUNCTION__;
51 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54 m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
55 connect(m_zoomAnimation, SIGNAL(finished()),
56 this, SIGNAL(viewZoomFinished()));
57 setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
59 m_scroller = &MapScroller::getInstance();
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()));
68 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
70 qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
72 centerOn(sceneCoordinate);
75 void MapView::doubleTapZoomFinished()
77 qDebug() << __PRETTY_FUNCTION__;
79 m_doubleTapZoomRunning = false;
83 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
85 qDebug() << __PRETTY_FUNCTION__;
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);
92 m_scrollAndZoomAnimation->stop();
93 m_doubleTapZoomRunning = true;
95 m_scroller->setEasingCurve(QEasingCurve::Linear);
96 m_scroller->setDuration(ZOOM_TIME_MS);
97 m_scroller->setStartValue(m_scenePosition);
98 m_scroller->setEndValue(zoomPosition);
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));
105 m_scrollAndZoomAnimation->start();
109 void MapView::mouseMoveEvent(QMouseEvent *event)
111 qDebug() << __PRETTY_FUNCTION__;
113 if (m_doubleTapZoomRunning)
116 m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
118 if (m_index >= VALUES)
121 m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
122 m_dragTime[m_index] = m_time.elapsed();
126 emit viewScrolled(m_scenePosition);
128 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
129 m_mouseLastViewPosition = event->pos();
132 void MapView::mousePressEvent(QMouseEvent *event)
134 qDebug() << __PRETTY_FUNCTION__;
136 if (m_doubleTapZoomRunning)
143 QGraphicsView::mousePressEvent(event);
145 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
146 m_mouseLastViewPosition = event->pos();
147 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
149 for (int i = 0; i < VALUES; i++) {
150 m_dragMovement[i] = QPoint();
156 void MapView::mouseReleaseEvent(QMouseEvent *event)
158 qDebug() << __PRETTY_FUNCTION__;
160 if (m_doubleTapZoomRunning)
163 int elapsed = m_time.elapsed();
165 QGraphicsView::mouseReleaseEvent(event);
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;
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();
180 if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
181 dragViewSpeed /= values;
182 QPointF effectViewDistance = dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
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;
189 QPointF effectSceneDistance = effectViewDistance
190 * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
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());
201 void MapView::resizeEvent(QResizeEvent *event)
203 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
205 m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
207 emit viewResized(event->size());
210 void MapView::setViewScale(qreal viewScale)
212 qDebug() << __PRETTY_FUNCTION__;
214 QTransform transform;
215 transform.scale(viewScale, viewScale);
216 setTransform(transform);
219 void MapView::setZoomLevel(int zoomLevel)
221 qDebug() << __PRETTY_FUNCTION__;
223 m_zoomLevel = zoomLevel;
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));
232 m_zoomAnimation->start();
236 qreal MapView::viewScale()
238 qDebug() << __PRETTY_FUNCTION__;
240 return transform().m11();
245 qDebug() << __PRETTY_FUNCTION__;
247 m_scrollAndZoomAnimation->removeAnimation(m_scroller);
248 delete m_scrollAndZoomAnimation;