diff --git a/app/inpututils.cpp b/app/inpututils.cpp index b5333c2ad..0a65a8119 100644 --- a/app/inpututils.cpp +++ b/app/inpututils.cpp @@ -160,9 +160,14 @@ QString InputUtils::formatNumber( const double number, int precision ) return QString::number( number, 'f', precision ); } -QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision ) +QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision, Qgis::DistanceUnit destUnit ) { - Qgis::DistanceUnit distUnit = QgsProject::instance()->distanceUnits(); + Qgis::DistanceUnit distUnit = destUnit; + + if ( distUnit == Qgis::DistanceUnit::Unknown ) + { + distUnit = QgsProject::instance()->distanceUnits(); + } if ( distUnit == Qgis::DistanceUnit::Unknown ) { diff --git a/app/inpututils.h b/app/inpututils.h index e930a2fff..342972d6f 100644 --- a/app/inpututils.h +++ b/app/inpututils.h @@ -75,7 +75,7 @@ class InputUtils: public QObject Q_INVOKABLE QString getFileName( const QString &filePath ); Q_INVOKABLE QString formatProjectName( const QString &fullProjectName ); Q_INVOKABLE QString formatNumber( const double number, int precision = 1 ); - Q_INVOKABLE QString formatDistanceInProjectUnit( const double distanceInMeters, int precision = 1 ); + Q_INVOKABLE QString formatDistanceInProjectUnit( const double distanceInMeters, int precision = 1, Qgis::DistanceUnit destUnit = Qgis::DistanceUnit::Unknown ); Q_INVOKABLE void setExtentToFeature( const FeatureLayerPair &pair, InputMapSettings *mapSettings, double panelOffsetRatio ); // utility functions to extract information from map settings diff --git a/app/test/testutilsfunctions.cpp b/app/test/testutilsfunctions.cpp index 74cbc222e..f737751f4 100644 --- a/app/test/testutilsfunctions.cpp +++ b/app/test/testutilsfunctions.cpp @@ -822,3 +822,36 @@ void TestUtilsFunctions::testParsePositionUpdates() } } } + +void TestUtilsFunctions::testFormatDistanceInDistanceUnit() +{ + QString dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 2, Qgis::DistanceUnit::Meters ); + QVERIFY( dist2str == "1222.23 m" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 1, Qgis::DistanceUnit::Meters ); + QVERIFY( dist2str == "1222.2 m" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 0, Qgis::DistanceUnit::Meters ); + QVERIFY( dist2str == "1222 m" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 700.22, 1, Qgis::DistanceUnit::Meters ); + QVERIFY( dist2str == "700.2 m" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 0.22, 0, Qgis::DistanceUnit::Meters ); + QVERIFY( dist2str == "0 m" ); + + dist2str = mUtils->formatDistanceInProjectUnit( -0.22, 0, Qgis::DistanceUnit::Meters ); + QVERIFY( dist2str == "-0 m" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 1.222234, 2, Qgis::DistanceUnit::Kilometers ); + QVERIFY( dist2str == "0.00 km" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 6000, 1, Qgis::DistanceUnit::Feet ); + QVERIFY( dist2str == "19685.0 ft" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 5, 1, Qgis::DistanceUnit::Feet ); + QVERIFY( dist2str == "16.4 ft" ); + + dist2str = mUtils->formatDistanceInProjectUnit( 7000, 1, Qgis::DistanceUnit::Feet ); + QVERIFY( dist2str == "22965.9 ft" ); +} diff --git a/app/test/testutilsfunctions.h b/app/test/testutilsfunctions.h index 41bd38ab6..4880a5956 100644 --- a/app/test/testutilsfunctions.h +++ b/app/test/testutilsfunctions.h @@ -47,6 +47,7 @@ class TestUtilsFunctions: public QObject void testInvalidGeometryWarning(); void testAttribution(); void testParsePositionUpdates(); + void testFormatDistanceInDistanceUnit(); private: void testFormatDuration( const QDateTime &t0, qint64 diffSecs, const QString &expectedResult );