Moved URL builder and parser to MapFetcher
[situare] / src / map / mapengine.cpp
index c4cdc9c..1cddef3 100644 (file)
@@ -36,20 +36,23 @@ MapEngine::MapEngine(QObject *parent)
     : QObject(parent)
     , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
     , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
-    , m_zoomedIn(NO)
+    , 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(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);
     connect(m_mapZoomPanel, SIGNAL(zoomInPressed()), this, SLOT(zoomIn()));
     connect(m_mapZoomPanel, SIGNAL(zoomOutPressed()), this, SLOT(zoomOut()));
+
+    m_ownLocation = new OwnLocationItem();
+    m_mapScene->addItem(m_ownLocation);
 }
 
 void MapEngine::init()
@@ -64,55 +67,21 @@ 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;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    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();
-        /// @todo SET SCENE LEVEL AUTOMATICALLY WHEN CHANGING ZOOM LEVEL
-        mapTile->setZoomLevel(zoom);
-        mapTile->setSceneLevel(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);
-        //m_mapScene->removeStackedTiles(mapTile, viewRect());
    }
 }
 
@@ -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);
 }
 
@@ -141,12 +112,12 @@ 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);
@@ -168,7 +139,7 @@ bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
 
 void MapEngine::getTiles(QPoint sceneCoordinate)
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
     updateViewTilesSceneRect();
@@ -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);
         }
     }
 }
@@ -223,21 +192,21 @@ void MapEngine::viewResized(const QSize &size)
 
 void MapEngine::viewZoomFinished()
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
-    if (m_zoomedIn == YES) {
-        m_zoomedIn = NO;
+    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 = YES;
+        m_zoomedIn = true;
         emit zoomLevelChanged(m_zoomLevel);
 
         m_mapScene->setTilesDrawingLevels(m_zoomLevel);
@@ -248,7 +217,7 @@ void MapEngine::zoomIn()
 
 void MapEngine::zoomOut()
 {
-//    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
         m_zoomLevel--;
@@ -289,7 +258,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();