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,
26 #include <QStringList>
29 #include <QHashIterator>
32 #include "mapengine.h"
35 MapEngine::MapEngine(QObject *parent)
37 , m_centerTile(QPoint(UNDEFINED, UNDEFINED))
38 , m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
40 , m_zoomLevel(DEFAULT_ZOOM_LEVEL)
42 m_mapScene = new MapScene(this);
44 m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
45 connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(enqueueFetchMapImage(QUrl)));
46 connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
47 SLOT(mapImageReceived(QUrl, QPixmap)));
49 m_mapZoomPanel = new MapZoomPanel(NULL, MAP_ZOOM_PANEL_POSITION_X, MAP_ZOOM_PANEL_POSITION_Y);
50 m_mapScene->addItem(m_mapZoomPanel);
51 connect(m_mapZoomPanel, SIGNAL(zoomInPressed()), this, SLOT(zoomIn()));
52 connect(m_mapZoomPanel, SIGNAL(zoomOutPressed()), this, SLOT(zoomOut()));
55 void MapEngine::init()
57 emit zoomLevelChanged(m_zoomLevel);
58 setViewLocation(QPointF(DEFAULT_LONGITUDE, DEFAULT_LATITUDE));
61 void MapEngine::setViewLocation(QPointF latLonCoordinate)
63 qDebug() << __PRETTY_FUNCTION__;
64 setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
67 QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
69 QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
70 .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
75 void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
77 QString path = url.path();
78 QStringList pathParts = path.split("/", QString::SkipEmptyParts);
80 int size = pathParts.size();
83 zoom = (pathParts.at(size-3)).toInt();
84 x = (pathParts.at(size-2)).toInt();
85 QString yString = pathParts.at(size-1);
91 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
93 //qDebug() << __PRETTY_FUNCTION__;
99 parseURL(url, zoom, x, y);
100 QString hashKey = tilePath(zoom, x, y);
101 if (!m_mapScene->isTileInScene(hashKey)) {
103 MapTile *mapTile = new MapTile();
104 /// @todo SET SCENE LEVEL AUTOMATICALLY WHEN CHANGING ZOOM LEVEL
105 mapTile->setZoomLevel(zoom);
106 mapTile->setSceneLevel(m_zoomLevel);
107 mapTile->setTileNumber(QPoint(x, y));
108 mapTile->setPixmap(pixmap);
110 m_mapScene->addTile(mapTile, hashKey);
112 m_mapScene->debugItemsCount();
114 m_mapScene->enqueueRemoveStackedTiles(mapTile);
115 //m_mapScene->removeStackedTiles(mapTile, viewRect());
119 QGraphicsScene* MapEngine::scene()
121 return dynamic_cast<QGraphicsScene *>(m_mapScene);
124 int MapEngine::tileMaxValue(int zoomLevel)
126 return (1 << zoomLevel) - 1;
129 QRect MapEngine::calculateTileGrid(QPoint sceneCoordinate)
131 QPoint tileCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
132 int gridWidth = (m_viewSize.width()/TILE_SIZE_X + 1) + (GRID_PADDING*2);
133 int gridHeight = (m_viewSize.height()/TILE_SIZE_Y + 1) + (GRID_PADDING*2);
134 int topLeftX = tileCoordinate.x() - (gridWidth/2);
135 int topLeftY = tileCoordinate.y() - (gridHeight/2);
137 return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
140 void MapEngine::alignImmovableItems(QPoint viewTopLeft)
142 m_mapZoomPanel->setPos(viewTopLeft);
144 // qDebug() << __PRETTY_FUNCTION__ << "viewTopLeft:" << viewTopLeft;
147 void MapEngine::setLocation(QPoint sceneCoordinate)
149 // qDebug() << __PRETTY_FUNCTION__;
151 m_sceneCoordinate = sceneCoordinate;
152 emit locationChanged(m_sceneCoordinate);
154 if (isCenterTileChanged(sceneCoordinate)) {
155 getTiles(sceneCoordinate);
156 m_mapScene->removeOutOfViewTiles();
160 bool MapEngine::isCenterTileChanged(QPoint sceneCoordinate)
162 QPoint centerTile = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
163 QPoint temp = m_centerTile;
164 m_centerTile = centerTile;
166 return (centerTile != temp);
169 void MapEngine::getTiles(QPoint sceneCoordinate)
171 // qDebug() << __PRETTY_FUNCTION__;
173 m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
174 updateViewTilesSceneRect();
176 int topLeftX = m_viewTilesGrid.topLeft().x();
177 int topLeftY = m_viewTilesGrid.topLeft().y();
178 int bottomRightX = m_viewTilesGrid.bottomRight().x();
179 int bottomRightY = m_viewTilesGrid.bottomRight().y();
181 int tileMaxVal = tileMaxValue(m_zoomLevel);
183 for (int x = topLeftX; x <= bottomRightX; ++x) {
184 for (int y = topLeftY; y <= bottomRightY; ++y) {
191 else if (tileX > tileMaxVal)
196 else if (tileY > tileMaxVal)
199 QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
201 if (!m_mapScene->isTileInScene(tilePath(m_zoomLevel, tileX, tileY)))
202 emit fetchImage(url);
207 void MapEngine::updateViewTilesSceneRect()
209 QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
210 QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
211 m_viewTilesGrid.bottomRight()
212 + QPoint(1, 1)) - QPoint(1, 1);
214 m_mapScene->viewRectUpdated(QRect(topLeft, bottomRight));
217 void MapEngine::viewResized(const QSize &size)
220 getTiles(m_sceneCoordinate);
221 m_mapScene->removeOutOfViewTiles();
224 void MapEngine::viewZoomFinished()
226 // qDebug() << __PRETTY_FUNCTION__;
228 if (m_zoomedIn == YES) {
230 m_mapScene->removeOutOfViewTiles();
234 void MapEngine::zoomIn()
236 // qDebug() << __PRETTY_FUNCTION__;
238 if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
241 emit zoomLevelChanged(m_zoomLevel);
243 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
245 getTiles(m_sceneCoordinate);
249 void MapEngine::zoomOut()
251 // qDebug() << __PRETTY_FUNCTION__;
253 if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
255 emit zoomLevelChanged(m_zoomLevel);
257 m_mapScene->setTilesDrawingLevels(m_zoomLevel);
259 getTiles(m_sceneCoordinate);
263 QString MapEngine::tilePath(int zoomLevel, int x, int y)
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 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
275 int x = static_cast<int>(sceneCoordinate.x() / (TILE_SIZE_X*pow));
276 int y = static_cast<int>(sceneCoordinate.y() / (TILE_SIZE_Y*pow));
281 QPoint MapEngine::convertTileNumberToSceneCoordinate(int zoomLevel, QPoint tileNumber)
283 int pow = 1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel);
284 int x = tileNumber.x() * TILE_SIZE_X * pow;
285 int y = tileNumber.y() * TILE_SIZE_Y * pow;
290 QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
292 // qDebug() << __PRETTY_FUNCTION__;
294 qreal longitude = latLonCoordinate.x();
295 qreal latitude = latLonCoordinate.y();
297 if ((longitude > MAX_LONGITUDE) || (longitude < MIN_LONGITUDE))
298 return QPoint(UNDEFINED, UNDEFINED);
299 if ((latitude > MAX_LATITUDE) || (latitude < MIN_LATITUDE))
300 return QPoint(UNDEFINED, UNDEFINED);
302 qreal z = static_cast<qreal>(1 << MAX_MAP_ZOOM_LEVEL);
304 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
305 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
306 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
308 return QPointF(x*z*TILE_SIZE_X, y*z*TILE_SIZE_Y).toPoint();