From 97fdcfdd74625c42a25c022031feccfba5c8f514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Wed, 6 Jul 2022 14:58:41 +0200 Subject: [PATCH] Code cleanup --- src/map/aqmmap.cpp | 29 +++++------------------------ src/map/onlinemap.cpp | 6 +++--- src/map/osmdroidmap.cpp | 29 +++++------------------------ src/map/sqlitemap.cpp | 29 +++++------------------------ src/map/tile.h | 27 +++++++++++++++++++++++---- src/map/tileloader.cpp | 35 ++++++++++++++--------------------- src/map/tileloader.h | 8 ++++---- src/map/wmsmap.cpp | 6 +++--- src/map/wmtsmap.cpp | 6 +++--- 9 files changed, 65 insertions(+), 110 deletions(-) diff --git a/src/map/aqmmap.cpp b/src/map/aqmmap.cpp index 7717d58a..70159274 100644 --- a/src/map/aqmmap.cpp +++ b/src/map/aqmmap.cpp @@ -5,31 +5,12 @@ #include #include #include "osm.h" +#include "tile.h" #include "aqmmap.h" #define MAGIC "FLATPACK1" -class AQTile -{ -public: - AQTile(const QPoint &xy, const QByteArray &data, const QString &key) - : _xy(xy), _data(data), _key(key) {} - - const QPoint &xy() const {return _xy;} - const QString &key() const {return _key;} - const QPixmap &pixmap() const {return _pixmap;} - - void load() {_pixmap.loadFromData(_data);} - -private: - QPoint _xy; - QByteArray _data; - QString _key; - QPixmap _pixmap; -}; - - static bool parseHeader(const QByteArray &data, QString &name) { QList lines = data.split('\n'); @@ -380,7 +361,7 @@ void AQMMap::draw(QPainter *painter, const QRectF &rect, Flags flags) int height = ceil(s.height() / tileSize()); - QList tiles; + QList tiles; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { @@ -395,16 +376,16 @@ void AQMMap::draw(QPainter *painter, const QRectF &rect, Flags flags) * tileSize()); drawTile(painter, pm, tp); } else { - tiles.append(AQTile(t, tileData(t), key)); + tiles.append(RenderTile(t, tileData(t), key)); } } } - QFuture future = QtConcurrent::map(tiles, &AQTile::load); + QFuture future = QtConcurrent::map(tiles, &RenderTile::load); future.waitForFinished(); for (int i = 0; i < tiles.size(); i++) { - const AQTile &mt = tiles.at(i); + const RenderTile &mt = tiles.at(i); QPixmap pm(mt.pixmap()); if (pm.isNull()) continue; diff --git a/src/map/onlinemap.cpp b/src/map/onlinemap.cpp index 9b72ef57..a88b0268 100644 --- a/src/map/onlinemap.cpp +++ b/src/map/onlinemap.cpp @@ -108,11 +108,11 @@ void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags) int width = _zoom ? qCeil(s.width() / tileSize()) : 1; int height = _zoom ? qCeil(s.height() / tileSize()) : 1; - QVector tiles; + QVector tiles; tiles.reserve(width * height); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) - tiles.append(Tile(QPoint(tile.x() + i, _invertY ? (1<<_zoom) + tiles.append(FetchTile(QPoint(tile.x() + i, _invertY ? (1<<_zoom) - (tile.y() + j) - 1 : tile.y() + j), _zoom)); if (flags & Map::Block) @@ -121,7 +121,7 @@ void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags) _tileLoader->loadTilesAsync(tiles); for (int i = 0; i < tiles.count(); i++) { - Tile &t = tiles[i]; + FetchTile &t = tiles[i]; QPointF tp = _zoom ? QPointF(tl.x() + (t.xy().x() - tile.x()) * tileSize(), tl.y() + ((_invertY ? (1<<_zoom) - t.xy().y() - 1 : t.xy().y()) - tile.y()) * tileSize()) diff --git a/src/map/osmdroidmap.cpp b/src/map/osmdroidmap.cpp index 0a163a5a..93b4f844 100644 --- a/src/map/osmdroidmap.cpp +++ b/src/map/osmdroidmap.cpp @@ -7,31 +7,12 @@ #include #include #include "osm.h" +#include "tile.h" #include "osmdroidmap.h" #define META_TYPE(type) static_cast(type) -class OsmdroidTile -{ -public: - OsmdroidTile(const QPoint &xy, const QByteArray &data, const QString &key) - : _xy(xy), _data(data), _key(key) {} - - const QPoint &xy() const {return _xy;} - const QString &key() const {return _key;} - const QPixmap &pixmap() const {return _pixmap;} - - void load() {_pixmap.loadFromData(_data);} - -private: - QPoint _xy; - QByteArray _data; - QString _key; - QPixmap _pixmap; -}; - - OsmdroidMap::OsmdroidMap(const QString &fileName, QObject *parent) : Map(fileName, parent), _mapRatio(1.0), _valid(false) { @@ -268,7 +249,7 @@ void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags) int height = ceil(s.height() / tileSize()); - QList tiles; + QList tiles; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { @@ -283,16 +264,16 @@ void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags) * tileSize()); drawTile(painter, pm, tp); } else { - tiles.append(OsmdroidTile(t, tileData(_zoom, t), key)); + tiles.append(RenderTile(t, tileData(_zoom, t), key)); } } } - QFuture future = QtConcurrent::map(tiles, &OsmdroidTile::load); + QFuture future = QtConcurrent::map(tiles, &RenderTile::load); future.waitForFinished(); for (int i = 0; i < tiles.size(); i++) { - const OsmdroidTile &mt = tiles.at(i); + const RenderTile &mt = tiles.at(i); QPixmap pm(mt.pixmap()); if (pm.isNull()) continue; diff --git a/src/map/sqlitemap.cpp b/src/map/sqlitemap.cpp index b2b065da..c03ae9e3 100644 --- a/src/map/sqlitemap.cpp +++ b/src/map/sqlitemap.cpp @@ -7,31 +7,12 @@ #include #include #include "osm.h" +#include "tile.h" #include "sqlitemap.h" #define META_TYPE(type) static_cast(type) -class SqliteTile -{ -public: - SqliteTile(const QPoint &xy, const QByteArray &data, const QString &key) - : _xy(xy), _data(data), _key(key) {} - - const QPoint &xy() const {return _xy;} - const QString &key() const {return _key;} - const QPixmap &pixmap() const {return _pixmap;} - - void load() {_pixmap.loadFromData(_data);} - -private: - QPoint _xy; - QByteArray _data; - QString _key; - QPixmap _pixmap; -}; - - SqliteMap::SqliteMap(const QString &fileName, QObject *parent) : Map(fileName, parent), _mapRatio(1.0), _valid(false) { @@ -215,7 +196,7 @@ void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags) int height = ceil(s.height() / tileSize()); - QList tiles; + QList tiles; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { @@ -230,16 +211,16 @@ void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags) * tileSize()); drawTile(painter, pm, tp); } else { - tiles.append(SqliteTile(t, tileData(_zoom, t), key)); + tiles.append(RenderTile(t, tileData(_zoom, t), key)); } } } - QFuture future = QtConcurrent::map(tiles, &SqliteTile::load); + QFuture future = QtConcurrent::map(tiles, &RenderTile::load); future.waitForFinished(); for (int i = 0; i < tiles.size(); i++) { - const SqliteTile &mt = tiles.at(i); + const RenderTile &mt = tiles.at(i); QPixmap pm(mt.pixmap()); if (pm.isNull()) continue; diff --git a/src/map/tile.h b/src/map/tile.h index 39b484cd..d098f6fc 100644 --- a/src/map/tile.h +++ b/src/map/tile.h @@ -7,11 +7,11 @@ #include #include "rectd.h" -class Tile +class FetchTile { public: - Tile() {} - Tile(const QPoint &xy, const QVariant &zoom, const RectD &bbox = RectD()) + FetchTile() {} + FetchTile(const QPoint &xy, const QVariant &zoom, const RectD &bbox = RectD()) : _xy(xy), _zoom(zoom), _bbox(bbox) {} const QVariant &zoom() const {return _zoom;} @@ -26,8 +26,27 @@ private: QPixmap _pixmap; }; +class RenderTile +{ +public: + RenderTile(const QPoint &xy, const QByteArray &data, const QString &key) + : _xy(xy), _data(data), _key(key) {} + + const QPoint &xy() const {return _xy;} + const QString &key() const {return _key;} + const QPixmap &pixmap() const {return _pixmap;} + + void load() {_pixmap.loadFromData(_data);} + +private: + QPoint _xy; + QByteArray _data; + QString _key; + QPixmap _pixmap; +}; + #ifndef QT_NO_DEBUG -inline QDebug operator<<(QDebug dbg, const Tile &tile) +inline QDebug operator<<(QDebug dbg, const FetchTile &tile) { dbg.nospace() << "Tile(" << tile.zoom() << ", " << tile.xy() << ", " << tile.bbox() << ")"; diff --git a/src/map/tileloader.cpp b/src/map/tileloader.cpp index 461d4c8d..45d072e1 100644 --- a/src/map/tileloader.cpp +++ b/src/map/tileloader.cpp @@ -11,30 +11,27 @@ class TileImage { public: TileImage() : _tile(0), _scaledSize(0) {} - TileImage(const QString &file, Tile *tile, int scaledSize) + TileImage(const QString &file, FetchTile *tile, int scaledSize) : _file(file), _tile(tile), _scaledSize(scaledSize) {} - void createPixmap() - { - _tile->pixmap().convertFromImage(_image); - } void load() { + QImage img; QByteArray z(_tile->zoom().toString().toLatin1()); QImageReader reader(_file, z); if (_scaledSize) reader.setScaledSize(QSize(_scaledSize, _scaledSize)); - reader.read(&_image); + reader.read(&img); + _tile->pixmap().convertFromImage(img); } const QString &file() const {return _file;} - Tile *tile() {return _tile;} + FetchTile *tile() {return _tile;} private: QString _file; - Tile *_tile; + FetchTile *_tile; int _scaledSize; - QImage _image; }; static QString quadKey(const QPoint &xy, int zoom) @@ -64,13 +61,13 @@ TileLoader::TileLoader(const QString &dir, QObject *parent) connect(_downloader, &Downloader::finished, this, &TileLoader::finished); } -void TileLoader::loadTilesAsync(QVector &list) +void TileLoader::loadTilesAsync(QVector &list) { QList dl; QList imgs; for (int i = 0; i < list.size(); i++) { - Tile &t = list[i]; + FetchTile &t = list[i]; QString file(tileFile(t)); if (QPixmapCache::find(file, &t.pixmap())) @@ -97,19 +94,18 @@ void TileLoader::loadTilesAsync(QVector &list) for (int i = 0; i < imgs.size(); i++) { TileImage &ti = imgs[i]; - ti.createPixmap(); QPixmapCache::insert(ti.file(), ti.tile()->pixmap()); } } -void TileLoader::loadTilesSync(QVector &list) +void TileLoader::loadTilesSync(QVector &list) { QList dl; - QList tl; + QList tl; QList imgs; for (int i = 0; i < list.size(); i++) { - Tile &t = list[i]; + FetchTile &t = list[i]; QString file(tileFile(t)); if (QPixmapCache::find(file, &t.pixmap())) @@ -137,7 +133,7 @@ void TileLoader::loadTilesSync(QVector &list) wait.exec(); for (int i = 0; i < tl.size(); i++) { - Tile *t = tl[i]; + FetchTile *t = tl[i]; QString file = tileFile(*t); if (QFileInfo(file).exists()) imgs.append(TileImage(file, t, _scaledSize)); @@ -146,9 +142,6 @@ void TileLoader::loadTilesSync(QVector &list) QFuture future = QtConcurrent::map(imgs, &TileImage::load); future.waitForFinished(); - - for (int i = 0; i < imgs.size(); i++) - imgs[i].createPixmap(); } void TileLoader::clearCache() @@ -173,7 +166,7 @@ void TileLoader::setScaledSize(int size) QPixmapCache::clear(); } -QUrl TileLoader::tileUrl(const Tile &tile) const +QUrl TileLoader::tileUrl(const FetchTile &tile) const { QString url(_url); @@ -195,7 +188,7 @@ QUrl TileLoader::tileUrl(const Tile &tile) const return QUrl(url); } -QString TileLoader::tileFile(const Tile &tile) const +QString TileLoader::tileFile(const FetchTile &tile) const { return _dir + QLatin1Char('/') + tile.zoom().toString() + QLatin1Char('-') + QString::number(tile.xy().x()) + QLatin1Char('-') diff --git a/src/map/tileloader.h b/src/map/tileloader.h index b0953809..9daf9c57 100644 --- a/src/map/tileloader.h +++ b/src/map/tileloader.h @@ -19,16 +19,16 @@ public: void setScaledSize(int size); void setQuadTiles(bool quadTiles) {_quadTiles = quadTiles;} - void loadTilesAsync(QVector &list); - void loadTilesSync(QVector &list); + void loadTilesAsync(QVector &list); + void loadTilesSync(QVector &list); void clearCache(); signals: void finished(); private: - QUrl tileUrl(const Tile &tile) const; - QString tileFile(const Tile &tile) const; + QUrl tileUrl(const FetchTile &tile) const; + QString tileFile(const FetchTile &tile) const; Downloader *_downloader; QString _url; diff --git a/src/map/wmsmap.cpp b/src/map/wmsmap.cpp index b49230ca..053d9591 100644 --- a/src/map/wmsmap.cpp +++ b/src/map/wmsmap.cpp @@ -174,7 +174,7 @@ void WMSMap::draw(QPainter *painter, const QRectF &rect, Flags flags) QPoint br = QPoint(qCeil(rect.right() / tileSize()), qCeil(rect.bottom() / tileSize())); - QVector tiles; + QVector tiles; tiles.reserve((br.x() - tl.x()) * (br.y() - tl.y())); for (int i = tl.x(); i < br.x(); i++) { for (int j = tl.y(); j < br.y(); j++) { @@ -186,7 +186,7 @@ void WMSMap::draw(QPainter *painter, const QRectF &rect, Flags flags) ? RectD(PointD(tbr.y(), tbr.x()), PointD(ttl.y(), ttl.x())) : RectD(ttl, tbr); - tiles.append(Tile(QPoint(i, j), _zoom, bbox)); + tiles.append(FetchTile(QPoint(i, j), _zoom, bbox)); } } @@ -196,7 +196,7 @@ void WMSMap::draw(QPainter *painter, const QRectF &rect, Flags flags) _tileLoader->loadTilesAsync(tiles); for (int i = 0; i < tiles.count(); i++) { - Tile &t = tiles[i]; + FetchTile &t = tiles[i]; QPointF tp(t.xy().x() * tileSize(), t.xy().y() * tileSize()); if (!t.pixmap().isNull()) { t.pixmap().setDevicePixelRatio(_mapRatio); diff --git a/src/map/wmtsmap.cpp b/src/map/wmtsmap.cpp index a97faa90..7896fbb3 100644 --- a/src/map/wmtsmap.cpp +++ b/src/map/wmtsmap.cpp @@ -180,11 +180,11 @@ void WMTSMap::draw(QPainter *painter, const QRectF &rect, Flags flags) QPoint br = QPoint(qCeil(rect.right() / ts.width()), qCeil(rect.bottom() / ts.height())); - QVector tiles; + QVector tiles; tiles.reserve((br.x() - tl.x()) * (br.y() - tl.y())); for (int i = tl.x(); i < br.x(); i++) for (int j = tl.y(); j < br.y(); j++) - tiles.append(Tile(QPoint(i, j), z.id())); + tiles.append(FetchTile(QPoint(i, j), z.id())); if (flags & Map::Block) _tileLoader->loadTilesSync(tiles); @@ -192,7 +192,7 @@ void WMTSMap::draw(QPainter *painter, const QRectF &rect, Flags flags) _tileLoader->loadTilesAsync(tiles); for (int i = 0; i < tiles.count(); i++) { - Tile &t = tiles[i]; + FetchTile &t = tiles[i]; QPointF tp(t.xy().x() * ts.width(), t.xy().y() * ts.height()); if (!t.pixmap().isNull()) { t.pixmap().setDevicePixelRatio(imageRatio());