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
32 #include <QAbstractKineticScroller>
33 #endif // Q_WS_MAEMO_5
38 MapView::MapView(QWidget *parent) : QGraphicsView(parent)
41 * Use OpenGL for desktop to gain some performance in map view.
42 * OpenGL can't be used in scratchbox.
45 setViewport(new QGLWidget);
46 #endif // !Q_WS_MAEMO_5
49 * Use kinetic scrolling for Maemo5 and QGraphicsViews drag mode
50 * ScrollHandDrag for other environments
53 QAbstractKineticScroller *scroller = property("kineticScroller")
54 .value<QAbstractKineticScroller *>();
56 scroller->setEnabled(true);
58 setDragMode(QGraphicsView::ScrollHandDrag);
59 #endif // Q_WS_MAEMO_5
61 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 void MapView::setZoomLevel(int zoomLevel)
67 m_zoomTargetScale = pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL);
68 m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * ZOOM_TIME);
70 startTimer(1000/ZOOM_FPS);
73 void MapView::timerEvent(QTimerEvent *event)
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());
86 scaleFactor += m_zoomScaleDelta;
89 // qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
90 // << "m_zoomScaleDelta" << m_zoomScaleDelta
91 // << "scaleFactor:" << scaleFactor;
94 transform.scale(scaleFactor, scaleFactor);
95 setTransform(transform);
98 qreal MapView::currentScale()
100 QTransform currentTransform = transform();
101 return currentTransform.m11();
104 //void MapView::scrollContentsBy (int dx, int dy)
106 // qDebug() << __PRETTY_FUNCTION__ << "dx:" << dx << "dy:" << dy;
108 // QGraphicsView::scrollContentsBy(dx, dy);
111 void MapView::mouseMoveEvent(QMouseEvent *event)
113 m_scenePosition += m_mousePosition - mapToScene(event->pos());
115 emit viewScrolled(m_scenePosition);
116 //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
118 m_mousePosition = mapToScene(event->pos());
121 void MapView::mousePressEvent(QMouseEvent *event)
123 m_mousePosition = mapToScene(event->pos());
124 m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1);
128 void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
130 //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
131 centerOn(sceneCoordinate);
134 void MapView::resizeEvent(QResizeEvent *event)
136 qDebug() << "Resize event: " << event->size();
137 emit viewResized(event->size());