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 int KINETIC_SCROLL_TIME_MS = 750;
40 const qreal KINETIC_SPEED_TO_DISTANCE_FACTOR = 0.15 * sqrt(KINETIC_SCROLL_TIME_MS / MS_PER_S);
42 MapView::MapView(QWidget *parent)
43 : QGraphicsView(parent)
45 qDebug() << __PRETTY_FUNCTION__;
47 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
51 connect(m_zoomAnimation, SIGNAL(finished()),
52 this, SIGNAL(viewZoomFinished()));
54 setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
56 m_scroller = &MapScroller::getInstance();
58 m_groupAnimation = new QParallelAnimationGroup(this);
59 m_groupAnimation->addAnimation(m_scroller);
60 m_groupAnimation->addAnimation(m_zoomAnimation);
63 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
65 qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
67 centerOn(sceneCoordinate);
70 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
72 QPoint pressPos = mapToScene(event->pos()).toPoint();
73 QPoint centerPos = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
74 QPoint zoomPoint = centerPos - ((centerPos - pressPos) / 2);
76 m_groupAnimation->stop();
78 m_scroller->setEasingCurve(QEasingCurve::OutCirc);
79 m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
80 m_scroller->setStartValue(m_scenePosition);
81 m_scroller->setEndValue(zoomPoint);
83 m_zoomAnimation->setEasingCurve(QEasingCurve::OutCirc);
84 m_zoomAnimation->setDuration(KINETIC_SCROLL_TIME_MS);
85 m_zoomAnimation->setStartValue(viewScale());
86 m_zoomAnimation->setEndValue(pow(2, ++m_zoomLevel - MAX_MAP_ZOOM_LEVEL));
89 m_groupAnimation->start();
92 void MapView::mouseMoveEvent(QMouseEvent *event)
94 m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
96 qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
98 if (m_index >= VALUES)
101 m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
102 m_dragTime[m_index] = m_time.elapsed();
106 emit viewScrolled(m_scenePosition);
108 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
109 m_mouseLastViewPosition = event->pos();
112 void MapView::mousePressEvent(QMouseEvent *event)
114 qDebug() << __PRETTY_FUNCTION__;
120 QGraphicsView::mousePressEvent(event);
122 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
123 m_mouseLastViewPosition = event->pos();
124 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
126 for (int i = 0; i < VALUES; i++) {
127 m_dragMovement[i] = QPoint();
133 void MapView::mouseReleaseEvent(QMouseEvent *event)
135 qDebug() << __PRETTY_FUNCTION__;
137 int elapsed = m_time.elapsed();
139 QGraphicsView::mouseReleaseEvent(event);
141 // start kinetic scroll only if there isn't too much time elapsed from the last mouse move event
142 if (elapsed <= KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS) {
143 QPointF dragViewSpeed;
146 for (int i = 0; i < VALUES; i++) {
147 if (m_dragTime[i] > 0) {
148 dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
149 dragLength += m_dragMovement[i].manhattanLength();
154 if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
155 dragViewSpeed /= values;
156 QPointF effectViewDistance= dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
157 QPointF effectSceneDistance = effectViewDistance
158 * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
160 m_scroller->setEasingCurve(QEasingCurve::OutCirc);
161 m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
162 m_scroller->setStartValue(m_scenePosition);
163 m_scroller->setEndValue(m_scenePosition + effectSceneDistance.toPoint());
169 void MapView::resizeEvent(QResizeEvent *event)
171 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
173 emit viewResized(event->size());
176 void MapView::setViewScale(qreal viewScale)
178 qDebug() << __PRETTY_FUNCTION__;
180 QTransform transform;
181 transform.scale(viewScale, viewScale);
182 setTransform(transform);
185 void MapView::setZoomLevel(int zoomLevel)
187 qDebug() << __PRETTY_FUNCTION__;
189 m_zoomLevel = zoomLevel;
191 if (m_zoomAnimation && (m_zoomAnimation->state() != QPropertyAnimation::Running)) {
192 m_zoomAnimation->stop();
193 m_zoomAnimation->setDuration(ZOOM_TIME);
194 m_zoomAnimation->setStartValue(viewScale());
195 m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
197 m_zoomAnimation->start();
201 qreal MapView::viewScale()
203 qDebug() << __PRETTY_FUNCTION__;
205 return transform().m11();