diff --git a/libs/map/mapview.cpp b/libs/map/mapview.cpp index d07a0e9..06866ca 100644 --- a/libs/map/mapview.cpp +++ b/libs/map/mapview.cpp @@ -104,6 +104,7 @@ void MapView::setTileObsoleteTime(int secs) { void MapView::setTileProvider(MapViewTileProviderBase * p) { + if (p) max_level = p->maximumLevel(); cache->clearCache(); downloader->setTileProvider(p); drawBackground(); @@ -116,8 +117,8 @@ MapViewTileProviderBase * MapView::tileProvider() const { void collectDownloadIndeces(MapViewTileCache * cache, - QQueue & indeces, - OSM::TileIndex index, + QQueue & indeces, + MapViewTypes::TileIndex index, int target_zoom_level, bool only_new) { if (only_new) { @@ -130,7 +131,7 @@ void collectDownloadIndeces(MapViewTileCache * cache, int y = index.y * 2; for (int i = 0; i < 2; ++i) { 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 ey = qMin(tiles_side, (int)ceil(view_rect.bottom() * tiles_side)); target_zoom_level = qMin(target_zoom_level, max_level); - QQueue indeces; + QQueue indeces; for (int ix = sx; ix < ex; ++ix) { 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; @@ -291,7 +292,7 @@ void MapView::drawBackground() { QRectF r((ix)*ts, (iy)*ts, ts, ts); r.translate(-offset); 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()) { p.drawPixmap(r, tile.pixmap, px_rct); } else { @@ -360,7 +361,7 @@ void MapView::zoomLevelChanged(int new_level) { tiles_side = 1 << zoom_level; // for (int x = 0; x < tiles_side; ++x) // for (int y = 0; y < tiles_side; ++y) - // downloader->queueTile(OSM::TileIndex{zoom_level, x, y}); + // downloader->queueTile(MapViewTypes::TileIndex{zoom_level, x, y}); } diff --git a/libs/map/mapview_tile_cache.cpp b/libs/map/mapview_tile_cache.cpp index e6772ec..0e8be98 100644 --- a/libs/map/mapview_tile_cache.cpp +++ b/libs/map/mapview_tile_cache.cpp @@ -5,8 +5,8 @@ MapViewTileCache::MapViewTileCache(MapView * p): QThread() { - qRegisterMetaType(); - qRegisterMetaType(); + qRegisterMetaType(); + qRegisterMetaType(); parent = p; setCacheRoot(QAD::userPath(QAD::ltCache, "mapview") + "/"); setAdditionalCacheSize(64); @@ -21,16 +21,16 @@ MapViewTileCache::~MapViewTileCache() { } -void MapViewTileCache::tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap) { - auto * tile = new OSM::TilePixmap(); +void MapViewTileCache::tileDownloaded(MapViewTypes::TileIndex index, const QPixmap & pixmap) { + auto * tile = new MapViewTypes::TilePixmap(); tile->index = index; tile->pixmap = pixmap; saveTile(tile); } -OSM::TilePixmap MapViewTileCache::getTile(OSM::TileIndex index, QRectF & rect_src) { - OSM::TilePixmap ret = getTileFromCache(index); +MapViewTypes::TilePixmap MapViewTileCache::getTile(MapViewTypes::TileIndex index, QRectF & rect_src) { + MapViewTypes::TilePixmap ret = getTileFromCache(index); if (ret.isEmpty() || ret.obsolete) { 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(); if (!cache_dir.exists(hashdir)) return false; QString hashname = hashdir + "/" + index.hashName(); @@ -91,8 +91,8 @@ void MapViewTileCache::setOfflineMode(bool yes) { } -OSM::TilePixmap MapViewTileCache::getTileFromCache(OSM::TileIndex index) { - OSM::TilePixmap ret; +MapViewTypes::TilePixmap MapViewTileCache::getTileFromCache(MapViewTypes::TileIndex index) { + MapViewTypes::TilePixmap ret; ret.index = index; auto * tile = tile_cache[index.hash()]; if (tile) { @@ -104,7 +104,7 @@ OSM::TilePixmap MapViewTileCache::getTileFromCache(OSM::TileIndex index) { if (cache_dir.exists(hashname)) { QString fname = cache_dir.absoluteFilePath(hashname); ret.pixmap.load(fname, "png"); - tile = new OSM::TilePixmap(); + tile = new MapViewTypes::TilePixmap(); tile->index = index; tile->pixmap = ret.pixmap; 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()); emit tileReady(*tile); 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(); if (!cache_dir.exists(hashdir)) cache_dir.mkdir(hashdir); QFile f(cache_dir.absoluteFilePath(hashdir + "/" + tile.index.hashName())); @@ -149,7 +149,7 @@ void MapViewTileCache::writeToDisk(const OSM::TilePixmap & tile) { void MapViewTileCache::run() { while (!isInterruptionRequested()) { - OSM::TilePixmap tile; + MapViewTypes::TilePixmap tile; cond_mutex.lock(); if (queue.isEmpty()) { cond.wait(&cond_mutex); diff --git a/libs/map/mapview_tile_cache_p.h b/libs/map/mapview_tile_cache_p.h index 88e9107..f16c3ff 100644 --- a/libs/map/mapview_tile_cache_p.h +++ b/libs/map/mapview_tile_cache_p.h @@ -20,7 +20,7 @@ #ifndef mapview_tile_cache_h #define mapview_tile_cache_h -#include "osm_types_p.h" +#include "mapview_types.h" #include #include @@ -48,11 +48,11 @@ public: } void setTileObsoleteTime(int secs) { secs_obsolete = secs; } - void tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap); - OSM::TilePixmap getTile(OSM::TileIndex index, QRectF & rect_src); + void tileDownloaded(MapViewTypes::TileIndex index, const QPixmap & pixmap); + MapViewTypes::TilePixmap getTile(MapViewTypes::TileIndex index, QRectF & rect_src); void clearCache(); - bool isTileFileExists(OSM::TileIndex index) const; + bool isTileFileExists(MapViewTypes::TileIndex index) const; QString cacheRoot() const { return cache_root; } void setCacheRoot(const QString & p); @@ -61,10 +61,10 @@ public: void setOfflineMode(bool yes); private: - OSM::TilePixmap getTileFromCache(OSM::TileIndex index); + MapViewTypes::TilePixmap getTileFromCache(MapViewTypes::TileIndex index); void updateCacheSize(); - void saveTile(OSM::TilePixmap * tile); - void writeToDisk(const OSM::TilePixmap & tile); + void saveTile(MapViewTypes::TilePixmap * tile); + void writeToDisk(const MapViewTypes::TilePixmap & tile); void run() override; MapView * parent; @@ -75,15 +75,15 @@ private: int secs_obsolete = -1; int fixed_size_b = 0, add_size_b = 0; bool is_offline = false; - QQueue queue; - QCache tile_cache; + QQueue queue; + QCache tile_cache; public slots: private slots: signals: - void tileReady(OSM::TilePixmap); + void tileReady(MapViewTypes::TilePixmap); }; diff --git a/libs/map/mapview_tile_downloader.cpp b/libs/map/mapview_tile_downloader.cpp index 3069e57..7d5abea 100644 --- a/libs/map/mapview_tile_downloader.cpp +++ b/libs/map/mapview_tile_downloader.cpp @@ -4,7 +4,7 @@ MapViewTileDownloader::MapViewTileDownloader(MapView * p): QThread() { - qRegisterMetaType(); + qRegisterMetaType(); parent = p; 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(); bool ret = false; cond_mutex.lock(); @@ -32,7 +32,7 @@ bool MapViewTileDownloader::queueTile(OSM::TileIndex index, bool force) { } -void MapViewTileDownloader::queueTiles(QList indeces) { +void MapViewTileDownloader::queueTiles(QList indeces) { cond_mutex.lock(); queue << indeces; 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; 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); cond_mutex.lock(); in_progress.remove(index.hash()); diff --git a/libs/map/mapview_tile_downloader_p.h b/libs/map/mapview_tile_downloader_p.h index c6d8db0..53e0bd2 100644 --- a/libs/map/mapview_tile_downloader_p.h +++ b/libs/map/mapview_tile_downloader_p.h @@ -21,7 +21,7 @@ #define mapview_tile_downloader_h #include "mapview_tile_provider_base.h" -#include "osm_types_p.h" +#include "mapview_types.h" #include #include @@ -43,8 +43,8 @@ public: explicit MapViewTileDownloader(MapView * p); ~MapViewTileDownloader(); - bool queueTile(OSM::TileIndex index, bool force = false); - void queueTiles(QList indeces); + bool queueTile(MapViewTypes::TileIndex index, bool force = false); + void queueTiles(QList indeces); void clearQueue(); void setTileProvider(MapViewTileProviderBase * p); @@ -52,19 +52,19 @@ public: private: void run() override; - void requestTile(OSM::TileIndex index); + void requestTile(MapViewTypes::TileIndex index); MapView * parent; MapViewTileProviderBase * provider = nullptr; QWaitCondition cond; QMutex cond_mutex; - QQueue queue; + QQueue queue; QSet in_progress; public slots: private slots: - void tileReady(OSM::TileIndex index, QPixmap pm); + void tileReady(MapViewTypes::TileIndex index, QPixmap pm); void parametersChanged(); signals: diff --git a/libs/map/mapview_tile_provider_base.h b/libs/map/mapview_tile_provider_base.h index f4f834c..f1bdc62 100644 --- a/libs/map/mapview_tile_provider_base.h +++ b/libs/map/mapview_tile_provider_base.h @@ -20,7 +20,7 @@ #ifndef 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 @@ -37,10 +37,11 @@ public: const PIValueTree & getParameters() const { return parameters; } void setParameters(const PIValueTree & vt); - virtual QString name() const = 0; - virtual bool requestTile(OSM::TileIndex index) = 0; - virtual QSize tileSize() = 0; + virtual QString name() const = 0; + virtual bool requestTile(MapViewTypes::TileIndex index) = 0; + virtual QSize tileSize() = 0; virtual QString cacheDir() const { return name(); } + virtual int maximumLevel() const = 0; protected: void initParameters(const PIValueTree & vt); @@ -52,7 +53,7 @@ private: PIValueTree parameters; signals: - void tileReady(OSM::TileIndex index, QPixmap pm); + void tileReady(MapViewTypes::TileIndex index, QPixmap pm); void parametersChanged(); }; diff --git a/libs/map/mapview_tile_provider_maptiler.cpp b/libs/map/mapview_tile_provider_maptiler.cpp index f230d4d..71db67b 100644 --- a/libs/map/mapview_tile_provider_maptiler.cpp +++ b/libs/map/mapview_tile_provider_maptiler.cpp @@ -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)); req.setHeader(QNetworkRequest::UserAgentHeader, "Qt/5"); 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() { url = PI2QString(getParameters().childValue("url").toString()); key = PI2QString(getParameters().childValue("key").toString()); diff --git a/libs/map/mapview_tile_provider_maptiler.h b/libs/map/mapview_tile_provider_maptiler.h index 6c02a5c..0e03f0d 100644 --- a/libs/map/mapview_tile_provider_maptiler.h +++ b/libs/map/mapview_tile_provider_maptiler.h @@ -33,9 +33,10 @@ public: ~MapViewTileProvider_MapTiler(); QString name() const override; - bool requestTile(OSM::TileIndex index) override; + bool requestTile(MapViewTypes::TileIndex index) override; QSize tileSize() override; QString cacheDir() const override; + int maximumLevel() const override; protected: void applyParameters() override; diff --git a/libs/map/mapview_tile_provider_osm.cpp b/libs/map/mapview_tile_provider_osm.cpp index bb5c4d2..a9a9a3e 100644 --- a/libs/map/mapview_tile_provider_osm.cpp +++ b/libs/map/mapview_tile_provider_osm.cpp @@ -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)); req.setHeader(QNetworkRequest::UserAgentHeader, "Qt/5"); auto * r = nam->get(req); @@ -51,6 +51,11 @@ QSize MapViewTileProvider_OSM::tileSize() { } +int MapViewTileProvider_OSM::maximumLevel() const { + return 19; +} + + void MapViewTileProvider_OSM::applyParameters() { url = PI2QString(getParameters().childValue("url").toString()); } diff --git a/libs/map/mapview_tile_provider_osm.h b/libs/map/mapview_tile_provider_osm.h index 9fcd579..ad37bc2 100644 --- a/libs/map/mapview_tile_provider_osm.h +++ b/libs/map/mapview_tile_provider_osm.h @@ -33,8 +33,9 @@ public: ~MapViewTileProvider_OSM(); QString name() const override; - bool requestTile(OSM::TileIndex index) override; + bool requestTile(MapViewTypes::TileIndex index) override; QSize tileSize() override; + int maximumLevel() const override; protected: void applyParameters() override; diff --git a/libs/map/osm_types.cpp b/libs/map/mapview_types.cpp similarity index 93% rename from libs/map/osm_types.cpp rename to libs/map/mapview_types.cpp index e9ea923..b5e42ba 100644 --- a/libs/map/osm_types.cpp +++ b/libs/map/mapview_types.cpp @@ -1,7 +1,7 @@ /* QAD - Qt ADvanced - Ivan Pelipenko peri4ko@yandex.ru + Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -17,11 +17,11 @@ along with this program. If not, see . */ -#include "osm_types_p.h" +#include "mapview_types.h" #include -using namespace OSM; +using namespace MapViewTypes; QString TileIndex ::hashName() const { diff --git a/libs/map/osm_types_p.h b/libs/map/mapview_types.h similarity index 78% rename from libs/map/osm_types_p.h rename to libs/map/mapview_types.h index f71c795..a586aa4 100644 --- a/libs/map/osm_types_p.h +++ b/libs/map/mapview_types.h @@ -1,7 +1,7 @@ /* QAD - Qt ADvanced - Ivan Pelipenko peri4ko@yandex.ru + Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -17,15 +17,17 @@ along with this program. If not, see . */ -#ifndef osm_types_h -#define osm_types_h +#ifndef mapview_types_h +#define mapview_types_h + +#include "qad_map_export.h" #include #include -namespace OSM { +namespace MapViewTypes { -struct TileIndex { +struct QAD_MAP_EXPORT TileIndex { int z; int x; int y; @@ -34,7 +36,7 @@ struct TileIndex { QString cacheDir() const; }; -struct TilePixmap { +struct QAD_MAP_EXPORT TilePixmap { TileIndex index; QPixmap pixmap; bool obsolete = false; @@ -50,9 +52,9 @@ inline bool operator==(const TilePixmap & v0, const TilePixmap & v1) { return v0.index == v1.index; } -} // namespace OSM +} // namespace MapViewTypes -Q_DECLARE_METATYPE(OSM::TileIndex); -Q_DECLARE_METATYPE(OSM::TilePixmap); +Q_DECLARE_METATYPE(MapViewTypes::TileIndex); +Q_DECLARE_METATYPE(MapViewTypes::TilePixmap); #endif