1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Fixed POI crash on vector reallocation

Improved error reporting
This commit is contained in:
Martin Tůma 2015-11-26 19:13:59 +01:00
parent d1bf74f43b
commit 4e543782ec
11 changed files with 135 additions and 129 deletions

View File

@ -1,45 +1,45 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS> <!DOCTYPE TS>
<TS version="2.1" language="cs_CZ"> <TS version="2.0" language="cs_CZ">
<context> <context>
<name>ElevationGraph</name> <name>ElevationGraph</name>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="12"/> <location filename="../src/elevationgraph.cpp" line="13"/>
<source>Distance</source> <source>Distance</source>
<translation>Vzdálenost</translation> <translation>Vzdálenost</translation>
</message> </message>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="13"/> <location filename="../src/elevationgraph.cpp" line="14"/>
<source>Elevation</source> <source>Elevation</source>
<translation>Výška</translation> <translation>Výška</translation>
</message> </message>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="14"/> <location filename="../src/elevationgraph.cpp" line="15"/>
<source>km</source> <source>km</source>
<translation>km</translation> <translation>km</translation>
</message> </message>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="15"/> <location filename="../src/elevationgraph.cpp" line="16"/>
<source>m</source> <source>m</source>
<translation>m</translation> <translation>m</translation>
</message> </message>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="51"/> <location filename="../src/elevationgraph.cpp" line="52"/>
<source>Ascent</source> <source>Ascent</source>
<translation>Stoupání</translation> <translation>Stoupání</translation>
</message> </message>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="53"/> <location filename="../src/elevationgraph.cpp" line="54"/>
<source>Descent</source> <source>Descent</source>
<translation>Klesání</translation> <translation>Klesání</translation>
</message> </message>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="57"/> <location filename="../src/elevationgraph.cpp" line="58"/>
<source>Minimum</source> <source>Minimum</source>
<translation>Minimum</translation> <translation>Minimum</translation>
</message> </message>
<message> <message>
<location filename="../src/elevationgraph.cpp" line="55"/> <location filename="../src/elevationgraph.cpp" line="56"/>
<source>Maximum</source> <source>Maximum</source>
<translation>Maximum</translation> <translation>Maximum</translation>
</message> </message>
@ -47,282 +47,259 @@
<context> <context>
<name>GUI</name> <name>GUI</name>
<message> <message>
<location filename="../src/gui.cpp" line="125"/> <location filename="../src/gui.cpp" line="133"/>
<source>About Qt</source> <source>About Qt</source>
<translation>O Qt</translation> <translation>O Qt</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="284"/> <location filename="../src/gui.cpp" line="292"/>
<source>GPXSee is distributed under the terms of the GNU General Public License version 3. For more info about GPXSee visit the project homepage at </source> <source>GPXSee is distributed under the terms of the GNU General Public License version 3. For more info about GPXSee visit the project homepage at </source>
<translation>Program GPXSee je distribuován pod podmínkami licence GNU General Public License verze 3. Pro více informací navštivte stránky programu na adrese </translation> <translation>Program GPXSee je distribuován pod podmínkami licence GNU General Public License verze 3. Pro více informací navštivte stránky programu na adrese </translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="314"/> <location filename="../src/gui.cpp" line="322"/>
<source>Open file</source> <source>Open file</source>
<translation>Otevřít soubor</translation> <translation>Otevřít soubor</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="139"/> <location filename="../src/gui.cpp" line="147"/>
<source>Save as</source> <source>Save as</source>
<translation>Uložit jako</translation> <translation>Uložit jako</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="360"/> <location filename="../src/gui.cpp" line="373"/>
<source>Open POI file</source> <source>Open POI file</source>
<translation>Otevřít POI soubor</translation> <translation>Otevřít POI soubor</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="130"/> <location filename="../src/gui.cpp" line="138"/>
<source>Open</source> <source>Open</source>
<translation>Otevřít</translation> <translation>Otevřít</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="115"/> <location filename="../src/gui.cpp" line="123"/>
<source>Quit</source> <source>Quit</source>
<translation>Ukončit</translation> <translation>Ukončit</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="120"/> <location filename="../src/gui.cpp" line="128"/>
<location filename="../src/gui.cpp" line="300"/> <location filename="../src/gui.cpp" line="308"/>
<location filename="../src/gui.cpp" line="301"/> <location filename="../src/gui.cpp" line="309"/>
<source>Keyboard controls</source> <source>Keyboard controls</source>
<translation>Ovládací klávesy</translation> <translation>Ovládací klávesy</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="134"/> <location filename="../src/gui.cpp" line="142"/>
<source>Save</source> <source>Save</source>
<translation>Uložit</translation> <translation>Uložit</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="144"/> <location filename="../src/gui.cpp" line="152"/>
<source>Close</source> <source>Close</source>
<translation>Zavřít</translation> <translation>Zavřít</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="149"/> <location filename="../src/gui.cpp" line="157"/>
<source>Reload</source> <source>Reload</source>
<translation>Znovu načíst</translation> <translation>Znovu načíst</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="233"/> <location filename="../src/gui.cpp" line="241"/>
<source>Show</source> <source>Show</source>
<translation>Zobrazit</translation> <translation>Zobrazit</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="188"/> <location filename="../src/gui.cpp" line="196"/>
<location filename="../src/gui.cpp" line="224"/> <location filename="../src/gui.cpp" line="232"/>
<source>File</source> <source>File</source>
<translation>Soubor</translation> <translation>Soubor</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="156"/> <location filename="../src/gui.cpp" line="164"/>
<source>Load POI file</source> <source>Load POI file</source>
<translation>Nahrát POI soubor</translation> <translation>Nahrát POI soubor</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="159"/> <location filename="../src/gui.cpp" line="167"/>
<source>Show POIs</source> <source>Show POIs</source>
<translation>Zobrazit POI</translation> <translation>Zobrazit POI</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="164"/> <location filename="../src/gui.cpp" line="172"/>
<source>Show map</source> <source>Show map</source>
<translation>Zobrazit mapu</translation> <translation>Zobrazit mapu</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="174"/> <location filename="../src/gui.cpp" line="182"/>
<source>Show graphs</source> <source>Show graphs</source>
<translation>Zobrazovat grafy</translation> <translation>Zobrazovat grafy</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="179"/> <location filename="../src/gui.cpp" line="187"/>
<source>Show toolbars</source> <source>Show toolbars</source>
<translation>Zobrazovat nástrojové lišty</translation> <translation>Zobrazovat nástrojové lišty</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="202"/> <location filename="../src/gui.cpp" line="210"/>
<source>Map</source> <source>Map</source>
<translation>Mapa</translation> <translation>Mapa</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="207"/> <location filename="../src/gui.cpp" line="215"/>
<source>POI</source> <source>POI</source>
<translation>POI</translation> <translation>POI</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="211"/> <location filename="../src/gui.cpp" line="219"/>
<source>Settings</source> <source>Settings</source>
<translation>Nastavení</translation> <translation>Nastavení</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="215"/> <location filename="../src/gui.cpp" line="223"/>
<source>Help</source> <source>Help</source>
<translation>Nápověda</translation> <translation>Nápověda</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="252"/> <location filename="../src/gui.cpp" line="260"/>
<source>Elevation</source> <source>Elevation</source>
<translation>Výška</translation> <translation>Výška</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="253"/> <location filename="../src/gui.cpp" line="261"/>
<source>Speed</source> <source>Speed</source>
<translation>Rychlost</translation> <translation>Rychlost</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="303"/> <location filename="../src/gui.cpp" line="311"/>
<source>Next file</source> <source>Next file</source>
<translation>Následující soubor</translation> <translation>Následující soubor</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="304"/> <location filename="../src/gui.cpp" line="312"/>
<source>Previous file</source> <source>Previous file</source>
<translation>Předchozí soubor</translation> <translation>Předchozí soubor</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="306"/> <location filename="../src/gui.cpp" line="314"/>
<source>Append modifier</source> <source>Append modifier</source>
<translation>Modifikátor nahradit/přidat</translation> <translation>Modifikátor nahradit/přidat</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="416"/> <location filename="../src/gui.cpp" line="364"/>
<location filename="../src/gui.cpp" line="380"/>
<source>Line: %1</source>
<translation>Řádka: %1</translation>
</message>
<message>
<location filename="../src/gui.cpp" line="432"/>
<source>Maximum</source> <source>Maximum</source>
<translation>Maximum</translation> <translation>Maximum</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="418"/> <location filename="../src/gui.cpp" line="434"/>
<source>Minimum</source> <source>Minimum</source>
<translation>Minimum</translation> <translation>Minimum</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="123"/> <location filename="../src/gui.cpp" line="131"/>
<location filename="../src/gui.cpp" line="280"/> <location filename="../src/gui.cpp" line="288"/>
<source>About GPXSee</source> <source>About GPXSee</source>
<translation>O aplikaci GPXSee</translation> <translation>O aplikaci GPXSee</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="282"/> <location filename="../src/gui.cpp" line="290"/>
<source>GPX viewer and analyzer</source> <source>GPX viewer and analyzer</source>
<translation>Prohlížeč a analyzátor GPX</translation> <translation>Prohlížeč a analyzátor GPX</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="409"/> <location filename="../src/gui.cpp" line="425"/>
<source>Distance</source> <source>Distance</source>
<translation>Vzdálenost</translation> <translation>Vzdálenost</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="411"/> <location filename="../src/gui.cpp" line="427"/>
<source>Time</source> <source>Time</source>
<translation>Čas</translation> <translation>Čas</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="412"/> <location filename="../src/gui.cpp" line="428"/>
<source>Ascent</source> <source>Ascent</source>
<translation>Stoupání</translation> <translation>Stoupání</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="413"/> <location filename="../src/gui.cpp" line="429"/>
<location filename="../src/gui.cpp" line="415"/> <location filename="../src/gui.cpp" line="431"/>
<location filename="../src/gui.cpp" line="417"/> <location filename="../src/gui.cpp" line="433"/>
<location filename="../src/gui.cpp" line="419"/> <location filename="../src/gui.cpp" line="435"/>
<source>m</source> <source>m</source>
<translation>m</translation> <translation>m</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="414"/> <location filename="../src/gui.cpp" line="430"/>
<source>Descent</source> <source>Descent</source>
<translation>Klesání</translation> <translation>Klesání</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="510"/> <location filename="../src/gui.cpp" line="526"/>
<source>%1 tracks</source> <source>%1 tracks</source>
<translation>Počet tras: %1</translation> <translation>Počet tras: %1</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="410"/> <location filename="../src/gui.cpp" line="426"/>
<location filename="../src/gui.cpp" line="513"/> <location filename="../src/gui.cpp" line="529"/>
<source>km</source> <source>km</source>
<translation>km</translation> <translation>km</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="352"/> <location filename="../src/gui.cpp" line="366"/>
<location filename="../src/gui.cpp" line="364"/> <location filename="../src/gui.cpp" line="381"/>
<source>Error</source> <source>Error</source>
<translation>Chyba</translation> <translation>Chyba</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="353"/> <location filename="../src/gui.cpp" line="361"/>
<source>Error loading GPX file: <source>Error loading GPX file:
%1</source> %1</source>
<translation>Soubor GPX nelze otevřít: <translation>Soubor GPX nelze otevřít:
%1</translation> %1</translation>
</message> </message>
<message> <message>
<location filename="../src/gui.cpp" line="365"/> <location filename="../src/gui.cpp" line="377"/>
<source>Error loading POI file: <source>Error loading POI file:
%1</source> %1</source>
<translation>Soubor POI nelze otevřít: <translation>Soubor POI nelze otevřít:
%1</translation> %1</translation>
</message> </message>
</context> </context>
<context>
<name>QObject</name>
<message>
<location filename="../src/parser.cpp" line="97"/>
<source>Not a GPX file.</source>
<translation>Neplatný GPX soubor.</translation>
</message>
<message>
<location filename="../src/parser.cpp" line="106"/>
<source>%1
Line %2</source>
<translation>%1
Rádka %2</translation>
</message>
<message>
<location filename="../src/poi.cpp" line="31"/>
<source>Invalid latitude
Line %1</source>
<translation>Neplatná zeměpisná šířka
Rádka %1</translation>
</message>
<message>
<location filename="../src/poi.cpp" line="36"/>
<source>Invalid longitude
Line %1</source>
<translation>Neplatná zeměpisná délka
Rádka %1</translation>
</message>
</context>
<context> <context>
<name>SpeedGraph</name> <name>SpeedGraph</name>
<message> <message>
<location filename="../src/speedgraph.cpp" line="11"/> <location filename="../src/speedgraph.cpp" line="9"/>
<source>Distance</source> <source>Distance</source>
<translation>Vzdálenost</translation> <translation>Vzdálenost</translation>
</message> </message>
<message> <message>
<location filename="../src/speedgraph.cpp" line="12"/> <location filename="../src/speedgraph.cpp" line="10"/>
<source>Speed</source> <source>Speed</source>
<translation>Rychlost</translation> <translation>Rychlost</translation>
</message> </message>
<message> <message>
<location filename="../src/speedgraph.cpp" line="13"/> <location filename="../src/speedgraph.cpp" line="11"/>
<source>km</source> <source>km</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../src/speedgraph.cpp" line="14"/> <location filename="../src/speedgraph.cpp" line="12"/>
<source>km/h</source> <source>km/h</source>
<translation>km/h</translation> <translation>km/h</translation>
</message> </message>
<message> <message>
<location filename="../src/speedgraph.cpp" line="37"/> <location filename="../src/speedgraph.cpp" line="35"/>
<source>Average</source> <source>Average</source>
<translation>Průměr</translation> <translation>Průměr</translation>
</message> </message>
<message> <message>
<location filename="../src/speedgraph.cpp" line="39"/> <location filename="../src/speedgraph.cpp" line="37"/>
<source>Maximum</source> <source>Maximum</source>
<translation>Maximum</translation> <translation>Maximum</translation>
</message> </message>

