2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
7 Situare is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
11 Situare is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Situare; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include <QMouseEvent>
29 #endif // Q_WS_MAEMO_5
31 #include "mapcommon.h"
34 MapView::MapView(QWidget *parent)
35 : QGraphicsView(parent)
39 * Use OpenGL for desktop to gain some performance in map view.
40 * OpenGL can't be used in scratchbox.
43 setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer)));
44 #endif // !Q_WS_MAEMO_5
46 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
49 m_zoomAnimation = new QPropertyAnimation(this, "viewScale");
50 connect(m_zoomAnimation, SIGNAL(finished()), this, SIGNAL(viewZoomFinished()));
53 void MapView::setZoomLevel(int zoomLevel)
55 // m_zoomTargetScale = pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL);
56 // m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * (ZOOM_TIME / 1000));
59 // killTimer(m_timerID);
61 // m_timerID = startTimer(1000/ZOOM_FPS);
63 m_zoomAnimation->stop();
64 m_zoomAnimation->setDuration(ZOOM_TIME);
65 m_zoomAnimation->setStartValue(viewScale());
66 m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
68 m_zoomAnimation->start();
71 void MapView::timerEvent(QTimerEvent *event)
73 if (event->timerId() == m_timerID) {
74 bool finished = false;
75 qreal scaleFactor = currentScale();
77 // qDebug() << __PRETTY_FUNCTION__
78 // << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
79 // << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
81 if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
82 scaleFactor = m_zoomTargetScale;
83 killTimer(event->timerId());
87 scaleFactor += m_zoomScaleDelta;
90 // qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
91 // << "m_zoomScaleDelta" << m_zoomScaleDelta
92 // << "scaleFactor:" << scaleFactor;
95 transform.scale(scaleFactor, scaleFactor);
96 setTransform(transform);
97 emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
99 if (finished && m_zoomScaleDelta > 0)
100 emit viewZoomFinished();
104 qreal MapView::currentScale()
106 QTransform currentTransform = transform();
107 return currentTransform.m11();
110 qreal MapView::viewScale()
112 QTransform currentTransform = transform();
113 return currentTransform.m11();
116 void MapView::setViewScale(qreal viewScale)
118 QTransform transform;
119 transform.scale(viewScale, viewScale);
120 setTransform(transform);
121 emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
124 void MapView::mouseMoveEvent(QMouseEvent *event)
126 m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
128 emit viewScrolled(m_scenePosition);
129 // qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
131 m_mousePosition = mapToScene(event->pos()).toPoint();
132 emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
135 void MapView::mousePressEvent(QMouseEvent *event)
137 QGraphicsView::mousePressEvent(event);
139 m_mousePosition = mapToScene(event->pos()).toPoint();
140 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
143 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
145 // qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
146 centerOn(sceneCoordinate);
149 void MapView::resizeEvent(QResizeEvent *event)
151 // qDebug() << "Resize event: " << event->size();
152 emit viewResized(event->size());
153 emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());