Modified MapView::mouseDoubleClickEvent.
[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     connect(m_zoomAnimation, SIGNAL(finished()),
43             this, SLOT(disableAnchor()));
44
45     setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
46 }
47
48 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
49 {
50     qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
51
52     centerOn(sceneCoordinate);
53 }
54
55 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
56 {
57     qWarning() << __PRETTY_FUNCTION__ << event->pos().x() << event->pos().y();
58     qWarning() << __PRETTY_FUNCTION__ << mapToScene(event->pos()).toPoint().x() <<
59             mapToScene(event->pos()).toPoint().y();
60
61     QPoint centerPosition = mapToScene(QPoint(width() / 2, height() / 2)).toPoint();
62     QPoint pressPosition = mapToScene(event->pos()).toPoint();
63     QPoint scenePosition = centerPosition - ((centerPosition - pressPosition) / 2);
64
65     setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
66     emit zoomIn();
67 //    emit viewScrolled(scenePosition);
68 //    setTransformationAnchor(QGraphicsView::NoAnchor);
69 }
70
71 void MapView::mouseMoveEvent(QMouseEvent *event)
72 {
73     m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
74
75     qWarning() << __PRETTY_FUNCTION__;
76
77     qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
78
79     emit viewScrolled(m_scenePosition);
80
81     m_mousePosition = mapToScene(event->pos()).toPoint();
82 }
83
84 void MapView::mousePressEvent(QMouseEvent *event)
85 {
86     qWarning() << __PRETTY_FUNCTION__;
87     qDebug() << __PRETTY_FUNCTION__;
88
89     QGraphicsView::mousePressEvent(event);
90
91     m_mousePosition = mapToScene(event->pos()).toPoint();
92     m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
93 }
94
95 void MapView::mouseReleaseEvent(QMouseEvent *event)
96 {
97     qWarning() << __PRETTY_FUNCTION__;
98
99     QGraphicsView::mouseReleaseEvent(event);
100 }
101
102 void MapView::resizeEvent(QResizeEvent *event)
103 {
104     qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
105
106     emit viewResized(event->size());
107 }
108
109 void MapView::setViewScale(qreal viewScale)
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     QTransform transform;
114     transform.scale(viewScale, viewScale);
115     setTransform(transform);
116 }
117
118 void MapView::setZoomLevel(int zoomLevel)
119 {
120     qDebug() << __PRETTY_FUNCTION__;
121
122     if (m_zoomAnimation) {
123         m_zoomAnimation->stop();
124         m_zoomAnimation->setDuration(ZOOM_TIME);
125         m_zoomAnimation->setStartValue(viewScale());
126         m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
127
128         m_zoomAnimation->start();
129     }
130 }
131
132 qreal MapView::viewScale()
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     return transform().m11();
137 }
138
139 void MapView::disableAnchor()
140 {
141     qWarning() << __PRETTY_FUNCTION__;
142
143     QCoreApplication::postEvent(this, new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
144                                                       Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
145
146 //    QGraphicsView::mouseReleaseEvent(new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
147 //                                                     Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
148     setTransformationAnchor(QGraphicsView::AnchorViewCenter);
149 }