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

View File

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

View File

@ -314,14 +314,25 @@ void PathView::rescale(qreal scale)
void PathView::setPOI(POI *poi)
{
if (_poi)
disconnect(_poi, SIGNAL(reloadRequired()), this, SLOT(updatePOI()));
_poi = poi;
clearPOI();
loadPOI();
if (_poi)
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)
return;
@ -334,18 +345,6 @@ void PathView::loadPOI()
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)
{
for (int i = 0; i < waypoints.size(); i++) {

View File

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

View File

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

View File

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