From 10e1b5c4fbb4ad23884ee465af30096e0700f613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sun, 6 Dec 2020 19:17:09 +0100 Subject: [PATCH] Open map files passed as program arguments closes #327 --- src/GUI/app.cpp | 27 ++++++++- src/GUI/gui.cpp | 150 +++++++++++++++++++++++----------------------- src/GUI/gui.h | 9 ++- src/data/data.cpp | 4 +- src/data/data.h | 2 +- 5 files changed, 110 insertions(+), 82 deletions(-) diff --git a/src/GUI/app.cpp b/src/GUI/app.cpp index 10e84d5c..48b5e97f 100644 --- a/src/GUI/app.cpp +++ b/src/GUI/app.cpp @@ -16,6 +16,7 @@ #include "opengl.h" #include "gui.h" #include "settings.h" +#include "mapaction.h" #include "app.h" @@ -79,8 +80,17 @@ int App::run() _gui->show(); QStringList args(arguments()); - for (int i = 1; i < args.count(); i++) - _gui->openFile(args.at(i)); + for (int i = 1; i < args.count(); i++) { + if (!_gui->openFile(args.at(i), true)) { + MapAction *a; + if (!_gui->loadMap(args.at(i), a, true)) + _gui->openFile(args.at(i), false); + else { + if (a) + a->trigger(); + } + } + } return exec(); } @@ -89,6 +99,19 @@ bool App::event(QEvent *event) { if (event->type() == QEvent::FileOpen) { QFileOpenEvent *e = static_cast(event); + + if (!_gui->openFile(e->file(), true)) { + MapAction *a; + if (!_gui->loadMap(e->file(), a, true)) + return _gui->openFile(e->file(), false); + else { + if (a) + a->trigger(); + return true; + } + } else + return true; + return _gui->openFile(e->file()); } diff --git a/src/GUI/gui.cpp b/src/GUI/gui.cpp index ac816dfc..2f52648a 100644 --- a/src/GUI/gui.cpp +++ b/src/GUI/gui.cpp @@ -774,12 +774,12 @@ void GUI::openFile() _dataDir = QFileInfo(files.last()).path(); } -bool GUI::openFile(const QString &fileName) +bool GUI::openFile(const QString &fileName, bool silent) { - if (fileName.isEmpty() || _files.contains(fileName)) + if (_files.contains(fileName)) return false; - if (!loadFile(fileName)) + if (!loadFile(fileName, silent)) return false; _files.append(fileName); @@ -796,67 +796,14 @@ bool GUI::openFile(const QString &fileName) return true; } -bool GUI::loadFile(const QString &fileName) +bool GUI::loadFile(const QString &fileName, bool silent) { - Data data(fileName); - QList > graphs; - QList paths; + Data data(fileName, !silent); if (data.isValid()) { - for (int i = 0; i < data.tracks().count(); i++) { - const Track &track = data.tracks().at(i); - _trackDistance += track.distance(); - _time += track.time(); - _movingTime += track.movingTime(); -#ifdef ENABLE_TIMEZONES - const QDateTime date = track.date().toTimeZone( - _options.timeZone.zone()); -#else // ENABLE_TIMEZONES - const QDateTime &date = track.date(); -#endif // ENABLE_TIMEZONES - if (_dateRange.first.isNull() || _dateRange.first > date) - _dateRange.first = date; - if (_dateRange.second.isNull() || _dateRange.second < date) - _dateRange.second = date; - } - _trackCount += data.tracks().count(); - - for (int i = 0; i < data.routes().count(); i++) - _routeDistance += data.routes().at(i).distance(); - _routeCount += data.routes().count(); - - _waypointCount += data.waypoints().count(); - _areaCount += data.areas().count(); - - if (_pathName.isNull()) { - if (data.tracks().count() == 1 && !data.routes().count()) - _pathName = data.tracks().first().name(); - else if (data.routes().count() == 1 && !data.tracks().count()) - _pathName = data.routes().first().name(); - } else - _pathName = QString(); - - for (int i = 0; i < _tabs.count(); i++) - graphs.append(_tabs.at(i)->loadData(data)); - if (updateGraphTabs()) - _splitter->refresh(); - paths = _mapView->loadData(data); - - for (int i = 0; i < paths.count(); i++) { - const PathItem *pi = paths.at(i); - for (int j = 0; j < graphs.count(); j++) { - const GraphItem *gi = graphs.at(j).at(i); - if (!gi) - continue; - connect(gi, SIGNAL(sliderPositionChanged(qreal)), pi, - SLOT(moveMarker(qreal))); - connect(pi, SIGNAL(selected(bool)), gi, SLOT(hover(bool))); - connect(gi, SIGNAL(selected(bool)), pi, SLOT(hover(bool))); - } - } - + loadData(data); return true; - } else { + } else if (!silent) { updateNavigationActions(); updateStatusBarInfo(); updateWindowTitle(); @@ -868,6 +815,65 @@ bool GUI::loadFile(const QString &fileName) error.append("\n" + tr("Line: %1").arg(data.errorLine())); QMessageBox::critical(this, APP_NAME, error); return false; + } else + return false; +} + +void GUI::loadData(const Data &data) +{ + QList > graphs; + QList paths; + + for (int i = 0; i < data.tracks().count(); i++) { + const Track &track = data.tracks().at(i); + _trackDistance += track.distance(); + _time += track.time(); + _movingTime += track.movingTime(); +#ifdef ENABLE_TIMEZONES + const QDateTime date = track.date().toTimeZone( + _options.timeZone.zone()); +#else // ENABLE_TIMEZONES + const QDateTime &date = track.date(); +#endif // ENABLE_TIMEZONES + if (_dateRange.first.isNull() || _dateRange.first > date) + _dateRange.first = date; + if (_dateRange.second.isNull() || _dateRange.second < date) + _dateRange.second = date; + } + _trackCount += data.tracks().count(); + + for (int i = 0; i < data.routes().count(); i++) + _routeDistance += data.routes().at(i).distance(); + _routeCount += data.routes().count(); + + _waypointCount += data.waypoints().count(); + _areaCount += data.areas().count(); + + if (_pathName.isNull()) { + if (data.tracks().count() == 1 && !data.routes().count()) + _pathName = data.tracks().first().name(); + else if (data.routes().count() == 1 && !data.tracks().count()) + _pathName = data.routes().first().name(); + } else + _pathName = QString(); + + for (int i = 0; i < _tabs.count(); i++) + graphs.append(_tabs.at(i)->loadData(data)); + if (updateGraphTabs()) + _splitter->refresh(); + paths = _mapView->loadData(data); + + for (int i = 0; i < paths.count(); i++) { + const PathItem *pi = paths.at(i); + for (int j = 0; j < graphs.count(); j++) { + const GraphItem *gi = graphs.at(j).at(i); + if (!gi) + continue; + connect(gi, SIGNAL(sliderPositionChanged(qreal)), pi, + SLOT(moveMarker(qreal))); + connect(pi, SIGNAL(selected(bool)), gi, SLOT(hover(bool))); + connect(gi, SIGNAL(selected(bool)), pi, SLOT(hover(bool))); + } } } @@ -1431,11 +1437,10 @@ void GUI::loadMap() { QStringList files(QFileDialog::getOpenFileNames(this, tr("Open map file"), _mapDir, MapList::formats())); - MapAction *lastReady = 0; + MapAction *a, *lastReady = 0; for (int i = 0; i < files.size(); i++) { - MapAction *a = loadMap(files.at(i)); - if (a) + if (loadMap(files.at(i), a) && a) lastReady = a; } if (!files.isEmpty()) @@ -1444,20 +1449,15 @@ void GUI::loadMap() lastReady->trigger(); } -MapAction *GUI::loadMap(const QString &fileName) +bool GUI::loadMap(const QString &fileName, MapAction *&action, bool silent) { - // On OS X fileName may be a directory! - - if (fileName.isEmpty()) - return 0; - QString error; QList maps = MapList::loadMaps(fileName, error); if (maps.isEmpty()) { - error = tr("Error loading map:") + "\n\n" - + fileName + "\n\n" + error; - QMessageBox::critical(this, APP_NAME, error); - return 0; + error = tr("Error loading map:") + "\n\n" + fileName + "\n\n" + error; + if (!silent) + QMessageBox::critical(this, APP_NAME, error); + return false; } MapAction *lastReady = 0; @@ -1473,7 +1473,9 @@ MapAction *GUI::loadMap(const QString &fileName) connect(a, SIGNAL(loaded()), this, SLOT(mapLoaded())); } - return lastReady; + action = lastReady; + + return true; } void GUI::mapLoaded() diff --git a/src/GUI/gui.h b/src/GUI/gui.h index 1c38ffac..c0063353 100644 --- a/src/GUI/gui.h +++ b/src/GUI/gui.h @@ -30,6 +30,7 @@ class Map; class POI; class QScreen; class MapAction; +class Data; class GUI : public QMainWindow { @@ -38,7 +39,9 @@ class GUI : public QMainWindow public: GUI(); - bool openFile(const QString &fileName); + bool openFile(const QString &fileName, bool silent = false); + bool loadMap(const QString &fileName, MapAction *&action, + bool silent = false); void show(); private slots: @@ -119,8 +122,8 @@ private: void createBrowser(); bool openPOIFile(const QString &fileName); - bool loadFile(const QString &fileName); - MapAction *loadMap(const QString &fileName); + bool loadFile(const QString &fileName, bool silent = false); + void loadData(const Data &data); void updateStatusBarInfo(); void updateWindowTitle(); void updateNavigationActions(); diff --git a/src/data/data.cpp b/src/data/data.cpp index c27f8e10..1a3c0a63 100644 --- a/src/data/data.cpp +++ b/src/data/data.cpp @@ -83,7 +83,7 @@ void Data::processData(QList &trackData, QList &routeData) _routes.append(Route(routeData.at(i))); } -Data::Data(const QString &fileName) +Data::Data(const QString &fileName, bool tryUnknown) { QFile file(fileName); QFileInfo fi(fileName); @@ -109,7 +109,7 @@ Data::Data(const QString &fileName) _errorLine = it.value()->errorLine(); _errorString = it.value()->errorString(); } - } else { + } else if (tryUnknown) { for (it = _parsers.begin(); it != _parsers.end(); it++) { if (it.value()->parse(&file, trackData, routeData, _polygons, _waypoints)) { diff --git a/src/data/data.h b/src/data/data.h index b171e3bc..76cb5ccd 100644 --- a/src/data/data.h +++ b/src/data/data.h @@ -13,7 +13,7 @@ class Data { public: - Data(const QString &fileName); + Data(const QString &fileName, bool full = true); bool isValid() const {return _valid;} const QString &errorString() const {return _errorString;}