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);
50 void MapView::setZoomLevel(int zoomLevel)
52 m_zoomTargetScale = pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL);
53 m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * (ZOOM_TIME / 1000));
58 m_timerID = startTimer(1000/ZOOM_FPS);
61 void MapView::timerEvent(QTimerEvent *event)
63 if (event->timerId() == m_timerID) {
64 qreal scaleFactor = currentScale();
66 // qDebug() << __PRETTY_FUNCTION__
67 // << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
68 // << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
70 if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
71 scaleFactor = m_zoomTargetScale;
72 killTimer(event->timerId());
75 scaleFactor += m_zoomScaleDelta;
78 // qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
79 // << "m_zoomScaleDelta" << m_zoomScaleDelta
80 // << "scaleFactor:" << scaleFactor;
83 transform.scale(scaleFactor, scaleFactor);
84 emit scalingFactorChanged(scaleFactor);
85 setTransform(transform);
89 qreal MapView::currentScale()
91 QTransform currentTransform = transform();
92 return currentTransform.m11();
95 void MapView::mouseMoveEvent(QMouseEvent *event)
97 m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
99 emit viewScrolled(m_scenePosition);
100 //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
102 m_mousePosition = mapToScene(event->pos()).toPoint();
105 void MapView::mousePressEvent(QMouseEvent *event)
107 m_mousePosition = mapToScene(event->pos()).toPoint();
108 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
112 void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
114 // qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
115 centerOn(sceneCoordinate);
118 void MapView::resizeEvent(QResizeEvent *event)
120 //qDebug() << "Resize event: " << event->size();
121 emit viewResized(event->size());