Re-factored MapScroller to use SceneCoordinate
authorSami Rämö <sami.ramo@ixonos.com>
Tue, 20 Jul 2010 08:51:27 +0000 (11:51 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Tue, 20 Jul 2010 08:51:27 +0000 (11:51 +0300)
 - Implemented some operators for SceneCoordinate calculations

 - Implemented QVariantAnimation interpolator for SceneCoordinate

src/coordinates/scenecoordinate.cpp
src/coordinates/scenecoordinate.h
src/map/mapengine.cpp
src/map/mapscroller.cpp
src/map/mapscroller.h
src/map/mapview.cpp

index 4618eb7..7bfdb4e 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <QDebug>
 #include <QPointF>
+#include <QVariant>
 
 #include "geocoordinate.h"
 #include "map/osm.h"
@@ -122,9 +123,43 @@ double SceneCoordinate::y() const
     return m_y;
 }
 
-QDebug operator<<(QDebug dbg, const SceneCoordinate &coordinate)
+SceneCoordinate::operator QVariant() const
 {
-    dbg.nospace() << "(" << coordinate.x() << ", " << coordinate.y() << ")";
+    return QVariant::fromValue(*this);
+}
 
-    return dbg.space();
+SceneCoordinate & SceneCoordinate::operator*=(double factor)
+{
+    m_x *= factor;
+    m_y *= factor;
+    return *this;
+}
+
+SceneCoordinate & SceneCoordinate::operator+=(const SceneCoordinate &coordinate)
+{
+    m_x += coordinate.x();
+    m_y += coordinate.y();
+    return *this;
+}
+
+SceneCoordinate & SceneCoordinate::operator-=(const SceneCoordinate &coordinate)
+{
+    m_x -= coordinate.x();
+    m_y -= coordinate.y();
+    return *this;
+}
+
+const SceneCoordinate operator* (double factor, const SceneCoordinate &coordinate)
+{
+    return SceneCoordinate(coordinate) *= factor;
+}
+
+const SceneCoordinate SceneCoordinate::operator+(const SceneCoordinate &other) const
+{
+  return SceneCoordinate(*this) += other;
+}
+
+const SceneCoordinate SceneCoordinate::operator-(const SceneCoordinate &other) const
+{
+  return SceneCoordinate(*this) -= other;
 }
index b1c41c8..09703a2 100644 (file)
@@ -141,10 +141,25 @@ private:
 private:
     double m_x;         ///< X value
     double m_y;         ///< Y value
+
+/*******************************************************************************
+ * OPERATORS
+ ******************************************************************************/
+public:
+    operator QVariant() const;
+
+    SceneCoordinate & operator*=(double factor);
+    SceneCoordinate & operator+=(const SceneCoordinate &coordinate);
+    SceneCoordinate & operator-=(const SceneCoordinate &coordinate);
+
+    const SceneCoordinate operator+(const SceneCoordinate &other) const;
+    const SceneCoordinate operator-(const SceneCoordinate &other) const;
 };
 
 QDebug operator<<(QDebug dbg, const SceneCoordinate &c);
 
+const SceneCoordinate operator*(double factor, const SceneCoordinate &coordinate);
+
 Q_DECLARE_METATYPE(SceneCoordinate)
 
 #endif // SCENECOORDINATE_H
index cd9d23d..aa4ebe0 100644 (file)
@@ -386,8 +386,8 @@ void MapEngine::scrollToPosition(SceneCoordinate coordinate)
     m_scroller->stop();
     m_scroller->setEasingCurve(QEasingCurve::InOutQuart);
     m_scroller->setDuration(SMOOTH_CENTERING_TIME_MS);
-    m_scroller->setStartValue(m_sceneCoordinate.toPointF());
-    m_scroller->setEndValue(coordinate.toPointF());
+    m_scroller->setStartValue(m_sceneCoordinate);
+    m_scroller->setEndValue(coordinate);
     m_smoothScrollRunning = true;
     m_scroller->start();
 }
index 30e54c6..8e04137 100644 (file)
 */
 
 #include <QDebug>
-#include <QPointF>
+#include <QVariantAnimation>
+
+#include "coordinates/scenecoordinate.h"
 
 #include "mapscroller.h"
 
+// scene coordinate interpolator function is not part of this class namespace
+QVariant sceneCoordinateInterpolator(const SceneCoordinate &start,
+                                     const SceneCoordinate &end,
+                                     qreal progress);
+
 MapScroller& MapScroller::getInstance()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    qRegisterAnimationInterpolator<SceneCoordinate>(sceneCoordinateInterpolator);
+
     static MapScroller instance;
     return instance;
 }
@@ -36,10 +45,14 @@ void MapScroller::updateCurrentValue(const QVariant &value)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    Q_ASSERT(value.type() == QVariant::PointF);
+    if ((state() == QAbstractAnimation::Running) && (value.canConvert<SceneCoordinate>()))
+        emit coordinateUpdated(value.value<SceneCoordinate>());
+}
 
-    if (state() == QAbstractAnimation::Running) {
-        QPointF point = value.toPointF();
-        emit coordinateUpdated(SceneCoordinate(point.x(), point.y()));
-    }
+// scene coordinate interpolator function is not part of this class namespace
+QVariant sceneCoordinateInterpolator(const SceneCoordinate &start,
+                                                  const SceneCoordinate &end,
+                                                  qreal progress)
+{
+    return SceneCoordinate(start + progress * (end - start));
 }
index f28c570..8f666f1 100644 (file)
@@ -83,6 +83,8 @@ public:
     /**
     * @brief Get reference to instance of this class
     *
+    * Also registers the custom SceneCoordinate interpolator method.
+    *
     * @return reference to instance of this class
     */
     static MapScroller &getInstance();
index 748d9a2..36b008e 100644 (file)
@@ -95,8 +95,8 @@ void MapView::mouseDoubleClickEvent(QMouseEvent *event)
 
         m_scroller->setEasingCurve(QEasingCurve::Linear);
         m_scroller->setDuration(ZOOM_TIME_MS);
-        m_scroller->setStartValue(QPointF(m_scenePosition));
-        m_scroller->setEndValue(QPointF(zoomPosition));
+        m_scroller->setStartValue(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()));
+        m_scroller->setEndValue(SceneCoordinate(zoomPosition.x(), zoomPosition.y()));
 
         m_zoomAnimation->setEasingCurve(QEasingCurve::InQuad);
         m_zoomAnimation->setDuration(ZOOM_TIME_MS);
@@ -192,9 +192,12 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
 
             m_scroller->setEasingCurve(QEasingCurve::OutCirc);
             m_scroller->setDuration(KINETIC_SCROLL_TIME_MS);
-            m_scroller->setStartValue(QPointF(m_scenePosition));
-            m_scroller->setEndValue(QPointF(m_scenePosition) + effectSceneDistance);
+            m_scroller->setStartValue(SceneCoordinate(m_scenePosition.x(), m_scenePosition.y()));
+            QPointF endValue = QPointF(m_scenePosition) + effectSceneDistance;
+            m_scroller->setEndValue(SceneCoordinate(endValue.x(), endValue.y()));
             m_scroller->start();
+
+            /// @todo modify calculations to use double
         }
     }
 }