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