Basic structure in MapEngine for updating the direction indicator
authorSami Rämö <sami.ramo@ixonos.com>
Wed, 28 Jul 2010 10:25:31 +0000 (13:25 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Wed, 28 Jul 2010 10:25:31 +0000 (13:25 +0300)
 - Implemented calculating the direction between two coordinates with
   SceneCoordinate::azimuthTo()

src/coordinates/scenecoordinate.cpp
src/coordinates/scenecoordinate.h
src/map/mapengine.cpp
src/map/mapengine.h
src/ui/indicatorbutton.cpp
src/ui/indicatorbutton.h

index fbe4204..e3de4c5 100644 (file)
@@ -22,6 +22,7 @@
 #include <cmath>
 
 #include <QDebug>
+#include <QLineF>
 #include <QPointF>
 #include <QVariant>
 
@@ -51,6 +52,12 @@ SceneCoordinate::SceneCoordinate(const GeoCoordinate &coordinate)
     convertFrom(coordinate);
 }
 
+qreal SceneCoordinate::azimuthTo(const SceneCoordinate &to) const
+{
+    QLineF line = QLineF(this->toPointF(), to.toPointF());
+    return line.angle();
+}
+
 void SceneCoordinate::convertFrom(const GeoCoordinate &coordinate)
 {
     qDebug() << __PRETTY_FUNCTION__;
index 8d823c8..e1679d1 100644 (file)
@@ -63,6 +63,14 @@ public:
  ******************************************************************************/
 public:
     /**
+    * @brief Returns the azimuth from this coordinate to other coordinate
+    *
+    * @param to Target coordinate
+    * @returns Azimuth in degrees
+    */
+    qreal azimuthTo(const SceneCoordinate &to) const;
+
+    /**
     * @brief Check if coordinate is (0.0, 0.0)
     *
     * @returns True if both X and Y are 0.0, otherwise false
index 0edebee..7d839a6 100644 (file)
@@ -245,6 +245,7 @@ void MapEngine::gpsPositionUpdate(GeoCoordinate position, qreal accuracy)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_gpsLocationItem->updatePosition(SceneCoordinate(position), accuracy);
+    m_gpsPosition = position;
     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
 
     if (m_autoCenteringEnabled) {
@@ -252,6 +253,8 @@ void MapEngine::gpsPositionUpdate(GeoCoordinate position, qreal accuracy)
         m_scrollStartedByGps = true;
         scrollToPosition(m_lastAutomaticPosition);
     }
+
+    updateDirectionIndicator();
 }
 
 qreal MapEngine::greatCircleDistance(GeoCoordinate firstLocation, GeoCoordinate secondLocation)
@@ -448,6 +451,8 @@ void MapEngine::setCenterPosition(SceneCoordinate coordinate)
 
     m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
     emit newMapResolution(sceneResolution());
+
+    updateDirectionIndicator();
 }
 
 void MapEngine::setGPSEnabled(bool enabled)
@@ -542,6 +547,16 @@ int MapEngine::tilesPerSide(int zoomLevel)
     return (1 << zoomLevel);
 }
 
+void MapEngine::updateDirectionIndicator()
+{
+    /// @todo implement distance calculation
+    qreal distance = 0;
+
+    qreal direction = m_sceneCoordinate.azimuthTo(SceneCoordinate(m_gpsPosition));
+
+    emit directionIndicatorValuesUpdate(direction, distance);
+}
+
 void MapEngine::updateViewTilesSceneRect()
 {
     qDebug() << __PRETTY_FUNCTION__;
index 0fe23aa..a651195 100644 (file)
@@ -280,6 +280,8 @@ private:
      */
     void setTilesGridSize(const QSize &viewSize);
 
+    void updateDirectionIndicator();
+
     /**
      * @brief Updates the current view rect including margins
      *
@@ -383,6 +385,12 @@ private slots:
  ******************************************************************************/
 signals:
     /**
+    * @brief Signal when direction and distance from current map center point to current GPS
+    *        location is changed
+    */
+    void directionIndicatorValuesUpdate(qreal direction, qreal distance);
+
+    /**
     * @brief Signals error
     *
     * @param context error context
@@ -475,6 +483,7 @@ private:
     QSize m_viewSize;               ///< Current view size
 
     FriendItemsHandler *m_friendItemsHandler;   ///< Handler for friend and group items
+    GeoCoordinate m_gpsPosition;                ///< Lates GPS position
     GPSLocationItem *m_gpsLocationItem;         ///< Item pointing current location from GPS
     MapFetcher *m_mapFetcher;                   ///< Fetcher for map tiles
     MapRouteItem *m_mapRouteItem;               ///< Map route item
index 9340a68..8bdc29f 100644 (file)
@@ -41,7 +41,8 @@ const int BUTTON_HEIGHT = 66;   ///< Button height
 
 IndicatorButton::IndicatorButton(QWidget *parent)
     : QToolButton(parent),
-      m_isDraggable(false)
+      m_isDraggable(false),
+      m_direction(0)
 {
     m_indicatorLeds[OFF].load(":res/images/led_red.png");
     m_indicatorLeds[ON].load(":res/images/led_red_s.png");
@@ -167,6 +168,11 @@ void IndicatorButton::mouseReleaseEvent(QMouseEvent *event)
     QToolButton::mouseReleaseEvent(event);
 }
 
+void IndicatorButton::setDirection(qreal direction)
+{
+    m_direction = direction;
+}
+
 void IndicatorButton::setDraggable(bool mode, QPoint eventPosition)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -259,7 +265,7 @@ void IndicatorButton::paintEvent(QPaintEvent *event)
     };
 
     QTransform rotationTransform;
-    rotationTransform.rotate(90);
+    rotationTransform.rotate(m_direction);
 
     QTransform translateTransform;
     translateTransform.translate(CENTER.x(), CENTER.y());
index 6a207f9..f9572f1 100644 (file)
@@ -103,6 +103,8 @@ public slots:
      */
     void screenResized(const QSize &size);
 
+    void setDirection(qreal direction);
+
     /**
      * @brief Toggle distance indicator button draggability
      */
@@ -137,6 +139,8 @@ private:
 
     bool m_isDraggable;             ///< Boolean for tracking the draggability state
 
+    qreal m_direction;                ///< Direction to the GPS position (in degrees)
+
     QColor *m_normalColor;                  ///< Normal background color
 
     QLinearGradient *m_selectedGradient;    ///< Selected background