Remove other level tiles when all current level tiles exists
authorSami Rämö <sami.ramo@ixonos.com>
Thu, 10 Jun 2010 12:07:43 +0000 (15:07 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Thu, 10 Jun 2010 12:07:43 +0000 (15:07 +0300)
src/map/mapengine.cpp
src/map/mapfetcher.cpp
src/map/mapscene.cpp
src/map/mapscene.h

index e83b280..ba65734 100644 (file)
@@ -209,6 +209,7 @@ void MapEngine::getTiles(QPoint sceneCoordinate)
 
     m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
     updateViewTilesSceneRect();
+    m_mapScene->setTilesGrid(m_viewTilesGrid);
 
     int topLeftX = m_viewTilesGrid.topLeft().x();
     int topLeftY = m_viewTilesGrid.topLeft().y();
index 55b740b..ae0ef99 100644 (file)
@@ -196,6 +196,9 @@ bool MapFetcher::loadImageFromCache(const QUrl &url)
         parseURL(url, &zoomLevel, &x, &y);
         int originalZoomLevel = zoomLevel;
 
+//        QTime time;
+//        time.start();
+
         // try to fetch requested and upper level images until found or MAX_UPPER_LEVELS
         // limit is reached
         const int MAX_UPPER_LEVELS = 4;
@@ -214,6 +217,8 @@ bool MapFetcher::loadImageFromCache(const QUrl &url)
                  && (originalZoomLevel - zoomLevel) < MAX_UPPER_LEVELS
                  && translateIndexesToUpperLevel(zoomLevel, x, y));
 
+//        qWarning() << __PRETTY_FUNCTION__ << "time:" << time.elapsed() << "ms";
+
         // check expiration if image was found from requested level
         if (imageFound && (originalZoomLevel == zoomLevel)) {
             // check if image is expired
index 69745d3..29edb27 100644 (file)
@@ -43,7 +43,7 @@ MapScene::MapScene(QObject *parent)
 
 void MapScene::addTile(int tileZoomLevel, QPoint tileNumber, const QPixmap &image, int viewZoomLevel)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+//    qWarning() << __PRETTY_FUNCTION__ << "x:" << tileNumber.x() << "y:" << tileNumber.y();
 
     // tile might already be in the scene if:
     //    - expired tile was returned from the cache to be temporarily displayed while downloading
@@ -64,7 +64,10 @@ void MapScene::addTile(int tileZoomLevel, QPoint tileNumber, const QPixmap &imag
     m_mapTilesInScene.insert(hashKey, tile);
     addItem(tile);
 
+    qWarning() << __PRETTY_FUNCTION__ << "tiles:" << m_mapTilesInScene.count();
+
     enqueueRemoveStackedTiles(tile);
+    removeOtherLevelTiles();
 }
 
 void MapScene::enqueueRemoveStackedTiles(MapTile *newTile)
@@ -115,18 +118,26 @@ void MapScene::runNextStackedTilesRemoval()
 
 void MapScene::removeOtherLevelTiles()
 {
-    qWarning() << __PRETTY_FUNCTION__;
+//    qWarning() << __PRETTY_FUNCTION__;
 
-    QList<QGraphicsItem *> allItems = items();
-    foreach(QGraphicsItem *item, allItems) {
-        MapTile *tile = dynamic_cast<MapTile *>(item);
-        if (tile && tile->zoomLevel() != m_zoomLevel) {
+    for (int x = m_viewTilesGrid.left(); x <= m_viewTilesGrid.right(); x++) {
+        for (int y = m_viewTilesGrid.top(); y <= m_viewTilesGrid.bottom(); y++) {
+//            qWarning() << __PRETTY_FUNCTION__ << "x:" << x << "y:" << y;
+            if (!m_mapTilesInScene.contains(MapEngine::tilePath(m_zoomLevel, x, y)))
+                return;
+        }
+    }
+
+//    qWarning() << __PRETTY_FUNCTION__ << "all current grid tiles are in the scene";
+
+    foreach(MapTile *tile, m_mapTilesInScene) {
+        if (tile->zoomLevel() != m_zoomLevel) {
             removeTile(tile);
-            qWarning() << __PRETTY_FUNCTION__ << "removed other level tile";
+            qWarning() << __PRETTY_FUNCTION__ << "removed other level tile -------------";
         }
     }
 
-    qWarning() << __PRETTY_FUNCTION__ << "items in scene:" << items().count();
+//    qWarning() << __PRETTY_FUNCTION__ << "items in scene:" << items().count();
 }
 
 void MapScene::removeOutOfViewTiles(QRect tilesGrid, int zoomLevel)
@@ -166,7 +177,7 @@ void MapScene::removeOutOfViewTiles(QRect tilesGrid, int zoomLevel)
             removeTile(tile);
     }
 
-    qWarning() << __PRETTY_FUNCTION__ << "items in scene:" << items().count();
+//    qWarning() << __PRETTY_FUNCTION__ << "items in scene:" << items().count();
 }
 
 void MapScene::removeStackedTiles(MapTile *newTile)
@@ -188,7 +199,7 @@ void MapScene::removeStackedTiles(MapTile *newTile)
             }
         }
     }
-    qWarning() << __PRETTY_FUNCTION__ << "items in scene:" << items().count();
+//    qWarning() << __PRETTY_FUNCTION__ << "items in scene:" << items().count();
 }
 
 void MapScene::removeTile(MapTile *tile)
@@ -201,6 +212,8 @@ void MapScene::removeTile(MapTile *tile)
     removeItem(tile);
     m_removeStackedTilesList.removeAll(tile);
     delete tile;
+
+    qWarning() << __PRETTY_FUNCTION__ << "tiles:" << m_mapTilesInScene.count();
 }
 
 void MapScene::setSceneVerticalOverlap(int viewHeight, int zoomLevel)
@@ -228,10 +241,15 @@ void MapScene::setTilesDrawingLevels(int zoomLevel)
     }
 }
 
+void MapScene::setTilesGrid(QRect grid)
+{
+    m_viewTilesGrid = grid;
+}
+
 void MapScene::setZoomLevel(int zoomLevel)
 {
     m_zoomLevel = zoomLevel;
-    QTimer::singleShot(10000, this, SLOT(removeOtherLevelTiles()));
+//    QTimer::singleShot(10000, this, SLOT(removeOtherLevelTiles()));
 }
 
 void MapScene::spanItems(int zoomLevel, QPoint sceneCoordinate, QSize viewSize)
index e924471..98305c9 100644 (file)
@@ -124,6 +124,8 @@ public:
     */
     void setTilesDrawingLevels(int zoomLevel);
 
+    void setTilesGrid(QRect grid);
+
     void setZoomLevel(int zoomLevel);
 
     void spanItems(int zoomLevel, QPoint sceneCoordinate, QSize viewSize);
@@ -171,6 +173,7 @@ private:
     QHash<QString, MapTile *> m_mapTilesInScene;  ///< List of map tiles in map scene
     QList<MapTile *> m_removeStackedTilesList; ///< "Queue" for stacked tiles removal requests
     QRect m_tilesSceneRect; ///< Current viewable area
+    QRect m_viewTilesGrid;
 };
 
 #endif // MAPSCENE_H