Moved URL builder and parser to MapFetcher
[situare] / src / map / mapengine.cpp
index 98a8767..1cddef3 100644 (file)
@@ -42,9 +42,9 @@ MapEngine::MapEngine(QObject *parent)
     m_mapScene = new MapScene(this);
 
     m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
-    connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(enqueueFetchMapImage(QUrl)));
-    connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
-            SLOT(mapImageReceived(QUrl, QPixmap)));
+    connect(this, SIGNAL(fetchImage(int,int,int)), m_mapFetcher, SLOT(enqueueFetchMapImage(int,int,int)));
+    connect(m_mapFetcher, SIGNAL(mapImageReceived(int,int,int,QPixmap)), this,
+            SLOT(mapImageReceived(int,int,int,QPixmap)));
 
     m_mapZoomPanel = new MapZoomPanel(NULL, MAP_ZOOM_PANEL_POSITION_X, MAP_ZOOM_PANEL_POSITION_Y);
     m_mapScene->addItem(m_mapZoomPanel);
@@ -67,51 +67,20 @@ void MapEngine::setViewLocation(QPointF latLonCoordinate)
     setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
 }
 
-QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
-{
-    QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
-                  .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
-
-    return QUrl(url);
-}
-
-void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
-{
-    QString path = url.path();
-    QStringList pathParts = path.split("/", QString::SkipEmptyParts);
-
-    int size = pathParts.size();
-
-    if (size >= 3) {
-        zoom = (pathParts.at(size-3)).toInt();
-        x = (pathParts.at(size-2)).toInt();
-        QString yString = pathParts.at(size-1);
-        yString.chop(4);
-        y = yString.toInt();
-    }
-}
-
-void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
+void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    int zoom = UNDEFINED;
-    int x = UNDEFINED;
-    int y = UNDEFINED;
-
-    parseURL(url, zoom, x, y);
-    QString hashKey = tilePath(zoom, x, y);
+    QString hashKey = tilePath(zoomLevel, x, y);
     if (!m_mapScene->isTileInScene(hashKey)) {
 
         MapTile *mapTile = new MapTile();
-        mapTile->setZoomLevel(zoom, m_zoomLevel);
+        mapTile->setZoomLevel(zoomLevel, m_zoomLevel);
         mapTile->setTileNumber(QPoint(x, y));
-        mapTile->setPixmap(pixmap);
+        mapTile->setPixmap(image);
 
         m_mapScene->addTile(mapTile, hashKey);
 
-        m_mapScene->debugItemsCount();
-
         m_mapScene->enqueueRemoveStackedTiles(mapTile);
    }
 }
@@ -134,6 +103,8 @@ QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
     int topLeftX = tileCoordinate.x() - (gridWidth/2);
     int topLeftY = tileCoordinate.y() - (gridHeight/2);
 
+    m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
+
     return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
 }
 
@@ -196,10 +167,8 @@ void MapEngine::getTiles(QPoint sceneCoordinate)
             else if (tileY > tileMaxVal)
                 tileY -= tileMaxVal;
 
-            QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
-
             if (!m_mapScene->isTileInScene(tilePath(m_zoomLevel, tileX, tileY)))
-                emit fetchImage(url);
+                emit fetchImage(m_zoomLevel, tileX, tileY);
         }
     }
 }