From 1a29ab6304577ce3efcb4fd2fe38a05e1b8474a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 16 Jan 2017 09:54:12 +0100 Subject: [PATCH] Enabled free map scroll. --- src/pathview.cpp | 105 ++++++++++++++++++++++++---------------------- src/pathview.h | 5 ++- src/scaleitem.cpp | 41 +++++------------- src/scaleitem.h | 6 +-- 4 files changed, 69 insertions(+), 88 deletions(-) diff --git a/src/pathview.cpp b/src/pathview.cpp index 144cf4de..86bbb456 100644 --- a/src/pathview.cpp +++ b/src/pathview.cpp @@ -4,6 +4,7 @@ #include #include "opengl.h" #include "rd.h" +#include "wgs84.h" #include "poi.h" #include "data.h" #include "map.h" @@ -56,10 +57,10 @@ qreal mapScale(int zoom) return ((360.0/(qreal)(1<setZValue(2.0); _zoom = ZOOM_MAX; + _res = 1.0; _map = 0; _poi = 0; @@ -206,24 +208,28 @@ QList PathView::loadData(const Data &data) for (int i = 0; i < data.tracks().count(); i++) paths.append(addTrack(*(data.tracks().at(i)))); + for (int i = 0; i < data.routes().count(); i++) paths.append(addRoute(*(data.routes().at(i)))); + addWaypoints(data.waypoints()); if (_tracks.empty() && _routes.empty() && _waypoints.empty()) return paths; - if ((_tracks.size() + _routes.size() > 1 && _zoom < zoom) - || (_waypoints.size() && _zoom < zoom)) + if (_zoom < zoom) rescale(_zoom); - else + else { + _scene->setSceneRect(scaled(QRectF(QPointF(-180, -180), + QSizeF(360, 360)), 1.0 / mapScale(_zoom))); updatePOIVisibility(); + } - QRectF sr = contentsSceneRect(); - _scene->setSceneRect(sr); - centerOn(sr.center()); + QPointF center = contentsCenter(); + centerOn(center); - _mapScale->setZoom(_zoom, -(sr.center().ry() * mapScale(_zoom))); + _res = zoom2resolution(_zoom, -(center.y() * mapScale(_zoom))); + _mapScale->setResolution(_res); if (_mapScale->scene() != _scene) _scene->addItem(_mapScale); @@ -236,7 +242,7 @@ void PathView::updateWaypointsBoundingRect(const QPointF &wp) if (_wp.isNull()) _wp = wp; else { - _wr = qrectf(_wp, wp); + _wr = QRectF(_wp, wp).normalized(); _wp = QPointF(); } } else @@ -258,19 +264,18 @@ qreal PathView::contentsScale() const return qMax(sc.x(), sc.y()); } -QRectF PathView::contentsSceneRect() const +QPointF PathView::contentsCenter() const { - qreal scale = mapScale(_zoom); QRectF br = _tr | _rr | _wr; if (!br.isNull() && !_wp.isNull()) unite(br, _wp); + qreal scale = mapScale(_zoom); + if (br.isNull()) - return QRectF(QPointF(_wp.x() / scale - Tile::size()/2, - _wp.y() /scale - Tile::size()/2), QSizeF(Tile::size(), Tile::size())); + return _wp / scale; else - return scaled(br, 1.0/scale).adjusted(-Tile::size(), -Tile::size(), - Tile::size(), Tile::size()); + return scaled(br, 1.0/scale).center(); } void PathView::updatePOIVisibility() @@ -299,6 +304,9 @@ void PathView::rescale(int zoom) _zoom = zoom; qreal scale = mapScale(zoom); + _scene->setSceneRect(scaled(QRectF(QPointF(-180, -180), QSizeF(360, 360)), + 1.0 / scale)); + for (int i = 0; i < _tracks.size(); i++) _tracks.at(i)->setScale(1.0/scale); @@ -418,16 +426,6 @@ void PathView::redraw() resetCachedContent(); } -void PathView::rescale() -{ - int zoom = scale2zoom(contentsScale()); - - if (zoom != _zoom) { - rescale(zoom); - _mapScale->setZoom(zoom); - } -} - void PathView::zoom(int z, const QPoint &pos) { if (_tracks.isEmpty() && _routes.isEmpty() && _waypoints.isEmpty()) @@ -441,16 +439,11 @@ void PathView::zoom(int z, const QPoint &pos) rescale(_zoom); - QRectF sr = contentsSceneRect(); - _scene->setSceneRect(sr); + QPointF center = (spos * (os/mapScale(_zoom))) - offset; + centerOn(center); - if (sr.width() < viewport()->size().width() - && sr.height() < viewport()->size().height()) - centerOn(sr.center()); - else - centerOn((spos * (os/mapScale(_zoom))) - offset); - - _mapScale->setZoom(_zoom); + _res = zoom2resolution(_zoom, -(center.y() * mapScale(_zoom))); + _mapScale->setResolution(_res); resetCachedContent(); } @@ -536,6 +529,7 @@ void PathView::clear() _palette.reset(); _zoom = ZOOM_MAX; + _res = 1.0; _tr = QRectF(); _rr = QRectF(); _wr = QRectF(); _wp = QPointF(); @@ -663,8 +657,8 @@ void PathView::drawBackground(QPainter *painter, const QRectF &rect) QList tiles; - for (int i = 0; i <= rr.size().width() / Tile::size() + 1; i++) - for (int j = 0; j <= rr.size().height() / Tile::size() + 1; j++) + for (int i = 0; i <= ceil(rr.size().width() / Tile::size()); i++) + for (int j = 0; j <= ceil(rr.size().height() / Tile::size()); j++) tiles.append(Tile(QPoint(tile.x() + i, tile.y() + j), _zoom)); _map->loadTiles(tiles, _plot); @@ -679,24 +673,20 @@ void PathView::drawBackground(QPainter *painter, const QRectF &rect) void PathView::resizeEvent(QResizeEvent *event) { + Q_UNUSED(event); + if (_tracks.isEmpty() && _routes.isEmpty() && _waypoints.isEmpty()) return; - rescale(); + int zoom = scale2zoom(contentsScale()); + if (zoom != _zoom) + rescale(zoom); - QRectF sr = contentsSceneRect(); + QPointF center = contentsCenter(); + centerOn(center); - if (sr.width() < event->size().width()) { - qreal diff = event->size().width() - sr.width(); - sr.adjust(-diff/2, 0, diff/2, 0); - } - if (sr.height() < event->size().height()) { - qreal diff = event->size().height() - sr.height(); - sr.adjust(0, -diff/2, 0, diff/2); - } - - _scene->setSceneRect(sr); - centerOn(sr.center()); + _res = zoom2resolution(_zoom, -(center.y() * mapScale(_zoom))); + _mapScale->setResolution(_res); resetCachedContent(); } @@ -712,6 +702,19 @@ void PathView::paintEvent(QPaintEvent *event) QGraphicsView::paintEvent(event); } +void PathView::scrollContentsBy(int dx, int dy) +{ + QGraphicsView::scrollContentsBy(dx, dy); + + QPointF center = mapToScene(viewport()->rect().center()); + qreal res = zoom2resolution(_zoom, -(center.y() * mapScale(_zoom))); + + if (qMax(res, _res) / qMin(res, _res) > 1.1) { + _mapScale->setResolution(res); + _res = res; + } +} + void PathView::useOpenGL(bool use) { if (use) { diff --git a/src/pathview.h b/src/pathview.h index a824b320..b50e8e90 100644 --- a/src/pathview.h +++ b/src/pathview.h @@ -73,9 +73,8 @@ private: void clearPOI(); qreal contentsScale() const; - QRectF contentsSceneRect() const; + QPointF contentsCenter() const; void rescale(int zoom); - void rescale(); void zoom(int z, const QPoint &pos); void updatePOIVisibility(); void updateWaypointsBoundingRect(const QPointF &wp); @@ -86,6 +85,7 @@ private: void drawBackground(QPainter *painter, const QRectF &rect); void resizeEvent(QResizeEvent *event); void paintEvent(QPaintEvent *event); + void scrollContentsBy(int dx, int dy); QGraphicsScene *_scene; ScaleItem *_mapScale; @@ -97,6 +97,7 @@ private: int _zoom; QRectF _tr, _rr, _wr; QPointF _wp; + qreal _res; Map *_map; POI *_poi; diff --git a/src/scaleitem.cpp b/src/scaleitem.cpp index 8299c09b..572f9b8a 100644 --- a/src/scaleitem.cpp +++ b/src/scaleitem.cpp @@ -1,8 +1,5 @@ #include #include "config.h" -#include "rd.h" -#include "wgs84.h" -#include "tile.h" #include "misc.h" #include "scaleitem.h" @@ -13,17 +10,11 @@ #define SEGMENTS 3 #define PADDING 4 -static qreal zoom2resolution(int zoom, qreal y) -{ - return (WGS84_RADIUS * 2 * M_PI / Tile::size() - * cos(2.0 * atan(exp(deg2rad(y))) - M_PI/2)) / (qreal)(1<= MIINFT) { - _length = niceNum((res * M2FT * FT2MI * SCALE_WIDTH) / SEGMENTS, 1); - _width = (_length / (res * M2FT * FT2MI)); + _length = niceNum((_res * M2FT * FT2MI * SCALE_WIDTH) / SEGMENTS, 1); + _width = (_length / (_res * M2FT * FT2MI)); _scale = true; } else { - _width = (_length / (res * M2FT)); + _width = (_length / (_res * M2FT)); _scale = false; } } else { - _length = niceNum((res * SCALE_WIDTH) / SEGMENTS, 1); + _length = niceNum((_res * SCALE_WIDTH) / SEGMENTS, 1); if (_length >= KMINM) { _length *= M2KM; - _width = (_length / (res * M2KM)); + _width = (_length / (_res * M2KM)); _scale = true; } else { - _width = (_length / res); + _width = (_length / _res); _scale = false; } } } -void ScaleItem::setZoom(int z, qreal lat) +void ScaleItem::setResolution(qreal res) { prepareGeometryChange(); - _zoom = z; - _lat = lat; - computeScale(); - updateBoundingRect(); - update(); -} - -void ScaleItem::setZoom(int z) -{ - prepareGeometryChange(); - _zoom = z; + _res = res; computeScale(); updateBoundingRect(); update(); diff --git a/src/scaleitem.h b/src/scaleitem.h index 29ab7e80..f6a96738 100644 --- a/src/scaleitem.h +++ b/src/scaleitem.h @@ -13,8 +13,7 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - void setZoom(int z); - void setZoom(int z, qreal lat); + void setResolution(qreal res); void setUnits(enum Units units); private: @@ -22,8 +21,7 @@ private: void computeScale(); QString units() const; - int _zoom; - qreal _lat; + qreal _res; qreal _width; qreal _length; Units _units;