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(m_friendItemsHandler, SIGNAL(locationItemClicked(QList<QString>)),
82 this, SIGNAL(locationItemClicked(QList<QString>)));
85 MapEngine::~MapEngine()
87 qDebug() << __PRETTY_FUNCTION__;
89 QSettings settings(DIRECTORY_NAME, FILE_NAME);
90 settings.setValue(MAP_LAST_POSITION,
91 convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate));
92 settings.setValue(MAP_LAST_ZOOMLEVEL, m_zoomLevel);
95 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
97 qDebug() << __PRETTY_FUNCTION__;
99 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
102 topLeft.setX(tileCoordinate.x() - (m_tilesGridSize.width() / 2));
103 topLeft.setY(tileCoordinate.y() - (m_tilesGridSize.height() / 2));
105 return QRect(topLeft, m_tilesGridSize);
108 QPointF MapEngine::centerGeoCoordinate()
110 qDebug() << __PRETTY_FUNCTION__;
112 return convertSceneCoordinateToLatLon(m_zoomLevel, m_sceneCoordinate);
115 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
117 qDebug() << __PRETTY_FUNCTION__;
119 qreal longitude = latLonCoordinate.x();
120 qreal latitude = latLonCoordinate.y();
122 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
123 return QPoint(UNDEFINED, UNDEFINED);
124 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
125 return QPoint(UNDEFINED, UNDEFINED);
127 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
129 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
130 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
131 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
133 return QPointF(x * z * TILE_SIZE_X, y * z * TILE_SIZE_Y).toPoint();
136 QPointF MapEngine::convertSceneCoordinateToLatLon(int zoomLevel, QPoint sceneCoordinate)
138 qDebug() << __PRETTY_FUNCTION__;
140 double tileFactor = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
141 double xFactor = (sceneCoordinate.x() / (TILE_SIZE_X*tileFactor));
142 double yFactor = (sceneCoordinate.y() / (TILE_SIZE_Y*tileFactor));
144 tileFactor = 1 << zoomLevel;
145 double longitude = xFactor / tileFactor * 360.0 - 180;
147 double n = M_PI - 2.0 * M_PI * yFactor / tileFactor;
148 double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
150 return QPointF(longitude, latitude);
153 QPoint MapEngine::convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
155 qDebug() << __PRETTY_FUNCTION__;
157 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
158 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X * pow));
159 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y * pow));
164 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
166 qDebug() << __PRETTY_FUNCTION__;
168 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
169 int x = tileNumber.x() * TILE_SIZE_X * pow;
170 int y = tileNumber.y() * TILE_SIZE_Y * pow;
175 bool MapEngine::disableAutoCentering(QPoint sceneCoordinate)
177 if (isAutoCenteringEnabled()) {
178 int zoomFactor = (1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel));
180 QPoint oldPixelValue = QPoint(m_lastManualPosition.x() / zoomFactor,
181 m_lastManualPosition.y() / zoomFactor);
183 QPoint newPixelValue = QPoint(sceneCoordinate.x() / zoomFactor,
184 sceneCoordinate.y() / zoomFactor);
186 if ((abs(oldPixelValue.x() - newPixelValue.x()) > AUTO_CENTERING_DISABLE_DISTANCE) ||
187 (abs(oldPixelValue.y() - newPixelValue.y()) > AUTO_CENTERING_DISABLE_DISTANCE))
194 void MapEngine::getTiles(QPoint sceneCoordinate)
196 qDebug() << __PRETTY_FUNCTION__;
198 m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
199 updateViewTilesSceneRect();
201 int topLeftX = m_viewTilesGrid.topLeft().x();
202 int topLeftY = m_viewTilesGrid.topLeft().y();
203 int bottomRightX = m_viewTilesGrid.bottomRight().x();
204 int bottomRightY = m_viewTilesGrid.bottomRight().y();
206 int tileMaxVal = tileMaxValue(m_zoomLevel);
208 for (int x = topLeftX; x <= bottomRightX; ++x) {
209 for (int y = topLeftY; y <= bottomRightY; ++y) {
214 // span in horizontal direction if world limits has been reached
216 // tileX += tileMaxVal + 1;
217 // else if (tileX > tileMaxVal)
218 // tileX -= tileMaxVal + 1;
220 // map doesn't span in vertical direction
221 if (tileY < 0 || tileY > tileMaxVal)
224 if (!m_mapScene->tileInScene(tilePath(m_zoomLevel, tileX, tileY)))
225 emit fetchImage(m_zoomLevel, normalize(tileX, 0, tileMaxVal), tileY);
229 // QRect spanRect = m_mapScene->spanItems(m_scrollDirection, m_zoomLevel, m_sceneCoordinate, m_viewSize);
230 // m_friendItemsHandler->spanHiddenFriendLocationItems(m_scrollDirection, spanRect, m_sceneCoordinate);
231 // m_friendItemsHandler->refactorFriendItems(m_zoomLevel);
236 void MapEngine::gpsPositionUpdate(QPointF position, qreal accuracy)
238 qDebug() << __PRETTY_FUNCTION__;
240 m_gpsLocationItem->updatePosition(convertLatLonToSceneCoordinate(position), accuracy);
242 if (m_autoCenteringEnabled)
243 setViewLocation(position);
246 void MapEngine::init()
248 qDebug() << __PRETTY_FUNCTION__;
250 QPointF startLocation;
251 QSettings settings(DIRECTORY_NAME, FILE_NAME);
253 if (settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString()
254 == ERROR_VALUE_NOT_FOUND_ON_SETTINGS || settings.value(MAP_LAST_ZOOMLEVEL,
255 ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toString() == ERROR_VALUE_NOT_FOUND_ON_SETTINGS) {
257 startLocation = QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE);
258 m_zoomLevel = qBound(MIN_VIEW_ZOOM_LEVEL, DEFAULT_START_ZOOM_LEVEL, MAX_MAP_ZOOM_LEVEL);
260 m_zoomLevel = settings.value(MAP_LAST_ZOOMLEVEL, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toInt();
261 startLocation = settings.value(MAP_LAST_POSITION,
262 ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
265 emit zoomLevelChanged(m_zoomLevel);
266 setViewLocation(QPointF(startLocation.x(), startLocation.y()));
269 bool MapEngine::isAutoCenteringEnabled()
271 return m_autoCenteringEnabled;
274 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
276 qDebug() << __PRETTY_FUNCTION__;
278 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
279 QPoint temp = m_centerTile;
280 m_centerTile = centerTile;
282 return (centerTile != temp);
285 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
290 qDebug() << __PRETTY_FUNCTION__; // << "x:" << x << "y:" << y;
293 QPoint tileNumber(x, y);
294 m_mapScene->addTile(zoomLevel, tileNumber, image, m_zoomLevel);
296 int tilesGridWidthHalf = (m_viewTilesGrid.width() + 1) / 2;
298 // expand to east side? (don't need to expand over padding)
299 if (tileNumber.x() < (tilesGridWidthHalf - GRID_PADDING)) {
300 QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
301 m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
302 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
305 // expand to west side? (don't need to expand over padding)
306 if (tileNumber.x() > (tileMaxValue(zoomLevel) - tilesGridWidthHalf + GRID_PADDING)) {
307 QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
308 m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
309 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
312 // // expanding is only done if received tile zoom level is same as current zoom level
313 // if (zoomLevel == m_zoomLevel) {
314 // // expand to east side?
315 // if (m_viewTilesGrid.right() > tileMaxValue(zoomLevel)) {
316 // int crossing = m_viewTilesGrid.right() - tileMaxValue(zoomLevel);
317 // if (tileNumber.x() < crossing) {
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 east, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
324 // // expand to west side?
325 // if (m_viewTilesGrid.left() < 0) {
326 // int crossing = -m_viewTilesGrid.left();
327 // if (tileNumber.x() > (tileMaxValue(zoomLevel) - crossing)) {
328 // QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
329 // m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
330 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x() << "y:" << adjustedTileNumber.y();
336 // int crossing = m_tilesGridSize.width() - tileMaxValue(zoomLevel) - 1;
337 //// qWarning() << __PRETTY_FUNCTION__ << "crossing:" << crossing;
338 // if (crossing > 0) {
339 // qWarning() << __PRETTY_FUNCTION__ << "grid was bigger than amount of tiles at this tile level";
341 // if (x < crossing) {
342 // // expand to east side
343 // QPoint adjustedTileNumber(tileNumber.x() + tileMaxValue(zoomLevel) + 1, tileNumber.y());
344 // m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
345 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to east, x:" << x << "->" << adjustedTileNumber.x();
348 // if (x > (tileMaxValue(zoomLevel) - crossing)) {
349 // // expand to west side
350 // QPoint adjustedTileNumber(tileNumber.x() - tileMaxValue(zoomLevel) - 1, tileNumber.y());
351 // m_mapScene->addTile(zoomLevel, adjustedTileNumber, image, m_zoomLevel);
352 // qWarning() << __PRETTY_FUNCTION__ << "duplicate tile to west, x:" << x << "->" << adjustedTileNumber.x();
356 // m_mapScene->spanItems(m_scrollDirection, m_zoomLevel);
359 int MapEngine::normalize(int value, int min, int max)
361 qDebug() << __PRETTY_FUNCTION__; // << "value:" << value << "min:" << min << "max:" << max;
362 Q_ASSERT_X(max >= min, "parameters", "max can't be smaller than min");
365 value += max - min + 1;
368 value -= max - min + 1;
373 void MapEngine::receiveOwnLocation(User *user)
375 qDebug() << __PRETTY_FUNCTION__;
378 QPoint newPosition = convertLatLonToSceneCoordinate(user->coordinates());
379 if (m_ownLocation->pos().toPoint() != newPosition) {
380 m_ownLocation->setPos(newPosition);
383 if (!m_ownLocation->isVisible())
384 m_ownLocation->show();
387 m_ownLocation->hide();
391 QGraphicsScene* MapEngine::scene()
393 qDebug() << __PRETTY_FUNCTION__;
398 void MapEngine::setAutoCentering(bool enabled)
400 m_autoCenteringEnabled = enabled;
403 void MapEngine::setGPSEnabled(bool enabled)
405 m_gpsLocationItem->setEnabled(enabled);
408 void MapEngine::setLocation(QPoint sceneCoordinate)
410 qDebug() << __PRETTY_FUNCTION__;
412 // jump to opposite side of the world if world limit is exceeded
413 if (sceneCoordinate.x() < 0)
414 sceneCoordinate.setX(sceneCoordinate.x() + WORLD_PIXELS_X);
415 else if (sceneCoordinate.x() > WORLD_PIXELS_X - 1)
416 sceneCoordinate.setX(sceneCoordinate.x() - WORLD_PIXELS_X);
418 if (sceneCoordinate.x() > m_sceneCoordinate.x())
419 m_scrollDirection = SCROLL_EAST;
421 m_scrollDirection = SCROLL_WEST;
423 if (disableAutoCentering(sceneCoordinate))
424 emit mapScrolledManually();
426 m_sceneCoordinate = sceneCoordinate;
427 emit locationChanged(m_sceneCoordinate);
429 if (isCenterTileChanged(sceneCoordinate)) {
430 getTiles(sceneCoordinate);
431 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
434 m_mapScene->spanItems(m_scrollDirection, m_zoomLevel, m_sceneCoordinate, m_viewSize);
437 void MapEngine::setZoomLevel(int newZoomLevel)
439 qDebug() << __PRETTY_FUNCTION__;
441 m_zoomLevel = newZoomLevel;
445 void MapEngine::setTilesGridSize(const QSize &viewSize)
447 qDebug() << __PRETTY_FUNCTION__;
449 // there must be scrolling reserve of at least half tile added to tile amount
450 // calculated from view size
451 const qreal SCROLLING_RESERVE = 0.5;
453 // converting scene tile to tile number does cause grid centering inaccuracy of one tile
454 const int CENTER_TILE_INACCURACY = 1;
456 int gridWidth = ceil(qreal(viewSize.width()) / TILE_SIZE_X + SCROLLING_RESERVE)
457 + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
458 int gridHeight = ceil(qreal(viewSize.height()) / TILE_SIZE_Y + SCROLLING_RESERVE)
459 + CENTER_TILE_INACCURACY + (GRID_PADDING * 2);
461 m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
463 m_tilesGridSize.setHeight(gridHeight);
464 m_tilesGridSize.setWidth(gridWidth);
466 // qWarning() << __PRETTY_FUNCTION__ << "tiles grid:" << m_tilesGridSize.width()
467 // << "*" << m_tilesGridSize.height();
470 void MapEngine::setViewLocation(QPointF latLonCoordinate)
472 qDebug() << __PRETTY_FUNCTION__;
474 QPoint sceneCoordinate = convertLatLonToSceneCoordinate(latLonCoordinate);
476 m_lastManualPosition = sceneCoordinate;
478 setLocation(sceneCoordinate);
481 int MapEngine::tileMaxValue(int zoomLevel)
483 qDebug() << __PRETTY_FUNCTION__;
485 return (1 << zoomLevel) - 1;
488 QString MapEngine::tilePath(int zoomLevel, int x, int y)
490 qDebug() << __PRETTY_FUNCTION__;
492 QString tilePathString(QString::number(zoomLevel) + "/");
493 tilePathString.append(QString::number(x) + "/");
494 tilePathString.append(QString::number(y));
496 return tilePathString;
499 void MapEngine::updateViewTilesSceneRect()
501 qDebug() << __PRETTY_FUNCTION__;
503 const QPoint ONE_TILE = QPoint(1, 1);
504 const QPoint ONE_PIXEL = QPoint(1, 1);
506 QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
507 // one tile - one pixel is added because returned coordinates are pointing to upper left corner
509 QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
510 m_viewTilesGrid.bottomRight()
511 + ONE_TILE) - ONE_PIXEL;
513 m_mapScene->tilesSceneRectUpdated(QRect(topLeft, bottomRight));
516 void MapEngine::viewResized(const QSize &size)
518 qDebug() << __PRETTY_FUNCTION__;
521 setTilesGridSize(m_viewSize);
523 emit locationChanged(m_sceneCoordinate);
524 getTiles(m_sceneCoordinate);
525 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
526 m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
529 void MapEngine::viewZoomFinished()
531 qDebug() << __PRETTY_FUNCTION__;
535 m_mapScene->removeOutOfViewTiles(m_viewTilesGrid, m_zoomLevel);
538 if (m_zoomLevel == MAX_MAP_ZOOM_LEVEL)
539 emit maxZoomLevelReached();
540 else if (m_zoomLevel == MIN_VIEW_ZOOM_LEVEL)
541 emit minZoomLevelReached();
544 void MapEngine::zoomed()
546 emit zoomLevelChanged(m_zoomLevel);
547 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
548 getTiles(m_sceneCoordinate);
549 m_mapScene->setSceneVerticalOverlap(m_viewSize.height(), m_zoomLevel);
552 void MapEngine::zoomIn()
554 qDebug() << __PRETTY_FUNCTION__;
556 if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
563 void MapEngine::zoomOut()
565 qDebug() << __PRETTY_FUNCTION__;
567 if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {