Merge branch 'master' of https://vcs.maemo.org/git/situare
[situare] / src / map / mapview.cpp
index 2eff41d..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
    USA.
 */
 
-#include <math.h>
+#include <cmath>
 
 #include <QDebug>
 #include <QMouseEvent>
 
+#include "mapcommon.h"
 #include "mapview.h"
-#include "mapengine.h"
 
-MapView::MapView(QWidget *parent) : QGraphicsView(parent)
+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()));
 }
 
 void MapView::setZoomLevel(int zoomLevel)
 {
-    double scaleFactor = pow(2, zoomLevel - MapEngine::MAX_ZOOM_LEVEL);
-    scale(scaleFactor, scaleFactor);
+    qDebug() << __PRETTY_FUNCTION__;
+
+    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();
+}
+
+void MapView::setViewScale(qreal viewScale)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QTransform transform;
+    transform.scale(viewScale, viewScale);
+    setTransform(transform);
+    emit viewContentChanged(mapToScene(viewport()->x(), viewport()->y()).toPoint());
+}
+
+void MapView::mouseMoveEvent(QMouseEvent *event)
+{
+    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::mousePressEvent(QMouseEvent *event)
 {
-    qDebug() << __PRETTY_FUNCTION__ << "scene coordinate:" << mapToScene(event->pos());
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QGraphicsView::mousePressEvent(event); // Temporary solution
+
+    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;
+
+    centerOn(sceneCoordinate);
+}
+
+void MapView::resizeEvent(QResizeEvent *event)
+{
+    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());
 }