Moved URL builder and parser to MapFetcher
[situare] / src / map / mapfetcher.cpp
index e6e7416..0617235 100644 (file)
@@ -47,6 +47,43 @@ MapFetcher::MapFetcher(QNetworkAccessManager *manager, QObject *parent)
             downloadFinished(QNetworkReply*)));
 }
 
+QUrl MapFetcher::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 MapFetcher::checkNextRequestFromCache()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    int i = newestRequestIndex(false);
+
+    if (i != -1) {
+        if (!m_pendingRequests[i].url.isEmpty() && m_pendingRequests[i].url.isValid()) {
+            if (loadImageFromCache(m_pendingRequests[i].url)) {
+                // was found, remove from the list
+                m_pendingRequests.removeAt(i);
+            }
+            else {
+                // didn't found from cache so mark cache checked and leave to queue
+                m_pendingRequests[i].cacheChecked = true;
+
+                if (m_currentDownloads.size() < MAX_PARALLEL_DOWNLOADS)
+                    startNextDownload();
+            }
+        }
+    }
+
+    // schedule checking of the next request if the list is not empty
+    if (newestRequestIndex(false) != -1)
+        QTimer::singleShot(0, this, SLOT(checkNextRequestFromCache()));
+    else
+        m_fetchMapImagesTimerRunning = false;
+}
+
 void MapFetcher::downloadFinished(QNetworkReply *reply)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -58,7 +95,12 @@ void MapFetcher::downloadFinished(QNetworkReply *reply)
         if (!image.load(reply, 0))
             image = QImage();
 
-        emit mapImageReceived(url, QPixmap::fromImage(image));
+        int zoomLevel;
+        int x;
+        int y;
+        parseURL(url, zoomLevel, x, y);
+
+        emit mapImageReceived(zoomLevel, x, y, QPixmap::fromImage(image));
     }
     else {
         emit error(reply->errorString());
@@ -69,8 +111,10 @@ void MapFetcher::downloadFinished(QNetworkReply *reply)
     startNextDownload();
 }
 
-void MapFetcher::enqueueFetchMapImage(QUrl url)
+void MapFetcher::enqueueFetchMapImage(int zoomLevel, int x, int y)
 {
+    QUrl url = buildURL(zoomLevel, QPoint(x, y));
+
     qDebug() << __PRETTY_FUNCTION__ << "url:" << url.toString();
 
     // check if new request is already in the list and move it to the begin of the list...
@@ -100,50 +144,6 @@ void MapFetcher::enqueueFetchMapImage(QUrl url)
     }
 }
 
-int MapFetcher::newestRequestIndex(bool cacheChecked)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (!m_pendingRequests.isEmpty()) {
-        for (int i = 0; i < m_pendingRequests.size(); i++) {
-            if (m_pendingRequests[i].cacheChecked == cacheChecked) {
-                return i;
-            }
-        }
-    }
-
-    return -1;
-}
-
-void MapFetcher::checkNextRequestFromCache()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    int i = newestRequestIndex(false);
-
-    if (i != -1) {
-        if (!m_pendingRequests[i].url.isEmpty() && m_pendingRequests[i].url.isValid()) {
-            if (loadImageFromCache(m_pendingRequests[i].url)) {
-                // was found, remove from the list
-                m_pendingRequests.removeAt(i);
-            }
-            else {
-                // didn't found from cache so mark cache checked and leave to queue
-                m_pendingRequests[i].cacheChecked = true;
-
-                if (m_currentDownloads.size() < MAX_PARALLEL_DOWNLOADS)
-                    startNextDownload();
-            }
-        }
-    }
-
-    // schedule checking of the next request if the list is not empty
-    if (newestRequestIndex(false) != -1)
-        QTimer::singleShot(0, this, SLOT(checkNextRequestFromCache()));
-    else
-        m_fetchMapImagesTimerRunning = false;
-}
-
 void MapFetcher::limitPendingRequestsListSize()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -180,7 +180,13 @@ bool MapFetcher::loadImageFromCache(const QUrl &url)
 
             if (image.load(cacheImage, 0)) {
                 imageFound = true;
-                emit mapImageReceived(url, QPixmap::fromImage(image));
+
+                int zoomLevel;
+                int x;
+                int y;
+                parseURL(url, zoomLevel, x, y);
+
+                emit mapImageReceived(zoomLevel, x, y, QPixmap::fromImage(image));
             }
 
             delete cacheImage;
@@ -190,6 +196,37 @@ bool MapFetcher::loadImageFromCache(const QUrl &url)
     return imageFound;
 }
 
+int MapFetcher::newestRequestIndex(bool cacheChecked)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (!m_pendingRequests.isEmpty()) {
+        for (int i = 0; i < m_pendingRequests.size(); i++) {
+            if (m_pendingRequests[i].cacheChecked == cacheChecked) {
+                return i;
+            }
+        }
+    }
+
+    return -1;
+}
+
+void MapFetcher::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 MapFetcher::setDownloadQueueSize(int size)
 {
     qDebug() << __PRETTY_FUNCTION__ << "size:" << size;