#include <QGLWidget>
#endif // Q_WS_MAEMO_5
-#ifdef Q_WS_MAEMO_5
- #include <QAbstractKineticScroller>
-#endif // Q_WS_MAEMO_5
-
-#include "common.h"
+#include "mapcommon.h"
#include "mapview.h"
-MapView::MapView(QWidget *parent) : QGraphicsView(parent)
+MapView::MapView(QWidget *parent)
+ : QGraphicsView(parent)
+ , m_timerID(NULL)
{
/**
* Use OpenGL for desktop to gain some performance in map view.
* OpenGL can't be used in scratchbox.
*/
#ifndef Q_WS_MAEMO_5
- setViewport(new QGLWidget);
+ setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer)));
#endif // !Q_WS_MAEMO_5
-/**
- * Use kinetic scrolling for Maemo5 and QGraphicsViews drag mode
- * ScrollHandDrag for other environments
- */
-#ifdef Q_WS_MAEMO_5
- QAbstractKineticScroller *scroller = property("kineticScroller")
- .value<QAbstractKineticScroller *>();
- if (scroller)
- scroller->setEnabled(true);
-#else
- setDragMode(QGraphicsView::ScrollHandDrag);
-#endif // Q_WS_MAEMO_5
-
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void MapView::setZoomLevel(int zoomLevel)
{
m_zoomTargetScale = pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL);
- m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * ZOOM_TIME);
+ m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * (ZOOM_TIME / 1000));
+
+ if (m_timerID)
+ killTimer(m_timerID);
- startTimer(1000/ZOOM_FPS);
+ m_timerID = startTimer(1000/ZOOM_FPS);
}
void MapView::timerEvent(QTimerEvent *event)
{
- qreal scaleFactor = currentScale();
-
-// qDebug() << __PRETTY_FUNCTION__
-// << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
-// << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
-
- if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
- scaleFactor = m_zoomTargetScale;
- killTimer(event->timerId());
+ if (event->timerId() == m_timerID) {
+ bool finished = false;
+ qreal scaleFactor = currentScale();
+
+// qDebug() << __PRETTY_FUNCTION__
+// << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
+// << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
+
+ if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
+ scaleFactor = m_zoomTargetScale;
+ killTimer(event->timerId());
+ finished = true;
+ }
+ else {
+ scaleFactor += m_zoomScaleDelta;
+ }
+
+// qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
+// << "m_zoomScaleDelta" << m_zoomScaleDelta
+// << "scaleFactor:" << scaleFactor;
+
+ QTransform transform;
+ transform.scale(scaleFactor, scaleFactor);
+ setTransform(transform);
+ emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
+
+ if (finished && m_zoomScaleDelta > 0)
+ emit viewZoomInFinished();
}
- else {
- scaleFactor += m_zoomScaleDelta;
- }
-
-// qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
-// << "m_zoomScaleDelta" << m_zoomScaleDelta
-// << "scaleFactor:" << scaleFactor;
-
- QTransform transform;
- transform.scale(scaleFactor, scaleFactor);
- setTransform(transform);
}
qreal MapView::currentScale()
return currentTransform.m11();
}
-void MapView::scrollContentsBy (int dx, int dy)
+void MapView::mouseMoveEvent(QMouseEvent *event)
+{
+ m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
+
+ emit viewScrolled(m_scenePosition);
+// qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
+
+ m_mousePosition = mapToScene(event->pos()).toPoint();
+ emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
+}
+
+void MapView::mousePressEvent(QMouseEvent *event)
{
- qDebug() << __PRETTY_FUNCTION__ << "dx:" << dx << "dy:" << dy;
+ QGraphicsView::mousePressEvent(event);
- QGraphicsView::scrollContentsBy(dx, dy);
+ m_mousePosition = mapToScene(event->pos()).toPoint();
+ m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
}
-//void MapView::mouseMoveEvent(QMouseEvent *event)
-//{
-// m_scenePosition += m_mousePosition - mapToScene(event->pos());
-//
-// emit viewScrolled(m_scenePosition);
-// //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
-//
-// m_mousePosition = mapToScene(event->pos());
-//}
-//
-//void MapView::mousePressEvent(QMouseEvent *event)
-//{
-// m_mousePosition = mapToScene(event->pos());
-// m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1);
-//}
-
-
-void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
+void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
{
- //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
+// qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
centerOn(sceneCoordinate);
}
void MapView::resizeEvent(QResizeEvent *event)
{
- qDebug() << "Resize event: " << event->size();
+// qDebug() << "Resize event: " << event->size();
emit viewResized(event->size());
+ emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
}