Modified mapEngine and mapFetcher classes.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 29 Mar 2010 09:23:05 +0000 (12:23 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 29 Mar 2010 09:23:05 +0000 (12:23 +0300)
Situare.pro
src/map/mapengine.cpp
src/map/mapengine.h
src/map/mapfetcher.cpp
src/map/mapfetcher.h

index 26f1ef4..9b1f980 100644 (file)
@@ -1,14 +1,14 @@
-#-------------------------------------------------
-#
+# -------------------------------------------------
 # Project created by QtCreator 2010-03-26T07:57:35
-#
-#-------------------------------------------------
-
+# -------------------------------------------------
 TARGET = Situare
 TEMPLATE = app
-
-
-SOURCES += src/main.cpp\
-        src/ui/mainwindow.cpp
-
-HEADERS  += src/ui/mainwindow.h
+SOURCES += src/main.cpp \
+    src/ui/mainwindow.cpp \
+    src/map/mapfetcher.cpp \
+    src/map/mapengine.cpp \
+    tests/testMap/testmapfetcher.cpp \
+    tests/testMap/testmapengine.cpp
+HEADERS += src/ui/mainwindow.h \
+    src/map/mapfetcher.h \
+    src/map/mapengine.h
index 575f8c9..9b171a5 100644 (file)
@@ -9,7 +9,7 @@ MapEngine::MapEngine(QObject *parent)
 
 }
 
-QPoint MapEngine::tileFromCoordinate(qreal latitude, qreal longitude, int zoom)
+/*QPoint MapEngine::latLonToTile(qreal latitude, qreal longitude, int zoom)
 {
     //Power of two
     qreal z = static_cast<qreal>(1 << zoom);
@@ -18,11 +18,10 @@ QPoint MapEngine::tileFromCoordinate(qreal latitude, qreal longitude, int zoom)
     qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
                                 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
 
-    qDebug() << x*z << "," << y*z;
     return QPoint(qFloor(x*z), qFloor(y*z));
-}
+}*/
 
-qreal MapEngine::longitudeFromTileX(int x, int zoom)
+qreal MapEngine::tileXToLongitude(int x, int zoom)
 {
     qreal z = static_cast<qreal>(1 << zoom);
     qreal lon = x / z * 360.0 - 180.0;
@@ -30,7 +29,7 @@ qreal MapEngine::longitudeFromTileX(int x, int zoom)
     return lon;
 }
 
-qreal MapEngine::latitudeFromTileY(int y, int zoom)
+qreal MapEngine::tileYToLatitude(int y, int zoom)
 {
     qreal z = static_cast<qreal>(1 << zoom);
     qreal n = M_PI - 2 * M_PI * y / zoom;
index c92ef88..2d8c028 100644 (file)
@@ -26,33 +26,42 @@ public:
     /**
     * @brief Transforms coordinates to tile x,y values.
     *
-    * @fn tileFromCoordinate
+    * @fn latLonToTile
     * @param latitude latitude value
     * @param longitude longitude value
     * @param zoom zoom level
     * @return QPoint tile x,y
     */
-    QPoint tileFromCoordinate(qreal latitude, qreal longitude, int zoom);
+    static QPoint latLonToTile(qreal latitude, qreal longitude, int zoom) {
+        //Power of two
+        qreal z = static_cast<qreal>(1 << zoom);
+
+        qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
+        qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
+                                    / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
+
+        return QPoint(qFloor(x*z), qFloor(y*z));
+    }
 
     /**
     * @brief Transforms tile x value to longitude.
     *
-    * @fn longitudeFromTile
+    * @fn tileXToLongitude
     * @param x tile x value
     * @param zoom zoom value
     * @return qreal longitude
     */
-    qreal longitudeFromTileX(int x, int zoom);
+    qreal tileXToLongitude(int x, int zoom);
 
     /**
     * @brief Transforms tile y value to latitude.
     *
-    * @fn latitudeFromTile
+    * @fn tileYToLatitude
     * @param y tile y value
     * @param zoom zoom value
     * @return qreal latitude
     */
-    qreal latitudeFromTileY(int y, int zoom);
+    qreal tileYToLatitude(int y, int zoom);
 
 };
 
index cdc7923..1fa5ee4 100644 (file)
@@ -4,23 +4,30 @@
 #include <QUrl>
 #include <QDebug>
 #include <QPixmap>
+#include <QNetworkDiskCache>
+#include <QDesktopServices>
 
 #include "mapfetcher.h"
 
 MapFetcher::MapFetcher(QObject *parent)
     : QObject(parent)
 {
+    QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
+    diskCache->setCacheDirectory(QDesktopServices::storageLocation(
+            QDesktopServices::CacheLocation));
+    m_manager.setCache(diskCache);
+
     connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
 }
 
 void MapFetcher::fetchMapImage(const QUrl &url)
 {
+    qDebug() << "fetchMapImage()";
     if (url.isEmpty())
         return;
 
     QNetworkRequest request(url);
     request.setRawHeader("User-Agent", "Map Test");
-    qDebug() << request.url();
     m_manager.get(request);
 }
 
@@ -37,7 +44,7 @@ void MapFetcher::downloadFinished(QNetworkReply *reply)
         if (!image.load(reply, 0))
             image = QImage();
 
-        emit mapImageReceived(image);
+        emit mapImageReceived(url, QPixmap::fromImage(image));
     }
 
     reply->deleteLater();
index 12ba61f..26fb27e 100644 (file)
@@ -9,20 +9,64 @@ class QUrl;
 
 #include "mapfetcher.h"
 
+/**
+* @brief MapFetcher handles requests to get map tiles.
+*
+*
+*
+* @class MapFetcher mapfetcher.h "map/mapfetcher.h"
+*/
 class MapFetcher : public QObject
 {
     Q_OBJECT
 
 public:
+    /**
+    * @brief Constructor for MapFetcher.
+    *
+    * @fn MapFetcher
+    * @param parent parent object
+    */
     MapFetcher(QObject *parent = 0);
+
     ~MapFetcher();
+
+    /**
+    * @brief Fetch image from given URL.
+    *
+    * @fn fetchMapImage
+    * @param url URL to image
+    */
     void fetchMapImage(const QUrl &url);
 
-signals:
-    void mapImageReceived(const QImage &image);
+signals:    
+    /**
+    * @brief Signal which is emitted when a map tile
+    * is received from the server and loaded to pixmap.
+    *
+    * @fn mapImageReceived
+    * @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.
+    *
+    * @fn error
+    * @param message error message
+    */
     void error(const QString &message);
 
-public slots:
+private slots:
+    /**
+    * @brief This slot is called when network manager has finished
+    * the download.
+    *
+    * @fn downloadFinished
+    * @param reply
+    */
     void downloadFinished(QNetworkReply *reply);
 
 private: