2016-10-24 00:21:40 +02:00
|
|
|
#include <QFile>
|
2016-10-26 20:17:05 +02:00
|
|
|
#include <QFileInfo>
|
2016-10-23 11:09:20 +02:00
|
|
|
#include <QLineF>
|
|
|
|
#include "gpxparser.h"
|
|
|
|
#include "tcxparser.h"
|
|
|
|
#include "csvparser.h"
|
2016-10-25 19:46:44 +02:00
|
|
|
#include "kmlparser.h"
|
2016-10-31 22:59:08 +01:00
|
|
|
#include "fitparser.h"
|
2016-11-10 00:08:11 +01:00
|
|
|
#include "igcparser.h"
|
2016-11-16 23:54:15 +01:00
|
|
|
#include "nmeaparser.h"
|
2016-10-23 11:09:20 +02:00
|
|
|
#include "data.h"
|
|
|
|
|
|
|
|
|
|
|
|
Data::Data() : _errorLine(0)
|
|
|
|
{
|
2016-10-28 14:33:36 +02:00
|
|
|
_parsers.insert("gpx", new GPXParser(_trackData, _routeData,
|
|
|
|
_waypointData));
|
|
|
|
_parsers.insert("tcx", new TCXParser(_trackData, _routeData,
|
|
|
|
_waypointData));
|
|
|
|
_parsers.insert("kml", new KMLParser(_trackData, _routeData,
|
|
|
|
_waypointData));
|
2016-10-31 22:59:08 +01:00
|
|
|
_parsers.insert("fit", new FITParser(_trackData, _routeData,
|
|
|
|
_waypointData));
|
2016-10-28 14:33:36 +02:00
|
|
|
_parsers.insert("csv", new CSVParser(_trackData, _routeData,
|
|
|
|
_waypointData));
|
2016-11-10 00:08:11 +01:00
|
|
|
_parsers.insert("igc", new IGCParser(_trackData, _routeData,
|
|
|
|
_waypointData));
|
2016-11-16 23:54:15 +01:00
|
|
|
_parsers.insert("nmea", new NMEAParser(_trackData, _routeData,
|
|
|
|
_waypointData));
|
2016-10-23 11:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Data::~Data()
|
|
|
|
{
|
2016-10-26 20:17:05 +02:00
|
|
|
QHash<QString, Parser*>::iterator it;
|
|
|
|
|
|
|
|
for (it = _parsers.begin(); it != _parsers.end(); it++)
|
|
|
|
delete it.value();
|
2016-10-23 11:09:20 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < _tracks.count(); i++)
|
|
|
|
delete _tracks.at(i);
|
|
|
|
for (int i = 0; i < _routes.count(); i++)
|
|
|
|
delete _routes.at(i);
|
|
|
|
}
|
|
|
|
|
2016-10-26 20:17:05 +02:00
|
|
|
void Data::createData()
|
|
|
|
{
|
2016-10-28 14:33:36 +02:00
|
|
|
for (int i = 0; i < _trackData.count(); i++)
|
|
|
|
_tracks.append(new Track(_trackData.at(i)));
|
|
|
|
for (int i = 0; i < _routeData.count(); i++)
|
|
|
|
_routes.append(new Route(_routeData.at(i)));
|
2016-10-26 20:17:05 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 11:09:20 +02:00
|
|
|
bool Data::loadFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
QFile file(fileName);
|
2016-10-26 20:17:05 +02:00
|
|
|
QFileInfo fi(fileName);
|
|
|
|
|
2016-10-23 11:09:20 +02:00
|
|
|
|
|
|
|
_errorString.clear();
|
|
|
|
_errorLine = 0;
|
|
|
|
|
2016-10-31 22:59:08 +01:00
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
2016-10-23 11:09:20 +02:00
|
|
|
_errorString = qPrintable(file.errorString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-26 20:17:05 +02:00
|
|
|
QHash<QString, Parser*>::iterator it;
|
|
|
|
if ((it = _parsers.find(fi.suffix().toLower())) != _parsers.end()) {
|
|
|
|
if (it.value()->loadFile(&file)) {
|
|
|
|
createData();
|
2016-10-23 11:09:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-26 20:17:05 +02:00
|
|
|
_errorLine = it.value()->errorLine();
|
|
|
|
_errorString = it.value()->errorString();
|
|
|
|
} else {
|
|
|
|
for (it = _parsers.begin(); it != _parsers.end(); it++) {
|
|
|
|
if (it.value()->loadFile(&file)) {
|
|
|
|
createData();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
file.reset();
|
2016-10-23 11:09:20 +02:00
|
|
|
}
|
2016-10-26 20:17:05 +02:00
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
qWarning("Error loading data file: %s:\n", qPrintable(fileName));
|
2016-10-26 20:17:05 +02:00
|
|
|
for (it = _parsers.begin(); it != _parsers.end(); it++)
|
2017-03-18 01:30:31 +01:00
|
|
|
qWarning("%s: line %d: %s\n", qPrintable(it.key()),
|
2016-10-26 20:17:05 +02:00
|
|
|
it.value()->errorLine(), qPrintable(it.value()->errorString()));
|
|
|
|
|
|
|
|
_errorLine = 0;
|
|
|
|
_errorString = "Unknown format";
|
2016-10-23 11:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|