Self made kinetic scrolling first working version
[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 MapView::MapView(QWidget *parent)
33     : QGraphicsView(parent)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36
37     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
38     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39
40     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
41     connect(m_zoomAnimation, SIGNAL(finished()),
42         this, SIGNAL(viewZoomFinished()));
43
44     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
45
46     m_scroller = &MapScroller::getInstance();
47 }
48
49 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
50 {
51     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
52
53     centerOn(sceneCoordinate);
54 }
55
56 void MapView::mouseMoveEvent(QMouseEvent *event)
57 {
58     m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
59
60     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
61
62     m_dragViewSpeed = m_mouseLastViewPosition - event->pos() ;
63 //    qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y();
64
65     emit viewScrolled(m_scenePosition);
66
67     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
68     m_mouseLastViewPosition = event->pos();
69 }
70
71 void MapView::mousePressEvent(QMouseEvent *event)
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     m_scroller->stop();
76
77     QGraphicsView::mousePressEvent(event);
78
79     m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
80     m_mouseLastViewPosition = event->pos();
81     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
82
83     m_dragViewSpeed = QPoint();
84 }
85
86 void MapView::mouseReleaseEvent(QMouseEvent *event)
87 {
88 //    qWarning() << __PRETTY_FUNCTION__;
89
90     QGraphicsView::mouseReleaseEvent(event);
91
92     QPoint dragSceneSpeed = m_dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
93
94     m_scroller->setStartValue(m_scenePosition);
95     m_scroller->setEndValue(m_scenePosition + dragSceneSpeed * 5);
96     m_scroller->setDuration(1000);
97     m_scroller->start();
98 }
99
100 void MapView::resizeEvent(QResizeEvent *event)
101 {
102     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
103
104     emit viewResized(event->size());
105 }
106
107 void MapView::setViewScale(qreal viewScale)
108 {
109     qDebug() << __PRETTY_FUNCTION__;
110
111     QTransform transform;
112     transform.scale(viewScale, viewScale);
113     setTransform(transform);
114 }
115
116 void MapView::setZoomLevel(int zoomLevel)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     m_zoomLevel = zoomLevel;
121
122     if (m_zoomAnimation) {
123         m_zoomAnimation->stop();
124         m_zoomAnimation->setDuration(ZOOM_TIME);
125         m_zoomAnimation->setStartValue(viewScale());
126         m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
127
128         m_zoomAnimation->start();
129     }
130 }
131
132 qreal MapView::viewScale()
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     return transform().m11();
137 }