#include <QString>
#include <QStringList>
#include <QUrl>
+#include <QHash>
+#include <QHashIterator>
+#include <QQueue>
#include "mapengine.h"
#include "maptile.h"
MapEngine::MapEngine(QObject *parent)
: QObject(parent)
, m_zoomLevel(14)
+ , m_viewSize(QSize(800, 480))
{
m_mapScene = new MapScene(this);
void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
{
- int zoom = -1;
- int x = -1;
- int y = -1;
+ if (!mapTilesInScene.contains(url.toString())) {
+ int zoom = -1;
+ int x = -1;
+ int y = -1;
- parseURL(url, zoom, x, y);
+ parseURL(url, zoom, x, y);
- MapTile *mapTile = new MapTile();
- mapTile->setZoomLevel(zoom);
- mapTile->setTileNumber(QPoint(x, y));
- mapTile->setPixmap(pixmap);
+ MapTile *mapTile = new MapTile();
+ mapTile->setZoomLevel(zoom);
+ mapTile->setTileNumber(QPoint(x, y));
+ mapTile->setPixmap(pixmap);
- if (!mapTilesInScene.contains(url.toString())) {
mapTilesInScene.insert(url.toString(), mapTile);
m_mapScene->addMapTile(mapTile);
qDebug() << "Tile count: " << mapTilesInScene.size();
}
+
+ removeTiles();
}
QGraphicsScene* MapEngine::scene()
int gridWidth = (m_viewSize.width()/TILE_SIZE_X + 1) + (GRID_PADDING*2);
int gridHeight = (m_viewSize.height()/TILE_SIZE_Y + 1) + (GRID_PADDING*2);
+ qDebug() << "Grid size: " << gridWidth << "x" << gridHeight;
+
int topLeftX = tileCoordinate.x() - (gridWidth/2);
int topLeftY = tileCoordinate.y() - (gridHeight/2);
int tileMaxVal = tileMaxValue(m_zoomLevel);
+ QList<QString> tiles;
+
for (int x = topLeftX; x <= bottomRightX; ++x) {
for (int y = topLeftY; y <= bottomRightY; ++y) {
QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
- if (!mapTilesInScene.contains(url.toString())) {
+ if (!mapTilesInScene.contains(url.toString()))
emit fetchImage(url);
- }
+
+ tiles.append(url.toString());
}
}
+
+ //removeTiles(tiles);
+}
+
+void MapEngine::removeTiles()
+{
+// QHash<QString, MapTile*> removedTiles = mapTilesInScene;
+//
+// foreach (QString tileUrl, tiles) {
+// if (!mapTilesInScene.contains(tileUrl))
+// tilesToRemove.enqueue(tileUrl);
+// }
+
+
+ int zoomFactor = 1 << (MAX_ZOOM_LEVEL - m_zoomLevel);
+
+ qreal topLeftX = m_sceneCoordinate.x() - (m_viewSize.width()/2*zoomFactor);
+ qreal topLeftY = m_sceneCoordinate.y() - (m_viewSize.height()/2*zoomFactor);
+
+ qreal width = m_viewSize.width()*zoomFactor;
+ qreal height = m_viewSize.height()*zoomFactor;
+
+ qDebug() << topLeftX << ", " << topLeftY << " " << m_viewSize.width() << "x" << m_viewSize.height();
+
+ QList<QGraphicsItem *> viewTiles = m_mapScene->items(topLeftX, topLeftY, width, height);
+ QList<QGraphicsItem *> allTiles = m_mapScene->items();
+
+ qDebug() << "All tiles size: " << allTiles.size();
+ qDebug() << "View tiles size: " << viewTiles.size();
+
+ foreach (QGraphicsItem *tile, viewTiles) {
+ allTiles.removeOne(tile);
+ }
+
+ qDebug() << "All tiles size after removing view items: " << allTiles.size();
+
+ foreach (QGraphicsItem *tile, allTiles) {
+ m_mapScene->removeItem(tile);
+ delete tile;
+ }
}
void MapEngine::viewResized(const QSize &size)
void MapEngine::zoomIn()
{
- if (m_zoomLevel >= MAX_ZOOM_LEVEL)
+ //removeTiles();
+
+ /*if (m_zoomLevel >= MAX_ZOOM_LEVEL)
return;
m_zoomLevel++;
emit zoomLevelChanged(m_zoomLevel);
/// @todo START FETCHING TILES
- calculateTileGrid();
+ calculateTileGrid();*/
}
void MapEngine::zoomOut()
/// @todo START FETCHING TILES
calculateTileGrid();
}
+
+void MapEngine::setZoomLevel(int zoomLevel)
+{
+ m_zoomLevel = zoomLevel;
+}
+
+int MapEngine::getZoomLevel()
+{
+ return m_zoomLevel;
+}
QRect calculateGrid(QPointF sceneCoordinate);
+ void removeTiles();
+
+ int getZoomLevel();
+ void setZoomLevel(int zoomLevel);
+
public slots:
/**
* @brief Slot for setting current view location
*/
void setLocation(QPointF sceneCoordinate);
+ void viewResized(const QSize &size);
+
private:
/**
* @brief Build URL for donwloading single map tile from OpenStreetMap tile server
*/
void mapImageReceived(const QUrl &url, const QPixmap &pixmap);
- void viewResized(const QSize &size);
-
/**
* @brief Slot for zooming in
*
private:
void calculateTileGrid();
+ //void removeTiles(QList<QString> tiles);
MapScene *m_mapScene; ///< Scene for map tiles
MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
QHash<QString, MapTile *> mapTilesInScene; ///< List of map tiles in map scene
QSize m_viewSize;
QPointF m_sceneCoordinate;
+ QQueue<QString> mapTilesInView;
};
#endif // MAPENGINE_H
//void convertLatLonToTile();
void calculateRect();
void setLocation();
+ void removeTiles();
};
/**
void TestMapEngine::calculateRect()
{
MapEngine engine;
+ engine.viewResized(QSize(800, 480));
+ engine.setZoomLevel(14);
- QRect grid1 = QRect(0, 0, GRID_WIDTH, GRID_HEIGHT);
- QCOMPARE(engine.calculateGrid(QPointF(550.23*16, 550.23*16)), grid1);
+ int zoomFactor = 1 << (MAX_ZOOM_LEVEL - engine.getZoomLevel());
- QRect grid2 = QRect(-2, -2, GRID_WIDTH, GRID_HEIGHT);
- QCOMPARE(engine.calculateGrid(QPointF(0.23*16, 0.23*16)), grid2);
+ QRect grid1 = QRect(-1, 0, 6, 4);
+ QCOMPARE(engine.calculateGrid(QPointF(550.23*zoomFactor, 550.23*zoomFactor)), grid1);
- QRect grid3 = QRect(1018, 498, GRID_WIDTH, GRID_HEIGHT);
- QCOMPARE(engine.calculateGrid(QPointF(1020*TILE_SIZE_X*16, 500*TILE_SIZE_Y*16)), grid3);
+ QRect grid2 = QRect(-3, -2, 6, 4);
+ QCOMPARE(engine.calculateGrid(QPointF(0.23*zoomFactor, 0.23*zoomFactor)), grid2);
+
+ engine.viewResized(QSize(1280, 1024));
+
+ QRect grid3 = QRect(1016, 497, 8, 7);
+ QCOMPARE(engine.calculateGrid(QPointF(1020*TILE_SIZE_X*zoomFactor, 500*TILE_SIZE_Y*zoomFactor)), grid3);
}
void TestMapEngine::setLocation()
{
MapEngine engine;
+ engine.setZoomLevel(14);
+ engine.viewResized(QSize(800, 480));
+
+ int zoomFactor = 1 << (MAX_ZOOM_LEVEL - engine.getZoomLevel());
QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(QUrl)));
+ QTest::qWait(2000);
+ fetchImageSpy.clear();
- engine.setLocation(QPointF(550.23*16, 550.23*16));
- QTest::qWait(1000);
- QCOMPARE(fetchImageSpy.count(), GRID_WIDTH*GRID_HEIGHT);
+ engine.setLocation(QPointF(1220.23*zoomFactor, 1220.23*zoomFactor));
+ QTest::qWait(2000);
+ QCOMPARE(fetchImageSpy.count(), 6*4);
//Move one tile right and one up = 7 new tiles
- engine.setLocation(QPointF((550.23 + 256.0)*16, (550.23 + 256.0)*16));
- QTest::qWait(1000);
- QCOMPARE(fetchImageSpy.count(), GRID_WIDTH*GRID_HEIGHT+7);
+// engine.setLocation(QPointF((550.23 + 256.0)*16, (550.23 + 256.0)*16));
+// QTest::qWait(2000);
+// QCOMPARE(fetchImageSpy.count(), 5*3+7);
+}
+
+void TestMapEngine::removeTiles()
+{
+ MapEngine engine;
+
+ engine.viewResized(QSize());
}
QTEST_MAIN(TestMapEngine)