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)));
67 m_ownLocation = new OwnLocationItem();
68 m_ownLocation->hide(); // hide until first location info is received
69 m_mapScene->addItem(m_ownLocation);
71 m_gpsLocationItem = new GPSLocationItem();
72 m_mapScene->addItem(m_gpsLocationItem);
74 m_friendItemsHandler = new FriendItemsHandler(m_mapScene, this);
75 connect(this, SIGNAL(zoomLevelChanged(int)),
76 m_friendItemsHandler, SLOT(refactorFriendItems(int)));
78 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
79 m_friendItemsHandler, SLOT(friendListUpdated(QList<User*>&)));
81 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
82 this, SLOT(friendsPositionsUpdated()));
84 connect(m_friendItemsHandler, SIGNAL(locationItemClicked(QList<QString>)),
85 this, SIGNAL(locationItemClicked(QList<QString>)));
88 MapEngine::~MapEngine()
90 qDebug() << __PRETTY_FUNCTION__;
92 QSettings settings(DIRECTORY_NAME, FILE_NAME);
93 settings.setValue(MAP_LAST_POSITION,
94 convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate));
95 settings.setValue(MAP_LAST_ZOOMLEVEL, m_zoomLevel);
98 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
100 qDebug() << __PRETTY_FUNCTION__;
102 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
105 topLeft.setX(tileCoordinate.x() - (m_tilesGridSize.width() / 2));
106 topLeft.setY(tileCoordinate.y() - (m_tilesGridSize.height() / 2));
108 return QRect(topLeft, m_tilesGridSize);
111 QPointF MapEngine::centerGeoCoordinate()
113 qDebug() << __PRETTY_FUNCTION__;
115 return convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate);
118 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
120 qDebug() << __PRETTY_FUNCTION__;
122 qreal longitude = latLonCoordinate.x();
123 qreal latitude = latLonCoordinate.y();
125 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
126 return QPoint(UNDEFINED, UNDEFINED);
127 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
128 return QPoint(UNDEFINED, UNDEFINED);
130 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
132 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
133 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
134 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
136 return QPointF(x * z * TILE_SIZE_X, y * z * TILE_SIZE_Y).toPoint();
139 QPointF MapEngine::convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate)
141 qDebug() << __PRETTY_FUNCTION__;
143 double tileFactor = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
144 double xFactor = (sceneCoordinate.x() / (TILE_SIZE_X*tileFactor));
145 double yFactor = (sceneCoordinate.y() / (TILE_SIZE_Y*tileFactor));
147 tileFactor = 1 << zoomLevel;
148 double longitude = xFactor / tileFactor * 360.0 - 180;
150 double n = M_PI - 2.0 * M_PI * yFactor / tileFactor;
151 double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
153 return QPointF(longitude, latitude);
156 QPoint MapEngine::convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
158 qDebug() << __PRETTY_FUNCTION__;
160 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
161 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X * pow));
162 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y * pow));
167 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
169 qDebug() << __PRETTY_FUNCTION__;
171 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
172 int x = tileNumber.x() * TILE_SIZE_X * pow;
173 int y = tileNumber.y() * TILE_SIZE_Y * pow;
178 bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
180 if (isAutoCenteringEnabled()) {
181 int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
183 QPoint oldPixelValue = QPoint(m_lastManualPosition.x() / zoomFactor,
184 m_lastManualPosition.y() / zoomFactor);
186 QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
187 sceneCoordinate.y() / zoomFactor);
189 if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE) ||
190 (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE))
197 void MapEngine::friendsPositionsUpdated()
199 qDebug() << __PRETTY_FUNCTION__;
201 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
204 void MapEngine::getTiles(QPoint sceneCoordinate)
206 qDebug() << __PRETTY_FUNCTION__;
208 m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
209 updateViewTilesSceneRect();
211 int topLeftX = m_viewTilesGrid.topLeft().x();
212 int topLeftY = m_viewTilesGrid.topLeft().y();
213 int bottomRightX = m_viewTilesGrid.bottomRight().x();
214 int bottomRightY = m_viewTilesGrid.bottomRight().y();
216 int tileMaxVal = tileMaxValue(m_zoomLevel);
218 for (int x = topLeftX; x <= bottomRightX; ++x) {
219 for (int y = topLeftY; y <= bottomRightY; ++y) {
224 // span in horizontal direction if world limits has been reached
226 // tileX += tileMaxVal + 1;
227 // else if (tileX > tileMaxVal)
228 // tileX -= tileMaxVal + 1;
230 // map doesn't span in vertical direction
231 if (tileY < 0 || tileY > tileMaxVal)
234 if (!m_mapScene->tileInScene(tilePath(m_zoomLevel, tileX, tileY)))
235 emit fetchImage(m_zoomLevel, normalize(tileX, 0, tileMaxVal), tileY);
239 // QRect spanRect = m_mapScene->spanItems(m_scrollDirection, m_zoomLevel, m_sceneCoordinate, m_viewSize);
240 // m_friendItemsHandler->spanHiddenFriendLocationItems(m_scrollDirection, spanRect, m_sceneCoordinate);
241 // m_friendItemsHandler->refactorFriendItems(m_zoomLevel);
246 void MapEngine::gpsPositionUpdate(QPointF position, qreal accuracy)
248 qDebug() << __PRETTY_FUNCTION__;
250 m_gpsLocationItem->updatePosition(convertLatLonToSceneCoordinate(position), accuracy);
251 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
253 if (m_autoCenteringEnabled)
254 setViewLocation(position);
257 void MapEngine::init()
259 qDebug() << __PRETTY_FUNCTION__;
261 QPointF startLocation;
262 QSettings settings(DIRECTORY_NAME, FILE_NAME);
264 if (settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString()
265 == ERROR_VALUE_NOT_FOUND_ON_SETTINGS || settings.value(MAP_LAST_ZOOMLEVEL,
266 ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString() == ERROR_VALUE_NOT_FOUND_ON_SETTINGS) {
268 startLocation = QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE);
269 m_zoomLevel = qBound(MIN_VIEW_ZOOM_LEVEL, DEFAULT_START_ZOOM_LEVEL, MAX_MAP_ZOOM_LEVEL);
271 m_zoomLevel = settings.value(MAP_LAST_ZOOMLEVEL, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toInt();
272 startLocation = settings.value(MAP_LAST_POSITION,
273 ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
276 emit zoomLevelChanged(m_zoomLevel);
277 setViewLocation(QPointF(startLocation.x(), startLocation.y()));
280 bool MapEngine::isAutoCenteringEnabled()
282 return m_autoCenteringEnabled;
285 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
287 qDebug() << __PRETTY_FUNCTION__;
289 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
290 QPoint temp = m_centerTile;
291 m_centerTile = centerTile;
293 return (centerTile != temp);
296 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
301 qDebug() << __PRETTY_FUNCTION__; // << "x:" << x << "y:" << y;
304 QPoint tileNumber(x, y);
305 m_mapScene->addTile(zoomLevel, tileNumber, image, m_zoomLevel);
307 int tilesGridWidthHalf = (m_viewTilesGrid.width() + 1) / 2;
309 // expand to east side? (don't need to expand over padding)
310 if (tileNumber.x() < (tilesGridWidthHalf - GRID_PADDING)) {
311 QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
312 m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
313 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
316 // expand to west side? (don't need to expand over padding)
317 if (tileNumber.x() > (tileMaxValue(zoomLevel) - tilesGridWidthHalf + GRID_PADDING)) {
318 QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
319 m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
320 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
323 // // expanding is only done if received tile zoom level is same as current zoom level
324 // if (zoomLevel == m_zoomLevel) {
325 // // expand to east side?
326 // if (m_viewTilesGrid.right() > tileMaxValue(zoomLevel)) {
327 // int crossing = m_viewTilesGrid.right() - tileMaxValue(zoomLevel);
328 // if (tileNumber.x() < crossing) {
329 // QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
330 // m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
331 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
335 // // expand to west side?
336 // if (m_viewTilesGrid.left() < 0) {
337 // int crossing = -m_viewTilesGrid.left();
338 // if (tileNumber.x() > (tileMaxValue(zoomLevel) - crossing)) {
339 // QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
340 // m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
341 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
347 // int crossing = m_tilesGridSize.width() - tileMaxValue(zoomLevel) - 1;
348 //// qWarning() << __PRETTY_FUNCTION__ << "crossing:" << crossing;
349 // if (crossing > 0) {
350 // qWarning() << __PRETTY_FUNCTION__ << "grid was bigger than amount of tiles at this tile level";
352 // if (x < crossing) {
353 // // expand to east side
354 // QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
355 // m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
356 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x();
359 // if (x > (tileMaxValue(zoomLevel) - crossing)) {
360 // // expand to west side
361 // QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
362 // m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
363 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x();
367 // m_mapScene->spanItems(m_scrollDirection, m_zoomLevel);
370 int MapEngine::normalize(int value, int min, int max)
372 qDebug() << __PRETTY_FUNCTION__; // << "value:" << value << "min:" << min << "max:" << max;
373 Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
376 value += max - min + 1;
379 value -= max - min + 1;
384 void MapEngine::receiveOwnLocation(User *user)
386 qDebug() << __PRETTY_FUNCTION__;
389 QPoint newPosition = convertLatLonToSceneCoordinate(user->coordinates());
390 if (m_ownLocation->pos().toPoint() != newPosition) {
391 m_ownLocation->setPos(newPosition);
394 if (!m_ownLocation->isVisible())
395 m_ownLocation->show();
398 m_ownLocation->hide();
401 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
404 QGraphicsScene* MapEngine::scene()
406 qDebug() << __PRETTY_FUNCTION__;
411 void MapEngine::setAutoCentering(bool enabled)
413 m_autoCenteringEnabled = enabled;
416 void MapEngine::setGPSEnabled(bool enabled)
418 m_gpsLocationItem->setEnabled(enabled);
421 void MapEngine::setLocation(QPoint sceneCoordinate)
423 qDebug() << __PRETTY_FUNCTION__;
425 // jump to opposite side of the world if world limit is exceeded
426 if (sceneCoordinate.x() < 0)
427 sceneCoordinate.setX(sceneCoordinate.x() + WORLD_PIXELS_X);
428 else if (sceneCoordinate.x() > WORLD_PIXELS_X - 1)
429 sceneCoordinate.setX(sceneCoordinate.x() - WORLD_PIXELS_X);
431 if (disableAutoCentering(sceneCoordinate))
432 emit mapScrolledManually();
434 m_sceneCoordinate = sceneCoordinate;
435 emit locationChanged(m_sceneCoordinate);
437 if (isCenterTileChanged(sceneCoordinate)) {
438 getTiles(sceneCoordinate);
439 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
442 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
445 void MapEngine::setZoomLevel(int newZoomLevel)
447 qDebug() << __PRETTY_FUNCTION__;
449 m_zoomLevel = newZoomLevel;
453 void MapEngine::setTilesGridSize(const QSize &viewSize)
455 qDebug() << __PRETTY_FUNCTION__;
457 // there must be scrolling reserve of at least half tile added to tile amount
458 // calculated from view size
459 const qreal SCROLLING_RESERVE = 0.5;
461 // converting scene tile to tile number does cause grid centering inaccuracy of one tile
462 const int CENTER_TILE_INACCURACY = 1;
464 int gridWidth = ceil(qreal(viewSize.width()) / TILE_SIZE_X + SCROLLING_RESERVE)
465 + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
466 int gridHeight = ceil(qreal(viewSize.height()) / TILE_SIZE_Y + SCROLLING_RESERVE)
467 + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
469 m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
471 m_tilesGridSize.setHeight(gridHeight);
472 m_tilesGridSize.setWidth(gridWidth);
474 // qWarning() << __PRETTY_FUNCTION__ << "tiles grid:" << m_tilesGridSize.width()
475 // << "*" << m_tilesGridSize.height();
478 void MapEngine::setViewLocation(QPointF latLonCoordinate)
480 qDebug() << __PRETTY_FUNCTION__;
482 QPoint sceneCoordinate = convertLatLonToSceneCoordinate(latLonCoordinate);
484 m_lastManualPosition = sceneCoordinate;
486 setLocation(sceneCoordinate);
489 int MapEngine::tileMaxValue(int zoomLevel)
491 qDebug() << __PRETTY_FUNCTION__;
493 return (1 << zoomLevel) - 1;
496 QString MapEngine::tilePath(int zoomLevel, int x, int y)
498 qDebug() << __PRETTY_FUNCTION__;
500 QString tilePathString(QString::number(zoomLevel) + "/");
501 tilePathString.append(QString::number(x) + "/");
502 tilePathString.append(QString::number(y));
504 return tilePathString;
507 void MapEngine::updateViewTilesSceneRect()
509 qDebug() << __PRETTY_FUNCTION__;
511 const QPoint ONE_TILE = QPoint(1, 1);
512 const QPoint ONE_PIXEL = QPoint(1, 1);
514 QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
515 // one tile - one pixel is added because returned coordinates are pointing to upper left corner
517 QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
518 m_viewTilesGrid.bottomRight()
519 + ONE_TILE) - ONE_PIXEL;
521 m_mapScene->tilesSceneRectUpdated(QRect(topLeft, bottomRight));
524 void MapEngine::viewResized(const QSize &size)
526 qDebug() << __PRETTY_FUNCTION__;
529 setTilesGridSize(m_viewSize);
531 emit locationChanged(m_sceneCoordinate);
532 getTiles(m_sceneCoordinate);
533 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
534 m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
537 void MapEngine::viewZoomFinished()
539 qDebug() << __PRETTY_FUNCTION__;
543 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
546 if (m_zoomLevel == MAX_MAP_ZOOM_LEVEL)
547 emit maxZoomLevelReached();
548 else if (m_zoomLevel == MIN_VIEW_ZOOM_LEVEL)
549 emit minZoomLevelReached();
552 void MapEngine::zoomed()
554 emit zoomLevelChanged(m_zoomLevel);
555 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
556 getTiles(m_sceneCoordinate);
557 m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
558 m_mapScene->spanItems(m_zoomLevel, m_sceneCoordinate, m_viewSize);
561 void MapEngine::zoomIn()
563 qDebug() << __PRETTY_FUNCTION__;
565 if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
572 void MapEngine::zoomOut()
574 qDebug() << __PRETTY_FUNCTION__;
576 if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {