3af9c6deacbaeed8d883c209da22b0ae3943f8d2
[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        Pekka Nissinen - pekka.nissinen@ixonos.com
7
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #include <cmath>
24
25 #include <QDebug>
26 #include <QMouseEvent>
27
28 #include "mapcommon.h"
29 #include "mapview.h"
30
31 MapView::MapView(QWidget *parent)
32     : QGraphicsView(parent)
33 {
34     qDebug() << __PRETTY_FUNCTION__;
35
36     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
37     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
38
39     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
40     connect(m_zoomAnimation, SIGNAL(finished()),
41         this, SIGNAL(viewZoomFinished()));
42 }
43
44 void MapView::setZoomLevel(int zoomLevel)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48     if (m_zoomAnimation) {
49         m_zoomAnimation->stop();
50         m_zoomAnimation->setDuration(ZOOM_TIME);
51         m_zoomAnimation->setStartValue(viewScale());
52         m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
53
54         m_zoomAnimation->start();
55     }
56 }
57
58 qreal MapView::viewScale()
59 {
60     qDebug() << __PRETTY_FUNCTION__;
61
62     return transform().m11();
63 }
64
65 void MapView::setViewScale(qreal viewScale)
66 {
67     qDebug() << __PRETTY_FUNCTION__;
68
69     QTransform transform;
70     transform.scale(viewScale, viewScale);
71     setTransform(transform);
72     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
73 }
74
75 void MapView::mouseMoveEvent(QMouseEvent *event)
76 {
77     m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
78
79     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
80
81     emit viewScrolled(m_scenePosition);
82
83     m_mousePosition = mapToScene(event->pos()).toPoint();
84     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
85 }
86
87 void MapView::mousePressEvent(QMouseEvent *event)
88 {
89     qDebug() << __PRETTY_FUNCTION__;
90
91     QGraphicsView::mousePressEvent(event); // Temporary solution
92
93     m_mousePosition = mapToScene(event->pos()).toPoint();
94     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
95 }
96
97 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
98 {
99     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
100
101     centerOn(sceneCoordinate);
102 }
103
104 void MapView::resizeEvent(QResizeEvent *event)
105 {
106     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
107
108     emit viewResized(event->size());
109     emit viewResizedNewSize(viewport()->width(), viewport()->height());
110     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
111 }
112
113 void MapView::showEvent(QShowEvent *event)
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     QGraphicsView::showEvent(event);
118     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
119 }