Enabled zValues for MapTiles and OpenGL for desktop
[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 #include "common.h"
32 #include "mapview.h"
33
34 MapView::MapView(QWidget *parent) : QGraphicsView(parent)
35 {
36 #ifndef Q_WS_MAEMO_5
37     // use opengl for desktop to gain some performance in map view
38     // opengl can't be used in scrathbox
39     setViewport(new QGLWidget);
40 #endif // Q_WS_MAEMO_5
41
42     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 }
45
46 void MapView::setZoomLevel(int zoomLevel)
47 {
48     m_zoomTargetScale = pow(2, zoomLevel - MAX_ZOOM_LEVEL);
49     m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * ZOOM_TIME);
50
51     startTimer(1000/ZOOM_FPS);
52 }
53
54 void MapView::timerEvent(QTimerEvent *event)
55 {
56     qreal scaleFactor = currentScale();
57
58 //    qDebug() << __PRETTY_FUNCTION__
59 //             << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
60 //             << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
61
62     if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
63         scaleFactor = m_zoomTargetScale;
64         killTimer(event->timerId());
65     }
66     else {
67         scaleFactor += m_zoomScaleDelta;
68     }
69
70 //    qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
71 //                                    << "m_zoomScaleDelta" << m_zoomScaleDelta
72 //                                    << "scaleFactor:" << scaleFactor;
73
74     QTransform transform;
75     transform.scale(scaleFactor, scaleFactor);
76     setTransform(transform);
77 }
78
79 qreal MapView::currentScale()
80 {
81     QTransform currentTransform = transform();
82     return currentTransform.m11();
83 }
84
85 void MapView::mouseMoveEvent(QMouseEvent *event)
86 {
87     m_scenePosition += m_mousePosition - mapToScene(event->pos());
88
89     emit viewScrolled(m_scenePosition);
90     //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
91
92     m_mousePosition = mapToScene(event->pos());
93 }
94
95 void MapView::mousePressEvent(QMouseEvent *event)
96 {
97     m_mousePosition = mapToScene(event->pos());
98     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1);
99 }
100
101
102 void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
103 {
104     //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
105     centerOn(sceneCoordinate);
106 }
107
108 void MapView::resizeEvent(QResizeEvent *event)
109 {
110     qDebug() << "Resize event: " << event->size();
111     emit viewResized(event->size());
112 }