View File

@ -8,6 +8,7 @@
#define FONT_FAMILY "Arial" #define FONT_FAMILY "Arial"
#define FONT_SIZE 12 #define FONT_SIZE 12
#define POI_DIR "POI"
#define TILES_DIR "tiles" #define TILES_DIR "tiles"
#define MAP_LIST_FILE TILES_DIR"/list.txt" #define MAP_LIST_FILE TILES_DIR"/list.txt"

View File

@ -87,14 +87,17 @@ bool GPX::loadFile(const QString &fileName)
_data.clear(); _data.clear();
_error.clear(); _error.clear();
_errorLine = 0;
if (!file.open(QFile::ReadOnly | QFile::Text)) { if (!file.open(QFile::ReadOnly | QFile::Text)) {
_error = qPrintable(file.errorString()); _error = qPrintable(file.errorString());
return false; return false;
} }
if (!(ret = _parser.loadFile(&file, _data))) if (!(ret = _parser.loadFile(&file, _data))) {
_error = _parser.errorString(); _error = _parser.errorString();
_errorLine = _parser.errorLine();
}
file.close(); file.close();
return ret; return ret;

View File

@ -11,6 +11,7 @@ class GPX
public: public:
bool loadFile(const QString &fileName); bool loadFile(const QString &fileName);
const QString &errorString() const {return _error;} const QString &errorString() const {return _error;}
int errorLine() const {return _errorLine;}
void elevationGraph(QVector<QPointF> &graph) const; void elevationGraph(QVector<QPointF> &graph) const;
void speedGraph(QVector<QPointF> &graph) const; void speedGraph(QVector<QPointF> &graph) const;
@ -23,6 +24,7 @@ private:
Parser _parser; Parser _parser;
QVector<TrackPoint> _data; QVector<TrackPoint> _data;
QString _error; QString _error;
int _errorLine;
}; };
#endif // GPX_H #endif // GPX_H

