Added RouteWaypointList- and RouteWaypointListView classes.
[situare] / src / engine / engine.cpp
index 93ed69d..57c14be 100644 (file)
     USA.
  */
 
+#include <cmath>
+
 #include <QMessageBox>
 #include <QNetworkReply>
 
 #ifdef Q_WS_MAEMO_5
 #include "application.h"
 #endif
+
 #include "common.h"
+#include "error.h"
 #include "facebookservice/facebookauthentication.h"
 #include "gps/gpsposition.h"
 #include "map/mapengine.h"
+#include "routing/geocodingservice.h"
 #include "routing/routingservice.h"
+#include "mce.h"
+#include "network/networkaccessmanager.h"
 #include "situareservice/situareservice.h"
 #include "ui/mainwindow.h"
-#include "network/networkaccessmanager.h"
-#include "mce.h"
-#include <cmath>
 
 #include "engine.h"
 
@@ -54,7 +58,7 @@ SituareEngine::SituareEngine()
       m_automaticUpdateRequest(false),
       m_userMoved(false),
       m_automaticUpdateIntervalTimer(0),
-      m_lastUpdatedGPSPosition(QPointF())
+      m_lastUpdatedGPSPosition(GeoCoordinate())
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -69,7 +73,7 @@ SituareEngine::SituareEngine()
             this, SLOT(enablePowerSave(bool)));
 #endif
 
-    m_networkAccessManager = NetworkAccessManager::instance();
+    m_networkAccessManager = new NetworkAccessManager(this);
 
     // build MapEngine
     m_mapEngine = new MapEngine(this);
@@ -87,9 +91,14 @@ SituareEngine::SituareEngine()
     // build routing service
     m_routingService = new RoutingService(this); // create this when needed, not in constructor!
 
+    // build geocoding service
+    m_geocodingService = new GeocodingService(this);
+
     // connect signals
     signalsFromMapEngine();
+    signalsFromGeocodingService();
     signalsFromGPS();
+    signalsFromRoutingService();
     signalsFromSituareService();
     signalsFromMainWindow();
     signalsFromFacebookAuthenticator();
@@ -143,10 +152,10 @@ SituareEngine::~SituareEngine()
 
 void SituareEngine::changeAutoCenteringSetting(bool enabled)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
 
     m_autoCenteringEnabled = enabled;
-    enableAutoCentering(enabled);
+    setAutoCentering(enabled);
 }
 
 void SituareEngine::disableAutoCentering()
@@ -154,50 +163,14 @@ void SituareEngine::disableAutoCentering()
     qDebug() << __PRETTY_FUNCTION__;
 
     changeAutoCenteringSetting(false);
-    m_ui->buildInformationBox(tr("Auto centering disabled"));
 }
 
-void SituareEngine::enableAutoCentering(bool enabled)
+void SituareEngine::draggingModeTriggered()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_ui->setAutoCenteringButtonEnabled(enabled);
-    m_mapEngine->setAutoCentering(enabled);
-
-    if (enabled)
-        m_gps->requestLastPosition();
-}
-
-void SituareEngine::enableGPS(bool enabled)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_ui->setOwnLocationCrosshairVisibility(!enabled);
-
-    if (m_gps->isInitialized()) {
-        m_ui->setGPSButtonEnabled(enabled);
-        m_mapEngine->setGPSEnabled(enabled);
-
-        if (enabled && !m_gps->isRunning()) {
-            m_gps->start();
-            enableAutoCentering(m_autoCenteringEnabled);
-            m_gps->requestLastPosition();
-
-            if(m_ui->loginState())
-                m_ui->readAutomaticLocationUpdateSettings();
-        }
-        else if (!enabled && m_gps->isRunning()) {
-            m_gps->stop();
-            enableAutoCentering(false);
-            enableAutomaticLocationUpdate(false);
-        }
-    }
-    else {
-        if (enabled)
-            m_ui->buildInformationBox(tr("Unable to start GPS"));
-        m_ui->setGPSButtonEnabled(false);
-        m_mapEngine->setGPSEnabled(false);
-    }
+    if (m_mce)
+        m_mce->vibrationFeedback();
 }
 
 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
@@ -218,14 +191,14 @@ void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateInterv
             else
                 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
 
-            connect(m_gps, SIGNAL(position(QPointF,qreal)),
-                    this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+            connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
 
             m_automaticUpdateIntervalTimer->start();
 
         } else {
-            disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
-                    this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+            disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
 
             m_automaticUpdateIntervalTimer->stop();
         }
@@ -234,7 +207,7 @@ void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateInterv
 
 void SituareEngine::enablePowerSave(bool enabled)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
 
     m_gps->enablePowerSave(enabled);
 
