Updated tests cases matching the new tabs
[situare] / src / map / mapfetcher.h
index 3f8b791..11c1838 100644 (file)
@@ -3,6 +3,7 @@
    Copyright (C) 2010  Ixonos Plc. Authors:
 
        Jussi Laitinen - jussi.laitinen@ixonos.com
+       Sami Rämö - sami.ramo@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -25,7 +26,8 @@
 #include <QtCore>
 #include <QNetworkAccessManager>
 
-#include "mapfetcher.h"
+#include "maptilerequest.h"
+#include "network/networkaccessmanager.h"
 
 class QNetworkReply;
 class QUrl;
@@ -34,6 +36,7 @@ class QUrl;
 * @brief MapFetcher handles requests to get map tiles.
 *
 * @author Jussi Laitinen jussi.laitinen@ixonos.com
+* @author Sami Rämö sami.ramo@ixonos.com
 */
 class MapFetcher : public QObject
 {
@@ -46,26 +49,92 @@ public:
     * @param manager Network access manager
     * @param parent parent object
     */
-    MapFetcher(QNetworkAccessManager *manager, QObject *parent = 0);
+    MapFetcher(NetworkAccessManager *manager, QObject *parent = 0);
 
 /*******************************************************************************
  * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
-public slots:
+public:
+    /**
+    * @brief Set size of the download queue
+    *
+    * @param size New size
+    */
+    void setDownloadQueueSize(int size);
 
-    void enqueueFetchMapImage(QUrl url);
+public slots:
+    /**
+    * @brief Enqueue fetching of map image
+    *
+    * Image fetching is triggered after events have been processed
+    * so the UI stays responsive.
+    *
+    * @param zoomLevel Zoom level
+    * @param x Tile x index
+    * @param y Tile y index
+    */
+    void enqueueFetchMapImage(int zoomLevel, int x, int y);
 
 private:
     /**
-    * @brief Loads image from cache if it's available and emits imageReveived
-    * signal with url and image. If image is in cache returns true, false
-    * otherwise.
+    * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
+    *
+    * @param zoomLevel Zoom level
+    * @param tileNumbers Tile x & y numbers
+    * @return URL for the required tile
+    */
+    QUrl buildURL(int zoomLevel, QPoint tileNumbers);
+
+    /**
+    * @brief Limit pending requests list size to m_pendingRequestsSize
+    *
+    */
+    void limitPendingRequestsListSize();
+
+    /**
+    * @brief Loads image from cache
     *
-    * @param url
-    * @return bool true if image was loaded from cache, false otherwise
+    * Tries to load requested, or upper zoom level image from the cache. Emits imageReveived signal
+    * if any image was found. If image found was from requested zoom level then expidation date is
+    * checked.
+    *
+    * @param url URL of the requested image
+    * @return True if requested zoom level image was found and was not expired. Otherwise false.
     */
     bool loadImageFromCache(const QUrl &url);
 
+    /**
+    * @brief Find first item based on criteria if the request is already checked from the cache
+    *
+    * If cacheChecked is true, then returns index of the first request which is already
+    * checked from the cache. If cacheChecked is false then returns first item which
+    * isn't checked from the cache. Returns -1 if the item is not found.
+    *
+    * @param cacheChecked Search criteria
+    * @return Index of the first matching request, or -1 if not found.
+    */
+    int newestRequestIndex(bool cacheChecked);
+
+    /**
+    * @brief Parse given URL to zoom, x and y values. Parsed values are
+    * placed in variables given as parameters.
+    *
+    * @param url url to parse
+    * @param [out] zoom zoom variable
+    * @param [out] x x variable
+    * @param [out] y y variable
+    */
+    void parseURL(const QUrl &url, int *zoom, int *x, int *y);
+
+    /**
+    * @brief Translate indexes to matching upper level map tile indexes
+    * @param[in,out] zoomLevel Zoom level
+    * @param[in,out] x x index
+    * @param[in,out] y y index
+    * @return true if translation succeeded, otherwise false
+    */
+    bool translateIndexesToUpperLevel(int &zoomLevel, int &x, int &y);
+
 private slots:
 
     /**
@@ -78,11 +147,14 @@ private slots:
     void downloadFinished(QNetworkReply *reply);
 
     /**
-    * @brief Fetch image from given URL.
+    * @brief Check next request if it is found from cache
     *
-    * @param url URL to image
+    * Next queued request, which is not already checked against cache, is taken
+    * from the queue and checked against cache. If not found from cache, then
+    * cache checked flag is set. New download is started if there aren't too
+    * many simultaneus downloads already running.
     */
-    void fetchMapImage();
+    void checkNextRequestFromCache();
 
     /**
     * @brief This slot is called when next download is started. Takes url
@@ -98,31 +170,30 @@ signals:
     * @brief Signal which is emitted when a map tile
     * is received from the server and loaded to pixmap.
     *
-    * @param url URL to image
+    * @param zoomLevel Zoom level
+    * @param x Tile x index
+    * @param y Tile y index
     * @param image image pixmap
     */
-    void mapImageReceived(const QUrl &url, const QPixmap &image);
+    void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);
 
     /**
-    * @brief Signal which is emitted when there is error
-    * in network reply.
+    * @brief Signals error
     *
-    * @param message error message
+    * @param context error context
+    * @param error error code
     */
-    void error(const QString &message);
+    void error(const int context, const int error);
 
 /*******************************************************************************
  * DATA MEMBERS
  ******************************************************************************/
 private:
-    static const int MAX_PARALLEL_DOWNLOADS = 2; ///< Max simultaneous parallel downloads
-    static const int DOWNLOAD_QUEUE_SIZE = 50; ///< Max downloads waiting in queue
-
     QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
-    QQueue<QUrl> m_downloadQueue;             ///< Queue of pending requests
-    QList<QUrl> m_FetchMapImagesList;
-    bool m_isFetchMapImagesTimerRunning;
-    QNetworkAccessManager *m_manager;       ///< Network access manager
+    int m_pendingRequestsSize; ///< Max number of pending requests
+    bool m_fetchMapImagesTimerRunning; ///< is the singleshot timer already running
+    NetworkAccessManager *m_manager; ///< Network access manager
+    QList<MapTileRequest> m_pendingRequests; ///< List of map image fetching requests
 };
 
 #endif