Added OpenStreetMaps license text feature
[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 #include "mapcommon.h"
28 #include "mapview.h"
29
30 MapView::MapView(QWidget *parent)
31     : QGraphicsView(parent)
32 {
33     qDebug() << __PRETTY_FUNCTION__;
34
35     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
36     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
37
38     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
39     connect(m_zoomAnimation, SIGNAL(finished()), this, SIGNAL(viewZoomFinished()));
40 }
41
42 void MapView::setZoomLevel(int zoomLevel)
43 {
44     qDebug() << __PRETTY_FUNCTION__;
45
46     if (m_zoomAnimation) {
47         m_zoomAnimation->stop();
48         m_zoomAnimation->setDuration(ZOOM_TIME);
49         m_zoomAnimation->setStartValue(viewScale());
50         m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
51
52         m_zoomAnimation->start();
53     }
54 }
55
56 qreal MapView::viewScale()
57 {
58     qDebug() << __PRETTY_FUNCTION__;
59
60     return transform().m11();
61 }
62
63 void MapView::setViewScale(qreal viewScale)
64 {
65     qDebug() << __PRETTY_FUNCTION__;
66
67     QTransform transform;
68     transform.scale(viewScale, viewScale);
69     setTransform(transform);
70     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
71 }
72
73 void MapView::mouseMoveEvent(QMouseEvent *event)
74 {
75     m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
76
77     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
78
79     emit viewScrolled(m_scenePosition);
80
81     m_mousePosition = mapToScene(event->pos()).toPoint();
82     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
83 }
84
85 void MapView::mousePressEvent(QMouseEvent *event)
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     QGraphicsView::mousePressEvent(event); // Temporary solution
90
91     m_mousePosition = mapToScene(event->pos()).toPoint();
92     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
93 }
94
95 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
96 {
97     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
98
99     centerOn(sceneCoordinate);
100 }
101
102 void MapView::resizeEvent(QResizeEvent *event)
103 {
104     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
105
106     emit viewResized(event->size());
107     emit viewResizedNewSize(viewport()->width(), viewport()->height());
108     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
109 }
110
111 void MapView::showEvent(QShowEvent *event)
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     QGraphicsView::showEvent(event);
116     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
117 }