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"
29 #include "mapscroller.h"
32 MapView::MapView(QWidget *parent)
33 : QGraphicsView(parent)
35 qDebug() << __PRETTY_FUNCTION__;
37 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
38 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40 m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
41 connect(m_zoomAnimation, SIGNAL(finished()),
42 this, SIGNAL(viewZoomFinished()));
44 setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
46 m_scroller = &MapScroller::getInstance();
49 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
51 qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
53 centerOn(sceneCoordinate);
56 void MapView::mouseMoveEvent(QMouseEvent *event)
58 m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
60 qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
62 m_dragViewSpeed = m_mouseLastViewPosition - event->pos() ;
63 // qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y();
65 emit viewScrolled(m_scenePosition);
67 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
68 m_mouseLastViewPosition = event->pos();
71 void MapView::mousePressEvent(QMouseEvent *event)
73 qDebug() << __PRETTY_FUNCTION__;
77 QGraphicsView::mousePressEvent(event);
79 m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
80 m_mouseLastViewPosition = event->pos();
81 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
83 m_dragViewSpeed = QPoint();
86 void MapView::mouseReleaseEvent(QMouseEvent *event)
88 // qWarning() << __PRETTY_FUNCTION__;
90 QGraphicsView::mouseReleaseEvent(event);
92 QPoint dragSceneSpeed = m_dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
94 m_scroller->setStartValue(m_scenePosition);
95 m_scroller->setEndValue(m_scenePosition + dragSceneSpeed * 5);
96 m_scroller->setDuration(1000);
100 void MapView::resizeEvent(QResizeEvent *event)
102 qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
104 emit viewResized(event->size());
107 void MapView::setViewScale(qreal viewScale)
109 qDebug() << __PRETTY_FUNCTION__;
111 QTransform transform;
112 transform.scale(viewScale, viewScale);
113 setTransform(transform);
116 void MapView::setZoomLevel(int zoomLevel)
118 qDebug() << __PRETTY_FUNCTION__;
120 m_zoomLevel = zoomLevel;
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();