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

POI code refactoring

This commit is contained in:
Martin Tůma 2016-10-09 23:46:30 +02:00
parent 89304c0d5c
commit c7d68f924f
6 changed files with 88 additions and 63 deletions

View File

@ -120,13 +120,15 @@ void GUI::loadPOIs()
QDir userDir(USER_POI_DIR); QDir userDir(USER_POI_DIR);
QDir globalDir(GLOBAL_POI_DIR); QDir globalDir(GLOBAL_POI_DIR);
_poi = new POI(this);
if (userDir.exists()) if (userDir.exists())
list = userDir.entryInfoList(QStringList(), QDir::Files); list = userDir.entryInfoList(QStringList(), QDir::Files);
else else
list = globalDir.entryInfoList(QStringList(), QDir::Files); list = globalDir.entryInfoList(QStringList(), QDir::Files);
for (int i = 0; i < list.size(); ++i) for (int i = 0; i < list.size(); ++i)
_poi.loadFile(list.at(i).absoluteFilePath()); _poi->loadFile(list.at(i).absoluteFilePath());
} }
void GUI::createMapActions() void GUI::createMapActions()
@ -154,7 +156,7 @@ void GUI::createPOIFilesActions()
{ {
_poiFilesSM = new QSignalMapper(this); _poiFilesSM = new QSignalMapper(this);
for (int i = 0; i < _poi.files().count(); i++) for (int i = 0; i < _poi->files().count(); i++)
createPOIFileAction(i); createPOIFileAction(i);
connect(_poiFilesSM, SIGNAL(mapped(int)), this, SLOT(poiFileChecked(int))); connect(_poiFilesSM, SIGNAL(mapped(int)), this, SLOT(poiFileChecked(int)));
@ -162,7 +164,7 @@ void GUI::createPOIFilesActions()
QAction *GUI::createPOIFileAction(int index) QAction *GUI::createPOIFileAction(int index)
{ {
QAction *a = new QAction(QFileInfo(_poi.files().at(index)).fileName(), QAction *a = new QAction(QFileInfo(_poi->files().at(index)).fileName(),
this); this);
a->setCheckable(true); a->setCheckable(true);
@ -466,7 +468,7 @@ void GUI::createPathView()
_pathView->setFrameShape(QFrame::NoFrame); _pathView->setFrameShape(QFrame::NoFrame);
#endif // Q_OS_WIN32 #endif // Q_OS_WIN32
_pathView->setPOI(&_poi); _pathView->setPOI(_poi);
} }
void GUI::createGraphTabs() void GUI::createGraphTabs()
@ -674,19 +676,18 @@ bool GUI::openPOIFile(const QString &fileName)
if (fileName.isEmpty()) if (fileName.isEmpty())
return false; return false;
if (!_poi.loadFile(fileName)) { if (!_poi->loadFile(fileName)) {
QString error = tr("Error loading POI file:\n%1") QString error = tr("Error loading POI file:\n%1")
.arg(_poi.errorString()) + QString("\n"); .arg(_poi->errorString()) + QString("\n");
if (_poi.errorLine()) if (_poi->errorLine())
error.append(tr("Line: %1").arg(_poi.errorLine())); error.append(tr("Line: %1").arg(_poi->errorLine()));
QMessageBox::critical(this, tr("Error"), error); QMessageBox::critical(this, tr("Error"), error);
return false; return false;
} else { } else {
_pathView->setPOI(&_poi);
_pathView->showPOI(true); _pathView->showPOI(true);
_showPOIAction->setChecked(true); _showPOIAction->setChecked(true);
QAction *action = createPOIFileAction(_poi.files().indexOf(fileName)); QAction *action = createPOIFileAction(_poi->files().indexOf(fileName));
action->setChecked(true); action->setChecked(true);
_poiFilesMenu->addAction(action); _poiFilesMenu->addAction(action);
@ -702,9 +703,7 @@ void GUI::closePOIFiles()
delete _poiFilesActions[i]; delete _poiFilesActions[i];
_poiFilesActions.clear(); _poiFilesActions.clear();
_pathView->setPOI(0); _poi->clear();
_poi.clear();
} }
void GUI::printFile() void GUI::printFile()
@ -1001,10 +1000,8 @@ void GUI::prevMap()
void GUI::poiFileChecked(int index) void GUI::poiFileChecked(int index)
{ {
_poi.enableFile(_poi.files().at(index), _poi->enableFile(_poi->files().at(index),
_poiFilesActions.at(index)->isChecked()); _poiFilesActions.at(index)->isChecked());
_pathView->setPOI(&_poi);
} }
void GUI::sliderPositionChanged(qreal pos) void GUI::sliderPositionChanged(qreal pos)
@ -1228,7 +1225,7 @@ void GUI::writeSettings()
for (int i = 0, j = 0; i < _poiFilesActions.count(); i++) { for (int i = 0, j = 0; i < _poiFilesActions.count(); i++) {
if (!_poiFilesActions.at(i)->isChecked()) { if (!_poiFilesActions.at(i)->isChecked()) {
settings.setArrayIndex(j++); settings.setArrayIndex(j++);
settings.setValue(DISABLED_POI_FILE_SETTING, _poi.files().at(i)); settings.setValue(DISABLED_POI_FILE_SETTING, _poi->files().at(i));
} }
} }
settings.endArray(); settings.endArray();
@ -1313,10 +1310,10 @@ void GUI::readSettings()
int size = settings.beginReadArray(DISABLED_POI_FILE_SETTINGS_PREFIX); int size = settings.beginReadArray(DISABLED_POI_FILE_SETTINGS_PREFIX);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
settings.setArrayIndex(i); settings.setArrayIndex(i);
int index = _poi.files().indexOf(settings.value( int index = _poi->files().indexOf(settings.value(
DISABLED_POI_FILE_SETTING).toString()); DISABLED_POI_FILE_SETTING).toString());
if (index >= 0) { if (index >= 0) {
_poi.enableFile(_poi.files().at(index), false); _poi->enableFile(_poi->files().at(index), false);
_poiFilesActions.at(index)->setChecked(false); _poiFilesActions.at(index)->setChecked(false);
} }
} }

