Fixed a bug in MapEngine::removeStackedTiles() (not tested)
authorSami Rämö <sami.ramo@ixonos.com>
Wed, 21 Apr 2010 13:29:59 +0000 (16:29 +0300)
committerSami Rämö <sami.ramo@ixonos.com>
Wed, 21 Apr 2010 13:29:59 +0000 (16:29 +0300)
src/map/mapengine.cpp
src/map/mapengine.h

index 831f008..392b5f0 100644 (file)
@@ -214,78 +214,67 @@ void MapEngine::removeTilesOutOfView()
 {
 //    qDebug() << __PRETTY_FUNCTION__;
 
-    QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.topLeft());
-    QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.bottomRight()
-                                                             + QPoint(1, 1));
-    qreal width = bottomRight.x() - topLeft.x();
-    qreal height = bottomRight.y() - topLeft.y();
-
-    QList<QGraphicsItem *> viewTiles = m_mapScene->items(topLeft.x(), topLeft.y(), width, height,
-                                                         Qt::ContainsItemBoundingRect);
+    QList<QGraphicsItem *> viewTiles = m_mapScene->items(viewRect(), Qt::ContainsItemBoundingRect);
     QList<QGraphicsItem *> allTiles = m_mapScene->items();
 
+//    qDebug() << __PRETTY_FUNCTION__ << "All tiles:" << allTiles.count();
+//    qDebug() << __PRETTY_FUNCTION__ << "Tiles in view area:" << viewTiles.count();
+
     //Remove tiles which are in view from allTiles
     foreach (QGraphicsItem *tile, viewTiles)
         allTiles.removeOne(tile);
 
+
     //Remove tiles out of view
     foreach (QGraphicsItem *tile, allTiles) {
         MapTile *tileToRemove = dynamic_cast<MapTile *>(tile);
-        removeTile(tileToRemove);
+        if (tileToRemove)
+            removeTile(tileToRemove);
     }
 }
 
