Skip to content

Commit

Permalink
Merge pull request #2845 from MerginMaps/master_testFormatDistanceInD…
Browse files Browse the repository at this point in the history
…istanceUnit

Added test function testFormatDistanceInDistanceUnit
  • Loading branch information
PeterPetrik authored Oct 11, 2023
2 parents a253c64 + 74b9c8e commit 6feeb57
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
9 changes: 7 additions & 2 deletions app/inpututils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
2 changes: 1 addition & 1 deletion app/inpututils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions app/test/testutilsfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
}
1 change: 1 addition & 0 deletions app/test/testutilsfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

1 comment on commit 6feeb57

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 23.10.467611 just submitted!

Please sign in to comment.