View File

@ -167,7 +167,7 @@ private:
QTabWidget *_graphTabWidget; QTabWidget *_graphTabWidget;
QList<GraphTab*> _tabs; QList<GraphTab*> _tabs;
POI _poi; POI *_poi;
QList<Map*> _maps; QList<Map*> _maps;
FileBrowser *_browser; FileBrowser *_browser;

View File

@ -314,14 +314,25 @@ void PathView::rescale(qreal scale)
void PathView::setPOI(POI *poi) void PathView::setPOI(POI *poi)
{ {
if (_poi)
disconnect(_poi, SIGNAL(reloadRequired()), this, SLOT(updatePOI()));
_poi = poi; _poi = poi;
clearPOI(); if (_poi)
loadPOI(); connect(_poi, SIGNAL(reloadRequired()), this, SLOT(updatePOI()));
} }
void PathView::loadPOI() void PathView::updatePOI()
{ {
QHash<Waypoint, WaypointItem*>::const_iterator it;
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
_scene->removeItem(it.value());
delete it.value();
}
_pois.clear();
if (!_poi) if (!_poi)
return; return;
@ -334,18 +345,6 @@ void PathView::loadPOI()
updatePOIVisibility(); updatePOIVisibility();
} }
void PathView::clearPOI()
{
QHash<Waypoint, WaypointItem*>::const_iterator it;
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
_scene->removeItem(it.value());
delete it.value();
}
_pois.clear();
}
void PathView::addPOI(const QVector<Waypoint> &waypoints) void PathView::addPOI(const QVector<Waypoint> &waypoints)
{ {
for (int i = 0; i < waypoints.size(); i++) { for (int i = 0; i < waypoints.size(); i++) {

View File

@ -54,6 +54,9 @@ public slots:
void showWaypoints(bool show); void showWaypoints(bool show);
void showRouteWaypoints(bool show); void showRouteWaypoints(bool show);
private slots:
void updatePOI();
private: private:
PathItem *addTrack(const Track &track); PathItem *addTrack(const Track &track);
PathItem *addRoute(const Route &route); PathItem *addRoute(const Route &route);

View File

@ -8,6 +8,12 @@
#include "poi.h" #include "poi.h"
POI::POI(QObject *parent) : QObject(parent)
{
_errorLine = 0;
_radius = 0.01;
}
bool POI::loadFile(const QString &fileName) bool POI::loadFile(const QString &fileName)
{ {
QString error; QString error;
@ -17,14 +23,17 @@ bool POI::loadFile(const QString &fileName)
_errorLine = 0; _errorLine = 0;
if (loadCSVFile(fileName)) if (loadCSVFile(fileName)) {
emit reloadRequired();
return true; return true;
else { } else {
error = _error; error = _error;
errorLine = _errorLine; errorLine = _errorLine;
} }
if (loadGPXFile(fileName)) if (loadGPXFile(fileName)) {
emit reloadRequired();
return true; return true;
}
fprintf(stderr, "Error loading POI file: %s:\n", qPrintable(fileName)); fprintf(stderr, "Error loading POI file: %s:\n", qPrintable(fileName));
fprintf(stderr, "CSV: line %d: %s\n", errorLine, qPrintable(error)); fprintf(stderr, "CSV: line %d: %s\n", errorLine, qPrintable(error));
@ -145,7 +154,7 @@ static bool cb(size_t data, void* context)
return true; return true;
} }
QVector<Waypoint> POI::points(const PathItem *path, qreal radius) const QVector<Waypoint> POI::points(const PathItem *path) const
{ {
QVector<Waypoint> ret; QVector<Waypoint> ret;
QSet<int> set; QSet<int> set;
@ -154,10 +163,10 @@ QVector<Waypoint> POI::points(const PathItem *path, qreal radius) const
for (int i = 0; i < pp.elementCount(); i++) { for (int i = 0; i < pp.elementCount(); i++) {
QPointF p = mercator2ll(pp.elementAt(i)); QPointF p = mercator2ll(pp.elementAt(i));
min[0] = p.x() - radius; min[0] = p.x() - _radius;
min[1] = -p.y() - radius; min[1] = -p.y() - _radius;
max[0] = p.x() + radius; max[0] = p.x() + _radius;
max[1] = -p.y() + radius; max[1] = -p.y() + _radius;
_tree.Search(min, max, cb, &set); _tree.Search(min, max, cb, &set);
} }
@ -170,7 +179,7 @@ QVector<Waypoint> POI::points(const PathItem *path, qreal radius) const
return ret; return ret;
} }
QVector<Waypoint> POI::points(const QList<WaypointItem*> &list, qreal radius) QVector<Waypoint> POI::points(const QList<WaypointItem*> &list)
const const
{ {
QVector<Waypoint> ret; QVector<Waypoint> ret;
@ -179,10 +188,10 @@ QVector<Waypoint> POI::points(const QList<WaypointItem*> &list, qreal radius)
for (int i = 0; i < list.count(); i++) { for (int i = 0; i < list.count(); i++) {
const QPointF &p = list.at(i)->waypoint().coordinates(); const QPointF &p = list.at(i)->waypoint().coordinates();
min[0] = p.x() - radius; min[0] = p.x() - _radius;
min[1] = p.y() - radius; min[1] = p.y() - _radius;
max[0] = p.x() + radius; max[0] = p.x() + _radius;
max[1] = p.y() + radius; max[1] = p.y() + _radius;
_tree.Search(min, max, cb, &set); _tree.Search(min, max, cb, &set);
} }
@ -195,7 +204,7 @@ QVector<Waypoint> POI::points(const QList<WaypointItem*> &list, qreal radius)
return ret; return ret;
} }
QVector<Waypoint> POI::points(const QList<Waypoint> &list, qreal radius) const QVector<Waypoint> POI::points(const QList<Waypoint> &list) const
{ {
QVector<Waypoint> ret; QVector<Waypoint> ret;
QSet<int> set; QSet<int> set;
@ -203,10 +212,10 @@ QVector<Waypoint> POI::points(const QList<Waypoint> &list, qreal radius) const
for (int i = 0; i < list.count(); i++) { for (int i = 0; i < list.count(); i++) {
const QPointF &p = list.at(i).coordinates(); const QPointF &p = list.at(i).coordinates();
min[0] = p.x() - radius; min[0] = p.x() - _radius;
min[1] = p.y() - radius; min[1] = p.y() - _radius;
max[0] = p.x() + radius; max[0] = p.x() + _radius;
max[1] = p.y() + radius; max[1] = p.y() + _radius;
_tree.Search(min, max, cb, &set); _tree.Search(min, max, cb, &set);
} }
@ -241,6 +250,8 @@ void POI::enableFile(const QString &fileName, bool enable)
_tree.Insert(c, c, j); _tree.Insert(c, c, j);
} }
} }
emit reloadRequired();
} }
void POI::clear() void POI::clear()
@ -249,4 +260,13 @@ void POI::clear()
_data.clear(); _data.clear();
_files.clear(); _files.clear();
_indexes.clear(); _indexes.clear();
emit reloadRequired();
}
void POI::setRadius(qreal radius)
{
_radius = radius;
emit reloadRequired();
} }

