Fixed (yet) another merge screw-up *sigh*
[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     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
34     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
35
36     m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
37     connect(m_zoomAnimation, SIGNAL(finished()), this, SIGNAL(viewZoomFinished()));
38 }
39
40 void MapView::setZoomLevel(int zoomLevel)
41 {
42     m_zoomAnimation->stop();
43     m_zoomAnimation->setDuration(ZOOM_TIME);
44     m_zoomAnimation->setStartValue(viewScale());
45     m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
46
47     m_zoomAnimation->start();
48 }
49
50 qreal MapView::currentScale()
51 {
52     QTransform currentTransform = transform();
53     return currentTransform.m11();
54 }
55
56 qreal MapView::viewScale()
57 {
58     QTransform currentTransform = transform();
59     return currentTransform.m11();
60 }
61
62 void MapView::setViewScale(qreal viewScale)
63 {
64     QTransform transform;
65     transform.scale(viewScale, viewScale);
66     setTransform(transform);
67     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
68 }
69
70 void MapView::mouseMoveEvent(QMouseEvent *event)
71 {
72     m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
73
74     emit viewScrolled(m_scenePosition);
75     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
76
77     m_mousePosition = mapToScene(event->pos()).toPoint();
78     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
79 }
80
81 void MapView::mousePressEvent(QMouseEvent *event)
82 {
83     QGraphicsView::mousePressEvent(event);
84
85     m_mousePosition = mapToScene(event->pos()).toPoint();
86     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
87 }
88
89 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
90 {
91     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
92     centerOn(sceneCoordinate);
93 }
94
95 void MapView::resizeEvent(QResizeEvent *event)
96 {
97     qDebug() << __PRETTY_FUNCTION__ << event->size();
98     emit viewResized(event->size());
99     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
100 }