: QObject(parent)
, m_centerTile(QPoint(UNDEFINED, UNDEFINED))
, m_viewSize(QSize(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT))
+ , m_zoomedIn(false)
, m_zoomLevel(DEFAULT_ZOOM_LEVEL)
{
m_mapScene = new MapScene(this);
m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
- connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(fetchMapImage(QUrl)));
- connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
- SLOT(mapImageReceived(QUrl, QPixmap)));
+ connect(this, SIGNAL(fetchImage(int,int,int)), m_mapFetcher, SLOT(enqueueFetchMapImage(int,int,int)));
+ connect(m_mapFetcher, SIGNAL(mapImageReceived(int,int,int,QPixmap)), this,
+ SLOT(mapImageReceived(int,int,int,QPixmap)));
m_mapZoomPanel = new MapZoomPanel(NULL, MAP_ZOOM_PANEL_POSITION_X, MAP_ZOOM_PANEL_POSITION_Y);
m_mapScene->addItem(m_mapZoomPanel);
connect(m_mapZoomPanel, SIGNAL(zoomInPressed()), this, SLOT(zoomIn()));
connect(m_mapZoomPanel, SIGNAL(zoomOutPressed()), this, SLOT(zoomOut()));
+
+ m_ownLocation = new OwnLocationItem();
+ m_mapScene->addItem(m_ownLocation);
}
void MapEngine::init()
setLocation(convertLatLonToSceneCoordinate(latLonCoordinate));
}
-QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
-{
- QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
- .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
-
- return QUrl(url);
-}
-
-void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
-{
- QString path = url.path();
- QStringList pathParts = path.split("/", QString::SkipEmptyParts);
-
- int size = pathParts.size();
-
- if (size >= 3) {
- zoom = (pathParts.at(size-3)).toInt();
- x = (pathParts.at(size-2)).toInt();
- QString yString = pathParts.at(size-1);
- yString.chop(4);
- y = yString.toInt();
- }
-}
-
-void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
+void MapEngine::mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image)
{
- //qDebug() << __PRETTY_FUNCTION__;
-
- int zoom = UNDEFINED;
- int x = UNDEFINED;
- int y = UNDEFINED;
+ qDebug() << __PRETTY_FUNCTION__;
- parseURL(url, zoom, x, y);
- QString hashKey = tilePath(zoom, x, y);
- if (!m_mapTilesInScene.contains(hashKey)) {
+ QString hashKey = tilePath(zoomLevel, x, y);
+ if (!m_mapScene->isTileInScene(hashKey)) {
MapTile *mapTile = new MapTile();
- mapTile->setZoomLevel(zoom);
+ mapTile->setZoomLevel(zoomLevel, m_zoomLevel);
mapTile->setTileNumber(QPoint(x, y));
- mapTile->setPixmap(pixmap);
+ mapTile->setPixmap(image);
- m_mapTilesInScene.insert(hashKey, mapTile);
- m_mapScene->addMapTile(mapTile);
+ m_mapScene->addTile(mapTile, hashKey);
- removeStackedTiles(mapTile);
+ m_mapScene->enqueueRemoveStackedTiles(mapTile);
}
}
int topLeftX = tileCoordinate.x() - (gridWidth/2);
int topLeftY = tileCoordinate.y() - (gridHeight/2);
+ m_mapFetcher->setDownloadQueueSize(gridWidth * gridHeight);
+
return QRect(topLeftX, topLeftY, gridWidth, gridHeight);
}
{
m_mapZoomPanel->setPos(viewTopLeft);
-// qDebug() << __PRETTY_FUNCTION__ << "viewTopLeft:" << viewTopLeft;
+ qDebug() << __PRETTY_FUNCTION__ << "viewTopLeft:" << viewTopLeft;
}
void MapEngine::setLocation(QPoint sceneCoordinate)
{
-// qDebug() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
m_sceneCoordinate = sceneCoordinate;
emit locationChanged(m_sceneCoordinate);
if (isCenterTileChanged(sceneCoordinate)) {
getTiles(sceneCoordinate);
- removeTilesOutOfView();
+ m_mapScene->removeOutOfViewTiles();
}
}
void MapEngine::getTiles(QPoint sceneCoordinate)
{
-// qDebug() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
- m_viewGrid = calculateTileGrid(sceneCoordinate);
+ m_viewTilesGrid = calculateTileGrid(sceneCoordinate);
+ updateViewTilesSceneRect();
- int topLeftX = m_viewGrid.topLeft().x();
- int topLeftY = m_viewGrid.topLeft().y();
- int bottomRightX = m_viewGrid.bottomRight().x();
- int bottomRightY = m_viewGrid.bottomRight().y();
+ int topLeftX = m_viewTilesGrid.topLeft().x();
+ int topLeftY = m_viewTilesGrid.topLeft().y();
+ int bottomRightX = m_viewTilesGrid.bottomRight().x();
+ int bottomRightY = m_viewTilesGrid.bottomRight().y();
int tileMaxVal = tileMaxValue(m_zoomLevel);
else if (tileY > tileMaxVal)
tileY -= tileMaxVal;
- QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
-
- if (!m_mapTilesInScene.contains(tilePath(m_zoomLevel, tileX, tileY)))
- emit fetchImage(url);
+ if (!m_mapScene->isTileInScene(tilePath(m_zoomLevel, tileX, tileY)))
+ emit fetchImage(m_zoomLevel, tileX, tileY);
}
}
}
-void MapEngine::removeTile(MapTile *tile)
+void MapEngine::updateViewTilesSceneRect()
{
-// qDebug() << __PRETTY_FUNCTION__;
+ QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewTilesGrid.topLeft());
+ QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel,
+ m_viewTilesGrid.bottomRight()
+ + QPoint(1, 1)) - QPoint(1, 1);
- if (tile) {
- m_mapTilesInScene.remove(tilePath(tile->zoomLevel(), tile->tileNumber().x(),
- tile->tileNumber().y()));
- m_mapScene->removeItem(tile);
- delete tile;
- }
-}
-
-void MapEngine::removeTilesOutOfView()
-{
-// qDebug() << __PRETTY_FUNCTION__;
-
- QList<QGraphicsItem *> viewTiles = m_mapScene->items(viewRect(), Qt::ContainsItemBoundingRect);
- QList<QGraphicsItem *> allTiles = m_mapScene->items();
-
-// qDebug() << __PRETTY_FUNCTION__ << "All tiles:" << allTiles.count();
-// qDebug() << __PRETTY_FUNCTION__ << "Tiles in view area:" << viewTiles.count();
-
- //Remove tiles which are in view from allTiles
- foreach (QGraphicsItem *tile, viewTiles)
- allTiles.removeOne(tile);
-
-
- //Remove tiles out of view
- foreach (QGraphicsItem *tile, allTiles) {
- MapTile *tileToRemove = dynamic_cast<MapTile *>(tile);
- if (tileToRemove)
- removeTile(tileToRemove);
- }
+ m_mapScene->viewRectUpdated(QRect(topLeft, bottomRight));
}
-QRect MapEngine::viewRect()
+void MapEngine::viewResized(const QSize &size)
{
- QPoint topLeft = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.topLeft());
- QPoint bottomRight = convertTileNumberToSceneCoordinate(m_zoomLevel, m_viewGrid.bottomRight()
- + QPoint(1, 1));
- return QRect(topLeft, bottomRight);
+ m_viewSize = size;
+ getTiles(m_sceneCoordinate);
+ m_mapScene->removeOutOfViewTiles();
}
-void MapEngine::removeStackedTiles(MapTile *newTile)
+void MapEngine::viewZoomFinished()
{
-// qDebug() << __PRETTY_FUNCTION__;
-
- QRectF newTileSceneRect = newTile->sceneBoundingRect();
-
- //Loop all items under new tile
- QList<QGraphicsItem *> collidingTiles = newTile->collidingItems(Qt::IntersectsItemBoundingRect);
-// qDebug() << __PRETTY_FUNCTION__ << "Colliding tiles before:" << collidingTiles.count();
- foreach (QGraphicsItem *collidingTile, collidingTiles) {
-
-
- QRectF collidingTileSceneRect = collidingTile->sceneBoundingRect();
+ qDebug() << __PRETTY_FUNCTION__;
- // remove tile if it is obscured by new tile
- if (newTileSceneRect.contains(collidingTileSceneRect)) {
- MapTile *tile = dynamic_cast<MapTile *>(collidingTile);
- if (tile)
- removeTile(tile);
- }
- else {
- // remove tile if it is obscured in the view area
- QRect collidingTileViewableArea =
- collidingTileSceneRect.intersected(viewRect()).toRect();
- if (collidingTile->isObscured(collidingTileViewableArea)) {
- qDebug() << __PRETTY_FUNCTION__ << "Deleting obscured item";
- MapTile *tile = dynamic_cast<MapTile *>(collidingTile);
- if (tile)
- removeTile(tile);
- }
- }
+ if (m_zoomedIn) {
+ m_zoomedIn = false;
+ m_mapScene->removeOutOfViewTiles();
}
-// qDebug() << __PRETTY_FUNCTION__ << "All tiles after:"
-// << newTile->collidingItems(Qt::IntersectsItemBoundingRect).count();
-}
-
-void MapEngine::viewResized(const QSize &size)
-{
- m_viewSize = size;
- getTiles(m_sceneCoordinate);
- removeTilesOutOfView();
}
void MapEngine::zoomIn()
{
-// qDebug() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
if (m_zoomLevel < MAX_MAP_ZOOM_LEVEL) {
m_zoomLevel++;
+ m_zoomedIn = true;
emit zoomLevelChanged(m_zoomLevel);
- setTilesDrawingLevels();
+ m_mapScene->setTilesDrawingLevels(m_zoomLevel);
getTiles(m_sceneCoordinate);
-
- // remove unused tiles after zooming is done
- QTimer::singleShot(ZOOM_TIME*2, this, SLOT(removeTilesOutOfView()));
}
}
void MapEngine::zoomOut()
{
-// qDebug() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
- if (m_zoomLevel > MIN_MAP_ZOOM_LEVEL) {
+ if (m_zoomLevel > MIN_VIEW_ZOOM_LEVEL) {
m_zoomLevel--;
emit zoomLevelChanged(m_zoomLevel);
- setTilesDrawingLevels();
+ m_mapScene->setTilesDrawingLevels(m_zoomLevel);
getTiles(m_sceneCoordinate);
}
}
-void MapEngine::setTilesDrawingLevels()
-{
-// qDebug() << __PRETTY_FUNCTION__ << "m_zoomLevel:" << m_zoomLevel;
-
- QList<QGraphicsItem *> items = m_mapScene->items();
-
- for (int i = 0; i < items.size(); ++i) {
- MapTile *item = dynamic_cast<MapTile *>(items.at(i));
- if (item)
- item->setSceneLevel(m_zoomLevel);
- }
-}
-
QString MapEngine::tilePath(int zoomLevel, int x, int y)
{
QString tilePathString(QString::number(zoomLevel) + "/");
QPoint MapEngine::convertLatLonToSceneCoordinate(QPointF latLonCoordinate)
{
-// qDebug() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
qreal longitude = latLonCoordinate.x();
qreal latitude = latLonCoordinate.y();