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>
28 #include "mapcommon.h"
29 #include "mapscroller.h"
32 const int KINETIC_SCROLL_TIME_MS = 7500;
33 const qreal MS_PER_S = 1000;
34 const qreal SPEED_TO_DISTANCE_FACTOR = 0.15 * sqrt(KINETIC_SCROLL_TIME_MS / MS_PER_S);
36 MapView::MapView(QWidget *parent)
37 : QGraphicsView(parent)
39 qDebug() << __PRETTY_FUNCTION__;
41 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
45 connect(m_zoomAnimation, SIGNAL(finished()),
46 this, SIGNAL(viewZoomFinished()));
48 setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
50 m_scroller = &MapScroller::getInstance();
52 qWarning() << __PRETTY_FUNCTION__ << "SPEED_TO_DISTANCE_FACTOR" << SPEED_TO_DISTANCE_FACTOR;
55 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
57 qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
59 centerOn(sceneCoordinate);
62 void MapView::mouseMoveEvent(QMouseEvent *event)
64 m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
66 qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
68 if (m_index >= VALUES)
71 m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
72 m_dragTime[m_index] = time.elapsed();
75 // qWarning() << __PRETTY_FUNCTION__ << m_index << m_dragMovement[m_index].x() << m_dragMovement[m_index].y() << m_dragTime[m_index];
79 // qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y();
80 // qWarning() << __PRETTY_FUNCTION__ << "elapsed:" << time.elapsed() << "ms";
82 emit viewScrolled(m_scenePosition);
84 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
85 m_mouseLastViewPosition = event->pos();
88 void MapView::mousePressEvent(QMouseEvent *event)
90 qDebug() << __PRETTY_FUNCTION__;
96 QGraphicsView::mousePressEvent(event);
98 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
99 m_mouseLastViewPosition = event->pos();
100 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
102 for (int i = 0; i < VALUES; i++) {
103 m_dragMovement[i] = QPoint();
109 void MapView::mouseReleaseEvent(QMouseEvent *event)
111 // qWarning() << __PRETTY_FUNCTION__;
113 QGraphicsView::mouseReleaseEvent(event);
115 QPointF dragViewSpeed;
117 for (int i = 0; i < VALUES; i++) {
118 if (m_dragTime > 0) {
119 dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
122 // qWarning() << __PRETTY_FUNCTION__ << m_dragMovement[i].x() << m_dragMovement[i].y() << m_dragTime[i];
124 dragViewSpeed /= values;
125 dragViewSpeed *= SPEED_TO_DISTANCE_FACTOR;
127 // qWarning() << __PRETTY_FUNCTION__ << dragViewSpeed.x() << dragViewSpeed.y();
129 QPointF dragSceneSpeed = dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
131 m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
132 m_scroller->setStartValue(m_scenePosition);
133 m_scroller->setEndValue(m_scenePosition + dragSceneSpeed.toPoint());
137 void MapView::resizeEvent(QResizeEvent *event)
139 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
141 emit viewResized(event->size());
144 void MapView::setViewScale(qreal viewScale)
146 qDebug() << __PRETTY_FUNCTION__;
148 QTransform transform;
149 transform.scale(viewScale, viewScale);
150 setTransform(transform);
153 void MapView::setZoomLevel(int zoomLevel)
155 qDebug() << __PRETTY_FUNCTION__;
157 m_zoomLevel = zoomLevel;
159 if (m_zoomAnimation) {
160 m_zoomAnimation->stop();
161 m_zoomAnimation->setDuration(ZOOM_TIME);
162 m_zoomAnimation->setStartValue(viewScale());
163 m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
165 m_zoomAnimation->start();
169 qreal MapView::viewScale()
171 qDebug() << __PRETTY_FUNCTION__;
173 return transform().m11();