2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
6 Pekka Nissinen - pekka.nissinen@ixonos.com
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.
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.
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,
26 #include <QMouseEvent>
28 #include "mapcommon.h"
31 MapView::MapView(QWidget *parent)
32 : QGraphicsView(parent)
34 qDebug() << __PRETTY_FUNCTION__;
36 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
37 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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()));
45 setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
48 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
50 qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
52 centerOn(sceneCoordinate);
55 void MapView::mouseDoubleClickEvent(QMouseEvent *event)
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();
61 QPoint centerPosition = mapToScene(QPoint(width() / 2, height() / 2)).toPoint();
62 QPoint pressPosition = mapToScene(event->pos()).toPoint();
63 QPoint scenePosition = centerPosition - ((centerPosition - pressPosition) / 2);
65 setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
67 // emit viewScrolled(scenePosition);
68 // setTransformationAnchor(QGraphicsView::NoAnchor);
71 void MapView::mouseMoveEvent(QMouseEvent *event)
73 m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
75 qWarning() << __PRETTY_FUNCTION__;
77 qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
79 emit viewScrolled(m_scenePosition);
81 m_mousePosition = mapToScene(event->pos()).toPoint();
84 void MapView::mousePressEvent(QMouseEvent *event)
86 qWarning() << __PRETTY_FUNCTION__;
87 qDebug() << __PRETTY_FUNCTION__;
89 QGraphicsView::mousePressEvent(event);
91 m_mousePosition = mapToScene(event->pos()).toPoint();
92 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
95 void MapView::mouseReleaseEvent(QMouseEvent *event)
97 qWarning() << __PRETTY_FUNCTION__;
99 QGraphicsView::mouseReleaseEvent(event);
102 void MapView::resizeEvent(QResizeEvent *event)
104 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
106 emit viewResized(event->size());
109 void MapView::setViewScale(qreal viewScale)
111 qDebug() << __PRETTY_FUNCTION__;
113 QTransform transform;
114 transform.scale(viewScale, viewScale);
115 setTransform(transform);
118 void MapView::setZoomLevel(int zoomLevel)
120 qDebug() << __PRETTY_FUNCTION__;
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));
128 m_zoomAnimation->start();
132 qreal MapView::viewScale()
134 qDebug() << __PRETTY_FUNCTION__;
136 return transform().m11();
139 void MapView::disableAnchor()
141 qWarning() << __PRETTY_FUNCTION__;
143 QCoreApplication::postEvent(this, new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
144 Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
146 // QGraphicsView::mouseReleaseEvent(new QMouseEvent(QEvent::MouseButtonRelease, QPoint(),
147 // Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
148 setTransformationAnchor(QGraphicsView::AnchorViewCenter);