Self made kinetic scrolling first version working.
[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
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <math.h>
23
24 #include <QDebug>
25 #include <QMouseEvent>
26
27 #ifdef Q_WS_MAEMO_51
28     #include <QAbstractKineticScroller>
29 #endif // Q_WS_MAEMO_5
30
31 #include "common.h"
32 #include "mapview.h"
33
34 MapView::MapView(QWidget *parent) : QGraphicsView(parent)
35 {
36     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
37     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
38
39      startTimer(1000/60);
40
41 #ifdef Q_WS_MAEMO_51
42     qDebug() << __PRETTY_FUNCTION__ << "QAbstractKineticScroller built in";
43
44     QAbstractKineticScroller *scroller = this->property("kineticScroller")
45                                              .value<QAbstractKineticScroller *>();
46
47     if (scroller)
48         scroller->setEnabled(true);
49 #endif // Q_WS_MAEMO_5
50 }
51
52 void MapView::setZoomLevel(int zoomLevel)
53 {
54     double scaleFactor = pow(2, zoomLevel - MAX_ZOOM_LEVEL);
55     QTransform transform;
56     transform.scale(scaleFactor, scaleFactor);
57     setTransform(transform);
58 }
59
60 void MapView::mouseMoveEvent(QMouseEvent *event)
61 {
62 //    qDebug() << __PRETTY_FUNCTION__ << "scene coordinate:" << mapToScene(event->pos());
63 //    if (event->buttons() & Qt::LeftButton)
64 //    {
65 //        QPointF delta = mapToScene(event->pos()) - m_mouseClickLocation;
66 //        qDebug() << __PRETTY_FUNCTION__ << "delta:" << delta;
67 //
68 //        int x = size().width() / 2;
69 //        int y = size().height() / 2;
70 //        QPointF sceneCoordinate = mapToScene(QPoint(x, y)) + delta;
71 //        centerToSceneCoordinates(sceneCoordinate);
72 //    }
73     m_scenePosition += m_mousePressPosition - event->pos();
74     centerToSceneCoordinates(m_scenePosition);
75
76     if (m_mouseIsDown)
77         m_mouseSpeed = event->pos() - m_mousePressPosition;
78
79     m_mousePressPosition = event->pos();
80 }
81
82 void MapView::mousePressEvent(QMouseEvent *event)
83 {
84 //    qDebug() << __PRETTY_FUNCTION__ << "scene coordinate:" << mapToScene(event->pos());
85 //
86 //    if (event->button() == Qt::LeftButton)
87 //        m_mouseClickLocation = mapToScene(event->pos());
88     m_mousePressPosition = event->pos();
89     m_mouseIsDown = true;
90     m_mouseSpeed = QPointF(0, 0);
91
92     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1);
93 }
94
95 void MapView::mouseReleaseEvent(QMouseEvent *) {
96     m_mouseIsDown = false;
97 }
98
99 void MapView::timerEvent(QTimerEvent *event) {
100     if (!m_mouseIsDown) {
101         m_mouseSpeed *= 0.97;
102         if (m_mouseSpeed.manhattanLength() < 0.1)
103             m_mouseSpeed = QPointF(0, 0);
104
105 //        qDebug() << __PRETTY_FUNCTION__ << "m_mouseSpeed" << m_mouseSpeed;
106
107         m_scenePosition -= m_mouseSpeed;
108         centerToSceneCoordinates(m_scenePosition);
109     }
110 }
111
112 void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
113 {
114     centerOn(sceneCoordinate);
115 }
116
117 //bool MapView::event(QEvent *event)
118 //{
119 //    qDebug() << __PRETTY_FUNCTION__ << "event type:" << event->type();
120 //    event->ignore();
121 //    QObject::event(event);
122 //    return false;
123 //}