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