Stacked tile removal zValue check, doxygen generating class diagrams
[situare] / src / map / mapfetcher.h
index f39fa39..af3818d 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
 class QNetworkReply;
 class QUrl;
 
-#include "mapfetcher.h"
-
 /**
 * @brief MapFetcher handles requests to get map tiles.
 *
-*
-*
-* @class MapFetcher mapfetcher.h "map/mapfetcher.h"
+* @author Jussi Laitinen jussi.laitinen@ixonos.com
+* @author Sami Rämö sami.ramo@ixonos.com
 */
 class MapFetcher : public QObject
 {
     Q_OBJECT
 
+    /**
+    * @brief Type for download requests
+    *
+    * @typedef Request
+    */    
+    /**
+    * @brief Struct for download requests
+    *
+    * @struct _Request
+    */
+    typedef struct _Request {
+        bool cacheChecked; ///< Is this request already checked from the cache
+        QUrl url; ///< URL
+    } Request;
+
 public:
     /**
     * @brief Constructor for MapFetcher.
     *
-    * @fn MapFetcher
+    * @param manager Network access manager
     * @param parent parent object
     */
-    MapFetcher(QObject *parent = 0, QNetworkAccessManager *manager = 0);
+    MapFetcher(QNetworkAccessManager *manager, QObject *parent = 0);
 
-    ~MapFetcher();
+/*******************************************************************************
+ * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+public:
+    /**
+    * @brief Set size of the download queue
+    *
+    * @param size New size
+    */
+    void setDownloadQueueSize(int size);
 
+public slots:
     /**
-    * @brief Fetch image from given URL.
+    * @brief Enqueue fetching of map image
     *
-    * @fn fetchMapImage
-    * @param url URL to image
+    * Image fetching is triggered after events have been processed
+    * so the UI stays responsive.
+    *
+    * @param url URL of the image to be fetched
     */
-    void fetchMapImage(const QUrl &url);
+    void enqueueFetchMapImage(QUrl url);
+
+private:
 
-signals:    
     /**
-    * @brief Signal which is emitted when a map tile
-    * is received from the server and loaded to pixmap.
+    * @brief Limit pending requests list size to m_pendingRequestsSize
     *
-    * @fn mapImageReceived
-    * @param url URL to image
-    * @param image image pixmap
     */
-    void mapImageReceived(const QUrl &url, const QPixmap &image);
+    void limitPendingRequestsListSize();
 
     /**
-    * @brief Signal which is emitted when there is error
-    * in network reply.
+    * @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.
     *
-    * @fn error
-    * @param message error message
+    * @param url
+    * @return bool true if image was loaded from cache, false otherwise
     */
-    void error(const QString &message);
+    bool loadImageFromCache(const QUrl &url);
 
-public slots:
+    /**
+    * @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);
+
+private slots:
 
     /**
     * @brief This slot is called when network manager has finished
-    * the download.
+    * the download. Loads image and emits imageReceived signal with
+    * url and image. If there was a error in reply emits error-signal.
     *
-    * @fn downloadFinished
     * @param reply
     */
     void downloadFinished(QNetworkReply *reply);
 
     /**
-    * @brief This slot is called when
+    * @brief Check next request if it is found from cache
     *
-    * @fn startNextDownload
+    * 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 checkNextRequestFromCache();
+
+    /**
+    * @brief This slot is called when next download is started. Takes url
+    * from queue, sends request and puts request to download queue.
     */
     void startNextDownload();
 
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
+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 image image pixmap
+    */
+    void mapImageReceived(const QUrl &url, const QPixmap &image);
+
+    /**
+    * @brief Signal which is emitted when there is error
+    * in network reply.
+    *
+    * @param message error message
+    */
+    void error(const QString &message);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
 private:
-    QNetworkAccessManager *m_manager;
-    QList<QNetworkReply*> currentDownloads;
-    QQueue<QUrl> downloadQueue;
+    static const int MAX_PARALLEL_DOWNLOADS = 2; ///< Max simultaneous parallel downloads
+
+    QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
+    int m_pendingRequestsSize; ///< Max number of pending requests
+    bool m_fetchMapImagesTimerRunning; ///< is the singleshot timer already running
+    QNetworkAccessManager *m_manager; ///< Network access manager
+    QList<Request> m_pendingRequests; ///< List of map image fetching requests
 };
 
 #endif