Enabled zValues for MapTiles and OpenGL for desktop
[situare] / src / map / mapview.cpp
index 24c14ee..5019e22 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
 
-    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>
 
+#ifndef Q_WS_MAEMO_5
+    #include <QGLWidget>
+#endif // Q_WS_MAEMO_5
+
+#include "common.h"
 #include "mapview.h"
-#include "mapengine.h"
 
 MapView::MapView(QWidget *parent) : QGraphicsView(parent)
 {
+#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);
+#endif // Q_WS_MAEMO_5
+
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 }
 
 void MapView::setZoomLevel(int zoomLevel)
 {
-    double scaleFactor = pow(2, zoomLevel - MapEngine::MAX_ZOOM_LEVEL);
-    scale(scaleFactor, scaleFactor);
+    m_zoomTargetScale = pow(2, zoomLevel - MAX_ZOOM_LEVEL);
+    m_zoomScaleDelta = (m_zoomTargetScale - currentScale()) / (ZOOM_FPS * ZOOM_TIME);
+
+    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());
+    }
+    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()
+{
+    QTransform currentTransform = transform();
+    return currentTransform.m11();
+}
+
+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)
 {
-    qDebug() << __PRETTY_FUNCTION__ << "scene coordinate:" << mapToScene(event->pos());
+    m_mousePosition = mapToScene(event->pos());
+    m_scenePosition = mapToScene(width() / 2 - 1, height() / 2 - 1);
+}
+
+
+void MapView::centerToSceneCoordinates(QPointF sceneCoordinate)
+{
+    //qDebug() << __PRETTY_FUNCTION__ << "sceneCoordinate" << sceneCoordinate;
+    centerOn(sceneCoordinate);
+}
+
+void MapView::resizeEvent(QResizeEvent *event)
+{
+    qDebug() << "Resize event: " << event->size();
+    emit viewResized(event->size());
 }