From 7f6ac2e4c30621f95008bbe89ff22e2ec2b1f8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Wed, 1 Sep 2021 13:08:34 +0200 Subject: [PATCH] Improved DEM downloads handling logic --- src/GUI/gui.cpp | 57 ++++++++++++++++++++++++++++----------- src/GUI/gui.h | 5 +++- src/GUI/mapview.cpp | 16 +++++++++++ src/GUI/mapview.h | 2 +- src/GUI/optionsdialog.cpp | 1 + src/data/dem.cpp | 4 +++ src/data/dem.h | 1 + src/data/demloader.cpp | 18 ++++++------- src/data/demloader.h | 3 ++- 9 files changed, 79 insertions(+), 28 deletions(-) diff --git a/src/GUI/gui.cpp b/src/GUI/gui.cpp index c71ddcf9..e73ae2bf 100644 --- a/src/GUI/gui.cpp +++ b/src/GUI/gui.cpp @@ -363,13 +363,12 @@ void GUI::createActions(TreeNode &mapActions, _showWaypointsAction = new QAction(tr("Show waypoints"), this); _showWaypointsAction->setMenuRole(QAction::NoRole); _showWaypointsAction->setCheckable(true); - connect(_showWaypointsAction, &QAction::triggered, _mapView, - &MapView::showWaypoints); + connect(_showWaypointsAction, &QAction::triggered, this, + &GUI::showWaypoints); _showAreasAction = new QAction(tr("Show areas"), this); _showAreasAction->setMenuRole(QAction::NoRole); _showAreasAction->setCheckable(true); - connect(_showAreasAction, &QAction::triggered, _mapView, - &MapView::showAreas); + connect(_showAreasAction, &QAction::triggered, this, &GUI::showAreas); _showWaypointLabelsAction = new QAction(tr("Waypoint labels"), this); _showWaypointLabelsAction->setMenuRole(QAction::NoRole); _showWaypointLabelsAction->setCheckable(true); @@ -867,6 +866,7 @@ bool GUI::loadFile(const QString &fileName, bool silent) updateStatusBarInfo(); updateWindowTitle(); updateGraphTabs(); + updateDEMDownloadAction(); QString error = tr("Error loading data file:") + "\n\n" + fileName + "\n\n" + data.errorString(); @@ -932,6 +932,8 @@ void GUI::loadData(const Data &data) pi->setMarkerPosition(gt->sliderPosition()); } } + + updateDEMDownloadAction(); } void GUI::openPOIFile() @@ -1073,7 +1075,6 @@ void GUI::openOptions() _dem->setAuthorization(options.demAuthorization ? Authorization(options.demUsername, options.demPassword) : Authorization()); - _downloadDEMAction->setEnabled(!options.demURL.isEmpty()); if (options.pixmapCache != _options.pixmapCache) QPixmapCache::setCacheLimit(options.pixmapCache * 1024); @@ -1094,6 +1095,8 @@ void GUI::openOptions() reloadFiles(); _options = options; + + updateDEMDownloadAction(); } void GUI::printFile() @@ -1387,6 +1390,7 @@ void GUI::reloadFiles() _fileActionGroup->setEnabled(false); else _browser->setCurrent(_files.last()); + updateDEMDownloadAction(); } void GUI::closeFiles() @@ -1419,6 +1423,7 @@ void GUI::closeAll() updateStatusBarInfo(); updateWindowTitle(); updateGraphTabs(); + updateDEMDownloadAction(); } void GUI::showGraphs(bool show) @@ -1478,6 +1483,7 @@ void GUI::showTracks(bool show) updateStatusBarInfo(); updateGraphTabs(); + updateDEMDownloadAction(); } void GUI::showRoutes(bool show) @@ -1489,6 +1495,19 @@ void GUI::showRoutes(bool show) updateStatusBarInfo(); updateGraphTabs(); + updateDEMDownloadAction(); +} + +void GUI::showWaypoints(bool show) +{ + _mapView->showWaypoints(show); + updateDEMDownloadAction(); +} + +void GUI::showAreas(bool show) +{ + _mapView->showAreas(show); + updateDEMDownloadAction(); } void GUI::showGraphGrids(bool show) @@ -1713,22 +1732,26 @@ void GUI::clearMapCache() void GUI::downloadDEM() { - _demRect = _mapView->boundingRect(); + RectC br(_mapView->boundingRect()); + _demRects.append(br); - if (_dem->loadTiles(_demRect)) - _downloadDEMAction->setEnabled(false); - else + if (!_dem->loadTiles(br) && _demRects.size() == 1) demLoaded(); } void GUI::demLoaded() { - if (!_dem->checkTiles(_demRect)) - QMessageBox::warning(this, APP_NAME, - tr("Could not download all required DEM files.")); + for (int i = 0; i < _demRects.size(); i++) { + if (!_dem->checkTiles(_demRects.at(i))) { + QMessageBox::warning(this, APP_NAME, + tr("Could not download all required DEM files.")); + break; + } + } + DEM::clearCache(); + _demRects.clear(); reloadFiles(); - _downloadDEMAction->setEnabled(!_options.demURL.isEmpty()); } void GUI::updateStatusBarInfo() @@ -1886,6 +1909,12 @@ bool GUI::updateGraphTabs() return (hidden != _graphTabWidget->isHidden()); } +void GUI::updateDEMDownloadAction() +{ + _downloadDEMAction->setEnabled(!_options.demURL.isEmpty() + && !_dem->checkTiles(_mapView->boundingRect())); +} + void GUI::setTimeType(TimeType type) { for (int i = 0; i <_tabs.count(); i++) @@ -2703,8 +2732,6 @@ void GUI::readSettings() if (_options.demAuthorization) _dem->setAuthorization(Authorization(_options.demUsername, _options.demPassword)); - if (!_options.demURL.isEmpty()) - _downloadDEMAction->setEnabled(true); QPixmapCache::setCacheLimit(_options.pixmapCache * 1024); diff --git a/src/GUI/gui.h b/src/GUI/gui.h index 9504a192..9b6b78b6 100644 --- a/src/GUI/gui.h +++ b/src/GUI/gui.h @@ -67,6 +67,8 @@ private slots: void showFullscreen(bool show); void showTracks(bool show); void showRoutes(bool show); + void showAreas(bool show); + void showWaypoints(bool show); void loadMap(); void loadMapDir(); void nextMap(); @@ -143,6 +145,7 @@ private: void updateStatusBarInfo(); void updateWindowTitle(); bool updateGraphTabs(); + void updateDEMDownloadAction(); TimeType timeType() const; Units units() const; @@ -268,7 +271,7 @@ private: Units _units; - RectC _demRect; + QList _demRects; }; #endif // GUI_H diff --git a/src/GUI/mapview.cpp b/src/GUI/mapview.cpp index 5d1f8205..7c68a568 100644 --- a/src/GUI/mapview.cpp +++ b/src/GUI/mapview.cpp @@ -1147,3 +1147,19 @@ void MapView::fitContentToSize() centerOn(contentCenter()); } + +RectC MapView::boundingRect() const +{ + RectC rect; + + if (_showTracks) + rect |= _tr; + if (_showRoutes) + rect |= _rr; + if (_showWaypoints) + rect |= _wr; + if (_showAreas) + rect |= _ar; + + return rect; +} diff --git a/src/GUI/mapview.h b/src/GUI/mapview.h index c235b784..7c5c2918 100644 --- a/src/GUI/mapview.h +++ b/src/GUI/mapview.h @@ -88,7 +88,7 @@ public: void clearMapCache(); void fitContentToSize(); - RectC boundingRect() const {return _tr | _rr | _wr | _ar;} + RectC boundingRect() const; public slots: void showMap(bool show); diff --git a/src/GUI/optionsdialog.cpp b/src/GUI/optionsdialog.cpp index 1f9a216f..5cfcc735 100644 --- a/src/GUI/optionsdialog.cpp +++ b/src/GUI/optionsdialog.cpp @@ -559,6 +559,7 @@ QWidget *OptionsDialog::createDEMPage() InfoLabel *info = new InfoLabel( tr("Use $lat and $lon for NYY/SYY and EXXX/WXXX in the URL.")); + info->setMinimumWidth(_demURL->minimumWidth()); #ifdef Q_OS_MAC QFormLayout *sourceLayout = new QFormLayout(); diff --git a/src/data/dem.cpp b/src/data/dem.cpp index c97ad07b..4214ce81 100644 --- a/src/data/dem.cpp +++ b/src/data/dem.cpp @@ -94,6 +94,10 @@ QString DEM::fileName(const QString &baseName) void DEM::setDir(const QString &path) { _dir = path; +} + +void DEM::clearCache() +{ _data.clear(); } diff --git a/src/data/dem.h b/src/data/dem.h index 344a819c..cf78a726 100644 --- a/src/data/dem.h +++ b/src/data/dem.h @@ -34,6 +34,7 @@ public: }; static void setDir(const QString &path); + static void clearCache(); static qreal elevation(const Coordinates &c); private: diff --git a/src/data/demloader.cpp b/src/data/demloader.cpp index e35bddd4..97977086 100644 --- a/src/data/demloader.cpp +++ b/src/data/demloader.cpp @@ -1,11 +1,10 @@ #include -#include #include #include "common/rectc.h" #include "demloader.h" -#define DOWNLOAD_LIMIT 32 +#define DOWNLOAD_LIMIT 16 static QList tiles(const RectC &rect) { @@ -42,13 +41,12 @@ bool DEMLoader::loadTiles(const RectC &rect) /* Create the user DEM dir only when a download is requested as it will override the global DEM dir. */ - if (!QDir().mkpath(_dir)) { - qWarning("%s: %s", qPrintable(_dir), "Error creating DEM directory"); + if (!_dir.mkpath(_dir.absolutePath())) { + qWarning("%s: %s", qPrintable(_dir.canonicalPath()), + "Error creating DEM directory"); return false; } - /* This also clears the DEM data cache which is necessary to use the data - from the downloaded DEM tiles. */ - DEM::setDir(_dir); + DEM::setDir(_dir.path()); for (int i = 0; i < tl.size(); i++) { const DEM::Tile &t = tl.at(i); @@ -62,8 +60,8 @@ bool DEMLoader::loadTiles(const RectC &rect) } if (dl.size() > DOWNLOAD_LIMIT) { - qWarning("DEM download limit exceeded. Limit/requested: %u/%u.", - DOWNLOAD_LIMIT, (unsigned)dl.size()); + qWarning("Requested DEM area is too large (%u tiles)", + (unsigned)dl.size()); return false; } @@ -98,5 +96,5 @@ QUrl DEMLoader::tileUrl(const DEM::Tile &tile) const QString DEMLoader::tileFile(const DEM::Tile &tile) const { - return _dir + QLatin1Char('/') + tile.baseName(); + return _dir.absoluteFilePath(tile.baseName()); } diff --git a/src/data/demloader.h b/src/data/demloader.h index 50cff3f0..7fdfae6d 100644 --- a/src/data/demloader.h +++ b/src/data/demloader.h @@ -2,6 +2,7 @@ #define DEMLOADER_H #include +#include #include "common/downloader.h" #include "dem.h" @@ -30,7 +31,7 @@ private: Downloader *_downloader; QString _url; - QString _dir; + QDir _dir; Authorization _authorization; };