version 2.21.1
Map download API mapviewer download feature
This commit is contained in:
@@ -7,8 +7,10 @@
|
||||
|
||||
#include <QGraphicsPixmapItem>
|
||||
#include <QGraphicsScene>
|
||||
#include <QMetaMethod>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
#include <QProgressDialog>
|
||||
#include <QScrollBar>
|
||||
#include <QWheelEvent>
|
||||
|
||||
@@ -19,7 +21,9 @@ MapView::MapView(QWidget * parent): QWidget(parent) {
|
||||
downloader = new OSMDownloader(this);
|
||||
cache = new OSMTileCache(this);
|
||||
setMouseTracking(true);
|
||||
connect(cache, &OSMTileCache::tileReady, this, [this]() { drawBackground(); });
|
||||
connect(cache, &OSMTileCache::tileReady, this, [this]() {
|
||||
if (!is_downloading) drawBackground();
|
||||
});
|
||||
int size = 16;
|
||||
QPixmap px(QSize(size, size) * 2);
|
||||
QPainter p(&px);
|
||||
@@ -77,6 +81,88 @@ void MapView::setZoom(double z) {
|
||||
}
|
||||
|
||||
|
||||
QString MapView::cachePath() const {
|
||||
return cache->cacheRoot();
|
||||
}
|
||||
|
||||
|
||||
void MapView::setCachePath(const QString & p) {
|
||||
cache->setCacheRoot(p);
|
||||
drawBackground();
|
||||
}
|
||||
|
||||
|
||||
void collectDownloadIndeces(QQueue<OSM::TileIndex> & indeces, OSM::TileIndex index, int target_zoom_level) {
|
||||
indeces << index;
|
||||
if (index.z >= target_zoom_level) return;
|
||||
int z = index.z + 1;
|
||||
int x = index.x * 2;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MapView::downloadCurrentView(int target_zoom_level) {
|
||||
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));
|
||||
int ey = qMin(tiles_side, (int)ceil(view_rect.bottom() * tiles_side));
|
||||
target_zoom_level = qMin(target_zoom_level, max_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);
|
||||
}
|
||||
}
|
||||
if (indeces.size() < 4) return;
|
||||
QProgressDialog dialog;
|
||||
dialog.setWindowTitle(tr("MapView"));
|
||||
dialog.setLabelText(tr("Downloading ..."));
|
||||
dialog.setCancelButtonText(tr("Cancel"));
|
||||
dialog.setMaximum(indeces.size());
|
||||
dialog.setValue(0);
|
||||
downloader->clearQueue();
|
||||
std::atomic_int value(0);
|
||||
auto conn = connect(
|
||||
downloader,
|
||||
&OSMDownloader::tileDone,
|
||||
this,
|
||||
[this, &dialog, &indeces, &value]() {
|
||||
// qDebug() << "done";
|
||||
value++;
|
||||
int cv = value;
|
||||
dialog.setValue(cv);
|
||||
if (indeces.isEmpty())
|
||||
dialog.cancel();
|
||||
else {
|
||||
downloader->queueTile(indeces.dequeue(), true);
|
||||
}
|
||||
},
|
||||
Qt::DirectConnection);
|
||||
connect(&dialog, &QProgressDialog::canceled, this, [this, &dialog]() {
|
||||
dialog.cancel();
|
||||
downloader->clearQueue();
|
||||
});
|
||||
is_downloading = true;
|
||||
for (int i = 0; i < 1; ++i)
|
||||
downloader->queueTile(indeces.dequeue());
|
||||
// dialog.exec();
|
||||
dialog.setModal(true);
|
||||
dialog.show();
|
||||
while (!dialog.isHidden()) {
|
||||
QThread::msleep(1);
|
||||
QApplication::processEvents();
|
||||
}
|
||||
disconnect(conn);
|
||||
is_downloading = false;
|
||||
drawBackground();
|
||||
}
|
||||
|
||||
|
||||
void MapView::mousePressEvent(QMouseEvent * e) {
|
||||
is_pan = false;
|
||||
press_point = e->pos();
|
||||
|
||||
Reference in New Issue
Block a user