@@ -319,7 +292,7 @@ void SituareEngine::error(const int context, const int error)
         m_ui->buildInformationBox(tr("Map image download failed"), true);
         break;
     case SituareError::GPS_INITIALIZATION_FAILED:
-        enableGPS(false);
+        setGPS(false);
         m_ui->buildInformationBox(tr("GPS initialization failed"), true);
         break;
     case SituareError::INVALID_JSON:
@@ -334,9 +307,9 @@ void SituareEngine::error(const int context, const int error)
     default:
         m_ui->toggleProgressIndicator(false);
         if(context == ErrorContext::NETWORK)
-            qCritical() << "QNetworkReply::NetworkError: " << error;
+            qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
         else
-            qCritical() << "Unknown error: " << error;
+            qCritical() << __PRETTY_FUNCTION__ << "Unknown error: " << error;
 
         break;
     }
@@ -371,30 +344,34 @@ void SituareEngine::initializeGpsAndAutocentering()
 
         if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
 
-            connect(m_gps, SIGNAL(position(QPointF,qreal)),
-                    this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
+            connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+                    this, SLOT(setFirstStartZoomLevel()));
 
             changeAutoCenteringSetting(true);
-            enableGPS(true);
+            setGPS(true);
 
             m_ui->buildInformationBox(tr("GPS enabled"));
-            m_ui->buildInformationBox(tr("Auto centering enabled"));
 
         } else { // Normal start
             changeAutoCenteringSetting(autoCenteringEnabled.toBool());
-            enableGPS(gpsEnabled.toBool());
+            setGPS(gpsEnabled.toBool());
 
             if (gpsEnabled.toBool())
                 m_ui->buildInformationBox(tr("GPS enabled"));
-
-            if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
-                m_ui->buildInformationBox(tr("Auto centering enabled"));
         }
     } else {
-        enableGPS(false);
+        setGPS(false);
     }
 }
 
+void SituareEngine::locationSearch(QString location)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(!location.isEmpty())
+        m_geocodingService->requestLocation(location);
+}
+
 void SituareEngine::loginActionPressed()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -493,13 +470,13 @@ void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
     }
 }
 
-void SituareEngine::requestAutomaticUpdateIfMoved(QPointF position)
+void SituareEngine::requestAutomaticUpdateIfMoved(GeoCoordinate position)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
+    if ((fabs(m_lastUpdatedGPSPosition.longitude() - position.longitude()) >
          USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
-        (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
+        (fabs(m_lastUpdatedGPSPosition.latitude() - position.latitude()) >
          USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
 
         m_lastUpdatedGPSPosition = position;
@@ -513,18 +490,79 @@ void SituareEngine::requestAutomaticUpdateIfMoved(QPointF position)
     }
 }
 
-void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
+void SituareEngine::routeParsed(Route &route)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    Q_UNUSED(latLonCoordinate);
-    Q_UNUSED(accuracy);
+    Q_UNUSED(route);
+
+    m_ui->toggleProgressIndicator(false);
+}
+
+void SituareEngine::routeTo(const GeoCoordinate &endPointCoordinates)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_ui->toggleProgressIndicator(true);
+
+    if (m_gps->isRunning())
+        m_routingService->requestRoute(m_gps->lastPosition(), endPointCoordinates);
+    else
+        m_routingService->requestRoute(m_mapEngine->centerGeoCoordinate(), endPointCoordinates);
+}
+
+void SituareEngine::setAutoCentering(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
+
+    m_ui->setIndicatorButtonEnabled(enabled);
+    m_mapEngine->setAutoCentering(enabled);
+    m_ui->setOwnLocationCrosshairVisibility(!enabled);
+
+    if (enabled) {
+        setGPS(true);
+        m_gps->requestLastPosition();
+    }
+}
+
+void SituareEngine::setFirstStartZoomLevel()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
     if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
         m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
 
-    disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
-               this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
+    disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+               this, SLOT(setFirstStartZoomLevel()));
+}
+
+void SituareEngine::setGPS(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__ << enabled;
+
+    if (m_gps->isInitialized()) {
+        m_ui->setGPSButtonEnabled(enabled);
+        m_mapEngine->setGPSEnabled(enabled);
+
+        if (enabled && !m_gps->isRunning()) {
+            m_gps->start();
+            m_gps->requestLastPosition();
+
+            if(m_ui->loginState())
+                m_ui->readAutomaticLocationUpdateSettings();
+        }
+        else if (!enabled && m_gps->isRunning()) {
+            m_gps->stop();
+            changeAutoCenteringSetting(false);
+            enableAutomaticLocationUpdate(false);
+        }
+    }
+    else {
+        if (enabled)
+            m_ui->buildInformationBox(tr("Unable to start GPS"));
+        m_ui->setGPSButtonEnabled(false);
+        m_mapEngine->setGPSEnabled(false);
+    }
 }
 
 void SituareEngine::signalsFromFacebookAuthenticator()
