1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01:00

Open map files passed as program arguments

closes #327
This commit is contained in:
Martin Tůma 2020-12-06 19:17:09 +01:00
parent 07fa377e38
commit 10e1b5c4fb
5 changed files with 110 additions and 82 deletions

View File

@ -16,6 +16,7 @@
#include "opengl.h" #include "opengl.h"
#include "gui.h" #include "gui.h"
#include "settings.h" #include "settings.h"
#include "mapaction.h"
#include "app.h" #include "app.h"
@ -79,8 +80,17 @@ int App::run()
_gui->show(); _gui->show();
QStringList args(arguments()); QStringList args(arguments());
for (int i = 1; i < args.count(); i++) for (int i = 1; i < args.count(); i++) {
_gui->openFile(args.at(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(); return exec();
} }
@ -89,6 +99,19 @@ bool App::event(QEvent *event)
{ {
if (event->type() == QEvent::FileOpen) { if (event->type() == QEvent::FileOpen) {
QFileOpenEvent *e = static_cast<QFileOpenEvent *>(event); QFileOpenEvent *e = static_cast<QFileOpenEvent *>(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()); return _gui->openFile(e->file());
} }

View File

@ -774,12 +774,12 @@ void GUI::openFile()
_dataDir = QFileInfo(files.last()).path(); _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; return false;
if (!loadFile(fileName)) if (!loadFile(fileName, silent))
return false; return false;
_files.append(fileName); _files.append(fileName);
@ -796,67 +796,14 @@ bool GUI::openFile(const QString &fileName)
return true; return true;
} }
bool GUI::loadFile(const QString &fileName) bool GUI::loadFile(const QString &fileName, bool silent)
{ {
Data data(fileName); Data data(fileName, !silent);
QList<QList<GraphItem*> > graphs;
QList<PathItem*> paths;
if (data.isValid()) { if (data.isValid()) {
for (int i = 0; i < data.tracks().count(); i++) { loadData(data);
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)));
}
}
return true; return true;
} else { } else if (!silent) {
updateNavigationActions(); updateNavigationActions();
updateStatusBarInfo(); updateStatusBarInfo();
updateWindowTitle(); updateWindowTitle();
@ -868,6 +815,65 @@ bool GUI::loadFile(const QString &fileName)
error.append("\n" + tr("Line: %1").arg(data.errorLine())); error.append("\n" + tr("Line: %1").arg(data.errorLine()));
QMessageBox::critical(this, APP_NAME, error); QMessageBox::critical(this, APP_NAME, error);
return false; return false;
} else
return false;
}
void GUI::loadData(const Data &data)
{
QList<QList<GraphItem*> > graphs;
QList<PathItem*> 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"), QStringList files(QFileDialog::getOpenFileNames(this, tr("Open map file"),
_mapDir, MapList::formats())); _mapDir, MapList::formats()));
MapAction *lastReady = 0; MapAction *a, *lastReady = 0;
for (int i = 0; i < files.size(); i++) { for (int i = 0; i < files.size(); i++) {
MapAction *a = loadMap(files.at(i)); if (loadMap(files.at(i), a) && a)
if (a)
lastReady = a; lastReady = a;
} }
if (!files.isEmpty()) if (!files.isEmpty())
@ -1444,20 +1449,15 @@ void GUI::loadMap()
lastReady->trigger(); 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; QString error;
QList<Map*> maps = MapList::loadMaps(fileName, error); QList<Map*> maps = MapList::loadMaps(fileName, error);
if (maps.isEmpty()) { if (maps.isEmpty()) {
error = tr("Error loading map:") + "\n\n" error = tr("Error loading map:") + "\n\n" + fileName + "\n\n" + error;
+ fileName + "\n\n" + error; if (!silent)
QMessageBox::critical(this, APP_NAME, error); QMessageBox::critical(this, APP_NAME, error);
return 0; return false;
} }
MapAction *lastReady = 0; MapAction *lastReady = 0;
@ -1473,7 +1473,9 @@ MapAction *GUI::loadMap(const QString &fileName)
connect(a, SIGNAL(loaded()), this, SLOT(mapLoaded())); connect(a, SIGNAL(loaded()), this, SLOT(mapLoaded()));
} }
return lastReady; action = lastReady;
return true;
} }
void GUI::mapLoaded() void GUI::mapLoaded()

View File

@ -30,6 +30,7 @@ class Map;
class POI; class POI;
class QScreen; class QScreen;
class MapAction; class MapAction;
class Data;
class GUI : public QMainWindow class GUI : public QMainWindow
{ {
@ -38,7 +39,9 @@ class GUI : public QMainWindow
public: public:
GUI(); 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(); void show();
private slots: private slots:
@ -119,8 +122,8 @@ private:
void createBrowser(); void createBrowser();
bool openPOIFile(const QString &fileName); bool openPOIFile(const QString &fileName);
bool loadFile(const QString &fileName); bool loadFile(const QString &fileName, bool silent = false);
MapAction *loadMap(const QString &fileName); void loadData(const Data &data);
void updateStatusBarInfo(); void updateStatusBarInfo();
void updateWindowTitle(); void updateWindowTitle();
void updateNavigationActions(); void updateNavigationActions();

View File

@ -83,7 +83,7 @@ void Data::processData(QList<TrackData> &trackData, QList<RouteData> &routeData)
_routes.append(Route(routeData.at(i))); _routes.append(Route(routeData.at(i)));
} }
Data::Data(const QString &fileName) Data::Data(const QString &fileName, bool tryUnknown)
{ {
QFile file(fileName); QFile file(fileName);
QFileInfo fi(fileName); QFileInfo fi(fileName);
@ -109,7 +109,7 @@ Data::Data(const QString &fileName)
_errorLine = it.value()->errorLine(); _errorLine = it.value()->errorLine();
_errorString = it.value()->errorString(); _errorString = it.value()->errorString();
} }
} else { } else if (tryUnknown) {
for (it = _parsers.begin(); it != _parsers.end(); it++) { for (it = _parsers.begin(); it != _parsers.end(); it++) {
if (it.value()->parse(&file, trackData, routeData, _polygons, if (it.value()->parse(&file, trackData, routeData, _polygons,
_waypoints)) { _waypoints)) {

View File

@ -13,7 +13,7 @@
class Data class Data
{ {
public: public:
Data(const QString &fileName); Data(const QString &fileName, bool full = true);
bool isValid() const {return _valid;} bool isValid() const {return _valid;}
const QString &errorString() const {return _errorString;} const QString &errorString() const {return _errorString;}