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_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_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_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);
129 QRect calculateGrid(QPointF sceneCoordinate);
133 * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
135 * @param zoomLevel Zoom level
136 * @param tileNumbers Tile x & y numbers
137 * @return URL for the required tile
139 QUrl buildURL(int zoomLevel, QPoint tileNumbers);
142 * @brief Parses given URL to zoom, x and y values. Parsed values are
143 * placed in variables given as parameters.
145 * @param url url to parse
146 * @param [out] zoom zoom variable
147 * @param [out] x x variable
148 * @param [out] y y variable
150 void parseURL(const QUrl &url, int &zoom, int &x, int &y);
152 int tileMaxValue(int zoomLevel);
157 * @brief Slot for received map tile images
159 * Does add MapTile objects to MapScene. Zoom level and location is parsed from URL.
160 * @param url URL of the received image
161 * @param pixmap Received pixmap
163 void mapImageReceived(const QUrl &url, const QPixmap &pixmap);
166 * @brief Slot for setting current view location
168 * Emits locationChanged signal.
169 * @param sceneCoordinate Scene coordinates for new position
171 void setLocation(QPointF sceneCoordinate);
175 * @brief Signal for view location change
177 * @param sceneCoordinate New scene coordinates
179 void locationChanged(QPointF sceneCoordinate);
182 * @brief Signal for zoom level change
184 * @param newZoomLevel New zoom level
186 void zoomLevelChanged(int newZoomLevel);
188 void fetchImage(const QUrl &url);
190 void centerToSceneCoordinates(QPointF sceneCoordinate);
193 MapScene *m_mapScene; ///< Scene for map tiles
194 MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
195 int m_zoomLevel; ///< Current zoom level
196 QHash<QString, MapTile *> mapTilesInScene; ///< List of map tiles in map scene
199 #endif // MAPENGINE_H