15bb3ef3a6aa1acb70554efdf5364f837eb0b4a1
[situare] / src / map / mapengine.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        Jussi Laitinen - jussi.laitinen@ixonos.com
7        Pekka Nissinen - pekka.nissinen@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #ifndef MAPENGINE_H
25 #define MAPENGINE_H
26
27 #include <QtCore>
28
29 #include "mapcommon.h"
30 #include "mapfetcher.h"
31 #include "mapscene.h"
32 #include "maptile.h"
33 #include "mapzoompanel.h"
34 #include "ownlocationitem.h"
35 #include "friendlocationitem.h"
36 #include "user/user.h"
37
38 /**
39 * @brief Map engine
40 *
41 * Logic for controlling map functionality. Does also include static methods for
42 * converting coordinates.
43 * @author Sami Rämö - sami.ramo (at) ixonos.com
44 * @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
45 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
46 */
47 class MapEngine : public QObject
48 {
49     Q_OBJECT
50
51 public:
52     /**
53     * @brief Constructor
54     *
55     * @param parent Parent
56     */
57     MapEngine(QObject *parent = 0);
58
59     ~MapEngine();
60
61 /*******************************************************************************
62  * MEMBER FUNCTIONS AND SLOTS
63  ******************************************************************************/
64 public:
65     /**
66     * @brief Convert latitude and longitude to scene coordinates.
67     *
68     * @param latLonCoordinate latitude and longitude values
69     * @return scene coordinate
70     */
71     static QPoint convertLatLonToSceneCoordinate(QPointF latLonCoordinate);
72
73     /**
74     * @brief Convert MapScene coordinate to tile x & y numbers.
75     *
76     * @param zoomLevel ZoomLevel
77     * @param sceneCoordinate MapScene coordinate
78     * @return QPoint tile x & y numbers
79     */
80     static QPoint convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate);
81
82     /**
83     * @brief Convert tile x & y numbers to MapScene coordinates
84     *
85     * @param zoomLevel Zoom level
86     * @param tileNumber x & y numbers of the tile
87     * @return QPoint MapScene coordinate
88     */
89     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber);
90
91     /**
92     * @brief MapEngine initializer
93     *
94     * Set initial location and zoom level for the engine. locationChanged and
95     * zoomLevelChanged signals are emitted, so init should be called after
96     * those signals are connected to MapView.
97     */
98     void init();
99
100     /**
101     * @brief Helper for setting view location based on latitude and longitude
102     * coordinates
103     *
104     * @param latLonCoordinate Latitude & longitude coordinates for location
105     */
106     void setViewLocation(QPointF latLonCoordinate);
107
108     /**
109     * @brief Getter for scene
110     *
111     * @return QGraphicsScene
112     */
113     QGraphicsScene* scene();
114
115     /**
116     * @brief Return tile path created from tile values.
117     *
118     * @param zoomLevel tile's zoom level
119     * @param x tile's x value
120     * @param y tile's y value
121     * @return QString tile path
122     */
123     static QString tilePath(int zoomLevel, int x, int y);
124
125 public slots:
126     /**
127     * @brief Slot for immovable scene items position correction
128     *
129     * @param viewTopLeft Scene coordinate of the viewport top left corner
130     */
131     void alignImmovableItems(QPoint viewTopLeft);
132
133     /**
134     * @brief Slot for setting current view location
135     *
136     * Emits locationChanged signal.
137     * @param sceneCoordinate Scene coordinates for new position
138     */
139     void setLocation(QPoint sceneCoordinate);
140
141     /**
142     * @brief Slot for view resizing.
143     *
144     * @param size view size
145     */
146     void viewResized(const QSize &size);
147
148     /**
149     * @brief Slot to catch user own location data
150     *
151     * @param user User info
152     */
153     void receiveOwnLocation(User *user);
154     /**
155     * @brief Slot to catch friends location data
156     *
157     * @param friendsList QList item of friend information
158     */
159     void receiveFriendLocations(QList<User *> &friendsList);
160
161 private:
162     /**
163     * @brief Calculate grid of tile coordinates from current scene coordinate.
164     *
165     * Grid size is calculated from view size and scene's current center coordinate.
166     *
167     * @param sceneCoordinate scene's current center coordinate
168     * @return QRect grid of tile coordinates
169     */
170     QRect calculateTileGrid(QPoint sceneCoordinate);
171
172     /**
173     * @brief Get new tiles.
174     *
175     * Calculates which tiles has to be fetched. Does emit fetchImage for tiles which
176     * aren't already in the scene.
177     * @param sceneCoordinate scene's center coordinate
178     */
179     void getTiles(QPoint sceneCoordinate);
180
181     /**
182     * @brief Check if center tile has changed.
183     *
184     * @param sceneCoordinate scene's center coordinate
185     * @return bool true if center tile changed, false otherwise
186     */
187     bool isCenterTileChanged(QPoint sceneCoordinate);
188
189     /**
190     * @brief Calculate maximum value for tile in this zoom level.
191     *
192     * @param zoomLevel zoom level
193     * @return int tile's maximum value
194     */
195     int tileMaxValue(int zoomLevel);
196
197     /**
198     * @brief Updates the current view rect including margins
199     *
200     * Calculates tiles rect in scene based on m_viewTilesGrid and
201     * calls MapScene::viewRectUpdated()
202     */
203     void updateViewTilesSceneRect();
204
205 private slots:
206     /**
207     * @brief Slot for received map tile images
208     *
209     * Does add MapTile objects to MapScene.
210     * @param zoomLevel Zoom level
211     * @param x Tile x index
212     * @param y Tile y index
213     * @param image Received pixmap
214     */
215     void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);
216
217     /**
218     * @brief Slot for actions after view zoom is finished
219     *
220     * Does run removeOutOfViewTiles
221     */
222     void viewZoomFinished();
223
224     /**
225     * @brief Slot for zooming in
226     *
227     */
228     void zoomIn();
229
230     /**
231     * @brief Slot for zooming out
232     *
233     */
234     void zoomOut();
235
236 /*******************************************************************************
237  * SIGNALS
238  ******************************************************************************/
239 signals:   
240     /**
241     * @brief Signal for image fetching.
242     *
243     * @param zoomLevel Zoom level
244     * @param x Tile x index
245     * @param y Tile y index
246     */
247     void fetchImage(int zoomLevel, int x, int y);
248
249     /**
250     * @brief Signal for view location change
251     *
252     * @param sceneCoordinate New scene coordinates
253     */
254     void locationChanged(QPoint sceneCoordinate);
255
256     /**
257     * @brief Signal for zoom level change
258     *
259     * @param newZoomLevel New zoom level
260     */
261     void zoomLevelChanged(int newZoomLevel);
262
263 /*******************************************************************************
264  * DATA MEMBERS
265  ******************************************************************************/
266 private:
267     QPoint m_centerTile; ///< Current center tile
268     MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
269     MapScene *m_mapScene; ///< Scene for map tiles
270     MapZoomPanel *m_mapZoomPanel; ///< Toolbar for zoom buttons
271     OwnLocationItem *m_ownLocation; ///< Item to show own location
272     QPoint m_sceneCoordinate; ///< Current center coordinate
273     QRect m_viewTilesGrid; ///< Current grid of tiles in view (includes margin)
274     QSize m_viewSize; ///< Current view size
275     bool m_zoomedIn; ///< Flag for checking if zoomed in when zoom is finished
276     int m_zoomLevel; ///< Current zoom level
277     QList<FriendLocationItem *> m_friendsLocations; ///< Location of friends
278 };
279
280 #endif // MAPENGINE_H