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
9 Situare is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 version 2 as published by the Free Software Foundation.
13 Situare is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Situare; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24 #include <QtAlgorithms>
27 #include <QStringList>
30 #include <QHashIterator>
33 #include "mapengine.h"
36 MapEngine::MapEngine(QObject *parent)
38 , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
39 , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
41 , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
43 qDebug() << __PRETTY_FUNCTION__;
45 m_mapScene = new MapScene(this);
47 m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
48 connect(this, SIGNAL(fetchImage(int,int,int)),
49 m_mapFetcher, SLOT(enqueueFetchMapImage(int,int,int)));
50 connect(m_mapFetcher, SIGNAL(mapImageReceived(int,int,int,QPixmap)),
51 this, SLOT(mapImageReceived(int,int,int,QPixmap)));
53 m_mapZoomPanel = new MapZoomPanel(NULL, MAP_ZOOM_PANEL_POSITION_X, MAP_ZOOM_PANEL_POSITION_Y);
54 m_mapScene->addItem(m_mapZoomPanel);
55 connect(m_mapZoomPanel, SIGNAL(zoomInPressed()), this, SLOT(zoomIn()));
56 connect(m_mapZoomPanel, SIGNAL(zoomOutPressed()), this, SLOT(zoomOut()));
58 m_ownLocation = new OwnLocationItem(QPointF());
61 MapEngine::~MapEngine()
63 qDeleteAll(m_friendsLocations.begin(),m_friendsLocations.end());
64 m_friendsLocations.clear();
67 void MapEngine::init()
69 qDebug() << __PRETTY_FUNCTION__;
71 emit zoomLevelChanged(m_zoomLevel);
72 setViewLocation(QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE));
75 void MapEngine::setViewLocation(QPointF latLonCoordinate)
77 qDebug() << __PRETTY_FUNCTION__;
79 setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
82 void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
84 qDebug() << __PRETTY_FUNCTION__;
86 QString hashKey = tilePath(zoomLevel, x, y);
87 if (!m_mapScene->isTileInScene(hashKey)) {
89 MapTile *mapTile = new MapTile();
90 mapTile->setZoomLevel(zoomLevel, m_zoomLevel);
91 mapTile->setTileNumber(QPoint(x, y));
92 mapTile->setPixmap(image);
94 m_mapScene->addTile(mapTile, hashKey);
96 m_mapScene->enqueueRemoveStackedTiles(mapTile);
100 QGraphicsScene* MapEngine::scene()
102 qDebug() << __PRETTY_FUNCTION__;
107 int MapEngine::tileMaxValue(int zoomLevel)
109 qDebug() << __PRETTY_FUNCTION__;
111 return (1 << zoomLevel) - 1;
114 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
116 qDebug() << __PRETTY_FUNCTION__;
118 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
119 int gridWidth = (m_viewSize.width()/TILE_SIZE_X + 1) + (GRID_PADDING*2);
120 int gridHeight = (m_viewSize.height()/TILE_SIZE_Y + 1) + (GRID_PADDING*2);
121 int topLeftX = tileCoordinate.x() - (gridWidth/2);
122 int topLeftY = tileCoordinate.y() - (gridHeight/2);
124 m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
126 return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
129 void MapEngine::alignImmovableItems(QPoint viewTopLeft)
131 qDebug() << __PRETTY_FUNCTION__ << "viewTopLeft:" << viewTopLeft;
133 m_mapZoomPanel->setPos(viewTopLeft);
136 void MapEngine::setLocation(QPoint sceneCoordinate)
138 qDebug() << __PRETTY_FUNCTION__;
140 m_sceneCoordinate = sceneCoordinate;
141 emit locationChanged(m_sceneCoordinate);
143 if (isCenterTileChanged(sceneCoordinate)) {
144 getTiles(sceneCoordinate);
145 m_mapScene->removeOutOfViewTiles();
149 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
151 qDebug() << __PRETTY_FUNCTION__;
153 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
154 QPoint temp = m_centerTile;
155 m_centerTile = centerTile;
157 return (centerTile != temp);
160 void MapEngine::getTiles(QPoint sceneCoordinate)
162 qDebug() << __PRETTY_FUNCTION__;
164 m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
165 updateViewTilesSceneRect();
167 int topLeftX = m_viewTilesGrid.topLeft().x();
168 int topLeftY = m_viewTilesGrid.topLeft().y();
169 int bottomRightX = m_viewTilesGrid.bottomRight().x();
170 int bottomRightY = m_viewTilesGrid.bottomRight().y();
172 int tileMaxVal = tileMaxValue(m_zoomLevel);
174 for (int x = topLeftX; x <= bottomRightX; ++x) {
175 for (int y = topLeftY; y <= bottomRightY; ++y) {
182 else if (tileX > tileMaxVal)
187 else if (tileY > tileMaxVal)
190 if (!m_mapScene->isTileInScene(tilePath(m_zoomLevel, tileX, tileY)))
191 emit fetchImage(m_zoomLevel, tileX, tileY);
196 void MapEngine::updateViewTilesSceneRect()
198 qDebug() << __PRETTY_FUNCTION__;
200 const QPoint ONE_TILE = QPoint(1, 1);
201 const QPoint ONE_PIXEL = QPoint(1, 1);
203 QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
204 // one tile - one pixel is added because returned coordinates are pointing to upper left corner
206 QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
207 m_viewTilesGrid.bottomRight()
208 + ONE_TILE) - ONE_PIXEL;
210 m_mapScene->viewRectUpdated(QRect(topLeft, bottomRight));
213 void MapEngine::viewResized(const QSize &size)
215 qDebug() << __PRETTY_FUNCTION__;
218 getTiles(m_sceneCoordinate);
219 m_mapScene->removeOutOfViewTiles();
222 void MapEngine::viewZoomFinished()
224 qDebug() << __PRETTY_FUNCTION__;
228 m_mapScene->removeOutOfViewTiles();
232 void MapEngine::zoomIn()
234 qDebug() << __PRETTY_FUNCTION__;
236 if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
239 emit zoomLevelChanged(m_zoomLevel);
241 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
243 getTiles(m_sceneCoordinate);
247 void MapEngine::zoomOut()
249 qDebug() << __PRETTY_FUNCTION__;
251 if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
253 emit zoomLevelChanged(m_zoomLevel);
255 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
257 getTiles(m_sceneCoordinate);
261 QString MapEngine::tilePath(int zoomLevel, int x, int y)
263 qDebug() << __PRETTY_FUNCTION__;
265 QString tilePathString(QString::number(zoomLevel) + "/");
266 tilePathString.append(QString::number(x) + "/");
267 tilePathString.append(QString::number(y));
269 return tilePathString;
272 QPoint MapEngine::convertSceneCoordinateToTileNumber(int zoomLevel, QPoint sceneCoordinate)
274 qDebug() << __PRETTY_FUNCTION__;
276 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
277 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X*pow));
278 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y*pow));
283 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
285 qDebug() << __PRETTY_FUNCTION__;
287 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
288 int x = tileNumber.x() * TILE_SIZE_X * pow;
289 int y = tileNumber.y() * TILE_SIZE_Y * pow;
294 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
296 qDebug() << __PRETTY_FUNCTION__;
298 qreal longitude = latLonCoordinate.x();
299 qreal latitude = latLonCoordinate.y();
301 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
302 return QPoint(UNDEFINED, UNDEFINED);
303 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
304 return QPoint(UNDEFINED, UNDEFINED);
306 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
308 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
309 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
310 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
312 return QPointF(x*z*TILE_SIZE_X, y*z*TILE_SIZE_Y).toPoint();
315 void MapEngine::receiveOwnLocation(User *user)
317 qDebug() << __PRETTY_FUNCTION__;
318 m_ownLocation->setPosition(user->coordinates());
319 m_mapScene->addItem(m_ownLocation);
322 void MapEngine::receiveFriendLocations(QList<User *> &friendsList)
324 qDebug() << __PRETTY_FUNCTION__;
325 qDeleteAll(m_friendsLocations.begin(),m_friendsLocations.end());
326 m_friendsLocations.clear();
328 for(int i=0; i<friendsList.count(); i++) {
329 FriendLocationItem *friendLocation
330 = new FriendLocationItem(friendsList.at(i)->profileImage(),
331 friendsList.at(i)->coordinates(),0);
333 friendLocation->setUserId(friendsList.at(i)->userId());
334 m_friendsLocations.append(friendLocation);
335 m_mapScene->addItem(friendLocation);