USA.
*/
-#include <cmath>
-
#include <QDebug>
+#include "coordinates/scenecoordinate.h"
#include "mapcommon.h"
-#include "mapengine.h"
#include "maptile.h"
#include "mapscene.h"
-const int WORLD_PIXELS_Y = MAX_TILES_PER_SIDE * TILE_SIZE_Y;
-
MapScene::MapScene(QObject *parent)
: QGraphicsScene(parent)
, m_isRemoveStackedTilesRunning(false)
+ , m_zoomLevel(0)
, m_tilesSceneRect(QRect(0, 0, 0, 0))
+ , m_viewTilesGrid(QRect(0, 0, 0, 0))
{
qDebug() << __PRETTY_FUNCTION__;
setBackgroundBrush(Qt::lightGray);
- setSceneRect(-WORLD_PIXELS_X * 2, 0, WORLD_PIXELS_X * 5 - 1, WORLD_PIXELS_Y - 1);
+ setSceneRect(QRect(QPoint(MAP_SCENE_MIN_PIXEL_X, OSM_MAP_MIN_PIXEL_Y),
+ QPoint(MAP_SCENE_MAX_PIXEL_X, OSM_MAP_MAX_PIXEL_Y)));
}
void MapScene::addTile(int tileZoomLevel, QPoint tileNumber, const QPixmap &image, int viewZoomLevel)
qDebug() << __PRETTY_FUNCTION__;
// tile might already be in the scene if expired tile was returned from the cache to be
- // temporarily displayed while downloading the fresh one. Tile can also be in the scene if the
- // zoom level is low and world is spanning around, in which case the old tile removal is
- // unnecessary, but this situation can't be recognised with information currently available
- QString hashKey = MapEngine::tilePath(tileZoomLevel, tileNumber.x(), tileNumber.y());
+ // temporarily shown while downloading the fresh one.
+ QString hashKey = MapTile::tilePath(tileZoomLevel, tileNumber.x(), tileNumber.y());
MapTile *oldTile = tileInScene(hashKey);
if (oldTile)
removeTile(oldTile);
tile->setTileNumber(tileNumber);
tile->setPixmap(image);
- m_mapTilesInScene.insertMulti(hashKey, tile);
+ m_mapTilesInScene.insert(hashKey, tile);
addItem(tile);
+ qDebug() << __PRETTY_FUNCTION__ << "tiles count:" << m_mapTilesInScene.count();
+
enqueueRemoveStackedTiles(tile);
+ removeOtherLevelTiles();
}
void MapScene::enqueueRemoveStackedTiles(MapTile *newTile)
}
}
+qreal MapScene::horizontalResolutionAtLatitude(double latitude)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ const int SHIFT = 200;
+
+ GeoCoordinate from = GeoCoordinate(latitude, 0);
+
+ SceneCoordinate fromScene = SceneCoordinate(from);
+ SceneCoordinate toScene(fromScene.x() + SHIFT, fromScene.y());
+ GeoCoordinate to(toScene);
+
+ qreal distance = from.distanceTo(to);
+
+ return (distance / SHIFT);
+}
+
+void MapScene::moveIntersectingItemsHorizontally(QRect from, int distance)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QList<QGraphicsItem *> spanItems = items(from, Qt::IntersectsItemBoundingRect);
+ foreach (QGraphicsItem *item, spanItems) {
+ if (!dynamic_cast<MapTile *>(item))
+ item->moveBy(distance, 0);
+ }
+}
+
MapTile* MapScene::tileInScene(QString hashKey)
{
qDebug() << __PRETTY_FUNCTION__;
m_isRemoveStackedTilesRunning = false;
}
-void MapScene::removeOutOfViewTiles(QRect tilesGrid, int zoomLevel)
+void MapScene::removeOtherLevelTiles()
{
- qWarning() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
+
+ for (int x = m_viewTilesGrid.left(); x <= m_viewTilesGrid.right(); x++) {
+ for (int y = m_viewTilesGrid.top(); y <= m_viewTilesGrid.bottom(); y++) {
+ if (!m_mapTilesInScene.contains(MapTile::tilePath(m_zoomLevel, x, y)))
+ return;
+ }
+ }
- qWarning() << __PRETTY_FUNCTION__ << "m_tilesSceneRect:" << m_tilesSceneRect.left() << m_tilesSceneRect.top() << "/" << m_tilesSceneRect.right() << m_tilesSceneRect.bottom();
+ foreach(MapTile *tile, m_mapTilesInScene) {
+ if (tile->zoomLevel() != m_zoomLevel) {
+ removeTile(tile);
+ qDebug() << __PRETTY_FUNCTION__ << "removed other level tile";
+ }
+ }
+}
+
+void MapScene::removeOutOfViewTiles(QRect tilesGrid, int zoomLevel)
+{
+ qDebug() << __PRETTY_FUNCTION__;
- QList<QGraphicsItem *> viewTiles = items(m_tilesSceneRect, Qt::IntersectsItemBoundingRect);
- QList<QGraphicsItem *> allTiles = items();
+ QList<QGraphicsItem *> viewItems = items(m_tilesSceneRect, Qt::IntersectsItemBoundingRect);
+ QList<QGraphicsItem *> allItems = items();
//Remove tiles which are in view from allTiles
- foreach (QGraphicsItem *tile, viewTiles)
- allTiles.removeOne(tile);
+ foreach (QGraphicsItem *item, viewItems)
+ allItems.removeOne(item);
- int lastTile = MapEngine::tileMaxValue(zoomLevel);
- int half = (tilesGrid.width() + 0.5) / 2;
- qWarning() << __PRETTY_FUNCTION__ << "half:" << half;
+ // note: add 1 so odd values are rounded up
+ int tilesGridWidthHalf = (tilesGrid.width() + 1) / 2;
- if (tilesGrid.right() > (lastTile - half)) { /// @todo must be mirrored also when near the world limit
+ // if view is near east limit of the map, then there is duplicate tiles also on the opposite
+ // side of the world which are removed from allTiles
+ if (tilesGrid.right() > ((MapTile::lastTileIndex(zoomLevel)
+ - tilesGridWidthHalf
+ + MAP_GRID_PADDING))) {
QRect oppositeRect = m_tilesSceneRect;
- oppositeRect.translate(-WORLD_PIXELS_X, 0);
- qWarning() << __PRETTY_FUNCTION__ << "oppositeRect:" << oppositeRect.left() << oppositeRect.top() << "/" << oppositeRect.right() << oppositeRect.bottom();
- QList<QGraphicsItem *> oppositeTiles = items(oppositeRect, Qt::IntersectsItemBoundingRect);
- foreach (QGraphicsItem *tile, oppositeTiles)
- allTiles.removeOne(tile);
+ oppositeRect.translate(-OSM_MAP_PIXELS_X, 0);
+ QList<QGraphicsItem *> oppositeItems = items(oppositeRect, Qt::IntersectsItemBoundingRect);
+ foreach (QGraphicsItem *item, oppositeItems)
+ allItems.removeOne(item);
}
- if (tilesGrid.left() < half) { /// @todo must be mirrored also when near the world limit
+ // if view is near west limit of the map, then there is duplicate tiles also on the opposite
+ // side of the world which are removed from allTiles
+ if (tilesGrid.left() < (tilesGridWidthHalf - MAP_GRID_PADDING)) {
QRect oppositeRect = m_tilesSceneRect;
- oppositeRect.translate(WORLD_PIXELS_X, 0);
- QList<QGraphicsItem *> oppositeTiles = items(oppositeRect, Qt::IntersectsItemBoundingRect);
- foreach (QGraphicsItem *tile, oppositeTiles)
- allTiles.removeOne(tile);
+ oppositeRect.translate(OSM_MAP_PIXELS_X, 0);
+ QList<QGraphicsItem *> oppositeItems = items(oppositeRect, Qt::IntersectsItemBoundingRect);
+ foreach (QGraphicsItem *item, oppositeItems)
+ allItems.removeOne(item);
}
//Remove tiles out of view
- foreach (QGraphicsItem *tile, allTiles) {
- MapTile *tileToRemove = dynamic_cast<MapTile *>(tile);
- if (tileToRemove) {
- qWarning() << __PRETTY_FUNCTION__ << "removing tile, x:" << tileToRemove->tileNumber().x() << "y:" << tileToRemove->tileNumber().y() << "pos:" << tileToRemove->pos().x() << tileToRemove->pos().y();
- removeTile(tileToRemove);
- }
+ foreach (QGraphicsItem *item, allItems) {
+ MapTile *tile = dynamic_cast<MapTile *>(item);
+ if (tile)
+ removeTile(tile);
}
}
{
qDebug() << __PRETTY_FUNCTION__;
- m_mapTilesInScene.remove(MapEngine::tilePath(tile->zoomLevel(),
- tile->tileNumber().x(),
- tile->tileNumber().y()));
+ m_mapTilesInScene.remove(MapTile::tilePath(tile->zoomLevel(),
+ tile->tileNumber().x(),
+ tile->tileNumber().y()));
removeItem(tile);
m_removeStackedTilesList.removeAll(tile);
delete tile;
+
+ qDebug() << __PRETTY_FUNCTION__ << "tiles count:" << m_mapTilesInScene.count();
}
void MapScene::setSceneVerticalOverlap(int viewHeight, int zoomLevel)
{
qDebug() << __PRETTY_FUNCTION__;
- int overlap = viewHeight / 2 * (1 << (MAX_MAP_ZOOM_LEVEL - zoomLevel));
+ int overlap = viewHeight / 2 * (1 << (OSM_MAX_ZOOM_LEVEL - zoomLevel));
QRect rect = sceneRect().toRect();
- rect.setTop(-overlap);
- rect.setBottom(WORLD_PIXELS_Y + overlap - 1);
+ rect.setTop(OSM_MAP_MIN_PIXEL_Y - overlap);
+ rect.setBottom(OSM_MAP_MAX_PIXEL_Y + overlap);
setSceneRect(rect);
-
- qWarning() << __PRETTY_FUNCTION__ << "scene rect:" << rect.left() << rect.top() << rect.right() << rect.bottom();
}
void MapScene::setTilesDrawingLevels(int zoomLevel)
}
}
-QRect MapScene::spanItems(ScrollDirection direction, int zoomLevel)
+void MapScene::setTilesGrid(QRect grid)
{
- qDebug() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__ ;
- QRect spanRect; // = m_viewRect;
- spanRect.setTop(0);
- spanRect.setBottom(WORLD_PIXELS_Y);
- int delta;
+ m_viewTilesGrid = grid;
+}
- if (direction == SCROLL_WEST) {
- //spanRect.translate(WORLD_PIXELS_X, 0);
- spanRect.setLeft(m_tilesSceneRect.right());
- spanRect.setRight(sceneRect().right());
- delta = -WORLD_PIXELS_X;
- }
- else {
- //spanRect.translate(-WORLD_PIXELS_X, 0);
- spanRect.setRight(m_tilesSceneRect.left());
- spanRect.setLeft(sceneRect().left());
- delta = WORLD_PIXELS_X;
- }
+void MapScene::setZoomLevel(int zoomLevel)
+{
+ qDebug() << __PRETTY_FUNCTION__ ;
- QTransform transform;
- qreal scale = pow(2, zoomLevel - MAX_MAP_ZOOM_LEVEL);
- transform.scale(scale, scale);
- QList<QGraphicsItem *> spanItems = items(spanRect, Qt::IntersectsItemBoundingRect,
- Qt::DescendingOrder, transform);
+ m_zoomLevel = zoomLevel;
+}
- foreach (QGraphicsItem *item, spanItems) {
- if (dynamic_cast<MapTile *>(item))
- continue;
- item->moveBy(delta, 0);
- }
+void MapScene::spanItems(QRectF viewSceneRect)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ // create rects for left and right side
+ QRect leftRect = sceneRect().toRect(); // this way we get the horizontal limits of the scene
+ leftRect.setTop(OSM_MAP_MIN_PIXEL_Y);
+ leftRect.setBottom(OSM_MAP_MAX_PIXEL_Y);
+ QRect rightRect = leftRect;
+
+ // limit rects to include only area which really must be moved
+ leftRect.setRight(-1 - (OSM_MAP_PIXELS_X - 1 - viewSceneRect.right()));
+ rightRect.setLeft(OSM_MAP_PIXELS_X + viewSceneRect.left());
+
+ Q_ASSERT_X(leftRect.right() < viewSceneRect.left(), "spanning rect right value", "move rect is in the view area");
+ Q_ASSERT_X(rightRect.left() > viewSceneRect.right(), "spanning rect left value", "move rect is in the view area");
- return spanRect;
+ // move all items which intersects the rects
+ if (leftRect.left() < leftRect.right())
+ moveIntersectingItemsHorizontally(leftRect, OSM_MAP_PIXELS_X);
+ if (rightRect.left() < rightRect.right())
+ moveIntersectingItemsHorizontally(rightRect, -OSM_MAP_PIXELS_X);
}
void MapScene::tilesSceneRectUpdated(QRect tilesSceneRect)