This commit is contained in:
2024-02-04 13:02:19 +03:00
parent 4c7b9444b6
commit 18526cdf29
4 changed files with 27 additions and 7 deletions

View File

@@ -92,7 +92,11 @@ void MapView::setCachePath(const QString & p) {
}
void collectDownloadIndeces(QQueue<OSM::TileIndex> & indeces, OSM::TileIndex index, int target_zoom_level) {
void collectDownloadIndeces(OSMTileCache * cache, QQueue<OSM::TileIndex> & indeces, OSM::TileIndex index, int target_zoom_level, bool only_new) {
if (only_new) {
if (!cache->isTileFileExists(index))
indeces << index;
} else
indeces << index;
if (index.z >= target_zoom_level) return;
int z = index.z + 1;
@@ -100,13 +104,13 @@ void collectDownloadIndeces(QQueue<OSM::TileIndex> & indeces, OSM::TileIndex ind
int y = index.y * 2;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
collectDownloadIndeces(indeces, (OSM::TileIndex){z, x + i, y + j}, target_zoom_level);
collectDownloadIndeces(cache, indeces, (OSM::TileIndex){z, x + i, y + j}, target_zoom_level, only_new);
}
}
}
void MapView::downloadCurrentView(int target_zoom_level) {
void MapView::downloadCurrentView(int target_zoom_level, bool only_new) {
int sx = qMax(0, (int)floor(view_rect.left() * tiles_side));
int sy = qMax(0, (int)floor(view_rect.top() * tiles_side));
int ex = qMin(tiles_side, (int)ceil(view_rect.right() * tiles_side));
@@ -115,7 +119,7 @@ void MapView::downloadCurrentView(int target_zoom_level) {
QQueue<OSM::TileIndex> indeces;
for (int ix = sx; ix < ex; ++ix) {
for (int iy = sy; iy < ey; ++iy) {
collectDownloadIndeces(indeces, (OSM::TileIndex){zoom_level, ix, iy}, target_zoom_level);
collectDownloadIndeces(cache, indeces, (OSM::TileIndex){zoom_level, ix, iy}, target_zoom_level, only_new);
}
}
if (indeces.size() < 4) return;
@@ -166,6 +170,7 @@ void MapView::downloadCurrentView(int target_zoom_level) {
void MapView::mousePressEvent(QMouseEvent * e) {
is_pan = false;
press_point = e->pos();
zoom_anchor = press_point;
last_click_coord = OSM::xy2geo(mapToNorm(press_point));
}
@@ -199,6 +204,11 @@ void MapView::mouseMoveEvent(QMouseEvent * e) {
if ((e->pos() - press_point).manhattanLength() >= QApplication::startDragDistance()) is_pan = true;
}
}
if (e->buttons() == Qt::RightButton) {
auto dp = e->pos() - press_point;
press_point = e->pos();
zoom(1. - dp.y() / 50., zoom_anchor);
}
updateMouse(e->pos());
}

View File

@@ -61,7 +61,7 @@ public:
QString cachePath() const;
void setCachePath(const QString & p);
void downloadCurrentView(int target_zoom_level = 17);
void downloadCurrentView(int target_zoom_level = 17, bool only_new = true);
protected:
QSize sizeHint() const override { return QSize(200, 200); }
@@ -92,7 +92,7 @@ private:
MapItemBase * hover = nullptr;
QPointF center_ = QPointF(0.5, 0.5);
QPointF last_click_coord = center_;
QPoint press_point;
QPoint press_point, zoom_anchor;
QPixmap background;
QRectF view_rect;
QVector<MapItemBase *> items_;

View File

@@ -54,6 +54,14 @@ OSM::TilePixmap OSMTileCache::getTile(OSM::TileIndex index, QRectF & rect_src) {
}
bool OSMTileCache::isTileFileExists(OSM::TileIndex index) const {
QString hashdir = index.cacheDir();
if (!cache_dir.exists(hashdir)) return false;
QString hashname = hashdir + "/" + index.hashName();
return cache_dir.exists(hashname);
}
void OSMTileCache::setCacheRoot(const QString & p) {
cache_root = p;
cache_dir.setPath(cache_root);

View File

@@ -50,6 +50,8 @@ public:
void tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap);
OSM::TilePixmap getTile(OSM::TileIndex index, QRectF & rect_src);
bool isTileFileExists(OSM::TileIndex index) const;
QString cacheRoot() const { return cache_root; }
void setCacheRoot(const QString & p);