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 qWarning() << __PRETTY_FUNCTION__;
98 qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
100 if (m_index >= VALUES)
103 m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
104 m_dragTime[m_index] = m_time.elapsed();
108 emit viewScrolled(m_scenePosition);
110 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
111 m_mouseLastViewPosition = event->pos();
114 void MapView::mousePressEvent(QMouseEvent *event)
116 qWarning() << __PRETTY_FUNCTION__;
117 qDebug() << __PRETTY_FUNCTION__;
123 QGraphicsView::mousePressEvent(event);
125 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
126 m_mouseLastViewPosition = event->pos();
127 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
129 for (int i = 0; i < VALUES; i++) {
130 m_dragMovement[i] = QPoint();
136 void MapView::mouseReleaseEvent(QMouseEvent *event)
138 qDebug() << __PRETTY_FUNCTION__;
140 int elapsed = m_time.elapsed();
142 QGraphicsView::mouseReleaseEvent(event);
144 // start kinetic scroll only if there isn't too much time elapsed from the last mouse move event
145 if (elapsed <= KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS) {
146 QPointF dragViewSpeed;
149 for (int i = 0; i < VALUES; i++) {
150 if (m_dragTime[i] > 0) {
151 dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
152 dragLength += m_dragMovement[i].manhattanLength();
157 if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
158 dragViewSpeed /= values;
159 QPointF effectViewDistance= dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
160 QPointF effectSceneDistance = effectViewDistance
161 * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
163 m_scroller->setEasingCurve(QEasingCurve::OutCirc);
164 m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
165 m_scroller->setStartValue(m_scenePosition);
166 m_scroller->setEndValue(m_scenePosition + effectSceneDistance.toPoint());
172 void MapView::resizeEvent(QResizeEvent *event)
174 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
176 emit viewResized(event->size());
179 void MapView::setViewScale(qreal viewScale)
181 qDebug() << __PRETTY_FUNCTION__;
183 QTransform transform;
184 transform.scale(viewScale, viewScale);
185 setTransform(transform);
188 void MapView::setZoomLevel(int zoomLevel)
190 qDebug() << __PRETTY_FUNCTION__;
192 m_zoomLevel = zoomLevel;
194 if (m_zoomAnimation && (m_zoomAnimation->state() == QAbstractAnimation::Stopped)) {
195 m_zoomAnimation->stop();
196 m_zoomAnimation->setDuration(ZOOM_TIME);
197 m_zoomAnimation->setStartValue(viewScale());
198 m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
200 m_zoomAnimation->start();
204 qreal MapView::viewScale()
206 qDebug() << __PRETTY_FUNCTION__;
208 return transform().m11();