2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
6 Jussi Laitinen - jussi.laitinen@ixonos.com
8 Situare is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 Situare is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Situare; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 #include "mapcommon.h"
29 #include "mapfetcher.h"
36 * Logic for controlling map functionality. Does also include static methods for
37 * converting coordinates.
38 * @author Sami Rämö - sami.ramo (at) ixonos.com
40 class MapEngine : public QObject
47 * @param parent Parent
49 MapEngine(QObject *parent = 0);
52 * @brief MapEngine initializer
54 * Set initial location and zoom level for the engine. locationChanged and
55 * zoomLevelChanged signals are emitted, so init should be called after
56 * those signals are connected to MapView.
61 * @brief Convert tile x & y numbers to MapScene coordinates
63 * @param zoomLevel Zoom level
64 * @param tileNumber x & y numbers of the tile
65 * @return QPoint MapScene coordinate
67 static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
69 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
70 int x = tileNumber.x() * TILE_SIZE_X * pow;
71 int y = tileNumber.y() * TILE_SIZE_Y * pow;
77 * @brief Convert MapScene coordinate to tile x & y numbers.
79 * @param zoomLevel ZoomLevel
80 * @param sceneCoordinate MapScene coordinate
81 * @return QPoint tile x & y numbers
83 static QPoint convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
85 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
86 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X*pow));
87 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y*pow));
93 * @brief Getter for scene
95 * @return QGraphicsScene
97 QGraphicsScene* scene();
100 * @brief Helper for setting view location based on latitude and longitude
103 * @param latLonCoordinate Latitude & longitude coordinates for location
105 void setViewLocation(QPointF latLonCoordinate);
108 * @brief Convert latitude and longitude to scene coordinates.
110 * @param latLonCoordinate latitude and longitude values
111 * @return scene coordinate
113 static QPoint convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
115 /// @todo CREATE TEST CASE & CHECK CALCULATION
116 qDebug() << __PRETTY_FUNCTION__;
118 qreal longitude = latLonCoordinate.x();
119 qreal latitude = latLonCoordinate.y();
121 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
122 return QPoint(UNDEFINED, UNDEFINED);
123 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
124 return QPoint(UNDEFINED, UNDEFINED);
126 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
128 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
129 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
130 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
132 return QPointF(x*z*TILE_SIZE_X, y*z*TILE_SIZE_Y).toPoint();
137 * @brief Slot for setting current view location
139 * Emits locationChanged signal.
140 * @param sceneCoordinate Scene coordinates for new position
142 void setLocation(QPoint sceneCoordinate);
144 void viewResized(const QSize &size);
148 * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
150 * @param zoomLevel Zoom level
151 * @param tileNumbers Tile x & y numbers
152 * @return URL for the required tile
154 QUrl buildURL(int zoomLevel, QPoint tileNumbers);
157 * @brief Parse given URL to zoom, x and y values. Parsed values are
158 * placed in variables given as parameters.
160 * @param url url to parse
161 * @param [out] zoom zoom variable
162 * @param [out] x x variable
163 * @param [out] y y variable
165 void parseURL(const QUrl &url, int &zoom, int &x, int &y);
168 * @brief Set zValues for all tiles in the scene
170 * Drawing order of MapTiles, which has the zoom level higher than the current
171 * zoom level, is reversed and those MapTiles are mapped between lower level MapTiles.
172 * Example: If maximum zoom level is 18 and current view zoomlevel is 15, then
173 * the drawing order from top to bottom is 15, 16, 14, 17, 13, 18, 12, 11, 10, ...
178 * @brief Calculate maximum value for tile in this zoom level.
180 * @param zoomLevel zoom level
181 * @return int tile's maximum value
183 int tileMaxValue(int zoomLevel);
186 * @brief Calculate grid of tile coordinates from current scene coordinate.
188 * Grid size is calculated from view size and scene's current center coordinate.
190 * @param sceneCoordinate scene's current center coordinate
191 * @return QRect grid of tile coordinates
193 QRect calculateGrid(QPoint sceneCoordinate);
196 * @brief Calculate new tiles to fetch.
198 * @param sceneCoordinate scene's center coordinate
200 void calculateNewTiles(QPoint sceneCoordinate);
203 * @brief Remove tiles which are out of view bounds.
205 void removeOldTiles();
208 * @brief Remove tiles which are stacked.
210 * Iterate through tiles which are under this map tile and remove obscured
213 * @param newTile new tile covering old tiles
215 void removeStackedTiles(MapTile *newTile);
218 * @brief Check if center tile has changed.
220 * @param sceneCoordinate scene's center coordinate
221 * @return bool true if center tile changed, false otherwise
223 bool centerTileChanged(QPoint sceneCoordinate);
226 * @brief Return tile path created from tile values.
228 * @param zoomLevel tile's zoom level
229 * @param x tile's x value
230 * @param y tile's y value
231 * @return QString tile path
233 inline QString tilePath(int zoomLevel, int x, int y);
237 * @brief Slot for received map tile images
239 * Does add MapTile objects to MapScene. Zoom level and location is parsed from URL.
240 * @param url URL of the received image
241 * @param pixmap Received pixmap
243 void mapImageReceived(const QUrl &url, const QPixmap &pixmap);
246 * @brief Slot for zooming in
252 * @brief Slot for zooming out
259 * @brief Signal for view location change
261 * @param sceneCoordinate New scene coordinates
263 void locationChanged(QPoint sceneCoordinate);
266 * @brief Signal for zoom level change
268 * @param newZoomLevel New zoom level
270 void zoomLevelChanged(int newZoomLevel);
273 * @brief Signal for image fetching.
275 * @param url image url
277 void fetchImage(const QUrl &url);
281 MapScene *m_mapScene; ///< Scene for map tiles
282 MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
283 int m_zoomLevel; ///< Current zoom level
284 QHash<QString, MapTile *> mapTilesInScene; ///< List of map tiles in map scene
285 QSize m_viewSize; ///< Current view size
286 QPoint m_sceneCoordinate; ///< Current center coordinate
287 QPoint m_centerTile; ///< Current center tile
288 QRect viewGrid; ///< Current grid of tiles in view
291 #endif // MAPENGINE_H