Remove other level tiles when all current level tiles exists
[situare] / src / map / mapscene.h
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Sami Rämö - sami.ramo@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #ifndef MAPSCENE_H
23 #define MAPSCENE_H
24
25 #include <QGraphicsScene>
26
27 #include "maptile.h"
28
29 /**
30 * @brief Map scene for storing MapTile items
31 *
32 * @author Sami Rämö - sami.ramo (at) ixonos.com
33 */
34 class MapScene : public QGraphicsScene
35 {
36     Q_OBJECT
37 public:
38     /**
39     * @brief Constructor
40     *
41     * Scene size is set to the amount of pixels on closest zoom level
42     * @param parent Parent
43     */
44     MapScene(QObject *parent = 0);
45
46 /*******************************************************************************
47  * MEMBER FUNCTIONS AND SLOTS
48  ******************************************************************************/
49 public:
50     /**
51     * @brief Create and add new MapTile to MapScene
52     *
53     * If there is a tile with same parameters already in the scene, it will be removed
54     * before adding the new tile.
55     *
56     * @param zoomLevel Zoom level of the new tile
57     * @param tileNumber X & Y indexes of the tile
58     * @param image Map tile picture
59     * @param viewZoomLevel Current view zoom level (for setting the zValue)
60     */
61     void addTile(int zoomLevel, QPoint tileNumber, const QPixmap &image, int viewZoomLevel);
62
63     /**
64     * @brief Enqueue stacked tiles removal request
65     *
66     * Removal is triggered after events have been processed so the UI
67     * stays more responsive
68     * @param newTile New tile, which area under it is inspected
69     */
70     void enqueueRemoveStackedTiles(MapTile *newTile);
71
72     /**
73     * @brief Returns tile mathcing given hash key
74     *
75     * @param hashKey
76     * @return Returns tile matching given hash key, or 0 if no match found
77     */
78     MapTile* tileInScene(QString hashKey);
79
80     /**
81     * @brief Remove tiles which are out of view bounds.
82     *
83     */
84     void removeOutOfViewTiles(QRect tilesGrid, int zoomLevel);
85
86     /**
87     * @brief Remove tiles which are stacked.
88     *
89     * Iterate through tiles which are under this map tile and remove tiles which
90     * are fully obscured by this new tile. Tiles which are only partially
91     * obscured by this new tile are not checked, and thus deleted, because of
92     * the required complexity of the algorithm and processing power. Those tiles
93     * will be removed when they go out of the view area caused by scrolling or
94     * zooming in enough.
95     *
96     * @param newTile new tile covering old tiles
97     */
98     void removeStackedTiles(MapTile *newTile);
99
100     /**
101     * @brief Remove tile.
102     *
103     * Removes tile from scene and list of current tiles in scene.
104     * @param tile MapTile to remove
105     */
106     void removeTile(MapTile *tile);
107
108     /**
109       * @brief Set allowed amount of exceeding the world vertical limits
110       *
111       * Limit is set so that vertical limits of the world can be scrolled to middle of
112       * the view.
113       *
114       * @param viewHeight Height of the view
115       * @param zoomLevel Current zoom level
116       */
117     void setSceneVerticalOverlap(int viewHeight, int zoomLevel);
118
119     /**
120     * @brief Set drawing order of all tiles in the scene
121     *
122     * Check MapTile::setSceneLevel for more information.
123     * @param zoomLevel Current zoom level
124     */
125     void setTilesDrawingLevels(int zoomLevel);
126
127     void setTilesGrid(QRect grid);
128
129     void setZoomLevel(int zoomLevel);
130
131     void spanItems(int zoomLevel, QPoint sceneCoordinate, QSize viewSize);
132
133     /**
134     * @brief Save new tiles scene rect
135     *
136     * Tiles scene rect must be saved to local scope whenever it changes because
137     * it is used by removal algorithms and some of the algorithms are run
138     * delayed so the actual tiles scene rect may change before the cleanup
139     * algorithm is run.
140     *
141     * @param tilesSceneRect New view rect
142     */
143     void tilesSceneRectUpdated(QRect tilesSceneRect);
144
145 private:
146     /**
147       * @brief Move map items horizontally in the scene (not MapTile items)
148       *
149       * MapTile items are not moved!
150       *
151       * Move items which intersect the given rect.
152       *
153       * @param from Items colliding given rect are moved
154       * @param distance How much to move each item
155       */
156     void moveIntersectingItemsHorizontally(QRect from, int distance);
157
158 private slots:
159     void removeOtherLevelTiles();
160
161     /**
162     * @brief Slot for running next queued removal of stacked tiles
163     *
164     */
165     void runNextStackedTilesRemoval();
166
167 /*******************************************************************************
168  * DATA MEMBERS
169  ******************************************************************************/
170 private:
171     bool m_isRemoveStackedTilesRunning; ///< Is singleshot timer already running
172     int m_zoomLevel;
173     QHash<QString, MapTile *> m_mapTilesInScene;  ///< List of map tiles in map scene
174     QList<MapTile *> m_removeStackedTilesList; ///< "Queue" for stacked tiles removal requests
175     QRect m_tilesSceneRect; ///< Current viewable area
176     QRect m_viewTilesGrid;
177 };
178
179 #endif // MAPSCENE_H