4cee084252070a6c000c03d724cafc93efb4936a
[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     m_zoomAnimation->stop();
47     m_zoomAnimation->setDuration(ZOOM_TIME);
48     m_zoomAnimation->setStartValue(viewScale());
49     m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
50
51     m_zoomAnimation->start();
52 }
53
54 qreal MapView::currentScale()
55 {
56     qDebug() << __PRETTY_FUNCTION__;
57
58     QTransform currentTransform = transform();
59     return currentTransform.m11();
60 }
61
62 qreal MapView::viewScale()
63 {
64     qDebug() << __PRETTY_FUNCTION__;
65
66     QTransform currentTransform = transform();
67     return currentTransform.m11();
68 }
69
70 void MapView::setViewScale(qreal viewScale)
71 {
72     qDebug() << __PRETTY_FUNCTION__;
73
74     QTransform transform;
75     transform.scale(viewScale, viewScale);
76     setTransform(transform);
77     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
78 }
79
80 void MapView::mouseMoveEvent(QMouseEvent *event)
81 {
82     m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
83
84     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
85
86     emit viewScrolled(m_scenePosition);
87
88     m_mousePosition = mapToScene(event->pos()).toPoint();
89     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
90 }
91
92 void MapView::mousePressEvent(QMouseEvent *event)
93 {
94     qDebug() << __PRETTY_FUNCTION__;
95
96     QGraphicsView::mousePressEvent(event);
97
98     m_mousePosition = mapToScene(event->pos()).toPoint();
99     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
100 }
101
102 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
103 {
104     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
105     centerOn(sceneCoordinate);
106 }
107
108 void MapView::resizeEvent(QResizeEvent *event)
109 {
110     qDebug() << __PRETTY_FUNCTION__ << event->size();
111     emit viewResized(event->size());
112     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
113 }