version 2.13.0
add Map library (MapView with OSM maps and items) and mapviewer util
This commit is contained in:
100
libs/map/osm_tile_cache.cpp
Normal file
100
libs/map/osm_tile_cache.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "mapview.h"
|
||||
#include "osm_downloader_p.h"
|
||||
#include "osm_tile_cache_p.h"
|
||||
#include "qad_locations.h"
|
||||
|
||||
|
||||
OSMTileCache::OSMTileCache(MapView * p): QThread() {
|
||||
qRegisterMetaType<OSM::TileIndex>();
|
||||
qRegisterMetaType<OSM::TilePixmap>();
|
||||
parent = p;
|
||||
cache_root = QAD::userPath(QAD::ltCache, "map_osm") + "/";
|
||||
cache_dir.setPath(cache_root);
|
||||
qDebug() << "[OSMTileCache] save cache to" << cache_root;
|
||||
if (!cache_dir.exists()) cache_dir.mkpath(".");
|
||||
setAdditionalCacheSize(64);
|
||||
start();
|
||||
}
|
||||
|
||||
|
||||
OSMTileCache::~OSMTileCache() {
|
||||
requestInterruption();
|
||||
cond.wakeAll();
|
||||
wait();
|
||||
}
|
||||
|
||||
|
||||
void OSMTileCache::tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap) {
|
||||
auto * tile = new OSM::TilePixmap();
|
||||
tile->index = index;
|
||||
tile->pixmap = pixmap;
|
||||
saveTile(tile);
|
||||
}
|
||||
|
||||
|
||||
OSM::TilePixmap OSMTileCache::getTile(OSM::TileIndex index) {
|
||||
OSM::TilePixmap ret;
|
||||
ret.index = index;
|
||||
auto * tile = tile_cache[index.hash()];
|
||||
if (tile) {
|
||||
ret.pixmap = tile->pixmap;
|
||||
} else {
|
||||
QString hashdir = index.cacheDir();
|
||||
if (cache_dir.exists(hashdir)) {
|
||||
QString hashname = hashdir + "/" + index.hashName();
|
||||
if (cache_dir.exists(hashname)) {
|
||||
ret.pixmap.load(cache_dir.absoluteFilePath(hashname), "png");
|
||||
tile = new OSM::TilePixmap();
|
||||
tile->index = index;
|
||||
tile->pixmap = ret.pixmap;
|
||||
tile_cache.insert(tile->index.hash(), tile, tile->pixmapBytes());
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
parent->downloader->queueTile(index);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void OSMTileCache::updateCacheSize() {
|
||||
tile_cache.setMaxCost(fixed_size_b + add_size_b);
|
||||
}
|
||||
|
||||
|
||||
void OSMTileCache::saveTile(OSM::TilePixmap * tile) {
|
||||
tile_cache.insert(tile->index.hash(), tile, tile->pixmapBytes());
|
||||
emit tileReady(*tile);
|
||||
cond_mutex.lock();
|
||||
if (!queue.contains(*tile)) {
|
||||
queue.enqueue(*tile);
|
||||
cond.wakeOne();
|
||||
}
|
||||
cond_mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
void OSMTileCache::writeToDisk(const OSM::TilePixmap & tile) {
|
||||
QString hashdir = tile.index.cacheDir();
|
||||
if (!cache_dir.exists(hashdir)) cache_dir.mkdir(hashdir);
|
||||
QFile f(cache_dir.absoluteFilePath(hashdir + "/" + tile.index.hashName()));
|
||||
if (!f.open(QIODevice::ReadWrite)) return;
|
||||
f.resize(0);
|
||||
tile.pixmap.save(&f, "png");
|
||||
}
|
||||
|
||||
|
||||
void OSMTileCache::run() {
|
||||
while (!isInterruptionRequested()) {
|
||||
OSM::TilePixmap tile;
|
||||
cond_mutex.lock();
|
||||
if (queue.isEmpty()) {
|
||||
cond.wait(&cond_mutex);
|
||||
cond_mutex.unlock();
|
||||
} else {
|
||||
tile = queue.dequeue();
|
||||
cond_mutex.unlock();
|
||||
writeToDisk(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user