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 = 1000;
34 MapView::MapView(QWidget *parent)
35 : QGraphicsView(parent)
37 qDebug() << __PRETTY_FUNCTION__;
39 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42 m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
43 connect(m_zoomAnimation, SIGNAL(finished()),
44 this, SIGNAL(viewZoomFinished()));
46 setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
48 m_scroller = &MapScroller::getInstance();
51 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
53 qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
55 centerOn(sceneCoordinate);
58 void MapView::mouseMoveEvent(QMouseEvent *event)
60 m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
62 qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
64 if (m_index >= VALUES)
67 m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
68 m_dragTime[m_index] = time.elapsed();
71 // qWarning() << __PRETTY_FUNCTION__ << m_index << m_dragMovement[m_index].x() << m_dragMovement[m_index].y() << m_dragTime[m_index];
75 // qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y();
76 // qWarning() << __PRETTY_FUNCTION__ << "elapsed:" << time.elapsed() << "ms";
78 emit viewScrolled(m_scenePosition);
80 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
81 m_mouseLastViewPosition = event->pos();
84 void MapView::mousePressEvent(QMouseEvent *event)
86 qDebug() << __PRETTY_FUNCTION__;
92 QGraphicsView::mousePressEvent(event);
94 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
95 m_mouseLastViewPosition = event->pos();
96 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
98 for (int i = 0; i < VALUES; i++) {
99 m_dragMovement[i] = QPoint();
105 void MapView::mouseReleaseEvent(QMouseEvent *event)
107 // qWarning() << __PRETTY_FUNCTION__;
109 const qreal MS_PER_S = 1000;
110 const qreal SPEED_TO_DISTANCE_FACTOR = KINETIC_SCROLL_TIME_MS / MS_PER_S * 0.10;
112 QGraphicsView::mouseReleaseEvent(event);
114 QPointF dragViewSpeed;
116 for (int i = 0; i < VALUES; i++) {
117 if (m_dragTime > 0) {
118 dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
121 // qWarning() << __PRETTY_FUNCTION__ << m_dragMovement[i].x() << m_dragMovement[i].y() << m_dragTime[i];
123 dragViewSpeed /= values;
124 dragViewSpeed *= SPEED_TO_DISTANCE_FACTOR;
126 // qWarning() << __PRETTY_FUNCTION__ << dragViewSpeed.x() << dragViewSpeed.y();
128 QPointF dragSceneSpeed = dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
130 m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
131 m_scroller->setStartValue(m_scenePosition);
132 m_scroller->setEndValue(m_scenePosition + dragSceneSpeed.toPoint());
136 void MapView::resizeEvent(QResizeEvent *event)
138 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
140 emit viewResized(event->size());
143 void MapView::setViewScale(qreal viewScale)
145 qDebug() << __PRETTY_FUNCTION__;
147 QTransform transform;
148 transform.scale(viewScale, viewScale);
149 setTransform(transform);
152 void MapView::setZoomLevel(int zoomLevel)
154 qDebug() << __PRETTY_FUNCTION__;
156 m_zoomLevel = zoomLevel;
158 if (m_zoomAnimation) {
159 m_zoomAnimation->stop();
160 m_zoomAnimation->setDuration(ZOOM_TIME);
161 m_zoomAnimation->setStartValue(viewScale());
162 m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
164 m_zoomAnimation->start();
168 qreal MapView::viewScale()
170 qDebug() << __PRETTY_FUNCTION__;
172 return transform().m11();