Map API refactoring
[GPXSee.git] / src / map / encmap.h
blob6a4691121d43aef39289edcb6d5a896acef7a461
1 #ifndef ENCMAP_H
2 #define ENCMAP_H
4 #include <QtConcurrent>
5 #include "map.h"
6 #include "projection.h"
7 #include "transform.h"
8 #include "ENC/mapdata.h"
9 #include "ENC/rastertile.h"
11 class ENCMapJob : public QObject
13 Q_OBJECT
15 public:
16 ENCMapJob(const QList<ENC::RasterTile> &tiles)
17 : _tiles(tiles) {}
19 void run()
21 connect(&_watcher, &QFutureWatcher<void>::finished, this,
22 &ENCMapJob::handleFinished);
23 _future = QtConcurrent::map(_tiles, &ENC::RasterTile::render);
24 _watcher.setFuture(_future);
26 void cancel(bool wait)
28 _future.cancel();
29 if (wait)
30 _future.waitForFinished();
32 const QList<ENC::RasterTile> &tiles() const {return _tiles;}
34 signals:
35 void finished(ENCMapJob *job);
37 private slots:
38 void handleFinished() {emit finished(this);}
40 private:
41 QFutureWatcher<void> _watcher;
42 QFuture<void> _future;
43 QList<ENC::RasterTile> _tiles;
46 class ENCMap : public Map
48 Q_OBJECT
50 public:
51 ENCMap(const QString &fileName, QObject *parent = 0);
53 QString name() const {return _data.name();}
55 QRectF bounds() {return _bounds;}
56 RectC llBounds(const Projection &) {return _llBounds;}
58 int zoom() const {return _zoom;}
59 void setZoom(int zoom);
60 int zoomFit(const QSize &size, const RectC &rect);
61 int zoomIn();
62 int zoomOut();
64 void load(const Projection &in, const Projection &out, qreal deviceRatio,
65 bool hidpi);
66 void unload();
68 QPointF ll2xy(const Coordinates &c)
69 {return _transform.proj2img(_projection.ll2xy(c));}
70 Coordinates xy2ll(const QPointF &p)
71 {return _projection.xy2ll(_transform.img2proj(p));}
73 void draw(QPainter *painter, const QRectF &rect, Flags flags);
75 bool isValid() const {return _data.isValid();}
76 QString errorString() const {return _data.errorString();}
78 static Map *create(const QString &path, bool *isMap);
80 private slots:
81 void jobFinished(ENCMapJob *job);
83 private:
84 Transform transform(int zoom) const;
85 void updateTransform();
86 bool isRunning(int zoom, const QPoint &xy) const;
87 void runJob(ENCMapJob *job);
88 void removeJob(ENCMapJob *job);
89 void cancelJobs(bool wait);
90 QString key(int zoom, const QPoint &xy) const;
92 ENC::MapData _data;
93 Projection _projection;
94 Transform _transform;
95 qreal _tileRatio;
96 RectC _llBounds;
97 QRectF _bounds;
98 int _zoom;
100 QList<ENCMapJob*> _jobs;
102 bool _valid;
103 QString _errorString;
106 #endif // ENCMAP_H