fixed review issues
[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()), this, SIGNAL(viewZoomFinished()));
41 }
42
43 void MapView::setZoomLevel(int zoomLevel)
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     if (m_zoomAnimation) {
48         m_zoomAnimation->stop();
49         m_zoomAnimation->setDuration(ZOOM_TIME);
50         m_zoomAnimation->setStartValue(viewScale());
51         m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
52
53         m_zoomAnimation->start();
54     }
55 }
56
57 qreal MapView::viewScale()
58 {
59     qDebug() << __PRETTY_FUNCTION__;
60
61     return transform().m11();
62 }
63
64 void MapView::setViewScale(qreal viewScale)
65 {
66     qDebug() << __PRETTY_FUNCTION__;
67
68     QTransform transform;
69     transform.scale(viewScale, viewScale);
70     setTransform(transform);
71     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
72 }
73
74 void MapView::mouseMoveEvent(QMouseEvent *event)
75 {
76     m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
77
78     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
79
80     emit viewScrolled(m_scenePosition);
81
82     m_mousePosition = mapToScene(event->pos()).toPoint();
83     emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
84 }
85
86 void MapView::mousePressEvent(QMouseEvent *event)
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89
90     QGraphicsView::mousePressEvent(event); // Temporary solution
91
92     m_mousePosition = mapToScene(event->pos()).toPoint();
93     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
94 }
95
96 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
97 {
98     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
99
100     centerOn(sceneCoordinate);
101 }
102
103 void MapView::resizeEvent(QResizeEvent *event)
104 {
105     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
106
107     emit viewResized(event->size());
108     emit viewResizedNewSize(viewport()->width(), viewport()->height());
109     //emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
110 }
111
112 void MapView::showEvent(QShowEvent *event)
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     QGraphicsView::showEvent(event);
117     //emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
118 }
119
120 void MapView::updateViewPortContent()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     QPoint topLeft = mapToScene(viewport()->contentsRect().topLeft()).toPoint();
125     QPoint bottomRight = mapToScene(viewport()->contentsRect().bottomRight()).toPoint();
126     //emit viewContentChanged(QRect(topLeft, bottomRight));
127 }
128
129 QRect MapView::viewportContent()
130 {
131     QPoint topLeft = mapToScene(viewport()->contentsRect().topLeft()).toPoint();
132     QPoint bottomRight = mapToScene(viewport()->contentsRect().bottomRight()).toPoint();   
133     emit updateViewContent(QRect(topLeft, bottomRight));
134
135     return QRect(topLeft, bottomRight);
136 }
137