Reposition tiles in the scene
authorSami Rämö <sami.ramo@ixonos.com>
Fri, 28 May 2010 07:31:42 +0000 (10:31 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Fri, 28 May 2010 07:31:42 +0000 (10:31 +0300)
 - don't span in vertical direction

 - reposition tile if it was already in the scene and not
   asked from MapFetcher

 - renamed MapScene::isTileInScene to tileInScene and changed
   implementation to return pointer to requested tile

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

index a0b4dbf..1258458 100644 (file)
@@ -61,9 +61,7 @@ const int FRIEND_ITEM_PRESS_HEIGHT = 30;  ///< Friend item press area height
 * @brief layer of own location icon
 */
 const int OWN_LOCATION_ICON_Z_LEVEL = FRIEND_LOCATION_ICON_Z_LEVEL + 1;
-const int MAP_OWN_LOCATION_ICON_SIZE = 24; ///< Size of own location item icon
 
-const int ZOOM_FPS = 30; ///< FPS for the zoom effect
 const qreal ZOOM_TIME = 250; ///< Length of the zoom effect (ms)
 
 const qreal MAX_LATITUDE = 85.05112877980659237802;  ///< Maximum latitude value
index 7fb1fb0..5bf07b5 100644 (file)
@@ -102,7 +102,8 @@ QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
 
     m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
 
-//    qWarning() << __PRETTY_FUNCTION__ << topLeftX << topLeftY << gridWidth << gridHeight;
+    qWarning() << __PRETTY_FUNCTION__ << topLeftX << topLeftY << "->"
+                                      << topLeftX + gridWidth << topLeftY + gridHeight;
 
     return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
 }
@@ -195,7 +196,7 @@ bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
 
 void MapEngine::getTiles(QPoint sceneCoordinate)
 {
-    qWarning() << __PRETTY_FUNCTION__;
+    qDebug() << __PRETTY_FUNCTION__;
 
     m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
     updateViewTilesSceneRect();
@@ -216,22 +217,31 @@ void MapEngine::getTiles(QPoint sceneCoordinate)
             int tileX = x;
             int tileY = y;
 
-            int debugX = x;
+            int requestedX = x;
 
-            if (tileX < 0)
+            // span in horizontal direction if world limits has been reached
+            if (tileX < 0) {
                 tileX += tileMaxVal + 1;
-            else if (tileX > tileMaxVal)
+//                qWarning() << __PRETTY_FUNCTION__ << "x value:" << requestedX << "->" << tileX;
+            }
+            else if (tileX > tileMaxVal) {
                 tileX -= tileMaxVal + 1;
+//                qWarning() << __PRETTY_FUNCTION__ << "x value:" << requestedX << "->" << tileX;
+            }
 
-            if (tileY < 0)
-                tileY += tileMaxVal + 1;
-            else if (tileY > tileMaxVal)
-                tileY -= tileMaxVal + 1;
-
-            qWarning() << __PRETTY_FUNCTION__ << "x value:" << debugX << "->" << tileX;
+            // map doesn't span in vertical direction
+            if (tileY < 0 || tileY > tileMaxVal)
+                continue;
 
-            if (!m_mapScene->isTileInScene(tilePath(m_zoomLevel, tileX, tileY)))
+            MapTile *tile;
+            if (!(tile = m_mapScene->tileInScene(tilePath(m_zoomLevel, tileX, tileY)))) {
                 emit fetchImage(m_zoomLevel, tileX, tileY);
+            }
+            else {
+                // tile was already in the scene, maybe it should be moved?
+//                qWarning() << __PRETTY_FUNCTION__ << "tile was already in the scene";
+                m_mapScene->setTilePosition(tile);
+            }
         }
     }
 }
@@ -290,10 +300,10 @@ bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
 
 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__ << "x:" << x << "y:" << y;
 
     QString hashKey = tilePath(zoomLevel, x, y);
-    if (!m_mapScene->isTileInScene(hashKey)) {
+    if (!m_mapScene->tileInScene(hashKey)) {
 
         MapTile *mapTile = new MapTile();
         mapTile->setZoomLevel(zoomLevel, m_zoomLevel);
@@ -303,7 +313,10 @@ void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &ima
         m_mapScene->addTile(mapTile, hashKey);
 
         m_mapScene->enqueueRemoveStackedTiles(mapTile);
-   }
+    }
+    else {
+        qWarning() << __PRETTY_FUNCTION__ << "WARNING: Received tile which already is in the scene";
+    }
 }
 
 void MapEngine::receiveOwnLocation(User *user)