@@ -550,12 +588,20 @@ void SituareEngine::signalsFromFacebookAuthenticator()
             m_ui, SLOT(loginUsingCookies()));
 }
 
+void SituareEngine::signalsFromGeocodingService()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_geocodingService, SIGNAL(locationDataParsed(const QList<Location>&)),
+            m_ui, SIGNAL(locationDataParsed(const QList<Location>&)));
+}
+
 void SituareEngine::signalsFromGPS()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    connect(m_gps, SIGNAL(position(QPointF,qreal)),
-            m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
+    connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
+            m_mapEngine, SLOT(gpsPositionUpdate(GeoCoordinate, qreal)));
 
     connect(m_gps, SIGNAL(timeout()),
             m_ui, SLOT(gpsTimeout()));
@@ -584,8 +630,8 @@ void SituareEngine::signalsFromMainWindow()
             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
 
     // signals from map view
-    connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
-            m_mapEngine, SLOT(setCenterPosition(QPoint)));
+    connect(m_ui, SIGNAL(mapViewScrolled(SceneCoordinate)),
+            m_mapEngine, SLOT(setCenterPosition(SceneCoordinate)));
 
     connect(m_ui, SIGNAL(mapViewResized(QSize)),
             m_mapEngine, SLOT(viewResized(QSize)));
@@ -601,11 +647,8 @@ void SituareEngine::signalsFromMainWindow()
             m_mapEngine, SLOT(zoomOut()));
 
     // signals from menu buttons
-    connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
-            this, SLOT(changeAutoCenteringSetting(bool)));
-
     connect(m_ui, SIGNAL(gpsTriggered(bool)),
-            this, SLOT(enableGPS(bool)));
+            this, SLOT(setGPS(bool)));
 
     //signals from dialogs
     connect(m_ui, SIGNAL(cancelLoginProcess()),
@@ -624,12 +667,28 @@ void SituareEngine::signalsFromMainWindow()
     connect(m_ui, SIGNAL(refreshUserData()),
             this, SLOT(refreshUserData()));
 
-    connect(m_ui, SIGNAL(findUser(QPointF)),
-            m_mapEngine, SLOT(centerToCoordinates(QPointF)));
+    connect(m_ui, SIGNAL(centerToCoordinates(GeoCoordinate)),
+            m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
 
-    // signals from friend list tab
-    connect(m_ui, SIGNAL(findFriend(QPointF)),
-            m_mapEngine, SLOT(centerToCoordinates(QPointF)));
+
+    // signals from routing tab
+    connect(m_ui,
+            SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
+            m_mapEngine,
+            SLOT(showMapArea(const GeoCoordinate&, const GeoCoordinate&)));
+
+    // signals from distence indicator button
+    connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
+            this, SLOT(changeAutoCenteringSetting(bool)));
+
+    connect(m_ui, SIGNAL(searchForLocation(QString)),
+            this, SLOT(locationSearch(QString)));
+
+    connect(m_ui, SIGNAL(draggingModeTriggered()),
+            this, SLOT(draggingModeTriggered()));
+
+    connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
+            this, SLOT(routeTo(const GeoCoordinate&)));
 }
 
 void SituareEngine::signalsFromMapEngine()
@@ -639,8 +698,8 @@ void SituareEngine::signalsFromMapEngine()
     connect(m_mapEngine, SIGNAL(error(int, int)),
             this, SLOT(error(int, int)));
 
-    connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
-            m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
+    connect(m_mapEngine, SIGNAL(locationChanged(SceneCoordinate)),
+            m_ui, SIGNAL(centerToSceneCoordinates(SceneCoordinate)));
 
     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
             m_ui, SIGNAL(zoomLevelChanged(int)));
@@ -659,6 +718,23 @@ void SituareEngine::signalsFromMapEngine()
 
     connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
             m_ui, SIGNAL(newMapResolution(qreal)));
+
+    connect(m_mapEngine, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)),
+            m_ui, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)));
+}
+
+void SituareEngine::signalsFromRoutingService()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_routingService, SIGNAL(routeParsed(Route&)),
+            this, SLOT(routeParsed(Route&)));
+
+    connect(m_routingService, SIGNAL(routeParsed(Route&)),
+            m_mapEngine, SLOT(setRoute(Route&)));
+
+    connect(m_routingService, SIGNAL(routeParsed(Route&)),
+            m_ui, SIGNAL(routeParsed(Route&)));
 }
 
 void SituareEngine::signalsFromSituareService()