Merge branch 'friendlist' into integration
[situare] / src / map / mapengine.cpp
index 2942b08..8b3d4eb 100644 (file)
@@ -37,12 +37,13 @@ MapEngine::MapEngine(QObject *parent)
     : QObject(parent)
     , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
     , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
+    , m_zoomedIn(false)
     , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
 {
     m_mapScene = new MapScene(this);
 
     m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
-    connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(fetchMapImage(QUrl)));
+    connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(enqueueFetchMapImage(QUrl)));
     connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
             SLOT(mapImageReceived(QUrl, QPixmap)));
 
@@ -98,7 +99,7 @@ void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
 
 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
 {
-    //qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     int zoom = UNDEFINED;
     int x = UNDEFINED;
@@ -109,13 +110,15 @@ void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
     if (!m_mapScene->isTileInScene(hashKey)) {
 
         MapTile *mapTile = new MapTile();
-        mapTile->setZoomLevel(zoom);
+        mapTile->setZoomLevel(zoom, m_zoomLevel);
         mapTile->setTileNumber(QPoint(x, y));
         mapTile->setPixmap(pixmap);
 
         m_mapScene->addTile(mapTile, hashKey);
 
-        m_mapScene->removeStackedTiles(mapTile, viewRect());
+        m_mapScene->debugItemsCount();
+
+        m_mapScene->enqueueRemoveStackedTiles(mapTile);
    }
 }
 
@@ -144,19 +147,19 @@ void MapEngine::alignImmovableItems(QPoint viewTopLeft)
 {
     m_mapZoomPanel->setPos(viewTopLeft);
 
-//    qDebug() << __PRETTY_FUNCTION__ << "viewTopLeft:" << viewTopLeft;
+    qDebug() << __PRETTY_FUNCTION__ << "viewTopLeft:" << viewTopLeft;
 }
 
 void MapEngine::setLocation(QPoint sceneCoordinate)
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     m_sceneCoordinate = sceneCoordinate;
     emit locationChanged(m_sceneCoordinate);
 
     if (isCenterTileChanged(sceneCoordinate)) {
         getTiles(sceneCoordinate);
-        m_mapScene->removeOutOfViewTiles(viewRect());
+        m_mapScene->removeOutOfViewTiles();
     }
 }
 
@@ -171,14 +174,15 @@ bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
 
 void MapEngine::getTiles(QPoint sceneCoordinate)
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    m_viewGrid = calculateTileGrid(sceneCoordinate);
+    m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
+    updateViewTilesSceneRect();
 
-    int topLeftX = m_viewGrid.topLeft().x();
-    int topLeftY = m_viewGrid.topLeft().y();
-    int bottomRightX = m_viewGrid.bottomRight().x();
-    int bottomRightY = m_viewGrid.bottomRight().y();
+    int topLeftX = m_viewTilesGrid.topLeft().x();
+    int topLeftY = m_viewTilesGrid.topLeft().y();
+    int bottomRightX = m_viewTilesGrid.bottomRight().x();
+    int bottomRightY = m_viewTilesGrid.bottomRight().y();
 
     int tileMaxVal = tileMaxValue(m_zoomLevel);
 
@@ -206,33 +210,40 @@ void MapEngine::getTiles(QPoint sceneCoordinate)
     }
 }
 
-QRect MapEngine::viewRect()
+void MapEngine::updateViewTilesSceneRect()
 {
-    QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.topLeft());
-    QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.bottomRight()
-                                                             + QPoint(1, 1));
-    return QRect(topLeft, bottomRight);
+    QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
+    QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
+                                                            m_viewTilesGrid.bottomRight()
+                                                             + QPoint(1, 1)) - QPoint(1, 1);
+
+    m_mapScene->viewRectUpdated(QRect(topLeft, bottomRight));
 }
 
 void MapEngine::viewResized(const QSize &size)
 {
     m_viewSize = size;
     getTiles(m_sceneCoordinate);
-    m_mapScene->removeOutOfViewTiles(viewRect());
+    m_mapScene->removeOutOfViewTiles();
 }
 
-void MapEngine::viewZoomInFinished()
+void MapEngine::viewZoomFinished()
 {
     qDebug() << __PRETTY_FUNCTION__;
-    m_mapScene->removeOutOfViewTiles(viewRect());
+
+    if (m_zoomedIn) {
+        m_zoomedIn = false;
+        m_mapScene->removeOutOfViewTiles();
+    }
 }
 
 void MapEngine::zoomIn()
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
         m_zoomLevel++;
+        m_zoomedIn = true;
         emit zoomLevelChanged(m_zoomLevel);
 
         m_mapScene->setTilesDrawingLevels(m_zoomLevel);
@@ -243,9 +254,9 @@ void MapEngine::zoomIn()
 
 void MapEngine::zoomOut()
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    if (m_zoomLevel > MIN_MAP_ZOOM_LEVEL) {
+    if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
         m_zoomLevel--;
         emit zoomLevelChanged(m_zoomLevel);
 
@@ -284,7 +295,7 @@ QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileN
 
 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     qreal longitude = latLonCoordinate.x();
     qreal latitude = latLonCoordinate.y();