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
7 Pekka Nissinen - pekka.nissinen@ixonos.com
8 Ville Tiensuu - ville.tiensuu@ixonos.com
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.
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.
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,
25 #include <QtAlgorithms>
28 #include <QStringList>
31 #include <QHashIterator>
34 #include "frienditemshandler.h"
35 #include "gpslocationitem.h"
36 #include "mapcommon.h"
37 #include "mapengine.h"
40 MapEngine::MapEngine(QObject *parent)
42 , m_autoCenteringEnabled(false)
43 , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
44 , m_lastManualPosition(QPoint(0, 0))
45 , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
47 , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
49 qDebug() << __PRETTY_FUNCTION__;
51 m_mapScene = new MapScene(this);
53 m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
54 connect(this, SIGNAL(fetchImage(int, int, int)),
55 m_mapFetcher, SLOT(enqueueFetchMapImage(int, int, int)));
56 connect(m_mapFetcher, SIGNAL(mapImageReceived(int, int, int, QPixmap)),
57 this, SLOT(mapImageReceived(int, int, int, QPixmap)));
59 m_ownLocation = new OwnLocationItem();
60 m_ownLocation->hide(); // hide until first location info is received
61 m_mapScene->addItem(m_ownLocation);
63 m_gpsLocationItem = new GPSLocationItem();
64 m_mapScene->addItem(m_gpsLocationItem);
66 m_friendItemsHandler = new FriendItemsHandler(m_mapScene, this);
67 connect(this, SIGNAL(zoomLevelChanged(int)),
68 m_friendItemsHandler, SLOT(refactorFriendItems(int)));
70 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
71 m_friendItemsHandler, SLOT(friendListUpdated(QList<User*>&)));
74 void MapEngine::init()
76 qDebug() << __PRETTY_FUNCTION__;
78 emit zoomLevelChanged(m_zoomLevel);
79 setViewLocation(QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE));
82 void MapEngine::setViewLocation(QPointF latLonCoordinate)
84 qDebug() << __PRETTY_FUNCTION__;
86 QPoint sceneCoordinate = convertLatLonToSceneCoordinate(latLonCoordinate);
88 m_lastManualPosition = sceneCoordinate;
90 setLocation(sceneCoordinate);
93 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
95 qDebug() << __PRETTY_FUNCTION__;
97 QString hashKey = tilePath(zoomLevel, x, y);
98 if (!m_mapScene->isTileInScene(hashKey)) {
100 MapTile *mapTile = new MapTile();
101 mapTile->setZoomLevel(zoomLevel, m_zoomLevel);
102 mapTile->setTileNumber(QPoint(x, y));
103 mapTile->setPixmap(image);
105 m_mapScene->addTile(mapTile, hashKey);
107 m_mapScene->enqueueRemoveStackedTiles(mapTile);
111 QGraphicsScene* MapEngine::scene()
113 qDebug() << __PRETTY_FUNCTION__;
118 int MapEngine::tileMaxValue(int zoomLevel)
120 qDebug() << __PRETTY_FUNCTION__;
122 return (1 << zoomLevel) - 1;
125 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
127 qDebug() << __PRETTY_FUNCTION__;
129 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
130 int gridWidth = (m_viewSize.width() / TILE_SIZE_X + 1) + (GRID_PADDING * 2);
131 int gridHeight = (m_viewSize.height() / TILE_SIZE_Y + 1) + (GRID_PADDING * 2);
132 int topLeftX = tileCoordinate.x() - (gridWidth / 2);
133 int topLeftY = tileCoordinate.y() - (gridHeight / 2);
135 m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
137 return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
140 void MapEngine::setLocation(QPoint sceneCoordinate)
142 qDebug() << __PRETTY_FUNCTION__;
144 if (disableAutoCentering(sceneCoordinate))
145 emit mapScrolledManually();
147 m_sceneCoordinate = sceneCoordinate;
148 emit locationChanged(m_sceneCoordinate);
150 if (isCenterTileChanged(sceneCoordinate)) {
151 getTiles(sceneCoordinate);
152 m_mapScene->removeOutOfViewTiles();
156 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
158 qDebug() << __PRETTY_FUNCTION__;
160 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
161 QPoint temp = m_centerTile;
162 m_centerTile = centerTile;
164 return (centerTile != temp);
167 void MapEngine::getTiles(QPoint sceneCoordinate)
169 qDebug() << __PRETTY_FUNCTION__;
171 m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
172 updateViewTilesSceneRect();
174 int topLeftX = m_viewTilesGrid.topLeft().x();
175 int topLeftY = m_viewTilesGrid.topLeft().y();
176 int bottomRightX = m_viewTilesGrid.bottomRight().x();
177 int bottomRightY = m_viewTilesGrid.bottomRight().y();
179 int tileMaxVal = tileMaxValue(m_zoomLevel);
181 for (int x = topLeftX; x <= bottomRightX; ++x) {
182 for (int y = topLeftY; y <= bottomRightY; ++y) {
189 else if (tileX > tileMaxVal)
194 else if (tileY > tileMaxVal)
197 if (!m_mapScene->isTileInScene(tilePath(m_zoomLevel, tileX, tileY)))
198 emit fetchImage(m_zoomLevel, tileX, tileY);
203 void MapEngine::updateViewTilesSceneRect()
205 qDebug() << __PRETTY_FUNCTION__;
207 const QPoint ONE_TILE = QPoint(1, 1);
208 const QPoint ONE_PIXEL = QPoint(1, 1);
210 QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
211 // one tile - one pixel is added because returned coordinates are pointing to upper left corner
213 QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
214 m_viewTilesGrid.bottomRight()
215 + ONE_TILE) - ONE_PIXEL;
217 m_mapScene->viewRectUpdated(QRect(topLeft, bottomRight));
220 void MapEngine::viewResized(const QSize &size)
222 qDebug() << __PRETTY_FUNCTION__;
225 getTiles(m_sceneCoordinate);
226 m_mapScene->removeOutOfViewTiles();
229 void MapEngine::viewZoomFinished()
231 qDebug() << __PRETTY_FUNCTION__;
235 m_mapScene->removeOutOfViewTiles();
238 if (m_zoomLevel == MAX_MAP_ZOOM_LEVEL)
239 emit maxZoomLevelReached();
240 else if (m_zoomLevel == MIN_VIEW_ZOOM_LEVEL)
241 emit minZoomLevelReached();
244 void MapEngine::zoomIn()
246 qDebug() << __PRETTY_FUNCTION__;
248 if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
251 emit zoomLevelChanged(m_zoomLevel);
253 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
255 getTiles(m_sceneCoordinate);
259 void MapEngine::zoomOut()
261 qDebug() << __PRETTY_FUNCTION__;
263 if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
265 emit zoomLevelChanged(m_zoomLevel);
267 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
269 getTiles(m_sceneCoordinate);
273 QString MapEngine::tilePath(int zoomLevel, int x, int y)
275 qDebug() << __PRETTY_FUNCTION__;
277 QString tilePathString(QString::number(zoomLevel) + "/");
278 tilePathString.append(QString::number(x) + "/");
279 tilePathString.append(QString::number(y));
281 return tilePathString;
284 QPoint MapEngine::convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
286 qDebug() << __PRETTY_FUNCTION__;
288 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
289 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X * pow));
290 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y * pow));
295 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
297 qDebug() << __PRETTY_FUNCTION__;
299 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
300 int x = tileNumber.x() * TILE_SIZE_X * pow;
301 int y = tileNumber.y() * TILE_SIZE_Y * pow;
306 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
308 qDebug() << __PRETTY_FUNCTION__;
310 qreal longitude = latLonCoordinate.x();
311 qreal latitude = latLonCoordinate.y();
313 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
314 return QPoint(UNDEFINED, UNDEFINED);
315 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
316 return QPoint(UNDEFINED, UNDEFINED);
318 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
320 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
321 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
322 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
324 return QPointF(x * z * TILE_SIZE_X, y * z * TILE_SIZE_Y).toPoint();
327 void MapEngine::receiveOwnLocation(User *user)
329 qDebug() << __PRETTY_FUNCTION__;
331 QPoint newPosition = convertLatLonToSceneCoordinate(user->coordinates());
332 if (m_ownLocation->pos().toPoint() != newPosition) {
333 m_ownLocation->setPos(newPosition);
336 if (!m_ownLocation->isVisible())
337 m_ownLocation->show();
340 void MapEngine::gpsPositionUpdate(QPointF position, qreal accuracy)
342 m_gpsLocationItem->updatePosition(convertLatLonToSceneCoordinate(position), accuracy);
344 if (m_autoCenteringEnabled)
345 setViewLocation(position);
348 bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
350 if (isAutoCenteringEnabled()) {
351 int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
353 QPoint oldPixelValue = QPoint(m_lastManualPosition.x() / zoomFactor,
354 m_lastManualPosition.y() / zoomFactor);
356 QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
357 sceneCoordinate.y() / zoomFactor);
359 if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE) ||
360 (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE))
367 bool MapEngine::isAutoCenteringEnabled()
369 return m_autoCenteringEnabled;
372 void MapEngine::setAutoCentering(bool enabled)
374 m_autoCenteringEnabled = enabled;
377 void MapEngine::gpsEnabled(bool enabled)
379 m_gpsLocationItem->setEnabled(enabled);