+QRect MapEngine::viewRect()
+{
+    QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.topLeft());
+    QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.bottomRight()
+                                                             + QPoint(1, 1));
+    return QRect(topLeft, bottomRight);
+}
+
 void MapEngine::removeStackedTiles(MapTile *newTile)
 {
 //    qDebug() << __PRETTY_FUNCTION__;
 
     QRectF newTileSceneRect = newTile->sceneBoundingRect();
-    QList<QGraphicsItem *> collidingTiles = newTile->collidingItems(Qt::IntersectsItemBoundingRect);
 
     //Loop all items under new tile
+    QList<QGraphicsItem *> collidingTiles = newTile->collidingItems(Qt::IntersectsItemBoundingRect);
+//    qDebug() << __PRETTY_FUNCTION__ << "Colliding tiles before:" << collidingTiles.count();
     foreach (QGraphicsItem *collidingTile, collidingTiles) {
 
+
         QRectF collidingTileSceneRect = collidingTile->sceneBoundingRect();
 
-        //If new tile covers the tile under, remove the tile
+        // remove tile if it is obscured by new tile
         if (newTileSceneRect.contains(collidingTileSceneRect)) {
             MapTile *tile = dynamic_cast<MapTile *>(collidingTile);
-            removeTile(tile);
+            if (tile)
+                removeTile(tile);
         }
-
         else {
-            //Get tiles below removal candidate
-            QList<QGraphicsItem *> stackedTiles = m_mapScene->items(collidingTileSceneRect,
-                                                                    Qt::ContainsItemBoundingRect);
-            QRectF combined = combineTiles(collidingTile, stackedTiles);
-
-            //If combined tiles below removal candidate covers removal candidate, remove it
-            if (combined.contains(collidingTileSceneRect)) {
+            // remove tile if it is obscured in the view area
+            QRect collidingTileViewableArea =
+                    collidingTileSceneRect.intersected(viewRect()).toRect();
+            if (collidingTile->isObscured(collidingTileViewableArea)) {
+                qDebug() << __PRETTY_FUNCTION__ << "Deleting obscured item";
                 MapTile *tile = dynamic_cast<MapTile *>(collidingTile);
-                removeTile(tile);
+                if (tile)
+                    removeTile(tile);
             }
         }
     }
-}
-
-QRectF MapEngine::combineTiles(QGraphicsItem *parentTile,
-                               const QList<QGraphicsItem*> &stackedTiles)
-{
-    QRectF combined;
-    int count = 0;
-
-    foreach (QGraphicsItem *stackedTile, stackedTiles) {
-        if (stackedTile != parentTile) {
-            count++;
-            QRectF stackedTileSceneRect = stackedTile->sceneBoundingRect();
-            combined = combined.united(stackedTileSceneRect);
-        }
-    }
-
-    if (count < 4)
-        combined = QRectF();
-
-    return combined;
+//    qDebug() << __PRETTY_FUNCTION__ << "All tiles after:"
+//             << newTile->collidingItems(Qt::IntersectsItemBoundingRect).count();
 }
 
 void MapEngine::viewResized(const QSize &size)
@@ -297,7 +286,7 @@ void MapEngine::viewResized(const QSize &size)
 
 void MapEngine::zoomIn()
 {
-    qDebug() << __PRETTY_FUNCTION__;
+//    qDebug() << __PRETTY_FUNCTION__;
 
     if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
         m_zoomLevel++;
@@ -314,7 +303,7 @@ void MapEngine::zoomIn()
 
 void MapEngine::zoomOut()
 {
-    qDebug() << __PRETTY_FUNCTION__;
+//    qDebug() << __PRETTY_FUNCTION__;
 
     if (m_zoomLevel > MIN_MAP_ZOOM_LEVEL) {
         m_zoomLevel--;
index 9637592..00b5c27 100644 (file)
@@ -53,7 +53,7 @@ public:
     MapEngine(QObject *parent = 0);
 
 /*******************************************************************************
- * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
+ * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
     /**
     * @brief Convert latitude and longitude to scene coordinates.
@@ -151,7 +151,8 @@ private:
     /**
     * @brief Get new tiles.
     *
-    * Calculates which tiles has to be fetched.
+    * Calculates which tiles has to be fetched. Does emit fetchImage for tiles which
+    * aren't already in the scene.
     * @param sceneCoordinate scene's center coordinate
     */
     void getTiles(QPoint sceneCoordinate);
@@ -165,15 +166,6 @@ private:
     bool isCenterTileChanged(QPoint sceneCoordinate);
 
     /**
-    * @brief Combine tiles' rectangles to one rectangle.
-    *
-    * @param parentTile parent tile not to combine
-    * @param stackedTiles tiles to combine
-    * @return QRectF resulting rectangle
-    */
-    QRectF combineTiles(QGraphicsItem *parentTile, const QList<QGraphicsItem*> &stackedTiles);
-
-    /**
     * @brief Parse given URL to zoom, x and y values. Parsed values are
     * placed in variables given as parameters.
     *
@@ -227,6 +219,13 @@ private:
     */
     QString tilePath(int zoomLevel, int x, int y);
 
+    /**
+    * @brief Returns the current view rect including margins
+    *
+    * @return Current view rect
+    */
+    QRect viewRect();
+
 private slots:
     /**
     * @brief Slot for received map tile images
@@ -289,7 +288,7 @@ private:
     QHash<QString, MapTile *> m_mapTilesInScene;  ///< List of map tiles in map scene
     QPoint m_centerTile; ///< Current center tile
     QPoint m_sceneCoordinate; ///< Current center coordinate
-    QRect m_viewGrid; ///< Current grid of tiles in view
+    QRect m_viewGrid; ///< Current grid of tiles in view (includes margin)
     QSize m_viewSize; ///< Current view size
     int m_zoomLevel; ///< Current zoom level
 };