7fc59eb4b53f2303108f278f796fa51484101c0b
[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 <cmath>
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 #ifdef Q_WS_MAEMO_51
40     qDebug() << __PRETTY_FUNCTION__ << "QAbstractKineticScroller built in";
41
42     QAbstractKineticScroller *scroller = this->property("kineticScroller")
43                                              .value<QAbstractKineticScroller *>();
44
45     if (scroller)
46         scroller->setEnabled(true);
47 #endif // Q_WS_MAEMO_5
48 }
49
50 void MapView::setZoomLevel(int zoomLevel)
51 {
52     m_zoomTargetScale = pow(2, zoomLevel - MAX_ZOOM_LEVEL);
53     m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * ZOOM_TIME);
54
55     startTimer(1000/ZOOM_FPS);
56 }
57
58 void MapView::timerEvent(QTimerEvent *event)
59 {
60     qreal scaleFactor = currentScale();
61
62 //    qDebug() << __PRETTY_FUNCTION__
63 //             << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
64 //             << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
65
66     if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
67         scaleFactor = m_zoomTargetScale;
68         killTimer(event->timerId());
69     }
70     else {
71         scaleFactor += m_zoomScaleDelta;
72     }
73
74 //    qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
75 //                                    << "m_zoomScaleDelta" << m_zoomScaleDelta
76 //                                    << "scaleFactor:" << scaleFactor;
77
78     QTransform transform;
79     transform.scale(scaleFactor, scaleFactor);
80     setTransform(transform);
81 }
82
83 qreal MapView::currentScale()
84 {
85     QTransform currentTransform = transform();
86     return currentTransform.m11();
87 }
88
89 void MapView::mouseMoveEvent(QMouseEvent *event)
90 {
91     m_scenePosition += m_mousePosition - mapToScene(event->pos());
92
93     emit viewScrolled(m_scenePosition);
94     //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
95
96     m_mousePosition = mapToScene(event->pos());
97 }
98
99 void MapView::mousePressEvent(QMouseEvent *event)
100 {
101     m_mousePosition = mapToScene(event->pos());
102     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1);
103 }
104
105
106 void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
107 {
108     //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
109     centerOn(sceneCoordinate);
110 }
111
112 void MapView::resizeEvent(QResizeEvent *event)
113 {
114     qDebug() << "Resize event: " << event->size();
115     emit viewResized(event->size());
116 }