55559721a33a6e347735a8c08f148b9de8f1713e
[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  * MEMBER FUNCTIONS AND SLOTS
65  ******************************************************************************/
66 public:
67     /**
68       * @brief Coordinates  of the current center point
69       *
70       * @return Current coordinates (latitude & longitude)
71       */
72     QPointF centerGeoCoordinate();
73
74     /**
75     * @brief Convert latitude and longitude to scene coordinates.
76     *
77     * @param latLonCoordinate latitude and longitude values
78     * @return scene coordinate
79     */
80     static QPoint convertLatLonToSceneCoordinate(QPointF latLonCoordinate);
81
82     /**
83     * @brief converts scene coordinates to latitude and longitude
84     *
85     * @param zoomLevel current zoom level
86     * @param sceneCoordinate that will be converted
87     */
88     QPointF convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate);
89
90     /**
91     * @brief Convert MapScene coordinate to tile x & y numbers.
92     *
93     * @param zoomLevel ZoomLevel
94     * @param sceneCoordinate MapScene coordinate
95     * @return QPoint tile x & y numbers
96     */
97     static QPoint convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate);
98
99     /**
100     * @brief Convert tile x & y numbers to MapScene coordinates
101     *
102     * @param zoomLevel Zoom level
103     * @param tileNumber x & y numbers of the tile
104     * @return QPoint MapScene coordinate
105     */
106     static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber);
107
108     /**
109     * @brief MapEngine initializer
110     *
111     * Set initial location and zoom level for the engine. locationChanged and
112     * zoomLevelChanged signals are emitted, so init should be called after
113     * those signals are connected to MapView.
114     */
115     void init();
116
117     /**
118     * @brief Getter for scene
119     *
120     * @return QGraphicsScene
121     */
122     QGraphicsScene* scene();
123
124     /**
125     * @brief Return tile path created from tile values.
126     *
127     * @param zoomLevel tile's zoom level
128     * @param x tile's x value
129     * @param y tile's y value
130     * @return QString tile path
131     */
132     static QString tilePath(int zoomLevel, int x, int y);
133
134 public slots:
135     /**
136     * @brief Slot to catch user own location data
137     *
138     * @param user User info
139     */
140     void receiveOwnLocation(User *user);
141
142     /**
143     * @brief Set auto centering.
144     *
145     * @param enabled true if enabled, false otherwise
146     */
147     void setAutoCentering(bool enabled);
148
149     /**
150       * @brief Slot for enabling / disabling GPS
151       *
152       * GPS location item is disabled or enabled based on GPS state
153       *
154       * @param enabled True is GPS is enabled, otherwise false
155       */
156     void setGPSEnabled(bool enabled);
157
158     /**
159     * @brief Slot for setting current view location
160     *
161     * Emits locationChanged signal.
162     * @param sceneCoordinate Scene coordinates for new position
163     */
164     void setLocation(QPoint sceneCoordinate);
165
166     /**
167     * @brief Helper for setting view location based on latitude and longitude
168     * coordinates
169     *
170     * @param latLonCoordinate Latitude & longitude coordinates for location
171     */
172     void setViewLocation(QPointF latLonCoordinate);
173
174     /**
175     * @brief Slot for view resizing.
176     *
177     * @param size view size
178     */
179     void viewResized(const QSize &size);
180
181 private:
182     /**
183     * @brief Calculate grid of tile coordinates from current scene coordinate.
184     *
185     * Grid size is calculated from view size and scene's current center coordinate.
186     *
187     * @param sceneCoordinate scene's current center coordinate
188     * @return QRect grid of tile coordinates
189     */
190     QRect calculateTileGrid(QPoint sceneCoordinate);
191
192     /**
193     * @brief Check if auto centering should be disabled.
194     *
195     * @param sceneCoordinate scene's center coordinate
196     * @return bool true if auto centering should be disabled
197     */
198     bool disableAutoCentering(QPoint sceneCoordinate);
199
200     /**
201     * @brief Get new tiles.
202     *
203     * Calculates which tiles has to be fetched. Does emit fetchImage for tiles which
204     * aren't already in the scene.
205     * @param sceneCoordinate scene's center coordinate
206     */
207     void getTiles(QPoint sceneCoordinate);
208
209     /**
210     * @brief Check if auto centering is enabled
211     *
212     * @return true if enabled, false otherwise
213     */
214     bool isAutoCenteringEnabled();
215
216     /**
217     * @brief Check if center tile has changed.
218     *
219     * @param sceneCoordinate scene's center coordinate
220     * @return bool true if center tile changed, false otherwise
221     */
222     bool isCenterTileChanged(QPoint sceneCoordinate);
223
224     /**
225     * @brief Calculate maximum value for tile in this zoom level.
226     *
227     * @param zoomLevel zoom level
228     * @return int tile's maximum value
229     */
230     int tileMaxValue(int zoomLevel);
231
232     /**
233     * @brief Updates the current view rect including margins
234     *
235     * Calculates tiles rect in scene based on m_viewTilesGrid and
236     * calls MapScene::viewRectUpdated()
237     */
238     void updateViewTilesSceneRect();
239
240 private slots:
241     /**
242       * @brief Slot for GPS position updates
243       *
244       * GPS location item is updated and map centered to new location (if automatic
245       * centering is enabled).
246       *
247       * @param position New coordinates from GPS
248       * @param accuracy Accuracy of the GPS fix
249       */
250     void gpsPositionUpdate(QPointF position, qreal accuracy);
251
252     /**
253     * @brief Slot for received map tile images
254     *
255     * Does add MapTile objects to MapScene.
256     * @param zoomLevel Zoom level
257     * @param x Tile x index
258     * @param y Tile y index
259     * @param image Received pixmap
260     */
261     void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);
262
263     /**
264     * @brief Slot for actions after view zoom is finished
265     *
266     * Does run removeOutOfViewTiles
267     */
268     void viewZoomFinished();
269
270     /**
271     * @brief Slot for zooming in
272     */
273     void zoomIn();
274
275     /**
276     * @brief Slot for zooming out
277     */
278     void zoomOut();
279
280 /*******************************************************************************
281  * SIGNALS
282  ******************************************************************************/
283 signals:   
284     /**
285     * @brief Signal for image fetching.
286     *
287     * @param zoomLevel Zoom level
288     * @param x Tile x index
289     * @param y Tile y index
290     */
291     void fetchImage(int zoomLevel, int x, int y);
292
293     /**
294     * @brief Signal when friend list locations are fetched
295     *
296     * @param friendsList Friends list data
297     */
298     void friendsLocationsReady(QList<User *> &friendsList);
299
300     /**
301     * @brief Request view centering to new locaiton
302     *
303     * @param sceneCoordinate New scene coordinates
304     */
305     void locationChanged(QPoint sceneCoordinate);
306
307     /**
308     * @brief Signal is emitted when location item is clicked.
309     *
310     * @param userIDs list of friends user IDs in the group
311     */
312     void locationItemClicked(const QList<QString> &userIDs);
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 Request view changing zoom level
331     *
332     * @param newZoomLevel New zoom level
333     */
334     void zoomLevelChanged(int newZoomLevel);
335
336 /*******************************************************************************
337  * DATA MEMBERS
338  ******************************************************************************/
339 private:
340     bool m_autoCenteringEnabled;   ///< Auto centering enabled
341     bool m_zoomedIn;               ///< Flag for checking if zoomed in when zoom is finished
342
343     int m_zoomLevel;               ///< Current zoom level
344
345     QPoint m_centerTile;           ///< Current center tile
346     QPoint m_lastManualPosition;   ///< Last manually set position in scene coordinate
347     QPoint m_sceneCoordinate;      ///< Current center coordinate
348
349     QRect m_viewTilesGrid;         ///< Current grid of tiles in view (includes margin)
350
351     QSize m_viewSize;              ///< Current view size
352
353     FriendItemsHandler *m_friendItemsHandler;   ///< Handler for friend and group items
354     GPSLocationItem *m_gpsLocationItem;         ///< Item pointing current location from GPS
355     MapFetcher *m_mapFetcher;                   ///< Fetcher for map tiles
356     MapScene *m_mapScene;                       ///< Scene for map tiles
357     OwnLocationItem *m_ownLocation;             ///< Item to show own location
358 };
359
360 #endif // MAPENGINE_H