View File

@ -37,7 +37,7 @@ static QString timeSpan(qreal time)
GUI::GUI() GUI::GUI()
{ {
loadMaps(); loadFiles();
createActions(); createActions();
createMenus(); createMenus();
@ -71,9 +71,20 @@ GUI::GUI()
resize(600, 800); resize(600, 800);
} }
void GUI::loadMaps() void GUI::loadFiles()
{ {
// Maps
_maps = MapList::load(QString("%1/"MAP_LIST_FILE).arg(QDir::homePath())); _maps = MapList::load(QString("%1/"MAP_LIST_FILE).arg(QDir::homePath()));
// POI files
QDir dir(QString("%1/"POI_DIR).arg(QDir::homePath()));
QFileInfoList list = dir.entryInfoList(QStringList(), QDir::Files);
for (int i = 0; i < list.size(); ++i) {
if (!_poi.loadFile(list.at(i).absoluteFilePath()))
fprintf(stderr, "Error loading POI file: %s: %s",
qPrintable(list.at(i).absoluteFilePath()),
qPrintable(_poi.errorString()));
}
} }
void GUI::createMapActions() void GUI::createMapActions()
@ -346,8 +357,13 @@ bool GUI::loadFile(const QString &fileName)
return true; return true;
} else { } else {
QMessageBox::critical(this, tr("Error"), fileName + QString("\n\n") QString error = fileName + QString("\n\n")
+ tr("Error loading GPX file:\n%1").arg(gpx.errorString())); + tr("Error loading GPX file:\n%1").arg(gpx.errorString())
+ QString("\n");
if (gpx.errorLine())
error.append(tr("Line: %1").arg(gpx.errorLine()));
QMessageBox::critical(this, tr("Error"), error);
return false; return false;
} }
} }
@ -358,8 +374,11 @@ void GUI::openPOIFile()
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
if (!_poi.loadFile(fileName)) { if (!_poi.loadFile(fileName)) {
QMessageBox::critical(this, tr("Error"), QString error = tr("Error loading POI file:\n%1")
tr("Error loading POI file:\n%1").arg(_poi.errorString())); .arg(_poi.errorString()) + QString("\n");
if (_poi.errorLine())
error.append(tr("Line: %1").arg(_poi.errorLine()));
QMessageBox::critical(this, tr("Error"), error);
} else { } else {
_showPOIAction->setChecked(true); _showPOIAction->setChecked(true);
_track->loadPOI(_poi); _track->loadPOI(_poi);
@ -527,7 +546,6 @@ void GUI::graphChanged(int index)
_speedGraph->setSliderPosition(_elevationGraph->sliderPosition()); _speedGraph->setSliderPosition(_elevationGraph->sliderPosition());
} }
void GUI::keyPressEvent(QKeyEvent *event) void GUI::keyPressEvent(QKeyEvent *event)
{ {
QString file; QString file;

View File

@ -45,7 +45,7 @@ private slots:
void graphChanged(int); void graphChanged(int);
private: private:
void loadMaps(); void loadFiles();
void createMapActions(); void createMapActions();
void createActions(); void createActions();

View File

@ -1,13 +1,17 @@
#include <QFile> #include <QFile>
#include <QFileInfo>
#include "maplist.h" #include "maplist.h"
QList<Map*> MapList::load(const QString &fileName) QList<Map*> MapList::load(const QString &fileName)
{ {
QFile file(fileName);
QList<Map*> mapList; QList<Map*> mapList;
int ln = 1; QFileInfo fi(fileName);
if (!fi.exists())
return mapList;
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) { if (!file.open(QFile::ReadOnly | QFile::Text)) {
fprintf(stderr, "Error opening map list file: %s: %s\n", fprintf(stderr, "Error opening map list file: %s: %s\n",
@ -15,6 +19,7 @@ QList<Map*> MapList::load(const QString &fileName)
return mapList; return mapList;
} }
int ln = 1;
while (!file.atEnd()) { while (!file.atEnd()) {
QByteArray line = file.readLine(); QByteArray line = file.readLine();
QList<QByteArray> list = line.split('\t'); QList<QByteArray> list = line.split('\t');

View File

@ -92,20 +92,12 @@ bool Parser::parse(QVector<TrackPoint> &data)
if (_reader.name() == "gpx") if (_reader.name() == "gpx")
gpx(data); gpx(data);
else else
_reader.raiseError(QObject::tr("Not a GPX file.")); _reader.raiseError("Not a GPX file.");
} }
return !_reader.error(); return !_reader.error();
} }
QString Parser::errorString() const
{
return QObject::tr("%1\nLine %2")
.arg(_reader.errorString())
.arg(_reader.lineNumber());
}
bool Parser::loadFile(QIODevice *device, QVector<TrackPoint> &data) bool Parser::loadFile(QIODevice *device, QVector<TrackPoint> &data)
{ {
_reader.clear(); _reader.clear();

View File

@ -20,7 +20,8 @@ class Parser
{ {
public: public:
bool loadFile(QIODevice *device, QVector<TrackPoint> &data); bool loadFile(QIODevice *device, QVector<TrackPoint> &data);
QString errorString() const; QString errorString() const {return _reader.errorString();}
int errorLine() const {return _reader.lineNumber();}
private: private:
bool parse(QVector<TrackPoint> &data); bool parse(QVector<TrackPoint> &data);

View File

@ -11,9 +11,10 @@ bool POI::loadFile(const QString &fileName)
{ {
QFile file(fileName); QFile file(fileName);
bool ret; bool ret;
int ln = 1; int ln = 1, cnt = _data.size();
_error.clear(); _error.clear();
_errorLine = 0;
if (!file.open(QFile::ReadOnly | QFile::Text)) { if (!file.open(QFile::ReadOnly | QFile::Text)) {
_error = qPrintable(file.errorString()); _error = qPrintable(file.errorString());
@ -24,18 +25,21 @@ bool POI::loadFile(const QString &fileName)
QByteArray line = file.readLine(); QByteArray line = file.readLine();
QList<QByteArray> list = line.split(','); QList<QByteArray> list = line.split(',');
if (list.size() < 3) { if (list.size() < 3) {
_error = QString("Parse error\nLine %1").arg(ln); _error = "Parse error";
_errorLine = ln;
return false; return false;
} }
qreal lat = list[0].trimmed().toDouble(&ret); qreal lat = list[0].trimmed().toDouble(&ret);
if (!ret) { if (!ret) {
_error = QObject::tr("Invalid latitude\nLine %1").arg(ln); _error = "Invalid latitude";
_errorLine = ln;
return false; return false;
} }
qreal lon = list[1].trimmed().toDouble(&ret); qreal lon = list[1].trimmed().toDouble(&ret);
if (!ret) { if (!ret) {
_error = QObject::tr("Invalid longitude\nLine %1").arg(ln); _error = "Invalid longitude";
_errorLine = ln;
return false; return false;
} }
QByteArray ba = list[2].trimmed(); QByteArray ba = list[2].trimmed();
@ -48,20 +52,20 @@ bool POI::loadFile(const QString &fileName)
ln++; ln++;
} }
for (int i = 0; i < _data.size(); ++i) { for (int i = cnt; i < _data.size(); ++i) {
qreal c[2]; qreal c[2];
c[0] = _data.at(i).coordinates.x(); c[0] = _data.at(i).coordinates.x();
c[1] = _data.at(i).coordinates.y(); c[1] = _data.at(i).coordinates.y();
_tree.Insert(c, c, &_data.at(i)); _tree.Insert(c, c, i);
} }
return true; return true;
} }
static bool cb(const Entry* data, void* context) static bool cb(size_t data, void* context)
{ {
QSet<const Entry*> *set = (QSet<const Entry*>*) context; QSet<int> *set = (QSet<int>*) context;
set->insert(data); set->insert((int)data);
return true; return true;
} }
@ -69,7 +73,7 @@ static bool cb(const Entry* data, void* context)
QVector<Entry> POI::points(const QVector<QPointF> &path) const QVector<Entry> POI::points(const QVector<QPointF> &path) const
{ {
QVector<Entry> ret; QVector<Entry> ret;
QSet<const Entry*> set; QSet<int> set;
qreal min[2], max[2]; qreal min[2], max[2];
for (int i = 0; i < path.count(); i++) { for (int i = 0; i < path.count(); i++) {
@ -80,10 +84,10 @@ QVector<Entry> POI::points(const QVector<QPointF> &path) const
_tree.Search(min, max, cb, &set); _tree.Search(min, max, cb, &set);
} }
QSet<const Entry *>::const_iterator i = set.constBegin(); QSet<int>::const_iterator i = set.constBegin();
while (i != set.constEnd()) { while (i != set.constEnd()) {
ret.append(*(*i)); ret.append(_data.at(*i));
++i; ++i;
} }
return ret; return ret;

View File

@ -28,16 +28,19 @@ class POI
public: public:
bool loadFile(const QString &fileName); bool loadFile(const QString &fileName);
QString errorString() const {return _error;} QString errorString() const {return _error;}
int errorLine() const {return _errorLine;}
QVector<Entry> points(const QVector<QPointF> &path) const; QVector<Entry> points(const QVector<QPointF> &path) const;
void clear(); void clear();
private: private:
typedef RTree<const Entry*, qreal, 2> POITree; typedef RTree<size_t, qreal, 2> POITree;
POITree _tree; POITree _tree;
QVector<Entry> _data; QVector<Entry> _data;
QString _error; QString _error;
int _errorLine;
}; };
#endif // POI_H #endif // POI_H