Changed deceleration curve and adjusted scroll distance calculation
[situare] / src / map / mapview.cpp
index 24c14ee..32e3554 100644 (file)
- /*
-    Situare - A location system for Facebook
-    Copyright (C) 2010  Ixonos Plc. Authors:
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
 
-        Sami Rämö - sami.ramo@ixonos.com
+       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
-    as published by the Free Software Foundation; either version 2
-    of the License, or (at your option) any later version.
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
 
-    Situare is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-    You should have received a copy of the GNU General Public License
-    along with Situare; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
-    USA.
- */
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
 
-#include <math.h>
+#include <cmath>
 
 #include <QDebug>
 #include <QMouseEvent>
 
+#include "mapcommon.h"
+#include "mapscroller.h"
 #include "mapview.h"
-#include "mapengine.h"
 
-MapView::MapView(QWidget *parent) : QGraphicsView(parent)
+const int KINETIC_SCROLL_TIME_MS = 1000;
+
+MapView::MapView(QWidget *parent)
+    : QGraphicsView(parent)
 {
+    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();
 }
 
-void MapView::setZoomLevel(int zoomLevel)
+void MapView::centerToSceneCoordinates(QPoint sceneCoordinate)
 {
-    double scaleFactor = pow(2, zoomLevel - MapEngine::MAX_ZOOM_LEVEL);
-    scale(scaleFactor, scaleFactor);
+    qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
+
+    centerOn(sceneCoordinate);
+}
+
+void MapView::mouseMoveEvent(QMouseEvent *event)
+{
+    m_scenePosition += m_mouseLastScenePosition - mapToScene(event->pos()).toPoint();
+
+    qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
+
+    if (m_index >= VALUES)
+        m_index = 0;
+
+    m_dragMovement[m_index] = m_mouseLastViewPosition - event->pos();
+    m_dragTime[m_index] = time.elapsed();
+    time.start();
+
+//    qWarning() << __PRETTY_FUNCTION__ << m_index << m_dragMovement[m_index].x() << m_dragMovement[m_index].y() << m_dragTime[m_index];
+
+    m_index++;
+
+//    qWarning() << __PRETTY_FUNCTION__ << m_dragViewSpeed.x() << m_dragViewSpeed.y();
+//    qWarning() << __PRETTY_FUNCTION__  << "elapsed:" << time.elapsed() << "ms";
+
+    emit viewScrolled(m_scenePosition);
+
+    m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
+    m_mouseLastViewPosition = event->pos();
 }
 
 void MapView::mousePressEvent(QMouseEvent *event)
 {
-    qDebug() << __PRETTY_FUNCTION__ << "scene coordinate:" << mapToScene(event->pos());
+    qDebug() << __PRETTY_FUNCTION__;
+
+    time.start();
+
+    m_scroller->stop();
+
+    QGraphicsView::mousePressEvent(event);
+
+    m_mouseLastScenePosition = mapToScene(event->pos()).toPoint();
+    m_mouseLastViewPosition = event->pos();
+    m_scenePosition = 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)
+{
+//    qWarning() << __PRETTY_FUNCTION__;
+
+    const qreal MS_PER_S = 1000;
+    const qreal SPEED_TO_DISTANCE_FACTOR = KINETIC_SCROLL_TIME_MS / MS_PER_S * 0.10;
+
+    QGraphicsView::mouseReleaseEvent(event);
+
+    QPointF dragViewSpeed;
+    int values = 0;
+    for (int i = 0; i < VALUES; i++) {
+        if (m_dragTime > 0) {
+            dragViewSpeed += m_dragMovement[i] / (m_dragTime[i] / MS_PER_S);
+            values++;
+        }
+//        qWarning() << __PRETTY_FUNCTION__ << m_dragMovement[i].x() << m_dragMovement[i].y() << m_dragTime[i];
+    }
+    dragViewSpeed /= values;
+    dragViewSpeed *= SPEED_TO_DISTANCE_FACTOR;
+
+//    qWarning() << __PRETTY_FUNCTION__ << dragViewSpeed.x() << dragViewSpeed.y();
+
+    QPointF dragSceneSpeed = dragViewSpeed * (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
+
+    m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
+    m_scroller->setStartValue(m_scenePosition);
+    m_scroller->setEndValue(m_scenePosition + dragSceneSpeed.toPoint());
+    m_scroller->start();
+}
+
+void MapView::resizeEvent(QResizeEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
+
+    emit viewResized(event->size());
+}
+
+void MapView::setViewScale(qreal viewScale)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QTransform transform;
+    transform.scale(viewScale, viewScale);
+    setTransform(transform);
+}
+
+void MapView::setZoomLevel(int zoomLevel)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_zoomLevel = zoomLevel;
+
+    if (m_zoomAnimation) {
+        m_zoomAnimation->stop();
+        m_zoomAnimation->setDuration(ZOOM_TIME);
+        m_zoomAnimation->setStartValue(viewScale());
+        m_zoomAnimation->setEndValue(pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL));
+
+        m_zoomAnimation->start();
+    }
+}
+
+qreal MapView::viewScale()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return transform().m11();
 }