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
8 Situare is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 Situare is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Situare; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <QStringList>
30 #include <QHashIterator>
33 #include "mapengine.h"
36 MapEngine::MapEngine(QObject *parent)
38 , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
39 , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
40 , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
42 m_mapScene = new MapScene(this);
44 m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
45 connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(fetchMapImage(QUrl)));
46 connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
47 SLOT(mapImageReceived(QUrl, QPixmap)));
50 void MapEngine::init()
52 emit zoomLevelChanged(m_zoomLevel);
53 setViewLocation(QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE));
56 void MapEngine::setViewLocation(QPointF latLonCoordinate)
58 qDebug() << __PRETTY_FUNCTION__;
59 setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
62 QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
64 QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
65 .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
70 void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
72 QString path = url.path();
73 QStringList pathParts = path.split("/", QString::SkipEmptyParts);
75 int size = pathParts.size();
80 zoom = (pathParts.at(size-3)).toInt();
81 x = (pathParts.at(size-2)).toInt();
82 QString yString = pathParts.at(size-1);
87 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
89 //qDebug() << __PRETTY_FUNCTION__;
91 if (!mapTilesInScene.contains(url.toString())) {
96 parseURL(url, zoom, x, y);
98 MapTile *mapTile = new MapTile();
99 mapTile->setZoomLevel(zoom);
100 mapTile->setTileNumber(QPoint(x, y));
101 mapTile->setPixmap(pixmap);
103 mapTilesInScene.insert(tilePath(zoom, x, y), mapTile);
104 m_mapScene->addMapTile(mapTile);
106 removeStackedTiles(mapTile);
110 QGraphicsScene* MapEngine::scene()
112 return dynamic_cast<QGraphicsScene *>(m_mapScene);
115 int MapEngine::tileMaxValue(int zoomLevel)
117 return (1 << zoomLevel) - 1;
120 QRect MapEngine::calculateGrid(QPoint sceneCoordinate)
122 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
123 int gridWidth = (m_viewSize.width()/TILE_SIZE_X + 1) + (GRID_PADDING*2);
124 int gridHeight = (m_viewSize.height()/TILE_SIZE_Y + 1) + (GRID_PADDING*2);
125 int topLeftX = tileCoordinate.x() - (gridWidth/2);
126 int topLeftY = tileCoordinate.y() - (gridHeight/2);
128 return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
131 void MapEngine::setLocation(QPoint sceneCoordinate)
133 //qDebug() << __PRETTY_FUNCTION__;
135 m_sceneCoordinate = sceneCoordinate;
136 emit locationChanged(m_sceneCoordinate);
138 if (centerTileChanged(sceneCoordinate)) {
139 calculateNewTiles(sceneCoordinate);
144 bool MapEngine::centerTileChanged(QPoint sceneCoordinate)
146 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
147 QPoint temp = m_centerTile;
148 m_centerTile = centerTile;
150 return (centerTile != temp);
153 void MapEngine::calculateNewTiles(QPoint sceneCoordinate)
155 //qDebug() << __PRETTY_FUNCTION__;
157 viewGrid = calculateGrid(sceneCoordinate);
159 int topLeftX = viewGrid.topLeft().x();
160 int topLeftY = viewGrid.topLeft().y();
161 int bottomRightX = viewGrid.bottomRight().x();
162 int bottomRightY = viewGrid.bottomRight().y();
164 int tileMaxVal = tileMaxValue(m_zoomLevel);
166 for (int x = topLeftX; x <= bottomRightX; ++x) {
167 for (int y = topLeftY; y <= bottomRightY; ++y) {
174 else if (tileX > tileMaxVal)
179 else if (tileY > tileMaxVal)
182 QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
184 if (!mapTilesInScene.contains(tilePath(m_zoomLevel, tileX, tileY)))
185 emit fetchImage(url);
190 void MapEngine::removeOldTiles()
192 //qDebug() << __PRETTY_FUNCTION__;
194 QPointF topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, viewGrid.topLeft());
195 QPointF bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel, viewGrid.bottomRight() + QPoint(1, 1));
196 qreal width = bottomRight.x() - topLeft.x();
197 qreal height = bottomRight.y() - topLeft.y();
199 QList<QGraphicsItem *> viewTiles = m_mapScene->items(topLeft.x(), topLeft.y(), width, height, Qt::IntersectsItemShape);
200 QList<QGraphicsItem *> allTiles = m_mapScene->items();
202 foreach (QGraphicsItem *tile, viewTiles)
203 allTiles.removeOne(tile);
205 QHashIterator<QString, MapTile *> i(mapTilesInScene);
207 while (i.hasNext()) {
209 if (allTiles.contains(i.value()) && mapTilesInScene.contains(i.key())) {
210 MapTile *tile = i.value();
212 mapTilesInScene.remove(i.key());
213 m_mapScene->removeItem(tile);
220 void MapEngine::removeStackedTiles(MapTile *newTile)
222 QRectF newTileSceneRect = newTile->mapRectToScene(newTile->boundingRect());
224 QList<QGraphicsItem *> collidingItems = newTile->collidingItems(Qt::IntersectsItemBoundingRect);
226 foreach (QGraphicsItem *collidingItem, collidingItems) {
228 QRectF collidingItemSceneRect = collidingItem->mapRectToScene(collidingItem->boundingRect());
230 if (collidingItem->isObscured(collidingItemSceneRect)) {
235 // qDebug() << "mapTilesInScene: " << mapTilesInScene.count();
236 // qDebug() << "m_mapScene: " << m_mapScene->items().count();
239 void MapEngine::viewResized(const QSize &size)
242 calculateNewTiles(m_sceneCoordinate);
246 void MapEngine::zoomIn()
248 qDebug() << __PRETTY_FUNCTION__;
250 if (m_zoomLevel >= MAX_MAP_ZOOM_LEVEL)
254 emit zoomLevelChanged(m_zoomLevel);
258 calculateNewTiles(m_sceneCoordinate);
262 void MapEngine::zoomOut()
264 qDebug() << __PRETTY_FUNCTION__;
266 if (m_zoomLevel <= MIN_MAP_ZOOM_LEVEL)
270 emit zoomLevelChanged(m_zoomLevel);
274 calculateNewTiles(m_sceneCoordinate);
277 void MapEngine::setZValues()
279 //qDebug() << __PRETTY_FUNCTION__ << "m_zoomLevel:" << m_zoomLevel;
281 QList<QGraphicsItem *> items = m_mapScene->items();
283 for (int i = 0; i < items.size(); ++i) {
284 MapTile *item = dynamic_cast<MapTile *>(items.at(i));
286 item->setSceneLevel(m_zoomLevel);
291 QString MapEngine::tilePath(int zoomLevel, int x, int y)
293 QString tilePathString(QString::number(zoomLevel) + "/");
294 tilePathString.append(QString::number(x) + "/");
295 tilePathString.append(QString::number(y));
297 return tilePathString;