View File

@ -11,27 +11,31 @@
class PathItem; class PathItem;
class WaypointItem; class WaypointItem;
#define POI_RADIUS 0.01 class POI : public QObject
class POI
{ {
Q_OBJECT
public: public:
POI() : _errorLine(0) {} POI(QObject *parent = 0);
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;} int errorLine() const {return _errorLine;}
QVector<Waypoint> points(const PathItem *path, qreal radius() const {return _radius;}
qreal radius = POI_RADIUS) const; void setRadius(qreal radius);
QVector<Waypoint> points(const QList<WaypointItem*> &list,
qreal radius = POI_RADIUS) const; QVector<Waypoint> points(const PathItem *path) const;
QVector<Waypoint> points(const QList<Waypoint> &list, QVector<Waypoint> points(const QList<WaypointItem*> &list) const;
qreal radius = POI_RADIUS) const; QVector<Waypoint> points(const QList<Waypoint> &list) const;
const QStringList &files() const {return _files;} const QStringList &files() const {return _files;}
void enableFile(const QString &fileName, bool enable); void enableFile(const QString &fileName, bool enable);
void clear(); void clear();
signals:
void reloadRequired();
private: private:
typedef RTree<size_t, qreal, 2> POITree; typedef RTree<size_t, qreal, 2> POITree;
typedef struct { typedef struct {
@ -48,6 +52,8 @@ private:
QStringList _files; QStringList _files;
QList<FileIndex> _indexes; QList<FileIndex> _indexes;
qreal _radius;
QString _error; QString _error;
int _errorLine; int _errorLine;
}; };