Copyright (C) 2010 Ixonos Plc. Authors:
Sami Rämö - sami.ramo@ixonos.com
+ Pekka Nissinen - pekka.nissinen@ixonos.com
Situare is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
#include <QDebug>
#include <QMouseEvent>
+#include <QParallelAnimationGroup>
-#ifndef Q_WS_MAEMO_5
- #include <QGLWidget>
-#endif // Q_WS_MAEMO_5
+#include "coordinates/scenecoordinate.h"
+#include "mapcommon.h"
+#include "mapscroller.h"
+#include "ui/panelcommon.h"
-#include "common.h"
#include "mapview.h"
-MapView::MapView(QWidget *parent) : QGraphicsView(parent)
+const qreal MS_PER_S = 1000;
+
+// const values for tuning the kinetic scroll effect
+const int KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS = 30;
+const int KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS = 100;
+const qreal KINETIC_MAX_VIEW_DISTANCE_FACTOR = 0.8;
+const int KINETIC_SCROLL_TIME_MS = 750;
+const qreal KINETIC_SPEED_TO_DISTANCE_FACTOR = 0.15 * sqrt(KINETIC_SCROLL_TIME_MS / MS_PER_S);
+
+const qreal ZOOM_TIME_MS = 350; ///< Length of the zoom effect (ms)
+
+MapView::MapView(QWidget *parent)
+ : QGraphicsView(parent),
+ m_doubleTapZoomRunning(false)
{
-#ifndef Q_WS_MAEMO_5
- // use opengl for desktop to gain some performance in map view
- // opengl can't be used in scrathbox
- setViewport(new QGLWidget);
- this->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
-#endif // Q_WS_MAEMO_5
+ qDebug() << __PRETTY_FUNCTION__;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+ m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
+ connect(m_zoomAnimation, SIGNAL(finished()),
+ this, SIGNAL(viewZoomFinished()));
+ setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);
+
+ m_scroller = &MapScroller::getInstance();
+
+ m_scrollAndZoomAnimation = new QParallelAnimationGroup();
+ m_scrollAndZoomAnimation->addAnimation(m_scroller);
+ m_scrollAndZoomAnimation->addAnimation(m_zoomAnimation);
+ connect(m_scrollAndZoomAnimation, SIGNAL(finished()),
+ this, SLOT(doubleTapZoomFinished()));
+
+ m_centerShiftAnimation = new QPropertyAnimation(this, "viewShift", this);
+ if (m_centerShiftAnimation) {
+ m_centerShiftAnimation->setStartValue(0.0);
+ m_centerShiftAnimation->setDuration(ZOOM_TIME_MS);
+ }
}
-void MapView::setZoomLevel(int zoomLevel)
+MapView::~MapView()
{
- m_zoomTargetScale = pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL);
- m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * ZOOM_TIME);
+ qDebug() << __PRETTY_FUNCTION__;
- startTimer(1000/ZOOM_FPS);
+ m_scrollAndZoomAnimation->removeAnimation(m_scroller);
+ delete m_scrollAndZoomAnimation;
}
-void MapView::timerEvent(QTimerEvent *event)
+QPointF MapView::center() const
{
- qreal scaleFactor = currentScale();
+ return mapToScene(m_viewCenterPoint) - m_centerHorizontalShiftPoint;
+}
-// qDebug() << __PRETTY_FUNCTION__
-// << "abs(m_zoomTargetScale - scaleFactor)" << fabs(m_zoomTargetScale - scaleFactor)
-// << "abs(m_zoomScaleDelta)" << fabs(m_zoomScaleDelta);
+void MapView::centerToSceneCoordinates(const SceneCoordinate &coordinate)
+{
+ qDebug() << __PRETTY_FUNCTION__ << "coordinate" << coordinate;
- if (fabs(m_zoomTargetScale - scaleFactor) <= fabs(m_zoomScaleDelta)) {
- scaleFactor = m_zoomTargetScale;
- killTimer(event->timerId());
- }
- else {
- scaleFactor += m_zoomScaleDelta;
+ QPointF target = coordinate.toPointF();
+ m_lastSetScenePosition = coordinate;
+
+ target += m_centerHorizontalShiftPoint;
+
+ centerOn(target);
+}
+
+void MapView::disableCenterShift()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_centerShiftAnimation) {
+ m_centerShiftAnimation->setDirection(QAbstractAnimation::Backward);
+ m_centerShiftAnimation->start();
}
+}
-// qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
-// << "m_zoomScaleDelta" << m_zoomScaleDelta
-// << "scaleFactor:" << scaleFactor;
+void MapView::doubleTapZoomFinished()
+{
+ qDebug() << __PRETTY_FUNCTION__;
- QTransform transform;
- transform.scale(scaleFactor, scaleFactor);
- setTransform(transform);
+ m_doubleTapZoomRunning = false;
+ emit zoomIn();
}
-qreal MapView::currentScale()
+void MapView::enableCenterShift()
{
- QTransform currentTransform = transform();
- return currentTransform.m11();
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_centerShiftAnimation) {
+ m_centerShiftAnimation->setDirection(QAbstractAnimation::Forward);
+ m_centerShiftAnimation->start();
+ }
+}
+
+void MapView::mouseDoubleClickEvent(QMouseEvent *event)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_zoomLevel + 1 <= OSM_MAX_ZOOM_LEVEL) {
+ QPoint pressPosition = mapToScene(event->pos()).toPoint();
+ QPoint viewCenterPosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
+ QPoint zoomPosition = viewCenterPosition - ((viewCenterPosition - pressPosition) / 2)
+ - m_centerHorizontalShiftPoint.toPoint() / 2;
+
+ m_scrollAndZoomAnimation->stop();
+ m_doubleTapZoomRunning = true;
+
+ m_scroller->setEasingCurve(QEasingCurve::Linear);
+ m_scroller->setDuration(ZOOM_TIME_MS);
+ QPointF centerPoint = center();
+ m_scroller->setStartValue(SceneCoordinate(centerPoint.x(), centerPoint.y()));
+ m_scroller->setEndValue(SceneCoordinate(zoomPosition.x(), zoomPosition.y()));
+
+ m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
+ m_zoomAnimation->setDuration(ZOOM_TIME_MS);
+ m_zoomAnimation->setStartValue(viewScale());
+ m_zoomAnimation->setEndValue(pow(2, m_zoomLevel + 1 - OSM_MAX_ZOOM_LEVEL));
+
+ m_scrollAndZoomAnimation->start();
+ }
}
void MapView::mouseMoveEvent(QMouseEvent *event)
{
- m_scenePosition += m_mousePosition - mapToScene(event->pos());
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_doubleTapZoomRunning)
+ return;
+
+ m_internalScenePosition += m_lastMouseEventScenePosition - mapToScene(event->pos()).toPoint();
+
+ if (m_index >= VALUES)
+ m_index = 0;
- emit viewScrolled(m_scenePosition);
- //qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition" << m_scenePosition;
+ m_dragMovement[m_index] = m_lastMouseEventViewPosition - event->pos();
+ m_dragTime[m_index] = m_time.elapsed();
+ m_time.start();
+ m_index++;
- m_mousePosition = mapToScene(event->pos());
+ QPointF viewCenterPoint = m_internalScenePosition - m_centerHorizontalShiftPoint;
+
+ emit viewScrolled(SceneCoordinate(viewCenterPoint.x(), viewCenterPoint.y()));
+
+ m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
+ m_lastMouseEventViewPosition = event->pos();
}
void MapView::mousePressEvent(QMouseEvent *event)
{
- m_mousePosition = mapToScene(event->pos());
- m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1);
-}
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_doubleTapZoomRunning)
+ return;
+
+ m_time.start();
+
+ m_scroller->stop();
+ QGraphicsView::mousePressEvent(event);
-void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
+ m_lastMouseEventScenePosition = mapToScene(event->pos()).toPoint();
+ m_lastMouseEventViewPosition = event->pos();
+ m_internalScenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
+
+ for (int i = 0; i < VALUES; i++) {
+ m_dragMovement[i] = QPoint();
+ m_dragTime[i] = 0;
+ }
+ m_index = 0;
+}
+
+void MapView::mouseReleaseEvent(QMouseEvent *event)
{
- //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
- centerOn(sceneCoordinate);
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_doubleTapZoomRunning)
+ return;
+
+ int elapsed = m_time.elapsed();
+
+ QGraphicsView::mouseReleaseEvent(event);
+
+ // start kinetic scroll only if there isn't too much time elapsed from the last mouse move event
+ if (elapsed <= KINETIC_MAX_TIME_FROM_LAST_MOUSE_EVENT_MS) {
+ QPointF dragViewSpeed;
+ int dragLength = 0;
+ int values = 0;
+ for (int i = 0; i < VALUES; i++) {
+ if (m_dragTime[i] > 0) {
+ dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
+ dragLength += m_dragMovement[i].manhattanLength();
+ values++;
+ }
+ }
+
+ if (dragLength >= KINETIC_MIN_DRAG_LENGTH_VIEW_PIXELS) {
+ dragViewSpeed /= values;
+ QPointF effectViewDistance = dragViewSpeed * KINETIC_SPEED_TO_DISTANCE_FACTOR;
+
+ // limit the scroll distance in screen pixels
+ qreal biggerDistance = qMax(abs(effectViewDistance.x()), abs(effectViewDistance.y()));
+ if (biggerDistance > m_kineticMaxViewDistance)
+ effectViewDistance /= biggerDistance / m_kineticMaxViewDistance;
+
+ QPointF effectSceneDistance = effectViewDistance
+ * (1 << (OSM_MAX_ZOOM_LEVEL - m_zoomLevel));
+
+ m_scroller->setEasingCurve(QEasingCurve::OutCirc);
+ m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
+ QPointF centerPoint = center();
+ m_scroller->setStartValue(SceneCoordinate(centerPoint.x(), centerPoint.y()));
+ QPointF endValue = centerPoint + effectSceneDistance;
+ m_scroller->setEndValue(SceneCoordinate(endValue.x(), endValue.y()));
+ m_scroller->start();
+ }
+ }
}
void MapView::resizeEvent(QResizeEvent *event)
{
- qDebug() << "Resize event: " << event->size();
+ qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
+
+ m_kineticMaxViewDistance = qMax(width(), height()) * KINETIC_MAX_VIEW_DISTANCE_FACTOR;
+
+ m_viewCenterPoint.setX(event->size().width() / 2);
+ m_viewCenterPoint.setY(event->size().height() / 2);
+
emit viewResized(event->size());
+
+ if (m_centerShiftAnimation) {
+ int mapVisibleWidth = event->size().width() - PANEL_WIDTH - PANEL_BAR_WIDTH;
+ int shiftFromMiddle = m_viewCenterPoint.x() - (mapVisibleWidth / 2);
+ m_centerShiftAnimation->setEndValue(shiftFromMiddle);
+ updateCenterShift();
+ }
+}
+
+void MapView::setViewScale(qreal viewScale)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QTransform transform;
+ transform.scale(viewScale, viewScale);
+ setTransform(transform);
+
+ updateCenterShift();
+}
+
+void MapView::setViewShift(qreal viewShift)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_centerHorizontalShiftViewPixels = viewShift;
+ emit horizontalShiftingChanged(m_centerHorizontalShiftViewPixels);
+
+ updateCenterShift();
+}
+
+void MapView::setZoomLevel(int zoomLevel)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_zoomLevel = zoomLevel;
+
+ if (m_zoomAnimation) {
+ m_zoomAnimation->stop();
+ m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
+ m_zoomAnimation->setDuration(ZOOM_TIME_MS);
+ m_zoomAnimation->setStartValue(viewScale());
+ m_zoomAnimation->setEndValue(pow(2, zoomLevel - OSM_MAX_ZOOM_LEVEL));
+
+ m_zoomAnimation->start();
+ }
+}
+
+void MapView::updateCenterShift()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_centerHorizontalShiftPoint = QPointF(m_centerHorizontalShiftViewPixels * (1.0 / viewScale()),
+ 0);
+
+ centerToSceneCoordinates(m_lastSetScenePosition);
+}
+
+qreal MapView::viewScale() const
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ return transform().m11();
+}
+
+qreal MapView::viewShift() const
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ return m_centerHorizontalShiftViewPixels;
}