Merge branch 'map' of https://vcs.maemo.org/git/situare into map
[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 #ifndef Q_WS_MAEMO_5
28     #include <QGLWidget>
29 #endif // Q_WS_MAEMO_5
30
31 #ifdef Q_WS_MAEMO_5
32     #include <QAbstractKineticScroller>
33 #endif // Q_WS_MAEMO_5
34
35 #include "common.h"
36 #include "mapview.h"
37
38 MapView::MapView(QWidget *parent) : QGraphicsView(parent)
39 {
40 /**
41   * Use OpenGL for desktop to gain some performance in map view.
42   * OpenGL can't be used in scratchbox.
43   */
44 #ifndef Q_WS_MAEMO_5
45     setViewport(new QGLWidget);
46 #endif // !Q_WS_MAEMO_5
47
48 /**
49   * Use kinetic scrolling for Maemo5 and QGraphicsViews drag mode
50   * ScrollHandDrag for other environments
51   */
52 #ifdef Q_WS_MAEMO_5
53     QAbstractKineticScroller *scroller = property("kineticScroller")
54                                          .value<QAbstractKineticScroller *>();
55     if (scroller)
56         scroller->setEnabled(true);
57 #else
58     setDragMode(QGraphicsView::ScrollHandDrag);
59 #endif // Q_WS_MAEMO_5
60
61     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63 }
64
65 void MapView::setZoomLevel(int zoomLevel)
66 {
67     m_zoomTargetScale = pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL);
68     m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * ZOOM_TIME);
69
70     startTimer(1000/ZOOM_FPS);
71 }
72
73 void MapView::timerEvent(QTimerEvent *event)
74 {
75     qreal scaleFactor = currentScale();
76
77 //    qDebug() << __PRETTY_FUNCTION__
78 //             << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
79 //             << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
80
81     if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
82         scaleFactor = m_zoomTargetScale;
83         killTimer(event->timerId());
84     }
85     else {
86         scaleFactor += m_zoomScaleDelta;
87     }
88
89 //    qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
90 //                                    << "m_zoomScaleDelta" << m_zoomScaleDelta
91 //                                    << "scaleFactor:" << scaleFactor;
92
93     QTransform transform;
94     transform.scale(scaleFactor, scaleFactor);
95     setTransform(transform);
96 }
97
98 qreal MapView::currentScale()
99 {
100     QTransform currentTransform = transform();
101     return currentTransform.m11();
102 }
103
104 void MapView::scrollContentsBy (int dx, int dy)
105 {
106 //    qDebug() << __PRETTY_FUNCTION__;
107
108     QGraphicsView::scrollContentsBy(dx, dy);
109
110     QPoint centerInScene = mapToScene(frameRect().center()).toPoint();
111 //    qDebug() << __PRETTY_FUNCTION__ << "centerInScene:" << centerInScene;
112     emit viewScrolled(centerInScene);
113 }
114
115 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
116 {
117 //    qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
118     centerOn(sceneCoordinate);
119 }
120
121 void MapView::resizeEvent(QResizeEvent *event)
122 {
123     //qDebug() << "Resize event: " << event->size();
124     emit viewResized(event->size());
125 }