Changed deceleration curve and adjusted scroll distance calculation
[situare] / src / map / mapview.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Sami Rämö - sami.ramo@ixonos.com
6        Pekka Nissinen - pekka.nissinen@ixonos.com
7
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.
11
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.
16
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,
20    USA.
21 */
22
23 #include <cmath>
24
25 #include <QDebug>
26 #include <QMouseEvent>
27
28 #include "mapcommon.h"
29 #include "mapscroller.h"
30 #include "mapview.h"
31
32 const int KINETIC_SCROLL_TIME_MS = 1000;
33
34 MapView::MapView(QWidget *parent)
35     : QGraphicsView(parent)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41
42     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
43     connect(m_zoomAnimation, SIGNAL(finished()),
44         this, SIGNAL(viewZoomFinished()));
45
46     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
47
48     m_scroller = &MapScroller::getInstance();
49 }
50
51 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
52 {
53     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
54
55     centerOn(sceneCoordinate);
56 }
57
58 void MapView::mouseMoveEvent(QMouseEvent *event)
59 {
60     m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
61
62     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
63
64     if (m_index >= VALUES)
65         m_index = 0;
66
67     m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
68     m_dragTime[m_index] = time.elapsed();
69     time.start();
70
71 //    qWarning() << __PRETTY_FUNCTION__ << m_index << m_dragMovement[m_index].x() << m_dragMovement[m_index].y() << m_dragTime[m_index];
72
73     m_index++;
74
75 //    qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y();
76 //    qWarning() << __PRETTY_FUNCTION__  << "elapsed:" << time.elapsed() << "ms";
77
78     emit viewScrolled(m_scenePosition);
79
80     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
81     m_mouseLastViewPosition = event->pos();
82 }
83
84 void MapView::mousePressEvent(QMouseEvent *event)
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     time.start();
89
90     m_scroller->stop();
91
92     QGraphicsView::mousePressEvent(event);
93
94     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
95     m_mouseLastViewPosition = event->pos();
96     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
97
98     for (int i = 0; i < VALUES; i++) {
99         m_dragMovement[i] = QPoint();
100         m_dragTime[i] = 0;
101     }
102     m_index = 0;
103 }
104
105 void MapView::mouseReleaseEvent(QMouseEvent *event)
106 {
107 //    qWarning() << __PRETTY_FUNCTION__;
108
109     const qreal MS_PER_S = 1000;
110     const qreal SPEED_TO_DISTANCE_FACTOR = KINETIC_SCROLL_TIME_MS / MS_PER_S * 0.10;
111
112     QGraphicsView::mouseReleaseEvent(event);
113
114     QPointF dragViewSpeed;
115     int values = 0;
116     for (int i = 0; i < VALUES; i++) {
117         if (m_dragTime > 0) {
118             dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
119             values++;
120         }
121 //        qWarning() << __PRETTY_FUNCTION__ << m_dragMovement[i].x() << m_dragMovement[i].y() << m_dragTime[i];
122     }
123     dragViewSpeed /= values;
124     dragViewSpeed *= SPEED_TO_DISTANCE_FACTOR;
125
126 //    qWarning() << __PRETTY_FUNCTION__ << dragViewSpeed.x() << dragViewSpeed.y();
127
128     QPointF dragSceneSpeed = dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
129
130     m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
131     m_scroller->setStartValue(m_scenePosition);
132     m_scroller->setEndValue(m_scenePosition + dragSceneSpeed.toPoint());
133     m_scroller->start();
134 }
135
136 void MapView::resizeEvent(QResizeEvent *event)
137 {
138     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
139
140     emit viewResized(event->size());
141 }
142
143 void MapView::setViewScale(qreal viewScale)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     QTransform transform;
148     transform.scale(viewScale, viewScale);
149     setTransform(transform);
150 }
151
152 void MapView::setZoomLevel(int zoomLevel)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     m_zoomLevel = zoomLevel;
157
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));
163
164         m_zoomAnimation->start();
165     }
166 }
167
168 qreal MapView::viewScale()
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     return transform().m11();
173 }