Added MapEngine::combineTiles method and modified
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 19 Apr 2010 08:37:07 +0000 (11:37 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 19 Apr 2010 08:37:07 +0000 (11:37 +0300)
MapEngine::removeStackedTiles.

src/map/mapengine.cpp
src/map/mapengine.h
src/map/mapfetcher.cpp
src/map/mapfetcher.h
tests/map/mapengine/testmapengine.cpp
tests/map/mapengine/testmapengine.pro

index d6832c7..590045c 100644 (file)
@@ -35,9 +35,9 @@
 
 MapEngine::MapEngine(QObject *parent)
     : QObject(parent)
-    , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
-    , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
     , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
+    , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
+    , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
 {
     m_mapScene = new MapScene(this);
 
@@ -87,6 +87,7 @@ void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
 {
     //qDebug() << __PRETTY_FUNCTION__;
+
     int zoom = UNDEFINED;
     int x = UNDEFINED;
     int y = UNDEFINED;
@@ -152,7 +153,7 @@ bool MapEngine::centerTileChanged(QPoint sceneCoordinate)
 
 void MapEngine::calculateNewTiles(QPoint sceneCoordinate)
 {
-    //qDebug() << __PRETTY_FUNCTION__;
+//    qDebug() << __PRETTY_FUNCTION__;
 
     m_viewGrid = calculateGrid(sceneCoordinate);
 
@@ -225,8 +226,6 @@ void MapEngine::removeOldTiles()
              removeTile(tile);
          }
      }
-
-//     qDebug() << m_mapScene->items().count();
 }
 
 void MapEngine::removeStackedTiles(MapTile *newTile)
@@ -234,44 +233,53 @@ void MapEngine::removeStackedTiles(MapTile *newTile)
     //qDebug() << __PRETTY_FUNCTION__;
 
     QRectF newTileSceneRect = newTile->mapRectToScene(newTile->boundingRect());
-    QList<QGraphicsItem *> collidingItems = newTile->collidingItems(Qt::IntersectsItemBoundingRect);
+    QList<QGraphicsItem *> collidingTiles = newTile->collidingItems(Qt::IntersectsItemBoundingRect);
 
     //Loop all items under new tile
-    foreach (QGraphicsItem *collidingItem, collidingItems) {
+    foreach (QGraphicsItem *collidingTile, collidingTiles) {
 
-        QRectF collidingItemSceneRect = collidingItem->sceneBoundingRect();
+        QRectF collidingTileSceneRect = collidingTile->sceneBoundingRect();
 
-        //If new tile covers the tile under, remove the tile  (zoom out)
-        if (newTileSceneRect.contains(collidingItemSceneRect)) {
-            MapTile *tile = dynamic_cast<MapTile *>(collidingItem);
+        //If new tile covers the tile under, remove the tile
+        if (newTileSceneRect.contains(collidingTileSceneRect)) {
+            MapTile *tile = dynamic_cast<MapTile *>(collidingTile);
             removeTile(tile);
         }
 
         else {
             //Get tiles below removal candidate
-            QList<QGraphicsItem *> stackedItems = m_mapScene->items(collidingItemSceneRect,
+            QList<QGraphicsItem *> stackedTiles = m_mapScene->items(collidingTileSceneRect,
                                                                     Qt::ContainsItemBoundingRect);
-            QRectF combined;
-            int count = 0;
-
-            //Loop all tiles below removal candidate and combine tiles
-            foreach (QGraphicsItem *stackedItem, stackedItems) {
-                if (stackedItem != collidingItem) {
-                    count++;
-                    QRectF stackedItemSceneRect = stackedItem->sceneBoundingRect();
-                    combined = combined.united(stackedItemSceneRect);
-                }
-            }
 
-            //If combined tiles below removal candidate covers removal candidate, remove it (zoom in)
-            if ((combined.contains(collidingItemSceneRect)) && (count >= 4)) {
-                MapTile *tile = dynamic_cast<MapTile *>(collidingItem);
+            QRectF combined = combineTiles(collidingTile, stackedTiles);
+
+            //If combined tiles below removal candidate covers removal candidate, remove it
+            if (combined.contains(collidingTileSceneRect)) {
+                MapTile *tile = dynamic_cast<MapTile *>(collidingTile);
                 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();
 
-//    qDebug() << m_mapScene->items().count();
+    return combined;
 }
 
 void MapEngine::viewResized(const QSize &size)
@@ -294,10 +302,6 @@ void MapEngine::zoomIn()
     setTilesDrawingLevels();
 
     calculateNewTiles(m_sceneCoordinate);
-
-    /**
-    * @todo Remove old tiles after zoom
-    */
     QTimer::singleShot(500, this, SLOT(removeOldTiles()));
 }
 
@@ -364,7 +368,7 @@ QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileN
 
 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+//    qDebug() << __PRETTY_FUNCTION__;
 
     qreal longitude = latLonCoordinate.x();
     qreal latitude = latLonCoordinate.y();
index 6cae423..d12d988 100644 (file)
@@ -49,6 +49,9 @@ public:
     */
     MapEngine(QObject *parent = 0);
 
+/*******************************************************************************
+ * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
     /**
     * @brief Convert latitude and longitude to scene coordinates.
     *
@@ -151,6 +154,14 @@ private:
     bool centerTileChanged(QPoint sceneCoordinate);
 
     /**
+    * @brief Combine tiles' rectangles to one rectangle.
+    *
+    * @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.
     *
@@ -240,6 +251,9 @@ private slots:
     */
     void zoomOut();
 
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
 signals:   
     /**
     * @brief Signal for image fetching.
@@ -262,15 +276,18 @@ signals:
     */
     void zoomLevelChanged(int newZoomLevel);
 
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
 private:
-    MapScene *m_mapScene; ///< Scene for map tiles
+    QPoint m_centerTile;    ///< Current center tile
     MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
-    int m_zoomLevel; ///< Current zoom level
+    MapScene *m_mapScene; ///< Scene for map tiles
     QHash<QString, MapTile *> m_mapTilesInScene;  ///< List of map tiles in map scene
-    QSize m_viewSize;   ///< Current view size
     QPoint m_sceneCoordinate;  ///< Current center coordinate
-    QPoint m_centerTile;    ///< Current center tile
     QRect m_viewGrid; ///< Current grid of tiles in view
+    QSize m_viewSize;   ///< Current view size
+    int m_zoomLevel; ///< Current zoom level
 };
 
 #endif // MAPENGINE_H
index 128b5f8..18f1a2b 100644 (file)
 
 #include "mapfetcher.h"
 
-static int MAX_PARALLEL_DOWNLOADS = 2;
-static int DOWNLOAD_QUEUE_SIZE = 50;
+const int MAX_PARALLEL_DOWNLOADS = 2;
+const int DOWNLOAD_QUEUE_SIZE = 50;
 
 MapFetcher::MapFetcher(QNetworkAccessManager *manager, QObject *parent)
-    : QObject(parent), m_manager(manager)
+    : QObject(parent)
+    , m_manager(manager)
 {
     QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
     diskCache->setCacheDirectory(QDesktopServices::storageLocation(
index ee54244..eb364a0 100644 (file)
@@ -55,6 +55,9 @@ public:
     */
     ~MapFetcher();
 
+/*******************************************************************************
+ * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
 public slots:
     /**
     * @brief Fetch image from given URL.
@@ -91,6 +94,9 @@ private slots:
     */
     void startNextDownload();
 
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
 signals:    
     /**
     * @brief Signal which is emitted when a map tile
@@ -109,11 +115,13 @@ signals:
     */
     void error(const QString &message);
 
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
 private:
-
-    QNetworkAccessManager *m_manager;       ///< Network access manager
     QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
     QQueue<QUrl> m_downloadQueue;             ///< Queue of pending requests
+    QNetworkAccessManager *m_manager;       ///< Network access manager   
 };
 
 #endif
index 40e785e..9685efa 100644 (file)
@@ -77,7 +77,7 @@ void TestMapEngine::convertLatLonToSceneCoordinate()
 
 void TestMapEngine::setLocationNewTilesCount()
 {
-       MapEngine engine;
+    MapEngine engine;
     engine.viewResized(QSize(800, 480));
 
     QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(QUrl)));
@@ -100,7 +100,7 @@ void TestMapEngine::setLocationRemovedTilesCount()
     MapEngine engine;
     engine.viewResized(QSize(800, 480));
 
-    const int maxItemsCount = 30;
+    const int maxItemsCount = 40;
 
     engine.setLocation(QPoint(1220*16, 1220*16));
     QTest::qWait(1000);
@@ -121,7 +121,7 @@ void TestMapEngine::zoomInRemovedTilesCount()
     MapEngine engine;
     engine.viewResized(QSize(800, 480));
 
-    const int maxItemsCount = 30;
+    const int maxItemsCount = 40;
 
     engine.setLocation(QPoint(1220*16, 1220*16));
     QTest::qWait(1000);
@@ -141,7 +141,7 @@ void TestMapEngine::zoomOutRemovedTilesCount()
     MapEngine engine;
     engine.viewResized(QSize(800, 480));
 
-    const int maxItemsCount = 30;
+    const int maxItemsCount = 40;
 
     engine.setLocation(QPoint(1220*16, 1220.23*16));
     QTest::qWait(1000);
index 181d5df..8ec760c 100644 (file)
@@ -8,7 +8,7 @@ TARGET =
 DEPENDPATH += .
 INCLUDEPATH += . \
     ../../../src/
-DEFINES += QT_NO_DEBUG_OUTPUT
+#DEFINES += QT_NO_DEBUG_OUTPUT
 
 # Input
 SOURCES += testmapengine.cpp \