Removed unused header file
[GPXSee.git] / src / map / mbtilesmap.h
blob842ac0e5233fc386e7306437689764bdc8284a30
1 #ifndef MBTILESMAP_H
2 #define MBTILESMAP_H
4 #include <QDebug>
5 #include <QSqlDatabase>
6 #include <QVector>
7 #include <QImageReader>
8 #include <QBuffer>
9 #include <QPixmap>
10 #include <QtConcurrent>
11 #include "map.h"
13 class MBTile
15 public:
16 MBTile(int zoom, int overzoom, int scaledSize, const QPoint &xy,
17 const QByteArray &data, const QString &key) : _zoom(zoom),
18 _overzoom(overzoom), _scaledSize(scaledSize), _xy(xy), _data(data),
19 _key(key) {}
21 const QPoint &xy() const {return _xy;}
22 const QString &key() const {return _key;}
23 const QPixmap &pixmap() const {return _pixmap;}
25 void load() {
26 QByteArray format(_overzoom
27 ? QByteArray::number(_zoom) + ';' + QByteArray::number(_overzoom)
28 : QByteArray::number(_zoom));
29 QBuffer buffer(&_data);
30 QImageReader reader(&buffer, format);
31 if (_scaledSize)
32 reader.setScaledSize(QSize(_scaledSize, _scaledSize));
33 _pixmap = QPixmap::fromImage(reader.read());
36 private:
37 int _zoom;
38 int _overzoom;
39 int _scaledSize;
40 QPoint _xy;
41 QByteArray _data;
42 QString _key;
43 QPixmap _pixmap;
46 class MBTilesMapJob : public QObject
48 Q_OBJECT
50 public:
51 MBTilesMapJob(const QList<MBTile> &tiles) : _tiles(tiles) {}
53 void run()
55 connect(&_watcher, &QFutureWatcher<void>::finished, this,
56 &MBTilesMapJob::handleFinished);
57 _future = QtConcurrent::map(_tiles, &MBTile::load);
58 _watcher.setFuture(_future);
60 void cancel(bool wait)
62 _future.cancel();
63 if (wait)
64 _future.waitForFinished();
66 const QList<MBTile> &tiles() const {return _tiles;}
68 signals:
69 void finished(MBTilesMapJob *job);
71 private slots:
72 void handleFinished() {emit finished(this);}
74 private:
75 QFutureWatcher<void> _watcher;
76 QFuture<void> _future;
77 QList<MBTile> _tiles;
80 class MBTilesMap : public Map
82 Q_OBJECT
84 public:
85 MBTilesMap(const QString &fileName, QObject *parent = 0);
87 QString name() const {return _name;}
89 QRectF bounds();
90 RectC llBounds() {return _bounds;}
91 qreal resolution(const QRectF &rect);
93 int zoom() const {return _zi;}
94 void setZoom(int zoom) {_zi = zoom;}
95 int zoomFit(const QSize &size, const RectC &rect);
96 int zoomIn();
97 int zoomOut();
99 QPointF ll2xy(const Coordinates &c);
100 Coordinates xy2ll(const QPointF &p);
102 void draw(QPainter *painter, const QRectF &rect, Flags flags);
104 void load(const Projection &in, const Projection &out, qreal deviceRatio,
105 bool hidpi);
106 void unload();
108 bool isValid() const {return _valid;}
109 QString errorString() const {return _errorString;}
111 static Map *create(const QString &path, const Projection &proj, bool *isDir);
113 private slots:
114 void jobFinished(MBTilesMapJob *job);
116 private:
117 struct Zoom {
118 Zoom() : z(-1), base(-1) {}
119 Zoom(int z, int base) : z(z), base(base) {}
121 int z;
122 int base;
125 bool getMinZoom(int &zoom);
126 bool getMaxZoom(int &zoom);
127 bool getZooms();
128 bool getBounds();
129 bool getTileSize();
130 void getTileFormat();
131 void getTilePixelRatio();
132 void getName();
133 qreal tileSize() const;
134 qreal coordinatesRatio() const;
135 qreal imageRatio() const;
136 QByteArray tileData(int zoom, const QPoint &tile) const;
137 void drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp);
138 bool isRunning(const QString &key) const;
139 void runJob(MBTilesMapJob *job);
140 void removeJob(MBTilesMapJob *job);
141 void cancelJobs(bool wait);
142 QPointF tilePos(const QPointF &tl, const QPoint &tc, const QPoint &tile,
143 unsigned overzoom) const;
145 friend QDebug operator<<(QDebug dbg, const Zoom &zoom);
147 QSqlDatabase _db;
149 QString _name;
150 RectC _bounds;
151 QVector<Zoom> _zooms, _zoomsBase;
152 int _zi;
153 int _tileSize;
154 qreal _mapRatio, _tileRatio;
155 bool _scalable;
156 int _scaledSize;
158 QList<MBTilesMapJob*> _jobs;
160 bool _valid;
161 QString _errorString;
164 #endif // MBTILESMAP_H