Merge branch 'master' of https://vcs.maemo.org/git/situare
[situare] / src / map / mapview.cpp
index db16240..baaafe1 100644 (file)
@@ -3,6 +3,7 @@
    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>
 
-#ifndef Q_WS_MAEMO_5
-    #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)
 {
-/**
-  * 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);
-#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
+    qDebug() << __PRETTY_FUNCTION__;
 
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+    m_zoomAnimation = new QPropertyAnimation(this, "viewScale", this);
+    connect(m_zoomAnimation, SIGNAL(finished()), this, SIGNAL(viewZoomFinished()));
 }
 
 void MapView::setZoomLevel(int zoomLevel)
 {
-    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);
+    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();
+    }
 }
 
-void MapView::timerEvent(QTimerEvent *event)
+qreal MapView::viewScale()
 {
-    qreal scaleFactor = currentScale();
+    qDebug() << __PRETTY_FUNCTION__;
 
-//    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());
-    }
-    else {
-        scaleFactor += m_zoomScaleDelta;
-    }
+    return transform().m11();
+}
 
-//    qDebug() << __PRETTY_FUNCTION__ << "currentScale:" << currentScale()
-//                                    << "m_zoomScaleDelta" << m_zoomScaleDelta
-//                                    << "scaleFactor:" << scaleFactor;
+void MapView::setViewScale(qreal viewScale)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
     QTransform transform;
-    transform.scale(scaleFactor, scaleFactor);
+    transform.scale(viewScale, viewScale);
     setTransform(transform);
+    emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
 }
 
-qreal MapView::currentScale()
+void MapView::mouseMoveEvent(QMouseEvent *event)
 {
-    QTransform currentTransform = transform();
-    return currentTransform.m11();
+    m_scenePosition += m_mousePosition - mapToScene(event->pos()).toPoint();
+
+    qDebug() << __PRETTY_FUNCTION__ << "m_scenePosition:" << m_scenePosition;
+
+    emit viewScrolled(m_scenePosition);
+
+    m_mousePosition = mapToScene(event->pos()).toPoint();
+    emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
 }
 
-void MapView::scrollContentsBy (int dx, int dy)
+void MapView::mousePressEvent(QMouseEvent *event)
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    QGraphicsView::scrollContentsBy(dx, dy);
+    QGraphicsView::mousePressEvent(event); // Temporary solution
 
-    QPoint centerInScene = mapToScene(frameRect().center()).toPoint();
-//    qDebug() << __PRETTY_FUNCTION__ << "centerInScene:" << centerInScene;
-    emit viewScrolled(centerInScene);
+    m_mousePosition = mapToScene(event->pos()).toPoint();
+    m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1).toPoint();
 }
 
 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() << __PRETTY_FUNCTION__ << "Resize:" << event->size();
+
     emit viewResized(event->size());
+    emit viewResizedNewSize(viewport()->width(), viewport()->height());
+    emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
+}
+
+void MapView::showEvent(QShowEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QGraphicsView::showEvent(event);
+    emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
 }