fix MapView, so tile provider can be accessible
add MapViewTileProviderBase::maximumLevel()
This commit is contained in:
@@ -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<OSM::TileIndex> & indeces,
|
||||
OSM::TileIndex index,
|
||||
QQueue<MapViewTypes::TileIndex> & 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<OSM::TileIndex> indeces;
|
||||
QQueue<MapViewTypes::TileIndex> 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});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
|
||||
MapViewTileCache::MapViewTileCache(MapView * p): QThread() {
|
||||
qRegisterMetaType<OSM::TileIndex>();
|
||||
qRegisterMetaType<OSM::TilePixmap>();
|
||||
qRegisterMetaType<MapViewTypes::TileIndex>();
|
||||
qRegisterMetaType<MapViewTypes::TilePixmap>();
|
||||
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);
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#ifndef mapview_tile_cache_h
|
||||
#define mapview_tile_cache_h
|
||||
|
||||
#include "osm_types_p.h"
|
||||
#include "mapview_types.h"
|
||||
|
||||
#include <QCache>
|
||||
#include <QDir>
|
||||
@@ -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<OSM::TilePixmap> queue;
|
||||
QCache<quint64, OSM::TilePixmap> tile_cache;
|
||||
QQueue<MapViewTypes::TilePixmap> queue;
|
||||
QCache<quint64, MapViewTypes::TilePixmap> tile_cache;
|
||||
|
||||
public slots:
|
||||
|
||||
private slots:
|
||||
|
||||
signals:
|
||||
void tileReady(OSM::TilePixmap);
|
||||
void tileReady(MapViewTypes::TilePixmap);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
|
||||
MapViewTileDownloader::MapViewTileDownloader(MapView * p): QThread() {
|
||||
qRegisterMetaType<OSM::TileIndex>();
|
||||
qRegisterMetaType<MapViewTypes::TileIndex>();
|
||||
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<OSM::TileIndex> indeces) {
|
||||
void MapViewTileDownloader::queueTiles(QList<MapViewTypes::TileIndex> 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());
|
||||
|
||||
@@ -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 <QMutex>
|
||||
#include <QNetworkAccessManager>
|
||||
@@ -43,8 +43,8 @@ public:
|
||||
explicit MapViewTileDownloader(MapView * p);
|
||||
~MapViewTileDownloader();
|
||||
|
||||
bool queueTile(OSM::TileIndex index, bool force = false);
|
||||
void queueTiles(QList<OSM::TileIndex> indeces);
|
||||
bool queueTile(MapViewTypes::TileIndex index, bool force = false);
|
||||
void queueTiles(QList<MapViewTypes::TileIndex> 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<OSM::TileIndex> queue;
|
||||
QQueue<MapViewTypes::TileIndex> queue;
|
||||
QSet<quint64> in_progress;
|
||||
|
||||
public slots:
|
||||
|
||||
private slots:
|
||||
void tileReady(OSM::TileIndex index, QPixmap pm);
|
||||
void tileReady(MapViewTypes::TileIndex index, QPixmap pm);
|
||||
void parametersChanged();
|
||||
|
||||
signals:
|
||||
|
||||
@@ -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 <QPixmap>
|
||||
@@ -38,9 +38,10 @@ public:
|
||||
void setParameters(const PIValueTree & vt);
|
||||
|
||||
virtual QString name() const = 0;
|
||||
virtual bool requestTile(OSM::TileIndex index) = 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();
|
||||
};
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "osm_types_p.h"
|
||||
#include "mapview_types.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
using namespace OSM;
|
||||
using namespace MapViewTypes;
|
||||
|
||||
|
||||
QString TileIndex ::hashName() const {
|
||||
@@ -17,15 +17,17 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef osm_types_h
|
||||
#define osm_types_h
|
||||
#ifndef mapview_types_h
|
||||
#define mapview_types_h
|
||||
|
||||
#include "qad_map_export.h"
|
||||
|
||||
#include <QMetaType>
|
||||
#include <QPixmap>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user