@@ -343,14 +356,14 @@ void MapEngine::setGPSEnabled(bool enabled)
 
 void MapEngine::setLocation(QPoint sceneCoordinate)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+//    qWarning() << __PRETTY_FUNCTION__ << "X:" << sceneCoordinate.x();
 
     if (sceneCoordinate.x() < 0) {
-        qWarning() << __PRETTY_FUNCTION__ << "World west limit reached";
+//        qWarning() << __PRETTY_FUNCTION__ << "World west limit reached";
         sceneCoordinate.setX(sceneCoordinate.x() + WORLD_PIXELS_X);
     }
     else if (sceneCoordinate.x() > WORLD_PIXELS_X - 1) {
-        qWarning() << __PRETTY_FUNCTION__ << "World east limit reached";
+//        qWarning() << __PRETTY_FUNCTION__ << "World east limit reached";
         sceneCoordinate.setX(sceneCoordinate.x() - WORLD_PIXELS_X);
     }
 
index 7c4a953..ce25991 100644 (file)
@@ -57,11 +57,11 @@ void MapScene::enqueueRemoveStackedTiles(MapTile *newTile)
     }
 }
 
-bool MapScene::isTileInScene(QString hashKey)
+MapTile* MapScene::tileInScene(QString hashKey)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    return m_mapTilesInScene.contains(hashKey);
+    return m_mapTilesInScene.value(hashKey, 0);
 }
 
 void MapScene::runNextStackedTilesRemoval()
@@ -124,12 +124,12 @@ void MapScene::removeTile(MapTile *tile)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_mapTilesInScene.remove(MapEngine::tilePath(tile->zoomLevel(),
-                                                 tile->tileNumber().x(),
-                                                 tile->tileNumber().y()));
-    removeItem(tile);
-    m_removeStackedTilesList.removeAll(tile);
-    delete tile;
+//    m_mapTilesInScene.remove(MapEngine::tilePath(tile->zoomLevel(),
+//                                                 tile->tileNumber().x(),
+//                                                 tile->tileNumber().y()));
+//    removeItem(tile);
+//    m_removeStackedTilesList.removeAll(tile);
+//    delete tile;
 }
 
 void MapScene::setTilePosition(MapTile *tile)
@@ -152,6 +152,7 @@ void MapScene::setTilePosition(MapTile *tile)
     }
     else {
         tile->setPos(UNDEFINED, UNDEFINED);
+        qCritical() << __PRETTY_FUNCTION__ << "Tile position is undefined";
     }
 }
 
index 144fc81..0a5b2d2 100644 (file)
@@ -65,12 +65,12 @@ public:
     void enqueueRemoveStackedTiles(MapTile *newTile);
 
     /**
-    * @brief Returns if tile mathcing hash key is already in the scene
+    * @brief Returns tile mathcing given hash key
     *
     * @param hashKey
-    * @return True if tile was in the scene, otherwise false
+    * @return Returns tile matching given hash key, or 0 if no match found
     */
-    bool isTileInScene(QString hashKey);
+    MapTile* tileInScene(QString hashKey);
 
     /**
     * @brief Remove tiles which are out of view bounds.
@@ -101,6 +101,16 @@ public:
     void removeTile(MapTile *tile);
 
     /**
+    * @brief Set position of the tile in the MapScene coordinate system
+    *
+    * Does set the position based on the tiles' m_zoomLevel and m_TileNumber and the current
+    * m_viewRect. Position is set to (UNDEFINED, UNDEFINED) if there is something wrong with zoom
+    * level or tile numbers. If tile is out of the current view rect, then it is moved to opposite
+    * side of the world to make spanning over world limit work.
+    */
+    void setTilePosition(MapTile *tile);
+
+    /**
     * @brief Set drawing order of all tiles in the scene
     *
     * Check MapTile::setSceneLevel for more information.
@@ -120,9 +130,6 @@ public:
     */
     void viewRectUpdated(QRect viewRect);
 
-private:
-    void setTilePosition(MapTile *tile);
-
 private slots:
     /**
     * @brief Slot for running next queued removal of stacked tiles