Merge branch 'power_save'
[situare] / src / engine / engine.cpp
index 778867f..c0fd719 100644 (file)
@@ -31,6 +31,7 @@
 #include "map/mapengine.h"
 #include "situareservice/situareservice.h"
 #include "ui/mainwindow.h"
+#include "mce.h"
 #include <cmath>
 
 #include "engine.h"
@@ -46,6 +47,7 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     : QObject(parent),
       m_autoCenteringEnabled(false),
       m_automaticUpdateFirstStart(true),
+      m_automaticUpdateRequest(false),
       m_userMoved(false),
       m_automaticUpdateIntervalTimer(0),
       m_lastUpdatedGPSPosition(QPointF())
@@ -88,7 +90,7 @@ SituareEngine::SituareEngine(QMainWindow *parent)
 
     m_automaticUpdateIntervalTimer = new QTimer(this);
     connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
-            this, SLOT(automaticUpdateIntervalTimerTimeout()));
+            this, SLOT(startAutomaticUpdate()));
 
     // signals connected, now it's time to show the main window
     // but init the MapEngine before so starting location is set
@@ -99,6 +101,9 @@ SituareEngine::SituareEngine(QMainWindow *parent)
 
     m_gps->setMode(GPSPosition::Default);
     initializeGpsAndAutocentering();
+
+    m_mce = new MCE(this);
+    connect(m_mce, SIGNAL(displayStateChanged(bool)), this, SLOT(displayStateChanged(bool)));
 }
 
 SituareEngine::~SituareEngine()
@@ -112,16 +117,6 @@ SituareEngine::~SituareEngine()
     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
 }
 
-void SituareEngine::automaticUpdateIntervalTimerTimeout()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (m_gps->isRunning() && m_userMoved) {
-        requestUpdateLocation();
-        m_userMoved = false;
-    }
-}
-
 void SituareEngine::changeAutoCenteringSetting(bool enabled)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -138,6 +133,16 @@ void SituareEngine::disableAutoCentering()
     m_ui->buildInformationBox(tr("Auto centering disabled"));
 }
 
+void SituareEngine::displayStateChanged(bool enabled)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_gps->enablePowerSave(!enabled);
+
+    if (m_autoCenteringEnabled)
+        enableAutoCentering(enabled);
+}
+
 void SituareEngine::enableAutoCentering(bool enabled)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -199,9 +204,15 @@ void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateInterv
             else
                 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
 
+            connect(m_gps, SIGNAL(position(QPointF,qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+
             m_automaticUpdateIntervalTimer->start();
 
         } else {
+            disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
+                    this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+
             m_automaticUpdateIntervalTimer->stop();
         }
     }
@@ -349,13 +360,6 @@ void SituareEngine::initializeGpsAndAutocentering()
     }
 }
 
-bool SituareEngine::isUserMoved()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    return m_userMoved;
-}
-
 void SituareEngine::loginActionPressed()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -435,7 +439,7 @@ void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
         m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
 }
 
-void SituareEngine::saveGPSPosition(QPointF position)
+void SituareEngine::requestAutomaticUpdateIfMoved(QPointF position)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -447,6 +451,12 @@ void SituareEngine::saveGPSPosition(QPointF position)
         m_lastUpdatedGPSPosition = position;
         m_userMoved = true;
     }
+
+    if (m_automaticUpdateRequest && m_userMoved) {
+        requestUpdateLocation(tr("Automatic location update."));
+        m_automaticUpdateRequest = false;
+        m_userMoved = false;
+    }
 }
 
 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
@@ -498,9 +508,6 @@ void SituareEngine::signalsFromGPS()
 
     connect(m_gps, SIGNAL(error(int, int)),
             this, SLOT(error(int, int)));
-
-    connect(m_gps, SIGNAL(position(QPointF,qreal)),
-            this, SLOT(saveGPSPosition(QPointF)));
 }
 
 void SituareEngine::signalsFromMainWindow()
@@ -623,6 +630,14 @@ void SituareEngine::signalsFromSituareService()
             m_ui, SIGNAL(clearUpdateLocationDialogData()));
 }
 
+void SituareEngine::startAutomaticUpdate()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_gps->requestUpdate();
+    m_automaticUpdateRequest = true;
+}
+
 void SituareEngine::updateWasSuccessful()
 {
     qDebug() << __PRETTY_FUNCTION__;