-#-------------------------------------------------
-#
+# -------------------------------------------------
# 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
}
-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);
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;
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;
/**
* @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);
};
#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);
}
if (!image.load(reply, 0))
image = QImage();
- emit mapImageReceived(image);
+ emit mapImageReceived(url, QPixmap::fromImage(image));
}
reply->deleteLater();
#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: