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