Try max 4 upper level images from cache
authorSami Rämö <sami.ramo@ixonos.com>
Wed, 9 Jun 2010 12:41:36 +0000 (15:41 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Wed, 9 Jun 2010 12:41:36 +0000 (15:41 +0300)
 - also some cleanup

src/map/mapcommon.h
src/map/mapengine.cpp
src/map/mapengine.h
src/map/mapfetcher.cpp
src/map/mapscene.cpp

index b3270c5..67a2d62 100644 (file)
@@ -35,6 +35,8 @@ const int MIN_MAP_SCENE_NORMAL_LEVEL = MAX_MAP_ZOOM_LEVEL + 1;
 
 const int MAX_TILES_PER_SIDE = (1 << MAX_MAP_ZOOM_LEVEL);
 const int WORLD_PIXELS_X = MAX_TILES_PER_SIDE * TILE_SIZE_X;
+const int MAP_SCENE_MIN_PIXEL_X = -WORLD_PIXELS_X / 2;
+const int MAP_SCENE_MAX_PIXEL_X = WORLD_PIXELS_X * 2;
 
 /**
 * @var DEFAULT_START_ZOOM_LEVEL
@@ -75,7 +77,7 @@ const int DEFAULT_ZOOM_LEVEL = 14;       ///< Default zoom level
 const qreal DEFAULT_LONGITUDE = 0.0000;  ///< Default longitude value
 const qreal DEFAULT_LATITUDE = 0.0000; ///< Default latitude value
 
-const int GRID_PADDING = 2;  ///< Grid padding used in tile grid calculation
+const int GRID_PADDING = 0;  ///< Grid padding used in tile grid calculation
 
 const QString OSM_LICENSE = QString::fromUtf8("© OpenStreetMap contributors, CC-BY-SA");
 
index 305d75d..25ae4de 100644 (file)
@@ -306,6 +306,7 @@ void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &ima
     QPoint tileNumber(x, y);
     m_mapScene->addTile(zoomLevel, tileNumber, image, m_zoomLevel);
 
+    // add 1 so odd width is rounded up and even is rounded down
     int tilesGridWidthHalf = (m_viewTilesGrid.width() + 1) / 2;
 
     // expand to east side? (don't need to expand over padding)
@@ -316,7 +317,7 @@ void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &ima
     }
 
     // expand to west side? (don't need to expand over padding)
-    if (tileNumber.x() > (tileMaxValue(zoomLevel) - tilesGridWidthHalf + GRID_PADDING)) {
+    if (tileNumber.x() > (tileMaxValue(zoomLevel) - tilesGridWidthHalf + GRID_PADDING - 1)) {
         QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
         m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
 //        qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
@@ -473,8 +474,9 @@ void MapEngine::setTilesGridSize(const QSize &viewSize)
     m_tilesGridSize.setHeight(gridHeight);
     m_tilesGridSize.setWidth(gridWidth);
 
-//    qWarning() << __PRETTY_FUNCTION__ << "tiles grid:" << m_tilesGridSize.width()
-//                                      << "*" << m_tilesGridSize.height();
+    qWarning() << __PRETTY_FUNCTION__ << "tiles grid:" << m_tilesGridSize.width()
+                                      << "*" << m_tilesGridSize.height()
+                                      << "=" << m_tilesGridSize.width() * m_tilesGridSize.height() << "tiles";
 }
 
 void MapEngine::setViewLocation(QPointF latLonCoordinate)
index d3cd9ea..716f18a 100644 (file)
@@ -299,7 +299,10 @@ private slots:
     /**
     * @brief Slot for received map tile images
     *
-    * Does add MapTile objects to MapScene. Does also remove old tile if tile already exists.
+    * Does receive map tile images from MapFetcher. Calls MapScene::addTile() for creating and adding
+    * the actual MapTile object to the MapScene.
+    *
+    * Tile is added also to outside the world horizontal limits, if required, for spanning the map.
     *
     * @param zoomLevel Zoom level
     * @param x Tile x index
index 7bac9a7..340ef2c 100644 (file)
@@ -184,6 +184,8 @@ bool MapFetcher::loadImageFromCache(const QUrl &url)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
+    const int MAX_UPPER_LEVELS = 4;
+
     bool imageFound = false;
 
     QAbstractNetworkCache *cache = m_manager->cache();
@@ -196,7 +198,8 @@ bool MapFetcher::loadImageFromCache(const QUrl &url)
         parseURL(url, &zoomLevel, &x, &y);
         int originalZoomLevel = zoomLevel;
 
-        // try to fetch requested and upper level images until found or all levels tried
+        // try to fetch requested and upper level images until found or MAX_UPPER_LEVELS
+        // limit is reached
         do {
             QIODevice *cacheImage = cache->data(buildURL(zoomLevel, QPoint(x, y)));
             if (cacheImage) {
@@ -208,7 +211,9 @@ bool MapFetcher::loadImageFromCache(const QUrl &url)
 
                 delete cacheImage;
             }
-        } while (!imageFound && translateIndexesToUpperLevel(zoomLevel, x, y));
+        } while (!imageFound
+                 && (originalZoomLevel - zoomLevel) < MAX_UPPER_LEVELS
+                 && translateIndexesToUpperLevel(zoomLevel, x, y));
 
         // check expiration if image was found from requested level
         if (imageFound && (originalZoomLevel == zoomLevel)) {
index 6d4b926..f4435b6 100644 (file)
@@ -29,7 +29,8 @@
 
 #include "mapscene.h"
 
-const int WORLD_PIXELS_Y = MAX_TILES_PER_SIDE * TILE_SIZE_Y;
+const int MAP_MIN_PIXEL_Y = 0;
+const int MAP_MAX_PIXEL_Y = MAX_TILES_PER_SIDE * TILE_SIZE_Y - 1;
 
 MapScene::MapScene(QObject *parent)
     : QGraphicsScene(parent)
@@ -39,7 +40,7 @@ MapScene::MapScene(QObject *parent)
     qDebug() << __PRETTY_FUNCTION__;
 
     setBackgroundBrush(Qt::lightGray);
-    setSceneRect(-WORLD_PIXELS_X * 2, 0, WORLD_PIXELS_X * 5 - 1, WORLD_PIXELS_Y - 1);
+    setSceneRect(MAP_SCENE_MIN_PIXEL_X, MAP_MIN_PIXEL_Y, MAP_SCENE_MAX_PIXEL_X, MAP_MAX_PIXEL_Y);
 }
 
 void MapScene::addTile(int tileZoomLevel, QPoint tileNumber, const QPixmap &image, int viewZoomLevel)
@@ -156,6 +157,8 @@ void MapScene::removeOutOfViewTiles(QRect tilesGrid, int zoomLevel)
             removeTile(tileToRemove);
         }
     }
+
+    qWarning() << __PRETTY_FUNCTION__ << "tiles in scene:" << items().count();
 }
 
 void MapScene::removeStackedTiles(MapTile *newTile)
@@ -177,6 +180,7 @@ void MapScene::removeStackedTiles(MapTile *newTile)
             }
         }
     }
+//    qWarning() << __PRETTY_FUNCTION__ << "tiles in scene:" << items().count();
 }
 
 void MapScene::removeTile(MapTile *tile)
@@ -198,8 +202,8 @@ void MapScene::setSceneVerticalOverlap(int viewHeight, int zoomLevel)
     int overlap = viewHeight / 2 * (1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel));
 
     QRect rect = sceneRect().toRect();
-    rect.setTop(-overlap);
-    rect.setBottom(WORLD_PIXELS_Y + overlap - 1);
+    rect.setTop(MAP_MIN_PIXEL_Y - overlap);
+    rect.setBottom(MAP_MAX_PIXEL_Y + overlap);
     setSceneRect(rect);
 
 //    qWarning() << __PRETTY_FUNCTION__ << "scene rect:" << rect.left() << rect.top() << rect.right() << rect.bottom();
@@ -224,8 +228,8 @@ void MapScene::spanItems(int zoomLevel, QPoint sceneCoordinate, QSize viewSize)
 
     // create rects for left and right side
     QRect leftRect = sceneRect().toRect(); // this way we get the horizontal limits of the scene
-    leftRect.setTop(0);
-    leftRect.setBottom(WORLD_PIXELS_Y);
+    leftRect.setTop(MAP_MIN_PIXEL_Y);
+    leftRect.setBottom(MAP_MAX_PIXEL_Y);
     QRect rightRect = leftRect;
 
     // calculate current horizontal area shown on the view
@@ -239,8 +243,8 @@ void MapScene::spanItems(int zoomLevel, QPoint sceneCoordinate, QSize viewSize)
     leftRect.setRight(-1 - (WORLD_PIXELS_X - 1 - viewSceneRight));
     rightRect.setLeft(WORLD_PIXELS_X + viewSceneLeft);
 
-//    Q_ASSERT_X(leftRect.right() < viewSceneLeft, "spanning rect right value", "move rect is in the view area");
-//    Q_ASSERT_X(rightRect.left() > viewSceneRight, "spanning rect left value", "move rect is in the view area");
+    Q_ASSERT_X(leftRect.right() < viewSceneLeft, "spanning rect right value", "move rect is in the view area");
+    Q_ASSERT_X(rightRect.left() > viewSceneRight, "spanning rect left value", "move rect is in the view area");
 
     // move all items which intersects the rects
     if (leftRect.left() < leftRect.right())