Fixed and re-factored tile grid calculation
authorSami Rämö <sami.ramo@ixonos.com>
Thu, 3 Jun 2010 06:27:30 +0000 (09:27 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Thu, 3 Jun 2010 06:27:30 +0000 (09:27 +0300)
src/map/mapcommon.h
src/map/mapengine.cpp
src/map/mapengine.h

index f8aa03c..6740710 100644 (file)
@@ -30,7 +30,7 @@ const int TILE_SIZE_Y = 256; ///< Tile image size in y direction
 
 const int MIN_MAP_ZOOM_LEVEL = 0; ///< Minimum zoom level
 const int MAX_MAP_ZOOM_LEVEL = 18; ///< Maximum zoom level
-const int MIN_VIEW_ZOOM_LEVEL = 3; ///< Minimum zoom level for MapView
+const int MIN_VIEW_ZOOM_LEVEL = 2; ///< Minimum zoom level for MapView
 const int MIN_MAP_SCENE_NORMAL_LEVEL = MAX_MAP_ZOOM_LEVEL + 1;
 
 const int MAX_TILES_PER_SIDE = (1 << MAX_MAP_ZOOM_LEVEL);
@@ -40,7 +40,7 @@ const int WORLD_PIXELS_X = MAX_TILES_PER_SIDE * TILE_SIZE_X;
 * @var DEFAULT_START_ZOOM_LEVEL
 * @brief Maps Default zoom level, used when latest zoom level is not available.
 */
-const int DEFAULT_START_ZOOM_LEVEL = 2;
+const int DEFAULT_START_ZOOM_LEVEL = MIN_VIEW_ZOOM_LEVEL;
 
 /**
 * @var FRIEND_LOCATION_ICON_Z_LEVEL
@@ -75,7 +75,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 = 1;  ///< Grid padding used in tile grid calculation
 
 const QString OSM_LICENSE = QString::fromUtf8("© OpenStreetMap contributors, CC-BY-SA");
 
index e584d1f..971b6b9 100644 (file)
@@ -51,6 +51,7 @@ MapEngine::MapEngine(QObject *parent)
       m_zoomLevel(DEFAULT_ZOOM_LEVEL),
       m_centerTile(QPoint(UNDEFINED, UNDEFINED)),
       m_lastManualPosition(QPoint(0, 0)),
+      m_tilesGridSize(QSize(0, 0)),
       m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -96,14 +97,12 @@ QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
     qDebug() << __PRETTY_FUNCTION__;
 
     QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, 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);
-    int topLeftX = tileCoordinate.x() - (gridWidth / 2);
-    int topLeftY = tileCoordinate.y() - (gridHeight / 2);
 
-    m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
+    QPoint topLeft;
+    topLeft.setX(tileCoordinate.x() - (m_tilesGridSize.width() / 2));
+    topLeft.setY(tileCoordinate.y() - (m_tilesGridSize.height() / 2));
 
-    return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
+    return QRect(topLeft, m_tilesGridSize);
 }
 
 QPointF MapEngine::centerGeoCoordinate()
@@ -254,7 +253,7 @@ void MapEngine::init()
         ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString() == ERROR_VALUE_NOT_FOUND_ON_SETTINGS) {
 
         startLocation = QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE);
-        m_zoomLevel = DEFAULT_START_ZOOM_LEVEL;
+        m_zoomLevel = qBound(MIN_VIEW_ZOOM_LEVEL, DEFAULT_START_ZOOM_LEVEL, MAX_MAP_ZOOM_LEVEL);
     } else {
         m_zoomLevel = settings.value(MAP_LAST_ZOOMLEVEL, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toInt();
         startLocation = settings.value(MAP_LAST_POSITION,
@@ -375,6 +374,28 @@ void MapEngine::setZoomLevel(int newZoomLevel)
     zoomed();
 }
 
+void MapEngine::setTilesGridSize(const QSize &viewSize)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    // there must be scrolling reserve of at least half tile added to tile amount
+    // calculated from view size
+    const qreal SCROLLING_RESERVE = 0.5;
+
+    // converting scene tile to tile number does cause grid centering inaccuracy of one tile
+    const int CENTER_TILE_INACCURACY = 1;
+
+    int gridWidth = ceil(qreal(viewSize.width()) / TILE_SIZE_X + SCROLLING_RESERVE)
+                    + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
+    int gridHeight = ceil(qreal(viewSize.height()) / TILE_SIZE_Y + SCROLLING_RESERVE)
+                     + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
+
+    m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
+
+    m_tilesGridSize.setHeight(gridHeight);
+    m_tilesGridSize.setWidth(gridWidth);
+}
+
 void MapEngine::setViewLocation(QPointF latLonCoordinate)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -426,6 +447,8 @@ void MapEngine::viewResized(const QSize &size)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_viewSize = size;
+    setTilesGridSize(m_viewSize);
+
     emit locationChanged(m_sceneCoordinate);
     getTiles(m_sceneCoordinate);
     m_mapScene->removeOutOfViewTiles();
index b4847c1..73a761f 100644 (file)
@@ -237,6 +237,13 @@ private:
     bool isCenterTileChanged(QPoint sceneCoordinate);
 
     /**
+      * @brief Set size of tiles grid based on view size
+      *
+      * @param viewSize Current view size
+      */
+    void setTilesGridSize(const QSize &viewSize);
+
+    /**
     * @brief Calculate maximum value for tile in this zoom level.
     *
     * @param zoomLevel zoom level
@@ -366,6 +373,7 @@ private:
 
     QRect m_viewTilesGrid;         ///< Current grid of tiles in view (includes margin)
 
+    QSize m_tilesGridSize;         ///< Current size of the tiles grid
     QSize m_viewSize;              ///< Current view size
 
     FriendItemsHandler *m_friendItemsHandler;   ///< Handler for friend and group items