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,
25 #include <QStringList>
28 #include <QHashIterator>
32 #include "mapengine.h"
38 MapEngine::MapEngine(QObject *parent)
41 , m_viewSize(QSize(973, 614))
42 , m_centerTile(QPoint(-1, -1))
44 m_mapScene = new MapScene(this);
46 m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
47 connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(fetchMapImage(QUrl)));
48 connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
49 SLOT(mapImageReceived(QUrl, QPixmap)));
52 void MapEngine::init()
54 emit zoomLevelChanged(m_zoomLevel);
55 setViewLocation(QPointF(25.5000, 65.0000));
57 // // Fetch some map tiles for demo purposes
58 // for (int x=9351; x<=9354; x++) {
59 // for (int y=4261; y<=4264; y++) {
60 // QUrl url = buildURL(m_zoomLevel, QPoint(x, y));
61 // m_mapFetcher->fetchMapImage(url);
66 void MapEngine::setViewLocation(QPointF latLonCoordinate)
68 qDebug() << __PRETTY_FUNCTION__;
69 setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
72 QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
74 QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
75 .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
80 void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
82 QString path = url.path();
83 QStringList pathParts = path.split("/", QString::SkipEmptyParts);
85 int size = pathParts.size();
90 zoom = (pathParts.at(size-3)).toInt();
91 x = (pathParts.at(size-2)).toInt();
92 QString yString = pathParts.at(size-1);
97 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
99 if (mapTilesInScene.contains(url.toString())) {
104 parseURL(url, zoom, x, y);
106 MapTile *mapTile = new MapTile();
107 mapTile->setZoomLevel(zoom);
108 mapTile->setTileNumber(QPoint(x, y));
109 mapTile->setPixmap(pixmap);
111 mapTilesInScene.insert(url.toString(), mapTile);
112 m_mapScene->addMapTile(mapTile);
116 QGraphicsScene* MapEngine::scene()
118 return dynamic_cast<QGraphicsScene *>(m_mapScene);
121 int MapEngine::tileMaxValue(int zoomLevel)
123 return (1 << zoomLevel) - 1;
126 QRect MapEngine::calculateGrid(QPointF sceneCoordinate)
128 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
130 int gridWidth = (m_viewSize.width()/TILE_SIZE_X + 1) + (GRID_PADDING*2);
131 int gridHeight = (m_viewSize.height()/TILE_SIZE_Y + 1) + (GRID_PADDING*2);
133 int topLeftX = tileCoordinate.x() - (gridWidth/2);
134 int topLeftY = tileCoordinate.y() - (gridHeight/2);
136 return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
139 void MapEngine::setLocation(QPointF sceneCoordinate)
141 m_sceneCoordinate = sceneCoordinate;
142 emit locationChanged(m_sceneCoordinate);
144 if (centerTileChanged(sceneCoordinate)) {
145 calculateTileGrid(sceneCoordinate);
146 removeTilesOutOfBounds();
150 bool MapEngine::centerTileChanged(QPointF sceneCoordinate)
152 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
153 QPoint temp = m_centerTile;
154 m_centerTile = centerTile;
156 return (centerTile != temp);
159 void MapEngine::calculateTileGrid(QPointF sceneCoordinate)
161 //qDebug() << __PRETTY_FUNCTION__;
163 viewGrid = calculateGrid(sceneCoordinate);
165 int topLeftX = viewGrid.topLeft().x();
166 int topLeftY = viewGrid.topLeft().y();
167 int bottomRightX = viewGrid.bottomRight().x();
168 int bottomRightY = viewGrid.bottomRight().y();
170 int tileMaxVal = tileMaxValue(m_zoomLevel);
172 for (int x = topLeftX; x <= bottomRightX; ++x) {
173 for (int y = topLeftY; y <= bottomRightY; ++y) {
180 else if (tileX > tileMaxVal)
185 else if (tileY > tileMaxVal)
188 QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
190 if (!mapTilesInScene.contains(url.toString())) {
191 mapTilesInScene.insert(url.toString(), NULL);
192 emit fetchImage(url);
198 void MapEngine::removeTilesOutOfBounds()
200 //qDebug() << __PRETTY_FUNCTION__;
202 int zoomFactor = 1 << (MAX_MAP_ZOOM_LEVEL - m_zoomLevel);
204 QPointF topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, viewGrid.topLeft());
205 QPointF bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel, viewGrid.bottomRight() + QPoint(1, 1));
206 qreal width = bottomRight.x() - topLeft.x();
207 qreal height = bottomRight.y() - topLeft.y();
209 QList<QGraphicsItem *> viewTiles = m_mapScene->items(topLeft.x(), topLeft.y(), width, height, Qt::IntersectsItemShape);
210 QList<QGraphicsItem *> allTiles = m_mapScene->items();
212 foreach (QGraphicsItem *tile, viewTiles)
213 allTiles.removeOne(tile);
215 QHashIterator<QString, MapTile *> i(mapTilesInScene);
217 while (i.hasNext()) {
219 if (allTiles.contains(i.value())) {
220 MapTile *tile = i.value();
222 mapTilesInScene.remove(i.key());
223 m_mapScene->removeItem(tile);
231 void MapEngine::viewResized(const QSize &size)
234 setLocation(m_sceneCoordinate);
237 void MapEngine::zoomIn()
239 if (m_zoomLevel >= MAX_MAP_ZOOM_LEVEL)
243 emit zoomLevelChanged(m_zoomLevel);
247 /// @todo START FETCHING TILES
248 calculateTileGrid(m_sceneCoordinate);
251 void MapEngine::zoomOut()
253 if (m_zoomLevel <= MIN_MAP_ZOOM_LEVEL)
257 emit zoomLevelChanged(m_zoomLevel);
261 /// @todo START FETCHING TILES
262 calculateTileGrid(m_sceneCoordinate);
265 void MapEngine::setZoomLevel(int zoomLevel)
267 m_zoomLevel = zoomLevel;
270 int MapEngine::getZoomLevel()
275 void MapEngine::setZValues()
277 //qDebug() << __PRETTY_FUNCTION__ << "m_zoomLevel:" << m_zoomLevel;
279 QList<QGraphicsItem *> items = m_mapScene->items();
281 for (int i = 0; i < items.size(); ++i) {
282 MapTile *item = dynamic_cast<MapTile *>(items.at(i));
284 item->setSceneLevel(m_zoomLevel);