5067057b052f3a7761d4487d1dae1a1aab81d30c
[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        Ville Tiensuu - ville.tiensuu@ixonos.com
9
10    Situare is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    version 2 as published by the Free Software Foundation.
13
14    Situare is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with Situare; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22    USA.
23 */
24
25 #ifndef MAPENGINE_H
26 #define MAPENGINE_H
27
28 #include <QtCore>
29
30 #include "mapfetcher.h"
31 #include "mapscene.h"
32 #include "maptile.h"
33 #include "ownlocationitem.h"
34 #include "user/user.h"
35
36 class FriendItemsHandler;
37 class GPSLocationItem;
38
39 /**
40 * @brief Map engine
41 *
42 * Logic for controlling map functionality. Does also include static methods for
43 * converting coordinates.
44 *
45 * @author Sami Rämö - sami.ramo (at) ixonos.com
46 * @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
47 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
48 * @author Ville Tiensuu - ville.tiensuu (at) ixonos.com
49 */
50 class MapEngine : public QObject
51 {
52     Q_OBJECT
53
54 public:
55     /**
56     * @brief Constructor
57     *
58     * @param parent Parent
59     */
60     MapEngine(QObject *parent = 0);
61
62 /*******************************************************************************
63  * MEMBER FUNCTIONS AND SLOTS
64  ******************************************************************************/
65 public:
66     /**
67     * @brief Convert latitude and longitude to scene coordinates.
68     *
69     * @param latLonCoordinate latitude and longitude values
70     * @return scene coordinate
71     */
72     static QPoint convertLatLonToSceneCoordinate(QPointF latLonCoordinate);
73
74     /**
75     * @brief Convert MapScene coordinate to tile x & y numbers.
76     *
77     * @param zoomLevel ZoomLevel
78     * @param sceneCoordinate MapScene coordinate
79     * @return QPoint tile x & y numbers
80     */
81     static QPoint convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate);
82
83     /**
84     * @brief Convert tile x & y numbers to MapScene coordinates
85     *
86     * @param zoomLevel Zoom level
87     * @param tileNumber x & y numbers of the tile
88     * @return QPoint MapScene coordinate
89     */
90     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber);
91
92     /**
93     * @brief MapEngine initializer
94     *
95     * Set initial location and zoom level for the engine. locationChanged and
96     * zoomLevelChanged signals are emitted, so init should be called after
97     * those signals are connected to MapView.
98     */
99     void init();
100
101     /**
102     * @brief Getter for scene
103     *
104     * @return QGraphicsScene
105     */
106     QGraphicsScene* scene();
107
108     /**
109     * @brief Return tile path created from tile values.
110     *
111     * @param zoomLevel tile's zoom level
112     * @param x tile's x value
113     * @param y tile's y value
114     * @return QString tile path
115     */
116     static QString tilePath(int zoomLevel, int x, int y);
117
118     /**
119     * @brief converts scene coordinates to latitude and longitude
120     *
121     * @param current zoom level
122     * @param sceneCoordinate that will be converted
123     */
124     QPointF convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate);
125
126 public slots:
127     /**
128     * @brief Slot for setting current view location
129     *
130     * Emits locationChanged signal.
131     * @param sceneCoordinate Scene coordinates for new position
132     */
133     void setLocation(QPoint sceneCoordinate);
134
135     /**
136     * @brief Helper for setting view location based on latitude and longitude
137     * coordinates
138     *
139     * @param latLonCoordinate Latitude & longitude coordinates for location
140     */
141     void setViewLocation(QPointF latLonCoordinate);
142
143     /**
144     * @brief Slot for view resizing.
145     *
146     * @param size view size
147     */
148     void viewResized(const QSize &size);
149
150     /**
151     * @brief Returns own location crosshair's latitude and longitude coordinates
152     *
153     */
154     void ownLocation();
155
156     /**
157     * @brief Slot to catch user own location data
158     *
159     * @param user User info
160     */
161     void receiveOwnLocation(User *user);    
162
163     /**
164     * @brief Slot to receive visible area of map scene
165     *
166     * @param visible area of map scene
167     */
168     void receiveViewSceneRect(QRect viewSceneRect);
169
170 private:
171     /**
172     * @brief Calculate grid of tile coordinates from current scene coordinate.
173     *
174     * Grid size is calculated from view size and scene's current center coordinate.
175     *
176     * @param sceneCoordinate scene's current center coordinate
177     * @return QRect grid of tile coordinates
178     */
179     QRect calculateTileGrid(QPoint sceneCoordinate);
180
181     /**
182     * @brief Get new tiles.
183     *
184     * Calculates which tiles has to be fetched. Does emit fetchImage for tiles which
185     * aren't already in the scene.
186     * @param sceneCoordinate scene's center coordinate
187     */
188     void getTiles(QPoint sceneCoordinate);
189
190     /**
191     * @brief Check if auto centering is enabled
192     *
193     * @return true if enabled, false otherwise
194     */
195     bool isAutoCenteringEnabled();
196
197     /**
198     * @brief Check if center tile has changed.
199     *
200     * @param sceneCoordinate scene's center coordinate
201     * @return bool true if center tile changed, false otherwise
202     */
203     bool isCenterTileChanged(QPoint sceneCoordinate);
204
205     /**
206     * @brief Check if auto centering should be disabled.
207     *
208     * @param sceneCoordinate scene's center coordinate
209     * @return bool true if auto centering should be disabled
210     */
211     bool disableAutoCentering(QPoint sceneCoordinate);
212
213     /**
214     * @brief Calculate maximum value for tile in this zoom level.
215     *
216     * @param zoomLevel zoom level
217     * @return int tile's maximum value
218     */
219     int tileMaxValue(int zoomLevel);
220
221     /**
222     * @brief Updates the current view rect including margins
223     *
224     * Calculates tiles rect in scene based on m_viewTilesGrid and
225     * calls MapScene::viewRectUpdated()
226     */
227     void updateViewTilesSceneRect();
228
229 private slots:
230     /**
231       * @brief Slot for enabling / disabling GPS
232       *
233       * GPS location item is disabled or enabled based on GPS state
234       *
235       * @param enabled True is GPS is enabled, otherwise false
236       */
237     void gpsEnabled(bool enabled);
238
239     /**
240       * @brief Slot for GPS position updates
241       *
242       * GPS location item is updated and map centered to new location (if automatic
243       * centering is enabled).
244       *
245       * @param position New coordinates from GPS
246       * @param accuracy Accuracy of the GPS fix
247       */
248     void gpsPositionUpdate(QPointF position, qreal accuracy);
249
250     /**
251     * @brief Slot for received map tile images
252     *
253     * Does add MapTile objects to MapScene.
254     * @param zoomLevel Zoom level
255     * @param x Tile x index
256     * @param y Tile y index
257     * @param image Received pixmap
258     */
259     void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);
260
261     /**
262     * @brief Set auto centering.
263     *
264     * @param enabled true if enabled, false otherwise
265     */
266     void setAutoCentering(bool enabled);
267
268     /**
269     * @brief Slot for actions after view zoom is finished
270     *
271     * Does run removeOutOfViewTiles
272     */
273     void viewZoomFinished();
274
275     /**
276     * @brief Slot for zooming in
277     *
278     */
279     void zoomIn();
280
281     /**
282     * @brief Slot for zooming out
283     *
284     */
285     void zoomOut();
286
287 /*******************************************************************************
288  * SIGNALS
289  ******************************************************************************/
290 signals:   
291     /**
292     * @brief Signal for image fetching.
293     *
294     * @param zoomLevel Zoom level
295     * @param x Tile x index
296     * @param y Tile y index
297     */
298     void fetchImage(int zoomLevel, int x, int y);
299
300     /**
301     * @brief Signal when friend list locations are fetched
302     *
303     * @param friendsList Friends list data
304     */
305     void friendsLocationsReady(QList<User *> &friendsList);
306
307     /**
308     * @brief Signal for view location change
309     *
310     * @param sceneCoordinate New scene coordinates
311     */
312     void locationChanged(QPoint sceneCoordinate);
313
314     /**
315     * @brief Signal to notify map scrolling.
316     */
317     void mapScrolledManually();
318
319     /**
320     * @brief Signal to notify when map is zoomed in to the maxmimum.
321     */
322     void maxZoomLevelReached();
323
324     /**
325     * @brief Signal to notify when map is zoomed out to the minimum.
326     */
327     void minZoomLevelReached();
328
329     /**
330     * @brief Signal request mapView to update view port contents
331     */
332     void requestToGetViewPortContents();
333
334     /**
335     * @brief Signal sends location of crosshair
336     *
337     * @param ownLocation location of crosshair (Latitude, Longitude)
338     */
339     void ownLocation(const QPointF ownLocation);
340
341     /**
342     * @brief Signal for zoom level change
343     *
344     * @param newZoomLevel New zoom level
345     */
346     void zoomLevelChanged(int newZoomLevel);
347
348 /*******************************************************************************
349  * DATA MEMBERS
350  ******************************************************************************/
351 private:
352     bool m_autoCenteringEnabled; ///< Auto centering enabled
353     QPoint m_centerTile; ///< Current center tile
354     FriendItemsHandler *m_friendItemsHandler; ///< Handler for friend and group items
355     GPSLocationItem *m_gpsLocationItem; ///< Item pointing current location from GPS
356     QPoint m_lastManualPosition;  ///< Last manually set position in scene coordinate
357     MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
358     MapScene *m_mapScene; ///< Scene for map tiles
359     OwnLocationItem *m_ownLocation; ///< Item to show own location
360     QPoint m_sceneCoordinate; ///< Current center coordinate
361     QRect m_viewTilesGrid; ///< Current grid of tiles in view (includes margin)
362     QSize m_viewSize; ///< Current view size
363     bool m_zoomedIn; ///< Flag for checking if zoomed in when zoom is finished
364     int m_zoomLevel; ///< Current zoom level
365     QRect m_viewArea; ///< Visible area of map scene
366 };
367
368 #endif // MAPENGINE_H