Fine tuned the 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 = 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);
35
36 MapView::MapView(QWidget *parent)
37     : QGraphicsView(parent)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43
44     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
45     connect(m_zoomAnimation, SIGNAL(finished()),
46         this, SIGNAL(viewZoomFinished()));
47
48     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
49
50     m_scroller = &MapScroller::getInstance();
51
52     qWarning() << __PRETTY_FUNCTION__ << "SPEED_TO_DISTANCE_FACTOR" << SPEED_TO_DISTANCE_FACTOR;
53 }
54
55 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
56 {
57     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
58
59     centerOn(sceneCoordinate);
60 }
61
62 void MapView::mouseMoveEvent(QMouseEvent *event)
63 {
64     m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
65
66     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
67
68     if (m_index >= VALUES)
69         m_index = 0;
70
71     m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
72     m_dragTime[m_index] = time.elapsed();
73     time.start();
74
75 //    qWarning() << __PRETTY_FUNCTION__ << m_index << m_dragMovement[m_index].x() << m_dragMovement[m_index].y() << m_dragTime[m_index];
76
77     m_index++;
78
79 //    qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y();
80 //    qWarning() << __PRETTY_FUNCTION__  << "elapsed:" << time.elapsed() << "ms";
81
82     emit viewScrolled(m_scenePosition);
83
84     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
85     m_mouseLastViewPosition = event->pos();
86 }
87
88 void MapView::mousePressEvent(QMouseEvent *event)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     time.start();
93
94     m_scroller->stop();
95
96     QGraphicsView::mousePressEvent(event);
97
98     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
99     m_mouseLastViewPosition = event->pos();
100     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
101
102     for (int i = 0; i < VALUES; i++) {
103         m_dragMovement[i] = QPoint();
104         m_dragTime[i] = 0;
105     }
106     m_index = 0;
107 }
108
109 void MapView::mouseReleaseEvent(QMouseEvent *event)
110 {
111 //    qWarning() << __PRETTY_FUNCTION__;
112
113     QGraphicsView::mouseReleaseEvent(event);
114
115     QPointF dragViewSpeed;
116     int values = 0;
117     for (int i = 0; i < VALUES; i++) {
118         if (m_dragTime > 0) {
119             dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
120             values++;
121         }
122 //        qWarning() << __PRETTY_FUNCTION__ << m_dragMovement[i].x() << m_dragMovement[i].y() << m_dragTime[i];
123     }
124     dragViewSpeed /= values;
125     dragViewSpeed *= SPEED_TO_DISTANCE_FACTOR;
126
127 //    qWarning() << __PRETTY_FUNCTION__ << dragViewSpeed.x() << dragViewSpeed.y();
128
129     QPointF dragSceneSpeed = dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
130
131     m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
132     m_scroller->setStartValue(m_scenePosition);
133     m_scroller->setEndValue(m_scenePosition + dragSceneSpeed.toPoint());
134     m_scroller->start();
135 }
136
137 void MapView::resizeEvent(QResizeEvent *event)
138 {
139     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
140
141     emit viewResized(event->size());
142 }
143
144 void MapView::setViewScale(qreal viewScale)
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147
148     QTransform transform;
149     transform.scale(viewScale, viewScale);
150     setTransform(transform);
151 }
152
153 void MapView::setZoomLevel(int zoomLevel)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     m_zoomLevel = zoomLevel;
158
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));
164
165         m_zoomAnimation->start();
166     }
167 }
168
169 qreal MapView::viewScale()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     return transform().m11();
174 }