From 36c827a6ffb345b38d99da794daec390e462f75f Mon Sep 17 00:00:00 2001 From: nnngrach Date: Tue, 6 Dec 2022 18:11:05 +0500 Subject: [PATCH 1/2] favorites_gpx_refactor --- include/OsmAndCore/FavoriteLocationsCollection.h | 4 ++-- include/OsmAndCore/IFavoriteLocation.h | 4 ++-- include/OsmAndCore/IFavoriteLocationsCollection.h | 4 ++-- src/FavoriteLocation.cpp | 8 ++++---- src/FavoriteLocation.h | 8 ++++---- src/FavoriteLocation_P.cpp | 4 ++-- src/FavoriteLocation_P.h | 6 +++--- src/FavoriteLocationsCollection.cpp | 4 ++-- src/FavoriteLocationsCollection_P.cpp | 4 ++-- src/FavoriteLocationsCollection_P.h | 4 ++-- src/FavoriteLocationsGpxCollection_P.cpp | 13 ++++++++++--- 11 files changed, 35 insertions(+), 28 deletions(-) diff --git a/include/OsmAndCore/FavoriteLocationsCollection.h b/include/OsmAndCore/FavoriteLocationsCollection.h index 0bca26dfc..171011020 100644 --- a/include/OsmAndCore/FavoriteLocationsCollection.h +++ b/include/OsmAndCore/FavoriteLocationsCollection.h @@ -32,7 +32,7 @@ namespace OsmAnd virtual std::shared_ptr createFavoriteLocation( const PointI position31, - const QString& elevation = QString(), + const double elevation = 0, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), @@ -46,7 +46,7 @@ namespace OsmAnd const bool calendarEvent = false); virtual std::shared_ptr createFavoriteLocation( const LatLon latLon, - const QString& elevation = QString(), + const double elevation = 0, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), diff --git a/include/OsmAndCore/IFavoriteLocation.h b/include/OsmAndCore/IFavoriteLocation.h index cfa5f2657..3cf85c6db 100644 --- a/include/OsmAndCore/IFavoriteLocation.h +++ b/include/OsmAndCore/IFavoriteLocation.h @@ -38,8 +38,8 @@ namespace OsmAnd virtual bool isHidden() const = 0; virtual void setIsHidden(const bool isHidden) = 0; - virtual QString getElevation() const = 0; - virtual void setElevation(const QString& newElevation) = 0; + virtual double getElevation() const = 0; + virtual void setElevation(const double newElevation) = 0; virtual QString getTime() const = 0; virtual void setTime(const QString& newTime) = 0; diff --git a/include/OsmAndCore/IFavoriteLocationsCollection.h b/include/OsmAndCore/IFavoriteLocationsCollection.h index db5ad2c28..ac674ae25 100644 --- a/include/OsmAndCore/IFavoriteLocationsCollection.h +++ b/include/OsmAndCore/IFavoriteLocationsCollection.h @@ -32,7 +32,7 @@ namespace OsmAnd virtual std::shared_ptr createFavoriteLocation( const PointI position31, - const QString& elevation = QString(), + const double elevation = 0, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), @@ -46,7 +46,7 @@ namespace OsmAnd const bool calendarEvent = false) = 0; virtual std::shared_ptr createFavoriteLocation( const LatLon latLon, - const QString& elevation = QString(), + const double elevation = 0, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), diff --git a/src/FavoriteLocation.cpp b/src/FavoriteLocation.cpp index fac3bbae8..6cfc8f072 100644 --- a/src/FavoriteLocation.cpp +++ b/src/FavoriteLocation.cpp @@ -6,7 +6,7 @@ OsmAnd::FavoriteLocation::FavoriteLocation( const std::shared_ptr< Link >& containerLink_, const PointI position31_, - const QString& elevation_, + const double elevation_, const QString& time_, const QString& pickupTime_, const QString& title_, @@ -41,7 +41,7 @@ OsmAnd::FavoriteLocation::FavoriteLocation( OsmAnd::FavoriteLocation::FavoriteLocation( const std::shared_ptr< Link >& containerLink_, const LatLon latLon_, - const QString& elevation_, + const double elevation_, const QString& time_, const QString& pickupTime_, const QString& title_, @@ -119,12 +119,12 @@ void OsmAnd::FavoriteLocation::setIsHidden(const bool isHidden) _p->setIsHidden(isHidden); } -QString OsmAnd::FavoriteLocation::getElevation() const +double OsmAnd::FavoriteLocation::getElevation() const { return _p->getElevation(); } -void OsmAnd::FavoriteLocation::setElevation(const QString& newElevation) +void OsmAnd::FavoriteLocation::setElevation(const double newElevation) { _p->setElevation(newElevation); } diff --git a/src/FavoriteLocation.h b/src/FavoriteLocation.h index 2298b70a7..433fa3ea0 100644 --- a/src/FavoriteLocation.h +++ b/src/FavoriteLocation.h @@ -29,7 +29,7 @@ namespace OsmAnd FavoriteLocation( const std::shared_ptr< Link >& containerLink, const PointI position31, - const QString& elevation, + const double elevation, const QString& time, const QString& creationTime, const QString& title, @@ -45,7 +45,7 @@ namespace OsmAnd FavoriteLocation( const std::shared_ptr< Link >& containerLink, const LatLon latLon, - const QString& elevation, + const double elevation, const QString& time, const QString& creationTime, const QString& title, @@ -77,8 +77,8 @@ namespace OsmAnd virtual bool isHidden() const; virtual void setIsHidden(const bool isHidden); - virtual QString getElevation() const; - virtual void setElevation(const QString& newElevation); + virtual double getElevation() const; + virtual void setElevation(const double newElevation); virtual QString getTime() const; virtual void setTime(const QString& newTime); diff --git a/src/FavoriteLocation_P.cpp b/src/FavoriteLocation_P.cpp index 4cf3c969e..95400f05b 100644 --- a/src/FavoriteLocation_P.cpp +++ b/src/FavoriteLocation_P.cpp @@ -32,14 +32,14 @@ void OsmAnd::FavoriteLocation_P::setIsHidden(const bool isHidden) link->_p->notifyFavoriteLocationChanged(owner); } -QString OsmAnd::FavoriteLocation_P::getElevation() const +double OsmAnd::FavoriteLocation_P::getElevation() const { QReadLocker scopedLocker(&_lock); return _elevation; } -void OsmAnd::FavoriteLocation_P::setElevation(const QString& newElevation) +void OsmAnd::FavoriteLocation_P::setElevation(const double newElevation) { QWriteLocker scopedLocker(&_lock); diff --git a/src/FavoriteLocation_P.h b/src/FavoriteLocation_P.h index 19114af1c..1f99b6d86 100644 --- a/src/FavoriteLocation_P.h +++ b/src/FavoriteLocation_P.h @@ -33,7 +33,7 @@ namespace OsmAnd mutable QReadWriteLock _lock; bool _isHidden; - QString _elevation; + double _elevation; QString _time; QString _pickupTime; QString _title; @@ -58,8 +58,8 @@ namespace OsmAnd bool isHidden() const; void setIsHidden(const bool isHidden); - QString getElevation() const; - void setElevation(const QString& newElevation); + double getElevation() const; + void setElevation(const double newElevation); QString getTime() const; void setTime(const QString& newTitle); diff --git a/src/FavoriteLocationsCollection.cpp b/src/FavoriteLocationsCollection.cpp index ed737733f..583feec34 100644 --- a/src/FavoriteLocationsCollection.cpp +++ b/src/FavoriteLocationsCollection.cpp @@ -20,7 +20,7 @@ OsmAnd::FavoriteLocationsCollection::~FavoriteLocationsCollection() std::shared_ptr OsmAnd::FavoriteLocationsCollection::createFavoriteLocation( const PointI position31, - const QString& elevation /*= QString::null*/, + const double elevation /*= QString::null*/, const QString& time /*= QString::null*/, const QString& pickupTime /*= QString::null*/, const QString& title /*= QString::null*/, @@ -38,7 +38,7 @@ std::shared_ptr OsmAnd::FavoriteLocationsCollection:: std::shared_ptr OsmAnd::FavoriteLocationsCollection::createFavoriteLocation( const LatLon latLon, - const QString& elevation /*= QString::null*/, + const double elevation /*= QString::null*/, const QString& time /*= QString::null*/, const QString& pickupTime /*= QString::null*/, const QString& title /*= QString::null*/, diff --git a/src/FavoriteLocationsCollection_P.cpp b/src/FavoriteLocationsCollection_P.cpp index eda7d5052..27858dd3e 100644 --- a/src/FavoriteLocationsCollection_P.cpp +++ b/src/FavoriteLocationsCollection_P.cpp @@ -27,7 +27,7 @@ void OsmAnd::FavoriteLocationsCollection_P::notifyFavoriteLocationChanged(Favori std::shared_ptr OsmAnd::FavoriteLocationsCollection_P::createFavoriteLocation( const PointI position, - const QString& elevation, + const double elevation, const QString& time, const QString& pickupTime, const QString& title, @@ -52,7 +52,7 @@ std::shared_ptr OsmAnd::FavoriteLocationsCollection_P std::shared_ptr OsmAnd::FavoriteLocationsCollection_P::createFavoriteLocation( const LatLon latLon, - const QString& elevation, + const double elevation, const QString& time, const QString& pickupTime, const QString& title, diff --git a/src/FavoriteLocationsCollection_P.h b/src/FavoriteLocationsCollection_P.h index 4bd619544..3060f6900 100644 --- a/src/FavoriteLocationsCollection_P.h +++ b/src/FavoriteLocationsCollection_P.h @@ -49,7 +49,7 @@ namespace OsmAnd std::shared_ptr createFavoriteLocation( const PointI position, - const QString& elevation, + const double elevation, const QString& time, const QString& creationTime, const QString& title, @@ -63,7 +63,7 @@ namespace OsmAnd const bool calendarEvent); std::shared_ptr createFavoriteLocation( const LatLon latLon, - const QString& elevation, + const double elevation, const QString& time, const QString& creationTime, const QString& title, diff --git a/src/FavoriteLocationsGpxCollection_P.cpp b/src/FavoriteLocationsGpxCollection_P.cpp index 93a3b7ed7..22edf4c21 100644 --- a/src/FavoriteLocationsGpxCollection_P.cpp +++ b/src/FavoriteLocationsGpxCollection_P.cpp @@ -131,10 +131,10 @@ bool OsmAnd::FavoriteLocationsGpxCollection_P::saveTo(QXmlStreamWriter& writer) writer.writeAttribute(QLatin1String("lat"), QString::number(item->latLon.latitude, 'f', 7)); writer.writeAttribute(QLatin1String("lon"), QString::number(item->latLon.longitude, 'f', 7)); - if (!item->getElevation().isNull() && item->getElevation().toDouble() > 0.) + if (item->getElevation() > 0.) { // - writer.writeTextElement(QLatin1String("ele"), item->getElevation()); + writer.writeTextElement(QLatin1String("ele"), QString::number(item->getElevation(), 'f', 1)); } if (!item->getComment().isNull()) @@ -284,7 +284,14 @@ bool OsmAnd::FavoriteLocationsGpxCollection_P::loadFrom(QXmlStreamReader& xmlRea return false; } - newItem->setElevation(xmlReader.readElementText()); + bool ok = true; + double ele = xmlReader.readElementText().toDouble(&ok); + if (!ok) + { + LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: invalid "); + return false; + } + newItem->setElevation(ele); } else if (tagName == QLatin1String("cmt")) { From dc619c458e7327a09cc2ea97c74beac78c32c635 Mon Sep 17 00:00:00 2001 From: nnngrach Date: Wed, 7 Dec 2022 15:17:47 +0500 Subject: [PATCH 2/2] review fix --- include/OsmAndCore/FavoriteLocationsCollection.h | 4 ++-- include/OsmAndCore/IFavoriteLocationsCollection.h | 4 ++-- src/FavoriteLocationsGpxCollection_P.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/OsmAndCore/FavoriteLocationsCollection.h b/include/OsmAndCore/FavoriteLocationsCollection.h index 171011020..d1f2bc239 100644 --- a/include/OsmAndCore/FavoriteLocationsCollection.h +++ b/include/OsmAndCore/FavoriteLocationsCollection.h @@ -32,7 +32,7 @@ namespace OsmAnd virtual std::shared_ptr createFavoriteLocation( const PointI position31, - const double elevation = 0, + const double elevation = NAN, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), @@ -46,7 +46,7 @@ namespace OsmAnd const bool calendarEvent = false); virtual std::shared_ptr createFavoriteLocation( const LatLon latLon, - const double elevation = 0, + const double elevation = NAN, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), diff --git a/include/OsmAndCore/IFavoriteLocationsCollection.h b/include/OsmAndCore/IFavoriteLocationsCollection.h index ac674ae25..2956400de 100644 --- a/include/OsmAndCore/IFavoriteLocationsCollection.h +++ b/include/OsmAndCore/IFavoriteLocationsCollection.h @@ -32,7 +32,7 @@ namespace OsmAnd virtual std::shared_ptr createFavoriteLocation( const PointI position31, - const double elevation = 0, + const double elevation = NAN, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), @@ -46,7 +46,7 @@ namespace OsmAnd const bool calendarEvent = false) = 0; virtual std::shared_ptr createFavoriteLocation( const LatLon latLon, - const double elevation = 0, + const double elevation = NAN, const QString& time = QString(), const QString& creationTime = QString(), const QString& title = QString(), diff --git a/src/FavoriteLocationsGpxCollection_P.cpp b/src/FavoriteLocationsGpxCollection_P.cpp index 22edf4c21..0b4d4fdfe 100644 --- a/src/FavoriteLocationsGpxCollection_P.cpp +++ b/src/FavoriteLocationsGpxCollection_P.cpp @@ -131,7 +131,7 @@ bool OsmAnd::FavoriteLocationsGpxCollection_P::saveTo(QXmlStreamWriter& writer) writer.writeAttribute(QLatin1String("lat"), QString::number(item->latLon.latitude, 'f', 7)); writer.writeAttribute(QLatin1String("lon"), QString::number(item->latLon.longitude, 'f', 7)); - if (item->getElevation() > 0.) + if (!isnan(item->getElevation())) { // writer.writeTextElement(QLatin1String("ele"), QString::number(item->getElevation(), 'f', 1));