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,
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
68 static QPoint convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
70 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
71 int x = tileNumber.x() * TILE_SIZE_X * pow;
72 int y = tileNumber.y() * TILE_SIZE_Y * pow;
77 static QPoint convertSceneCoordinateToTileNumber(int zoomLevel, QPointF sceneCoordinate)
79 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
80 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X*pow));
81 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y*pow));
87 * @brief Getter for scene
89 * @return QGraphicsScene
91 QGraphicsScene* scene();
94 * @brief Helper for setting view location based on latitude and longitude
97 * @param latLonCoordinate Latitude & longitude coordinates for location
99 void setViewLocation(QPointF latLonCoordinate);
102 * @brief Converts latitude, longitude and zoom to tile x, y values.
104 * @param latLonCoordinate latitude and longitude values
105 * @return QPoint tile x,y value
107 static QPointF convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
109 /// @todo CREATE TEST CASE & CHECK CALCULATION
110 qDebug() << __PRETTY_FUNCTION__;
112 qreal longitude = latLonCoordinate.x();
113 qreal latitude = latLonCoordinate.y();
115 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
116 return QPoint(UNDEFINED, UNDEFINED);
117 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
118 return QPoint(UNDEFINED, UNDEFINED);
120 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
122 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
123 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
124 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
126 return QPointF(x*z*TILE_SIZE_X, y*z*TILE_SIZE_Y);
131 * @brief Slot for setting current view location
133 * Emits locationChanged signal.
134 * @param sceneCoordinate Scene coordinates for new position
136 void setLocation(QPointF sceneCoordinate);
138 void viewResized(const QSize &size);
142 * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
144 * @param zoomLevel Zoom level
145 * @param tileNumbers Tile x & y numbers
146 * @return URL for the required tile
148 QUrl buildURL(int zoomLevel, QPoint tileNumbers);
151 * @brief Parses given URL to zoom, x and y values. Parsed values are
152 * placed in variables given as parameters.
154 * @param url url to parse
155 * @param [out] zoom zoom variable
156 * @param [out] x x variable
157 * @param [out] y y variable
159 void parseURL(const QUrl &url, int &zoom, int &x, int &y);
162 * @brief Set zValues for all tiles in the scene
164 * Drawing order of MapTiles, which has the zoom level higher than the current
165 * zoom level, is reversed and those MapTiles are mapped between lower level MapTiles.
166 * Example: If maximum zoom level is 18 and current view zoomlevel is 15, then
167 * the drawing order from top to bottom is 15, 16, 14, 17, 13, 18, 12, 11, 10, ...
171 int tileMaxValue(int zoomLevel);
174 * @brief Calculates grid of tile coordinates from current scene coordinate.
176 * Grid size is calculated from view size and scene's current center coordinate.
178 * @param sceneCoordinate scene's current center coordinate
179 * @return QRect grid of tile coordinates
181 QRect calculateGrid(QPointF sceneCoordinate);
184 * @brief Calculate new tiles to fetch.
186 * @param sceneCoordinate scene's center coordinate
188 void calculateNewTiles(QPointF sceneCoordinate);
191 * @brief Removes tiles which are out of view bounds.
193 void removeOldTiles();
196 * @brief Checks if center tile has changed.
198 * @param sceneCoordinate scene's center coordinate
199 * @return bool true if center tile changed, false otherwise
201 bool centerTileChanged(QPointF sceneCoordinate);
205 * @brief Slot for received map tile images
207 * Does add MapTile objects to MapScene. Zoom level and location is parsed from URL.
208 * @param url URL of the received image
209 * @param pixmap Received pixmap
211 void mapImageReceived(const QUrl &url, const QPixmap &pixmap);
214 * @brief Slot for zooming in
220 * @brief Slot for zooming out
227 * @brief Signal for view location change
229 * @param sceneCoordinate New scene coordinates
231 void locationChanged(QPointF sceneCoordinate);
234 * @brief Signal for zoom level change
236 * @param newZoomLevel New zoom level
238 void zoomLevelChanged(int newZoomLevel);
240 void fetchImage(const QUrl &url);
242 void centerToSceneCoordinates(QPointF sceneCoordinate);
246 MapScene *m_mapScene; ///< Scene for map tiles
247 MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
248 int m_zoomLevel; ///< Current zoom level
249 QHash<QString, MapTile *> mapTilesInScene; ///< List of map tiles in map scene
251 QPointF m_sceneCoordinate;
252 QQueue<QString> mapTilesInView;
257 #endif // MAPENGINE_H