fix MapView, so tile provider can be accessible

add MapViewTileProviderBase::maximumLevel()
This commit is contained in:
2025-03-18 16:24:02 +03:00
parent a0f2d80b4f
commit f0c2369df0
12 changed files with 78 additions and 62 deletions

View File

@@ -104,6 +104,7 @@ void MapView::setTileObsoleteTime(int secs) {
void MapView::setTileProvider(MapViewTileProviderBase * p) { void MapView::setTileProvider(MapViewTileProviderBase * p) {
if (p) max_level = p->maximumLevel();
cache->clearCache(); cache->clearCache();
downloader->setTileProvider(p); downloader->setTileProvider(p);
drawBackground(); drawBackground();
@@ -116,8 +117,8 @@ MapViewTileProviderBase * MapView::tileProvider() const {
void collectDownloadIndeces(MapViewTileCache * cache, void collectDownloadIndeces(MapViewTileCache * cache,
QQueue<OSM::TileIndex> & indeces, QQueue<MapViewTypes::TileIndex> & indeces,
OSM::TileIndex index, MapViewTypes::TileIndex index,
int target_zoom_level, int target_zoom_level,
bool only_new) { bool only_new) {
if (only_new) { if (only_new) {
@@ -130,7 +131,7 @@ void collectDownloadIndeces(MapViewTileCache * cache,
int y = index.y * 2; int y = index.y * 2;
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) { for (int j = 0; j < 2; ++j) {
collectDownloadIndeces(cache, indeces, (OSM::TileIndex){z, x + i, y + j}, target_zoom_level, only_new); collectDownloadIndeces(cache, indeces, (MapViewTypes::TileIndex){z, x + i, y + j}, target_zoom_level, only_new);
} }
} }
} }
@@ -142,10 +143,10 @@ void MapView::downloadCurrentView(int target_zoom_level, bool only_new, int para
int ex = qMin(tiles_side, (int)ceil(view_rect.right() * 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)); int ey = qMin(tiles_side, (int)ceil(view_rect.bottom() * tiles_side));
target_zoom_level = qMin(target_zoom_level, max_level); target_zoom_level = qMin(target_zoom_level, max_level);
QQueue<OSM::TileIndex> indeces; QQueue<MapViewTypes::TileIndex> indeces;
for (int ix = sx; ix < ex; ++ix) { for (int ix = sx; ix < ex; ++ix) {
for (int iy = sy; iy < ey; ++iy) { for (int iy = sy; iy < ey; ++iy) {
collectDownloadIndeces(cache, indeces, (OSM::TileIndex){zoom_level, ix, iy}, target_zoom_level, only_new); collectDownloadIndeces(cache, indeces, (MapViewTypes::TileIndex){zoom_level, ix, iy}, target_zoom_level, only_new);
} }
} }
if (indeces.size() < 4) return; if (indeces.size() < 4) return;
@@ -291,7 +292,7 @@ void MapView::drawBackground() {
QRectF r((ix)*ts, (iy)*ts, ts, ts); QRectF r((ix)*ts, (iy)*ts, ts, ts);
r.translate(-offset); r.translate(-offset);
QRectF px_rct(QPointF(), tile_size /*QSizeF(tileSize, tileSize)*/); QRectF px_rct(QPointF(), tile_size /*QSizeF(tileSize, tileSize)*/);
auto tile = cache->getTile((OSM::TileIndex){zoom_level, ix, iy}, px_rct); auto tile = cache->getTile((MapViewTypes::TileIndex){zoom_level, ix, iy}, px_rct);
if (!tile.isEmpty()) { if (!tile.isEmpty()) {
p.drawPixmap(r, tile.pixmap, px_rct); p.drawPixmap(r, tile.pixmap, px_rct);
} else { } else {
@@ -360,7 +361,7 @@ void MapView::zoomLevelChanged(int new_level) {
tiles_side = 1 << zoom_level; tiles_side = 1 << zoom_level;
// for (int x = 0; x < tiles_side; ++x) // for (int x = 0; x < tiles_side; ++x)
// for (int y = 0; y < tiles_side; ++y) // for (int y = 0; y < tiles_side; ++y)
// downloader->queueTile(OSM::TileIndex{zoom_level, x, y}); // downloader->queueTile(MapViewTypes::TileIndex{zoom_level, x, y});
} }

View File

@@ -5,8 +5,8 @@
MapViewTileCache::MapViewTileCache(MapView * p): QThread() { MapViewTileCache::MapViewTileCache(MapView * p): QThread() {
qRegisterMetaType<OSM::TileIndex>(); qRegisterMetaType<MapViewTypes::TileIndex>();
qRegisterMetaType<OSM::TilePixmap>(); qRegisterMetaType<MapViewTypes::TilePixmap>();
parent = p; parent = p;
setCacheRoot(QAD::userPath(QAD::ltCache, "mapview") + "/"); setCacheRoot(QAD::userPath(QAD::ltCache, "mapview") + "/");
setAdditionalCacheSize(64); setAdditionalCacheSize(64);
@@ -21,16 +21,16 @@ MapViewTileCache::~MapViewTileCache() {
} }
void MapViewTileCache::tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap) { void MapViewTileCache::tileDownloaded(MapViewTypes::TileIndex index, const QPixmap & pixmap) {
auto * tile = new OSM::TilePixmap(); auto * tile = new MapViewTypes::TilePixmap();
tile->index = index; tile->index = index;
tile->pixmap = pixmap; tile->pixmap = pixmap;
saveTile(tile); saveTile(tile);
} }
OSM::TilePixmap MapViewTileCache::getTile(OSM::TileIndex index, QRectF & rect_src) { MapViewTypes::TilePixmap MapViewTileCache::getTile(MapViewTypes::TileIndex index, QRectF & rect_src) {
OSM::TilePixmap ret = getTileFromCache(index); MapViewTypes::TilePixmap ret = getTileFromCache(index);
if (ret.isEmpty() || ret.obsolete) { if (ret.isEmpty() || ret.obsolete) {
if (!is_offline) parent->downloader->queueTile(index); if (!is_offline) parent->downloader->queueTile(index);
} }
@@ -61,7 +61,7 @@ void MapViewTileCache::clearCache() {
} }
bool MapViewTileCache::isTileFileExists(OSM::TileIndex index) const { bool MapViewTileCache::isTileFileExists(MapViewTypes::TileIndex index) const {
QString hashdir = index.cacheDir(); QString hashdir = index.cacheDir();
if (!cache_dir.exists(hashdir)) return false; if (!cache_dir.exists(hashdir)) return false;
QString hashname = hashdir + "/" + index.hashName(); QString hashname = hashdir + "/" + index.hashName();
@@ -91,8 +91,8 @@ void MapViewTileCache::setOfflineMode(bool yes) {
} }
OSM::TilePixmap MapViewTileCache::getTileFromCache(OSM::TileIndex index) { MapViewTypes::TilePixmap MapViewTileCache::getTileFromCache(MapViewTypes::TileIndex index) {
OSM::TilePixmap ret; MapViewTypes::TilePixmap ret;
ret.index = index; ret.index = index;
auto * tile = tile_cache[index.hash()]; auto * tile = tile_cache[index.hash()];
if (tile) { if (tile) {
@@ -104,7 +104,7 @@ OSM::TilePixmap MapViewTileCache::getTileFromCache(OSM::TileIndex index) {
if (cache_dir.exists(hashname)) { if (cache_dir.exists(hashname)) {
QString fname = cache_dir.absoluteFilePath(hashname); QString fname = cache_dir.absoluteFilePath(hashname);
ret.pixmap.load(fname, "png"); ret.pixmap.load(fname, "png");
tile = new OSM::TilePixmap(); tile = new MapViewTypes::TilePixmap();
tile->index = index; tile->index = index;
tile->pixmap = ret.pixmap; tile->pixmap = ret.pixmap;
if (secs_obsolete > 0) { if (secs_obsolete > 0) {
@@ -125,7 +125,7 @@ void MapViewTileCache::updateCacheSize() {
} }
void MapViewTileCache::saveTile(OSM::TilePixmap * tile) { void MapViewTileCache::saveTile(MapViewTypes::TilePixmap * tile) {
tile_cache.insert(tile->index.hash(), tile, tile->pixmapBytes()); tile_cache.insert(tile->index.hash(), tile, tile->pixmapBytes());
emit tileReady(*tile); emit tileReady(*tile);
cond_mutex.lock(); cond_mutex.lock();
@@ -137,7 +137,7 @@ void MapViewTileCache::saveTile(OSM::TilePixmap * tile) {
} }
void MapViewTileCache::writeToDisk(const OSM::TilePixmap & tile) { void MapViewTileCache::writeToDisk(const MapViewTypes::TilePixmap & tile) {
QString hashdir = tile.index.cacheDir(); QString hashdir = tile.index.cacheDir();
if (!cache_dir.exists(hashdir)) cache_dir.mkdir(hashdir); if (!cache_dir.exists(hashdir)) cache_dir.mkdir(hashdir);
QFile f(cache_dir.absoluteFilePath(hashdir + "/" + tile.index.hashName())); QFile f(cache_dir.absoluteFilePath(hashdir + "/" + tile.index.hashName()));
@@ -149,7 +149,7 @@ void MapViewTileCache::writeToDisk(const OSM::TilePixmap & tile) {
void MapViewTileCache::run() { void MapViewTileCache::run() {
while (!isInterruptionRequested()) { while (!isInterruptionRequested()) {
OSM::TilePixmap tile; MapViewTypes::TilePixmap tile;
cond_mutex.lock(); cond_mutex.lock();
if (queue.isEmpty()) { if (queue.isEmpty()) {
cond.wait(&cond_mutex); cond.wait(&cond_mutex);

View File

@@ -20,7 +20,7 @@
#ifndef mapview_tile_cache_h #ifndef mapview_tile_cache_h
#define mapview_tile_cache_h #define mapview_tile_cache_h
#include "osm_types_p.h" #include "mapview_types.h"
#include <QCache> #include <QCache>
#include <QDir> #include <QDir>
@@ -48,11 +48,11 @@ public:
} }
void setTileObsoleteTime(int secs) { secs_obsolete = secs; } void setTileObsoleteTime(int secs) { secs_obsolete = secs; }
void tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap); void tileDownloaded(MapViewTypes::TileIndex index, const QPixmap & pixmap);
OSM::TilePixmap getTile(OSM::TileIndex index, QRectF & rect_src); MapViewTypes::TilePixmap getTile(MapViewTypes::TileIndex index, QRectF & rect_src);
void clearCache(); void clearCache();
bool isTileFileExists(OSM::TileIndex index) const; bool isTileFileExists(MapViewTypes::TileIndex index) const;
QString cacheRoot() const { return cache_root; } QString cacheRoot() const { return cache_root; }
void setCacheRoot(const QString & p); void setCacheRoot(const QString & p);
@@ -61,10 +61,10 @@ public:
void setOfflineMode(bool yes); void setOfflineMode(bool yes);
private: private:
OSM::TilePixmap getTileFromCache(OSM::TileIndex index); MapViewTypes::TilePixmap getTileFromCache(MapViewTypes::TileIndex index);
void updateCacheSize(); void updateCacheSize();
void saveTile(OSM::TilePixmap * tile); void saveTile(MapViewTypes::TilePixmap * tile);
void writeToDisk(const OSM::TilePixmap & tile); void writeToDisk(const MapViewTypes::TilePixmap & tile);
void run() override; void run() override;
MapView * parent; MapView * parent;
@@ -75,15 +75,15 @@ private:
int secs_obsolete = -1; int secs_obsolete = -1;
int fixed_size_b = 0, add_size_b = 0; int fixed_size_b = 0, add_size_b = 0;
bool is_offline = false; bool is_offline = false;
QQueue<OSM::TilePixmap> queue; QQueue<MapViewTypes::TilePixmap> queue;
QCache<quint64, OSM::TilePixmap> tile_cache; QCache<quint64, MapViewTypes::TilePixmap> tile_cache;
public slots: public slots:
private slots: private slots:
signals: signals:
void tileReady(OSM::TilePixmap); void tileReady(MapViewTypes::TilePixmap);
}; };

View File

@@ -4,7 +4,7 @@
MapViewTileDownloader::MapViewTileDownloader(MapView * p): QThread() { MapViewTileDownloader::MapViewTileDownloader(MapView * p): QThread() {
qRegisterMetaType<OSM::TileIndex>(); qRegisterMetaType<MapViewTypes::TileIndex>();
parent = p; parent = p;
start(); start();
} }
@@ -18,7 +18,7 @@ MapViewTileDownloader::~MapViewTileDownloader() {
} }
bool MapViewTileDownloader::queueTile(OSM::TileIndex index, bool force) { bool MapViewTileDownloader::queueTile(MapViewTypes::TileIndex index, bool force) {
// auto hash = tile.hash(); // auto hash = tile.hash();
bool ret = false; bool ret = false;
cond_mutex.lock(); cond_mutex.lock();
@@ -32,7 +32,7 @@ bool MapViewTileDownloader::queueTile(OSM::TileIndex index, bool force) {
} }
void MapViewTileDownloader::queueTiles(QList<OSM::TileIndex> indeces) { void MapViewTileDownloader::queueTiles(QList<MapViewTypes::TileIndex> indeces) {
cond_mutex.lock(); cond_mutex.lock();
queue << indeces; queue << indeces;
cond.wakeOne(); cond.wakeOne();
@@ -62,13 +62,13 @@ void MapViewTileDownloader::setTileProvider(MapViewTileProviderBase * p) {
} }
void MapViewTileDownloader::requestTile(OSM::TileIndex index) { void MapViewTileDownloader::requestTile(MapViewTypes::TileIndex index) {
if (!provider) return; if (!provider) return;
provider->requestTile(index); provider->requestTile(index);
} }
void MapViewTileDownloader::tileReady(OSM::TileIndex index, QPixmap pm) { void MapViewTileDownloader::tileReady(MapViewTypes::TileIndex index, QPixmap pm) {
if (!pm.isNull()) parent->cache->tileDownloaded(index, pm); if (!pm.isNull()) parent->cache->tileDownloaded(index, pm);
cond_mutex.lock(); cond_mutex.lock();
in_progress.remove(index.hash()); in_progress.remove(index.hash());

View File

@@ -21,7 +21,7 @@
#define mapview_tile_downloader_h #define mapview_tile_downloader_h
#include "mapview_tile_provider_base.h" #include "mapview_tile_provider_base.h"
#include "osm_types_p.h" #include "mapview_types.h"
#include <QMutex> #include <QMutex>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
@@ -43,8 +43,8 @@ public:
explicit MapViewTileDownloader(MapView * p); explicit MapViewTileDownloader(MapView * p);
~MapViewTileDownloader(); ~MapViewTileDownloader();
bool queueTile(OSM::TileIndex index, bool force = false); bool queueTile(MapViewTypes::TileIndex index, bool force = false);
void queueTiles(QList<OSM::TileIndex> indeces); void queueTiles(QList<MapViewTypes::TileIndex> indeces);
void clearQueue(); void clearQueue();
void setTileProvider(MapViewTileProviderBase * p); void setTileProvider(MapViewTileProviderBase * p);
@@ -52,19 +52,19 @@ public:
private: private:
void run() override; void run() override;
void requestTile(OSM::TileIndex index); void requestTile(MapViewTypes::TileIndex index);
MapView * parent; MapView * parent;
MapViewTileProviderBase * provider = nullptr; MapViewTileProviderBase * provider = nullptr;
QWaitCondition cond; QWaitCondition cond;
QMutex cond_mutex; QMutex cond_mutex;
QQueue<OSM::TileIndex> queue; QQueue<MapViewTypes::TileIndex> queue;
QSet<quint64> in_progress; QSet<quint64> in_progress;
public slots: public slots:
private slots: private slots:
void tileReady(OSM::TileIndex index, QPixmap pm); void tileReady(MapViewTypes::TileIndex index, QPixmap pm);
void parametersChanged(); void parametersChanged();
signals: signals:

View File

@@ -20,7 +20,7 @@
#ifndef mapview_tile_provider_base_h #ifndef mapview_tile_provider_base_h
#define mapview_tile_provider_base_h #define mapview_tile_provider_base_h
#include "osm_types_p.h" #include "mapview_types.h"
#include "qad_map_export.h" #include "qad_map_export.h"
#include <QPixmap> #include <QPixmap>
@@ -38,9 +38,10 @@ public:
void setParameters(const PIValueTree & vt); void setParameters(const PIValueTree & vt);
virtual QString name() const = 0; virtual QString name() const = 0;
virtual bool requestTile(OSM::TileIndex index) = 0; virtual bool requestTile(MapViewTypes::TileIndex index) = 0;
virtual QSize tileSize() = 0; virtual QSize tileSize() = 0;
virtual QString cacheDir() const { return name(); } virtual QString cacheDir() const { return name(); }
virtual int maximumLevel() const = 0;
protected: protected:
void initParameters(const PIValueTree & vt); void initParameters(const PIValueTree & vt);
@@ -52,7 +53,7 @@ private:
PIValueTree parameters; PIValueTree parameters;
signals: signals:
void tileReady(OSM::TileIndex index, QPixmap pm); void tileReady(MapViewTypes::TileIndex index, QPixmap pm);
void parametersChanged(); void parametersChanged();
}; };

View File

@@ -27,7 +27,7 @@ QString MapViewTileProvider_MapTiler::name() const {
} }
bool MapViewTileProvider_MapTiler::requestTile(OSM::TileIndex index) { bool MapViewTileProvider_MapTiler::requestTile(MapViewTypes::TileIndex index) {
QNetworkRequest req(url + QString("/%1/%2/%3/%4.jpg?key=%5").arg(tileset).arg(index.z).arg(index.x).arg(index.y).arg(key)); QNetworkRequest req(url + QString("/%1/%2/%3/%4.jpg?key=%5").arg(tileset).arg(index.z).arg(index.x).arg(index.y).arg(key));
req.setHeader(QNetworkRequest::UserAgentHeader, "Qt/5"); req.setHeader(QNetworkRequest::UserAgentHeader, "Qt/5");
auto * r = nam->get(req); auto * r = nam->get(req);
@@ -63,6 +63,11 @@ QString MapViewTileProvider_MapTiler::cacheDir() const {
} }
int MapViewTileProvider_MapTiler::maximumLevel() const {
return 20;
}
void MapViewTileProvider_MapTiler::applyParameters() { void MapViewTileProvider_MapTiler::applyParameters() {
url = PI2QString(getParameters().childValue("url").toString()); url = PI2QString(getParameters().childValue("url").toString());
key = PI2QString(getParameters().childValue("key").toString()); key = PI2QString(getParameters().childValue("key").toString());

View File

@@ -33,9 +33,10 @@ public:
~MapViewTileProvider_MapTiler(); ~MapViewTileProvider_MapTiler();
QString name() const override; QString name() const override;
bool requestTile(OSM::TileIndex index) override; bool requestTile(MapViewTypes::TileIndex index) override;
QSize tileSize() override; QSize tileSize() override;
QString cacheDir() const override; QString cacheDir() const override;
int maximumLevel() const override;
protected: protected:
void applyParameters() override; void applyParameters() override;

View File

@@ -24,7 +24,7 @@ QString MapViewTileProvider_OSM::name() const {
} }
bool MapViewTileProvider_OSM::requestTile(OSM::TileIndex index) { bool MapViewTileProvider_OSM::requestTile(MapViewTypes::TileIndex index) {
QNetworkRequest req(url + QString("/%1/%2/%3.png").arg(index.z).arg(index.x).arg(index.y)); QNetworkRequest req(url + QString("/%1/%2/%3.png").arg(index.z).arg(index.x).arg(index.y));
req.setHeader(QNetworkRequest::UserAgentHeader, "Qt/5"); req.setHeader(QNetworkRequest::UserAgentHeader, "Qt/5");
auto * r = nam->get(req); auto * r = nam->get(req);
@@ -51,6 +51,11 @@ QSize MapViewTileProvider_OSM::tileSize() {
} }
int MapViewTileProvider_OSM::maximumLevel() const {
return 19;
}
void MapViewTileProvider_OSM::applyParameters() { void MapViewTileProvider_OSM::applyParameters() {
url = PI2QString(getParameters().childValue("url").toString()); url = PI2QString(getParameters().childValue("url").toString());
} }

View File

@@ -33,8 +33,9 @@ public:
~MapViewTileProvider_OSM(); ~MapViewTileProvider_OSM();
QString name() const override; QString name() const override;
bool requestTile(OSM::TileIndex index) override; bool requestTile(MapViewTypes::TileIndex index) override;
QSize tileSize() override; QSize tileSize() override;
int maximumLevel() const override;
protected: protected:
void applyParameters() override; void applyParameters() override;

View File

@@ -17,11 +17,11 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "osm_types_p.h" #include "mapview_types.h"
#include <QString> #include <QString>
using namespace OSM; using namespace MapViewTypes;
QString TileIndex ::hashName() const { QString TileIndex ::hashName() const {

View File

@@ -17,15 +17,17 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef osm_types_h #ifndef mapview_types_h
#define osm_types_h #define mapview_types_h
#include "qad_map_export.h"
#include <QMetaType> #include <QMetaType>
#include <QPixmap> #include <QPixmap>
namespace OSM { namespace MapViewTypes {
struct TileIndex { struct QAD_MAP_EXPORT TileIndex {
int z; int z;
int x; int x;
int y; int y;
@@ -34,7 +36,7 @@ struct TileIndex {
QString cacheDir() const; QString cacheDir() const;
}; };
struct TilePixmap { struct QAD_MAP_EXPORT TilePixmap {
TileIndex index; TileIndex index;
QPixmap pixmap; QPixmap pixmap;
bool obsolete = false; bool obsolete = false;
@@ -50,9 +52,9 @@ inline bool operator==(const TilePixmap & v0, const TilePixmap & v1) {
return v0.index == v1.index; return v0.index == v1.index;
} }
} // namespace OSM } // namespace MapViewTypes
Q_DECLARE_METATYPE(OSM::TileIndex); Q_DECLARE_METATYPE(MapViewTypes::TileIndex);
Q_DECLARE_METATYPE(OSM::TilePixmap); Q_DECLARE_METATYPE(MapViewTypes::TilePixmap);
#endif #endif