From a56ad8a93377e55ad72e84d04257b802e0159682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sat, 1 Apr 2017 05:59:55 +0200 Subject: [PATCH] Code cleanup/optimizations --- src/atlas.cpp | 53 ++++++++++++++++++++++++++++------------------ src/atlas.h | 1 + src/offlinemap.cpp | 5 +++++ src/offlinemap.h | 2 ++ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/atlas.cpp b/src/atlas.cpp index 7e17d0dd..1009f42e 100644 --- a/src/atlas.cpp +++ b/src/atlas.cpp @@ -20,12 +20,12 @@ static bool resCmp(const OfflineMap *m1, const OfflineMap *m2) return r1 > r2; } -static bool lonCmp(const OfflineMap *m1, const OfflineMap *m2) +static bool xCmp(const OfflineMap *m1, const OfflineMap *m2) { return TL(m1).x() < TL(m2).x(); } -static bool latCmp(const OfflineMap *m1, const OfflineMap *m2) +static bool yCmp(const OfflineMap *m1, const OfflineMap *m2) { return TL(m1).y() > TL(m2).y(); } @@ -80,7 +80,7 @@ void Atlas::computeBounds() for (int i = _zooms.at(z).first; i <= _zooms.at(z).second; i++) m.append(_maps.at(i)); - qSort(m.begin(), m.end(), lonCmp); + qSort(m.begin(), m.end(), xCmp); offsets[_maps.indexOf(m.first())].setX(w); for (int i = 1; i < m.size(); i++) { if (TL(m.at(i)).x() > TL(m.at(i-1)).x()) { @@ -89,7 +89,7 @@ void Atlas::computeBounds() } } - qSort(m.begin(), m.end(), latCmp); + qSort(m.begin(), m.end(), yCmp); offsets[_maps.indexOf(m.first())].setY(h); for (int i = 1; i < m.size(); i++) { if (TL(m.at(i)).y() < TL(m.at(i-1)).y()) { @@ -182,8 +182,7 @@ qreal Atlas::zoomFit(const QSize &size, const QRectF &br) for (int z = 0; z < _zooms.count(); z++) { for (int i = _zooms.at(z).first; i <= _zooms.at(z).second; i++) { - if (_bounds.at(i).first.contains(_maps.at(i)->xy2pp( - _maps.at(i)->ll2xy(br.center())))) + if (_bounds.at(i).first.contains(_maps.at(i)->ll2pp(br.center()))) continue; QRect sbr = QRectF(_maps.at(i)->ll2xy(br.topLeft()), @@ -218,8 +217,7 @@ QPointF Atlas::ll2xy(const Coordinates &c) const int idx = _zooms.at(_zoom).first; for (int i = _zooms.at(_zoom).first; i <= _zooms.at(_zoom).second; i++) { - if (_bounds.at(i).first.contains(_maps.at(i)->xy2pp( - _maps.at(i)->ll2xy(c)))) { + if (_bounds.at(i).first.contains(_maps.at(i)->ll2pp(c))) { idx = i; break; } @@ -246,20 +244,33 @@ Coordinates Atlas::xy2ll(const QPointF &p) const void Atlas::draw(QPainter *painter, const QRectF &rect) { - painter->fillRect(rect, Qt::white); - + // All in one map for (int i = _zooms.at(_zoom).first; i <= _zooms.at(_zoom).second; i++) { - if (rect.intersects(_bounds.at(i).second)) { - OfflineMap *map = _maps.at(i); - QRectF ir = rect.intersected(_bounds.at(i).second); - const QPointF offset = _bounds.at(i).second.topLeft(); - QRectF pr = QRectF(ir.topLeft() - offset, ir.size()); - - map->load(); - - painter->translate(offset); - map->draw(painter, pr); - painter->translate(-offset); + QRectF ir = rect.intersected(_bounds.at(i).second); + if (ir == rect) { + draw(painter, rect, i); + return; } } + + // Multiple maps + painter->fillRect(rect, Qt::white); + for (int i = _zooms.at(_zoom).first; i <= _zooms.at(_zoom).second; i++) { + QRectF ir = rect.intersected(_bounds.at(i).second); + if (!ir.isNull()) + draw(painter, ir, i); + } +} + +void Atlas::draw(QPainter *painter, const QRectF &rect, int mapIndex) +{ + OfflineMap *map = _maps.at(mapIndex); + const QPointF offset = _bounds.at(mapIndex).second.topLeft(); + QRectF pr = QRectF(rect.topLeft() - offset, rect.size()); + + map->load(); + + painter->translate(offset); + map->draw(painter, pr); + painter->translate(-offset); } diff --git a/src/atlas.h b/src/atlas.h index 47c1262b..c2f9a5c8 100644 --- a/src/atlas.h +++ b/src/atlas.h @@ -31,6 +31,7 @@ public: bool isValid() {return _valid;} private: + void draw(QPainter *painter, const QRectF &rect, int mapIndex); bool isAtlas(const QFileInfoList &files); void computeZooms(); void computeBounds(); diff --git a/src/offlinemap.cpp b/src/offlinemap.cpp index 01170c23..da9cdad8 100644 --- a/src/offlinemap.cpp +++ b/src/offlinemap.cpp @@ -515,6 +515,11 @@ Coordinates OfflineMap::xy2ll(const QPointF &p) const return _projection->xy2ll(_transform.inverted().map(p)); } +QPointF OfflineMap::ll2pp(const Coordinates &c) const +{ + return _projection->ll2xy(c); +} + QPointF OfflineMap::xy2pp(const QPointF &p) const { return _transform.inverted().map(p); diff --git a/src/offlinemap.h b/src/offlinemap.h index 6c350460..5fd04621 100644 --- a/src/offlinemap.h +++ b/src/offlinemap.h @@ -38,6 +38,8 @@ public: void unload(); bool isValid() {return _valid;} + + QPointF ll2pp(const Coordinates &c) const; QPointF xy2pp(const QPointF &p) const; QPointF pp2xy(const QPointF &p) const;