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>
35 #include "frienditemshandler.h"
36 #include "gpslocationitem.h"
37 #include "mapcommon.h"
38 #include "mapfetcher.h"
41 #include "network/networkaccessmanager.h"
42 #include "ownlocationitem.h"
43 #include "user/user.h"
45 #include "mapengine.h"
47 MapEngine::MapEngine(QObject *parent)
49 m_autoCenteringEnabled(false),
51 m_zoomLevel(DEFAULT_ZOOM_LEVEL),
52 m_centerTile(QPoint(UNDEFINED, UNDEFINED)),
53 m_lastManualPosition(QPoint(0, 0)),
54 m_tilesGridSize(QSize(0, 0)),
55 m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
57 qDebug() << __PRETTY_FUNCTION__;
59 m_mapScene = new MapScene(this);
61 m_mapFetcher = new MapFetcher(NetworkAccessManager::instance(), this);
62 connect(this, SIGNAL(fetchImage(int, int, int)),
63 m_mapFetcher, SLOT(enqueueFetchMapImage(int, int, int)));
64 connect(m_mapFetcher, SIGNAL(mapImageReceived(int, int, int, QPixmap)),
65 this, SLOT(mapImageReceived(int, int, int, QPixmap)));
66 connect(m_mapFetcher, SIGNAL(error(QString)),
67 this, SIGNAL(error(QString)));
69 m_ownLocation = new OwnLocationItem();
70 m_ownLocation->hide(); // hide until first location info is received
71 m_mapScene->addItem(m_ownLocation);
73 m_gpsLocationItem = new GPSLocationItem();
74 m_mapScene->addItem(m_gpsLocationItem);
76 m_friendItemsHandler = new FriendItemsHandler(m_mapScene, this);
77 connect(this, SIGNAL(zoomLevelChanged(int)),
78 m_friendItemsHandler, SLOT(refactorFriendItems(int)));
80 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
81 m_friendItemsHandler, SLOT(friendListUpdated(QList<User*>&)));
83 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
84 this, SLOT(friendsPositionsUpdated()));
86 connect(m_friendItemsHandler, SIGNAL(locationItemClicked(QList<QString>)),
87 this, SIGNAL(locationItemClicked(QList<QString>)));
90 MapEngine::~MapEngine()
92 qDebug() << __PRETTY_FUNCTION__;
94 QSettings settings(DIRECTORY_NAME, FILE_NAME);
95 settings.setValue(MAP_LAST_POSITION,
96 convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate));
97 settings.setValue(MAP_LAST_ZOOMLEVEL, m_zoomLevel);
100 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
102 qDebug() << __PRETTY_FUNCTION__;
104 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
107 topLeft.setX(tileCoordinate.x() - (m_tilesGridSize.width() / 2));
108 topLeft.setY(tileCoordinate.y() - (m_tilesGridSize.height() / 2));
110 return QRect(topLeft, m_tilesGridSize);
113 QPointF MapEngine::centerGeoCoordinate()
115 qDebug() << __PRETTY_FUNCTION__;
117 return convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate);
120 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
122 qDebug() << __PRETTY_FUNCTION__;
124 qreal longitude = latLonCoordinate.x();
125 qreal latitude = latLonCoordinate.y();
127 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
128 return QPoint(UNDEFINED, UNDEFINED);
129 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
130 return QPoint(UNDEFINED, UNDEFINED);
132 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
134 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
135 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
136 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
138 return QPointF(x * z * TILE_SIZE_X, y * z * TILE_SIZE_Y).toPoint();
141 QPointF MapEngine::convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate)
143 qDebug() << __PRETTY_FUNCTION__;
145 double tileFactor = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
146 double xFactor = (sceneCoordinate.x() / (TILE_SIZE_X*tileFactor));
147 double yFactor = (sceneCoordinate.y() / (TILE_SIZE_Y*tileFactor));
149 tileFactor = 1 << zoomLevel;
150 double longitude = xFactor / tileFactor * 360.0 - 180;
152 double n = M_PI - 2.0 * M_PI * yFactor / tileFactor;
153 double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
155 return QPointF(longitude, latitude);
158 QPoint MapEngine::convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
160 qDebug() << __PRETTY_FUNCTION__;
162 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
163 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X * pow));
164 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y * pow));
169 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
171 qDebug() << __PRETTY_FUNCTION__;
173 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
174 int x = tileNumber.x() * TILE_SIZE_X * pow;
175 int y = tileNumber.y() * TILE_SIZE_Y * pow;
180 bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
182 if (isAutoCenteringEnabled()) {
183 int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
185 QPoint oldPixelValue = QPoint(m_lastManualPosition.x() / zoomFactor,
186 m_lastManualPosition.y() / zoomFactor);
188 QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
189 sceneCoordinate.y() / zoomFactor);
191 if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE) ||
192 (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE))
199 void MapEngine::friendsPositionsUpdated()
201 qDebug() << __PRETTY_FUNCTION__;
203 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
206 void MapEngine::getTiles(QPoint sceneCoordinate)
208 qDebug() << __PRETTY_FUNCTION__;
210 m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
211 updateViewTilesSceneRect();
212 m_mapScene->setTilesGrid(m_viewTilesGrid);
214 int topLeftX = m_viewTilesGrid.topLeft().x();
215 int topLeftY = m_viewTilesGrid.topLeft().y();
216 int bottomRightX = m_viewTilesGrid.bottomRight().x();
217 int bottomRightY = m_viewTilesGrid.bottomRight().y();
219 int tileMaxVal = tileMaxValue(m_zoomLevel);
221 for (int x = topLeftX; x <= bottomRightX; ++x) {
222 for (int y = topLeftY; y <= bottomRightY; ++y) {
224 // map doesn't span in vertical direction
225 if (y < 0 || y > tileMaxVal)
228 if (!m_mapScene->tileInScene(tilePath(m_zoomLevel, x, y)))
229 emit fetchImage(m_zoomLevel, normalize(x, 0, tileMaxVal), y);
234 void MapEngine::gpsPositionUpdate(QPointF position, qreal accuracy)
236 qDebug() << __PRETTY_FUNCTION__;
238 m_gpsLocationItem->updatePosition(convertLatLonToSceneCoordinate(position), accuracy);
239 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
241 if (m_autoCenteringEnabled)
242 setViewLocation(position);
245 void MapEngine::init()
247 qDebug() << __PRETTY_FUNCTION__;
249 QPointF startLocation;
250 QSettings settings(DIRECTORY_NAME, FILE_NAME);
252 if (settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString()
253 == ERROR_VALUE_NOT_FOUND_ON_SETTINGS || settings.value(MAP_LAST_ZOOMLEVEL,
254 ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString() == ERROR_VALUE_NOT_FOUND_ON_SETTINGS) {
256 startLocation = QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE);
257 m_zoomLevel = qBound(MIN_VIEW_ZOOM_LEVEL, DEFAULT_START_ZOOM_LEVEL, MAX_MAP_ZOOM_LEVEL);
259 m_zoomLevel = settings.value(MAP_LAST_ZOOMLEVEL, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toInt();
260 startLocation = settings.value(MAP_LAST_POSITION,
261 ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
264 emit zoomLevelChanged(m_zoomLevel);
265 setViewLocation(QPointF(startLocation.x(), startLocation.y()));
268 bool MapEngine::isAutoCenteringEnabled()
270 return m_autoCenteringEnabled;
273 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
275 qDebug() << __PRETTY_FUNCTION__;
277 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
278 QPoint temp = m_centerTile;
279 m_centerTile = centerTile;
281 return (centerTile != temp);
284 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
286 qDebug() << __PRETTY_FUNCTION__;
288 // add normal tile inside the world
289 QPoint tileNumber(x, y);
290 m_mapScene->addTile(zoomLevel, tileNumber, image, m_zoomLevel);
292 // note: add 1 so odd width is rounded up and even is rounded down
293 int tilesGridWidthHalf = (m_viewTilesGrid.width() + 1) / 2;
295 // duplicate to east side? (don't need to duplicate over padding)
296 if (tileNumber.x() < (tilesGridWidthHalf - GRID_PADDING)) {
297 QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
298 m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
301 // duplicate to west side? (don't need to duplicate over padding)
302 if (tileNumber.x() > (tileMaxValue(zoomLevel) - tilesGridWidthHalf + GRID_PADDING - 1)) {
303 QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
304 m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
308 int MapEngine::normalize(int value, int min, int max)
310 qDebug() << __PRETTY_FUNCTION__;
311 Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
314 value += max - min + 1;
317 value -= max - min + 1;
322 void MapEngine::receiveOwnLocation(User *user)
324 qDebug() << __PRETTY_FUNCTION__;
327 QPoint newPosition = convertLatLonToSceneCoordinate(user->coordinates());
328 if (m_ownLocation->pos().toPoint() != newPosition) {
329 m_ownLocation->setPos(newPosition);
332 if (!m_ownLocation->isVisible())
333 m_ownLocation->show();
336 m_ownLocation->hide();
339 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
342 QGraphicsScene* MapEngine::scene()
344 qDebug() << __PRETTY_FUNCTION__;
349 void MapEngine::setAutoCentering(bool enabled)
351 m_autoCenteringEnabled = enabled;
354 void MapEngine::setGPSEnabled(bool enabled)
356 m_gpsLocationItem->setEnabled(enabled);
359 void MapEngine::setLocation(QPoint sceneCoordinate)
361 qDebug() << __PRETTY_FUNCTION__;
363 // jump to opposite side of the world if world limit is exceeded
364 if (sceneCoordinate.x() < MAP_MIN_PIXEL_X)
365 sceneCoordinate.setX(sceneCoordinate.x() + MAP_PIXELS_X);
366 else if (sceneCoordinate.x() > MAP_MAX_PIXEL_X)
367 sceneCoordinate.setX(sceneCoordinate.x() - MAP_PIXELS_X);
369 if (disableAutoCentering(sceneCoordinate))
370 emit mapScrolledManually();
372 m_sceneCoordinate = sceneCoordinate;
373 emit locationChanged(m_sceneCoordinate);
375 if (isCenterTileChanged(sceneCoordinate)) {
376 getTiles(sceneCoordinate);
377 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
380 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
383 void MapEngine::setZoomLevel(int newZoomLevel)
385 qDebug() << __PRETTY_FUNCTION__;
387 m_zoomLevel = newZoomLevel;
391 void MapEngine::setTilesGridSize(const QSize &viewSize)
393 qDebug() << __PRETTY_FUNCTION__;
395 // there must be scrolling reserve of at least half tile added to tile amount
396 // calculated from view size
397 const qreal SCROLLING_RESERVE = 0.5;
399 // converting scene tile to tile number does cause grid centering inaccuracy of one tile
400 const int CENTER_TILE_INACCURACY = 1;
402 int gridWidth = ceil(qreal(viewSize.width()) / TILE_SIZE_X + SCROLLING_RESERVE)
403 + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
404 int gridHeight = ceil(qreal(viewSize.height()) / TILE_SIZE_Y + SCROLLING_RESERVE)
405 + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
407 m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
409 m_tilesGridSize.setHeight(gridHeight);
410 m_tilesGridSize.setWidth(gridWidth);
412 qWarning() << __PRETTY_FUNCTION__ << "tiles:" << m_tilesGridSize.width()
413 << "*" << m_tilesGridSize.height()
414 << "=" << m_tilesGridSize.width() * m_tilesGridSize.height();
417 void MapEngine::setViewLocation(QPointF latLonCoordinate)
419 qDebug() << __PRETTY_FUNCTION__;
421 QPoint sceneCoordinate = convertLatLonToSceneCoordinate(latLonCoordinate);
423 m_lastManualPosition = sceneCoordinate;
425 setLocation(sceneCoordinate);
428 int MapEngine::tileMaxValue(int zoomLevel)
430 qDebug() << __PRETTY_FUNCTION__;
432 return (1 << zoomLevel) - 1;
435 QString MapEngine::tilePath(int zoomLevel, int x, int y)
437 qDebug() << __PRETTY_FUNCTION__;
439 QString tilePathString(QString::number(zoomLevel) + "/");
440 tilePathString.append(QString::number(x) + "/");
441 tilePathString.append(QString::number(y));
443 return tilePathString;
446 void MapEngine::updateViewTilesSceneRect()
448 qDebug() << __PRETTY_FUNCTION__;
450 const QPoint ONE_TILE = QPoint(1, 1);
451 const QPoint ONE_PIXEL = QPoint(1, 1);
453 QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
454 // one tile - one pixel is added because returned coordinates are pointing to upper left corner
456 QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
457 m_viewTilesGrid.bottomRight()
458 + ONE_TILE) - ONE_PIXEL;
460 m_mapScene->tilesSceneRectUpdated(QRect(topLeft, bottomRight));
463 void MapEngine::viewResized(const QSize &size)
465 qDebug() << __PRETTY_FUNCTION__;
468 setTilesGridSize(m_viewSize);
470 emit locationChanged(m_sceneCoordinate);
471 getTiles(m_sceneCoordinate);
472 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
473 m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
476 void MapEngine::viewZoomFinished()
478 qDebug() << __PRETTY_FUNCTION__;
482 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
485 if (m_zoomLevel == MAX_MAP_ZOOM_LEVEL)
486 emit maxZoomLevelReached();
487 else if (m_zoomLevel == MIN_VIEW_ZOOM_LEVEL)
488 emit minZoomLevelReached();
490 m_mapScene->setZoomLevel(m_zoomLevel);
493 void MapEngine::zoomed()
495 emit zoomLevelChanged(m_zoomLevel);
496 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
497 getTiles(m_sceneCoordinate);
498 m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
499 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
502 void MapEngine::zoomIn()
504 qDebug() << __PRETTY_FUNCTION__;
506 if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
513 void MapEngine::zoomOut()
515 qDebug() << __PRETTY_FUNCTION__;
517 if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {