Removed qWarning from Engine.
[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 class QGraphicsScene;
31
32 class FriendItemsHandler;
33 class GPSLocationItem;
34 class MapFetcher;
35 class MapScene;
36 class MapTile;
37 class OwnLocationItem;
38 class User;
39
40 /**
41 * @brief Map engine
42 *
43 * Logic for controlling map functionality. Does also include static methods for
44 * converting coordinates.
45 *
46 * @author Sami Rämö - sami.ramo (at) ixonos.com
47 * @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
48 * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
49 * @author Ville Tiensuu - ville.tiensuu (at) ixonos.com
50 */
51 class MapEngine : public QObject
52 {
53     Q_OBJECT
54
55 public:
56     /**
57     * @brief Constructor
58     *
59     * @param parent Parent
60     */
61     MapEngine(QObject *parent = 0);
62
63     /**
64     * @brief Destructor
65     * Saves view of the map to settings file
66     */
67     ~MapEngine();
68
69 /*******************************************************************************
70  * MEMBER FUNCTIONS AND SLOTS
71  ******************************************************************************/
72 public:
73     /**
74       * @brief Coordinates  of the current center point
75       *
76       * @return Current coordinates (latitude & longitude)
77       */
78     QPointF centerGeoCoordinate();
79
80     /**
81     * @brief Convert latitude and longitude to scene coordinates.
82     *
83     * @param latLonCoordinate latitude and longitude values
84     * @return scene coordinate
85     */
86     static QPoint convertLatLonToSceneCoordinate(QPointF latLonCoordinate);
87
88     /**
89     * @brief converts scene coordinates to latitude and longitude
90     *
91     * @param zoomLevel current zoom level
92     * @param sceneCoordinate that will be converted
93     */
94     QPointF convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate);
95
96     /**
97     * @brief Convert MapScene coordinate to tile x & y numbers.
98     *
99     * @param zoomLevel ZoomLevel
100     * @param sceneCoordinate MapScene coordinate
101     * @return QPoint tile x & y numbers
102     */
103     static QPoint convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate);
104
105     /**
106     * @brief Convert tile x & y numbers to MapScene coordinates
107     *
108     * @param zoomLevel Zoom level
109     * @param tileNumber x & y numbers of the tile
110     * @return QPoint MapScene coordinate
111     */
112     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber);
113
114     /**
115     * @brief MapEngine initializer
116     *
117     * Set initial location and zoom level for the engine. locationChanged and
118     * zoomLevelChanged signals are emitted, so init should be called after
119     * those signals are connected to MapView.
120     */
121     void init();
122
123     /**
124     * @brief Getter for scene
125     *
126     * @return QGraphicsScene
127     */
128     QGraphicsScene* scene();
129
130     /**
131     * @brief Sets new zoom level
132     *
133     * @return newZoomLevel value that is set to new zoom level
134     */
135     void setZoomLevel(const int newZoomLevel);
136
137     /**
138     * @brief Return tile path created from tile values.
139     *
140     * @param zoomLevel tile's zoom level
141     * @param x tile's x value
142     * @param y tile's y value
143     * @return QString tile path
144     */
145     static QString tilePath(int zoomLevel, int x, int y);
146
147 public slots:
148     /**
149     * @brief Slot to catch user own location data
150     *
151     * @param user User info
152     */
153     void receiveOwnLocation(User *user);
154
155     /**
156     * @brief Set auto centering.
157     *
158     * @param enabled true if enabled, false otherwise
159     */
160     void setAutoCentering(bool enabled);
161
162     /**
163       * @brief Slot for enabling / disabling GPS
164       *
165       * GPS location item is disabled or enabled based on GPS state
166       *
167       * @param enabled True is GPS is enabled, otherwise false
168       */
169     void setGPSEnabled(bool enabled);
170
171     /**
172     * @brief Slot for setting current view location
173     *
174     * Emits locationChanged signal.
175     * @param sceneCoordinate Scene coordinates for new position
176     */
177     void setLocation(QPoint sceneCoordinate);
178
179     /**
180     * @brief Helper for setting view location based on latitude and longitude
181     * coordinates
182     *
183     * @param latLonCoordinate Latitude & longitude coordinates for location
184     */
185     void setViewLocation(QPointF latLonCoordinate);    
186
187     /**
188     * @brief Slot for view resizing.
189     *
190     * @param size view size
191     */
192     void viewResized(const QSize &size);
193
194 private:
195     /**
196     * @brief Calculate grid of tile coordinates from current scene coordinate.
197     *
198     * Grid size is calculated from view size and scene's current center coordinate.
199     *
200     * @param sceneCoordinate scene's current center coordinate
201     * @return QRect grid of tile coordinates
202     */
203     QRect calculateTileGrid(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 Get new tiles.
215     *
216     * Calculates which tiles has to be fetched. Does emit fetchImage for tiles which
217     * aren't already in the scene.
218     * @param sceneCoordinate scene's center coordinate
219     */
220     void getTiles(QPoint sceneCoordinate);
221
222     /**
223     * @brief Check if auto centering is enabled
224     *
225     * @return true if enabled, false otherwise
226     */
227     bool isAutoCenteringEnabled();
228
229     /**
230     * @brief Check if center tile has changed.
231     *
232     * @param sceneCoordinate scene's center coordinate
233     * @return bool true if center tile changed, false otherwise
234     */
235     bool isCenterTileChanged(QPoint sceneCoordinate);
236
237     /**
238     * @brief Calculate maximum value for tile in this zoom level.
239     *
240     * @param zoomLevel zoom level
241     * @return int tile's maximum value
242     */
243     int tileMaxValue(int zoomLevel);
244
245     /**
246     * @brief Updates the current view rect including margins
247     *
248     * Calculates tiles rect in scene based on m_viewTilesGrid and
249     * calls MapScene::viewRectUpdated()
250     */
251     void updateViewTilesSceneRect();
252
253 private slots:
254     /**
255       * @brief Slot for GPS position updates
256       *
257       * GPS location item is updated and map centered to new location (if automatic
258       * centering is enabled).
259       *
260       * @param position New coordinates from GPS
261       * @param accuracy Accuracy of the GPS fix
262       */
263     void gpsPositionUpdate(QPointF position, qreal accuracy);
264
265     /**
266     * @brief Slot for received map tile images
267     *
268     * Does add MapTile objects to MapScene.
269     * @param zoomLevel Zoom level
270     * @param x Tile x index
271     * @param y Tile y index
272     * @param image Received pixmap
273     */
274     void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);    
275
276     /**
277     * @brief Slot for actions after view zoom is finished
278     *
279     * Does run removeOutOfViewTiles
280     */
281     void viewZoomFinished();
282
283     /**
284     * @brief Slot for zooming in
285     */
286     void zoomIn();
287
288     /**
289     * @brief Slot for zooming out
290     */
291     void zoomOut();
292
293 /*******************************************************************************
294  * SIGNALS
295  ******************************************************************************/
296 signals:
297     /**
298     * @brief Signals error
299     *
300     * @param error error message
301     */
302     void error(const QString &error);
303
304     /**
305     * @brief Signal for image fetching.
306     *
307     * @param zoomLevel Zoom level
308     * @param x Tile x index
309     * @param y Tile y index
310     */
311     void fetchImage(int zoomLevel, int x, int y);
312
313     /**
314     * @brief Signal when friend list locations are fetched
315     *
316     * @param friendsList Friends list data
317     */
318     void friendsLocationsReady(QList<User *> &friendsList);
319
320     /**
321     * @brief Request view centering to new locaiton
322     *
323     * @param sceneCoordinate New scene coordinates
324     */
325     void locationChanged(QPoint sceneCoordinate);
326
327     /**
328     * @brief Signal is emitted when location item is clicked.
329     *
330     * @param userIDs list of friends user IDs in the group
331     */
332     void locationItemClicked(const QList<QString> &userIDs);
333
334     /**
335     * @brief Signal to notify map scrolling.
336     */
337     void mapScrolledManually();
338
339     /**
340     * @brief Signal to notify when map is zoomed in to the maxmimum.
341     */
342     void maxZoomLevelReached();
343
344     /**
345     * @brief Signal to notify when map is zoomed out to the minimum.
346     */
347     void minZoomLevelReached();
348
349     /**
350     * @brief Request view changing zoom level
351     *
352     * @param newZoomLevel New zoom level
353     */
354     void zoomLevelChanged(int newZoomLevel);
355
356 /*******************************************************************************
357  * DATA MEMBERS
358  ******************************************************************************/
359 private:
360     bool m_autoCenteringEnabled;   ///< Auto centering enabled
361     bool m_zoomedIn;               ///< Flag for checking if zoomed in when zoom is finished
362
363     int m_zoomLevel;               ///< Current zoom level
364
365     QPoint m_centerTile;           ///< Current center tile
366     QPoint m_lastManualPosition;   ///< Last manually set position in scene coordinate
367     QPoint m_sceneCoordinate;      ///< Current center coordinate
368
369     QRect m_viewTilesGrid;         ///< Current grid of tiles in view (includes margin)
370
371     QSize m_viewSize;              ///< Current view size
372
373     FriendItemsHandler *m_friendItemsHandler;   ///< Handler for friend and group items
374     GPSLocationItem *m_gpsLocationItem;         ///< Item pointing current location from GPS
375     MapFetcher *m_mapFetcher;                   ///< Fetcher for map tiles
376     MapScene *m_mapScene;                       ///< Scene for map tiles
377     OwnLocationItem *m_ownLocation;             ///< Item to show own location
378 };
379
380 #endif // MAPENGINE_H