Merge branch 'list_panel'
[situare] / src / map / mapengine.cpp
index 6d221c2..fce2881 100644 (file)
 
 MapEngine::MapEngine(QObject *parent)
     : QObject(parent)
+    , m_autoCenteringEnabled(false)
     , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
+    , m_lastManualPosition(QPoint(0, 0))
     , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
     , m_zoomedIn(false)
     , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
+
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -81,7 +84,11 @@ void MapEngine::setViewLocation(QPointF latLonCoordinate)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
+    QPoint sceneCoordinate = convertLatLonToSceneCoordinate(latLonCoordinate);
+
+    m_lastManualPosition = sceneCoordinate;
+
+    setLocation(sceneCoordinate);
 }
 
 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
@@ -142,6 +149,9 @@ void MapEngine::setLocation(QPoint sceneCoordinate)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    if (disableAutoCentering(sceneCoordinate))
+        emit mapScrolled();
+
     m_sceneCoordinate = sceneCoordinate;
     emit locationChanged(m_sceneCoordinate);
 
@@ -329,3 +339,32 @@ void MapEngine::receiveOwnLocation(User *user)
     if (!m_ownLocation->isVisible())
         m_ownLocation->show();
 }
+
+bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
+{
+    if (isAutoCenteringEnabled()) {
+        int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
+
+        QPoint oldPixelValue = QPoint(m_lastManualPosition.x() / zoomFactor,
+                                      m_lastManualPosition.y() / zoomFactor);
+
+        QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
+                                      sceneCoordinate.y() / zoomFactor);
+
+        if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE) ||
+            (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE))
+            return true;
+    }
+
+    return false;
+}
+
+bool MapEngine::isAutoCenteringEnabled()
+{
+    return m_autoCenteringEnabled;
+}
+
+void MapEngine::setAutoCentering(bool enabled)
+{
+    m_autoCenteringEnabled = enabled;
+}