Added map tile delete methods.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 12 Apr 2010 13:14:56 +0000 (16:14 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 12 Apr 2010 13:14:56 +0000 (16:14 +0300)
src/map/mapengine.cpp
src/map/mapengine.h
src/ui/mapviewscreen.cpp
tests/testmap/testmapengine/testmapengine.cpp

index 67b07c6..49eafb9 100644 (file)
@@ -24,6 +24,9 @@
 #include <QString>
 #include <QStringList>
 #include <QUrl>
+#include <QHash>
+#include <QHashIterator>
+#include <QQueue>
 
 #include "mapengine.h"
 #include "maptile.h"
@@ -34,6 +37,7 @@
 MapEngine::MapEngine(QObject *parent)
     : QObject(parent)
     , m_zoomLevel(14)
+    , m_viewSize(QSize(800, 480))
 {
     m_mapScene = new MapScene(this);
 
@@ -90,23 +94,25 @@ void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
 
 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()
@@ -126,6 +132,8 @@ QRect MapEngine::calculateGrid(QPointF sceneCoordinate)
     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);
 
@@ -149,6 +157,8 @@ void MapEngine::calculateTileGrid()
 
     int tileMaxVal = tileMaxValue(m_zoomLevel);
 
+    QList<QString> tiles;
+
     for (int x = topLeftX; x <= bottomRightX; ++x) {
         for (int y = topLeftY; y <= bottomRightY; ++y) {
 
@@ -167,11 +177,52 @@ void MapEngine::calculateTileGrid()
 
             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)
@@ -182,13 +233,15 @@ 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()
@@ -201,3 +254,13 @@ void MapEngine::zoomOut()
     /// @todo START FETCHING TILES
     calculateTileGrid();
 }
+
+void MapEngine::setZoomLevel(int zoomLevel)
+{
+    m_zoomLevel = zoomLevel;
+}
+
+int MapEngine::getZoomLevel()
+{
+    return m_zoomLevel;
+}
index 75b9275..269212b 100644 (file)
@@ -128,6 +128,11 @@ public:
 
     QRect calculateGrid(QPointF sceneCoordinate);
 
+    void removeTiles();
+
+    int getZoomLevel();
+    void setZoomLevel(int zoomLevel);
+
 public slots:
     /**
     * @brief Slot for setting current view location
@@ -137,6 +142,8 @@ public slots:
     */
     void setLocation(QPointF sceneCoordinate);
 
+    void viewResized(const QSize &size);
+
 private:
     /**
     * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
@@ -171,8 +178,6 @@ private slots:
     */
     void mapImageReceived(const QUrl &url, const QPixmap &pixmap);
 
-    void viewResized(const QSize &size);
-
     /**
     * @brief Slot for zooming in
     *
@@ -207,6 +212,7 @@ signals:
 private:
 
     void calculateTileGrid();
+    //void removeTiles(QList<QString> tiles);
 
     MapScene *m_mapScene; ///< Scene for map tiles
     MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
@@ -214,6 +220,7 @@ private:
     QHash<QString, MapTile *> mapTilesInScene;  ///< List of map tiles in map scene
     QSize m_viewSize;
     QPointF m_sceneCoordinate;
+    QQueue<QString> mapTilesInView;
 };
 
 #endif // MAPENGINE_H
index 8281a12..dd594d4 100644 (file)
@@ -72,7 +72,7 @@ void MapViewScreen::searchMap()
 
 void MapViewScreen::zoomInMap()
 {
-    //mapEngine->zoomLevelChanged();
+
 }
 
 void MapViewScreen::zoomOutMap()
index e0c67a0..3c5c50d 100644 (file)
@@ -32,6 +32,7 @@ private slots:
     //void convertLatLonToTile();
     void calculateRect();
     void setLocation();
+    void removeTiles();
 };
 
 /**
@@ -72,31 +73,50 @@ private slots:
 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)