Skip to content

Commit

Permalink
final adjustments for review
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Oct 11, 2024
1 parent 951a63f commit 8a6e67b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 47 deletions.
43 changes: 22 additions & 21 deletions app/inpututils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,45 +168,46 @@ QString InputUtils::formatNumber( const double number, int precision )
return QString::number( number, 'f', precision );
}

QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision, Qgis::DistanceUnit destUnit, QgsProject *activeProject )
QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision, QgsProject *activeProject )
{
Qgis::DistanceUnit distUnit = destUnit;
if ( !activeProject )
return QString();

if ( distUnit == Qgis::DistanceUnit::Unknown )
{
distUnit = activeProject->distanceUnits();
}
return InputUtils::formatDistanceHelper( distanceInMeters, precision, activeProject->distanceUnits() );
}

if ( distUnit == Qgis::DistanceUnit::Unknown )
QString InputUtils::formatDistanceHelper( const double distanceInMeters, int precision, Qgis::DistanceUnit destUnit )
{
if ( destUnit == Qgis::DistanceUnit::Unknown )
{
return QString::number( distanceInMeters, 'f', precision );
destUnit = Qgis::DistanceUnit::Meters;
}

double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Meters, distUnit );
double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Meters, destUnit );
double distance = distanceInMeters * factor;
QString abbreviation = QgsUnitTypes::toAbbreviatedString( distUnit );
QString abbreviation = QgsUnitTypes::toAbbreviatedString( destUnit );

return QString( "%1 %2" ).arg( QString::number( distance, 'f', precision ), abbreviation );
}

QString InputUtils::formatAreaInProjectUnit( const double areaInSquareMeters, int precision, Qgis::AreaUnit destUnit, QgsProject *activeProject )
QString InputUtils::formatAreaInProjectUnit( const double areaInSquareMeters, int precision, QgsProject *activeProject )
{
Qgis::AreaUnit areaUnit = destUnit;
if ( !activeProject )
return QString();

if ( areaUnit == Qgis::AreaUnit::Unknown )
{
areaUnit = activeProject->areaUnits();
}
return InputUtils::formatAreaHelper( areaInSquareMeters, precision, activeProject->areaUnits() );
}

if ( areaUnit == Qgis::AreaUnit::Unknown )
QString InputUtils::formatAreaHelper( const double areaInSquareMeters, int precision, Qgis::AreaUnit destUnit )
{
if ( destUnit == Qgis::AreaUnit::Unknown )
{
return QString::number( areaInSquareMeters, 'f', precision );
destUnit = Qgis::AreaUnit::SquareMeters;
}

double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareMeters, areaUnit );
double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::AreaUnit::SquareMeters, destUnit );
double area = areaInSquareMeters * factor;

QString abbreviation = QgsUnitTypes::toAbbreviatedString( areaUnit );
QString abbreviation = QgsUnitTypes::toAbbreviatedString( destUnit );

return QString( "%1 %2" ).arg( QString::number( area, 'f', precision ), abbreviation );
}
Expand Down
7 changes: 5 additions & 2 deletions app/inpututils.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ 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, Qgis::DistanceUnit destUnit = Qgis::DistanceUnit::Unknown, QgsProject *activeProject = QgsProject::instance() );
Q_INVOKABLE QString formatAreaInProjectUnit( const double areaInSquareMeters, int precision = 1, Qgis::AreaUnit destUnit = Qgis::AreaUnit::Unknown, QgsProject *activeProject = QgsProject::instance() );
Q_INVOKABLE QString formatDistanceInProjectUnit( const double distanceInMeters, int precision, QgsProject *activeProject );
Q_INVOKABLE QString formatAreaInProjectUnit( const double areaInSquareMeters, int precision, QgsProject *activeProject );

static QString formatDistanceHelper( const double distanceInMeters, int precision, Qgis::DistanceUnit destUnit );
static QString formatAreaHelper( const double areaInSquareMeters, int precision, Qgis::AreaUnit destUnit );

Q_INVOKABLE void setExtentToFeature( const FeatureLayerPair &pair, InputMapSettings *mapSettings );

Expand Down
2 changes: 1 addition & 1 deletion app/qml/map/MMMeasurementTools.qml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Item {
mapSettings: root.map.mapSettings
visible: !mapTool.measurementFinalized

text: __inputUtils.formatDistanceInProjectUnit( mapTool.lengthWithGuideline, __activeProject.qgsProject )
text: __inputUtils.formatDistanceInProjectUnit( mapTool.lengthWithGuideline, 1, __activeProject.qgsProject )
canCloseShape: mapTool.canCloseShape

onCloseShapeClicked: root.mapTool.finalizeMeasurement( true )
Expand Down
67 changes: 46 additions & 21 deletions app/test/testutilsfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,68 +856,93 @@ void TestUtilsFunctions::testParsePositionUpdates()
}
}

void TestUtilsFunctions::testFormatDistanceInDistanceUnit()
void TestUtilsFunctions::testFormatDistanceInProjectUnit()
{
QString dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 2, Qgis::DistanceUnit::Meters, QgsProject::instance() );
QgsProject *project = QgsProject::instance();
QVERIFY( project != nullptr );

// Set the project distance units to meters
project->setDistanceUnits( Qgis::DistanceUnit::Meters );

QString dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 2, project );
QVERIFY( dist2str == "1222.23 m" );

dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 1, Qgis::DistanceUnit::Meters, QgsProject::instance() );
dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 1, project );
QVERIFY( dist2str == "1222.2 m" );

dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 0, Qgis::DistanceUnit::Meters, QgsProject::instance() );
dist2str = mUtils->formatDistanceInProjectUnit( 1222.234, 0, project );
QVERIFY( dist2str == "1222 m" );

dist2str = mUtils->formatDistanceInProjectUnit( 700.22, 1, Qgis::DistanceUnit::Meters, QgsProject::instance() );
dist2str = mUtils->formatDistanceInProjectUnit( 700.22, 1, project );
QVERIFY( dist2str == "700.2 m" );

dist2str = mUtils->formatDistanceInProjectUnit( 0.22, 0, Qgis::DistanceUnit::Meters, QgsProject::instance() );
dist2str = mUtils->formatDistanceInProjectUnit( 0.22, 0, project );
QVERIFY( dist2str == "0 m" );

dist2str = mUtils->formatDistanceInProjectUnit( -0.22, 0, Qgis::DistanceUnit::Meters, QgsProject::instance() );
dist2str = mUtils->formatDistanceInProjectUnit( -0.22, 0, project );
QVERIFY( dist2str == "-0 m" );

dist2str = mUtils->formatDistanceInProjectUnit( 1.222234, 2, Qgis::DistanceUnit::Kilometers, QgsProject::instance() );
// Change project distance units to kilometers
project->setDistanceUnits( Qgis::DistanceUnit::Kilometers );

dist2str = mUtils->formatDistanceInProjectUnit( 1.222234, 2, project );
QVERIFY( dist2str == "0.00 km" );

dist2str = mUtils->formatDistanceInProjectUnit( 6000, 1, Qgis::DistanceUnit::Feet, QgsProject::instance() );
// Change project distance units to feet
project->setDistanceUnits( Qgis::DistanceUnit::Feet );

dist2str = mUtils->formatDistanceInProjectUnit( 6000, 1, project );
QVERIFY( dist2str == "19685.0 ft" );

dist2str = mUtils->formatDistanceInProjectUnit( 5, 1, Qgis::DistanceUnit::Feet, QgsProject::instance() );
dist2str = mUtils->formatDistanceInProjectUnit( 5, 1, project );
QVERIFY( dist2str == "16.4 ft" );

dist2str = mUtils->formatDistanceInProjectUnit( 7000, 1, Qgis::DistanceUnit::Feet, QgsProject::instance() );
dist2str = mUtils->formatDistanceInProjectUnit( 7000, 1, project );
QVERIFY( dist2str == "22965.9 ft" );
}


void TestUtilsFunctions::testFormatAreaInProjectUnit()
{
QString area2str = mUtils->formatAreaInProjectUnit( 1500.234, 2, Qgis::AreaUnit::SquareMeters, QgsProject::instance() );
QgsProject *project = QgsProject::instance();
QVERIFY( project != nullptr );

// Set project area units to square meters
project->setAreaUnits( Qgis::AreaUnit::SquareMeters );

QString area2str = mUtils->formatAreaInProjectUnit( 1500.234, 2, project );
QVERIFY( area2str == "1500.23 m²" );

area2str = mUtils->formatAreaInProjectUnit( 1500.234, 1, Qgis::AreaUnit::SquareMeters, QgsProject::instance() );
area2str = mUtils->formatAreaInProjectUnit( 1500.234, 1, project );
QVERIFY( area2str == "1500.2 m²" );

area2str = mUtils->formatAreaInProjectUnit( 1500.234, 0, Qgis::AreaUnit::SquareMeters, QgsProject::instance() );
area2str = mUtils->formatAreaInProjectUnit( 1500.234, 0, project );
QVERIFY( area2str == "1500 m²" );

area2str = mUtils->formatAreaInProjectUnit( 500.22, 1, Qgis::AreaUnit::SquareMeters, QgsProject::instance() );
area2str = mUtils->formatAreaInProjectUnit( 500.22, 1, project );
QVERIFY( area2str == "500.2 m²" );

area2str = mUtils->formatAreaInProjectUnit( 0.22, 0, Qgis::AreaUnit::SquareMeters, QgsProject::instance() );
area2str = mUtils->formatAreaInProjectUnit( 0.22, 0, project );
QVERIFY( area2str == "0 m²" );

area2str = mUtils->formatAreaInProjectUnit( -0.22, 0, Qgis::AreaUnit::SquareMeters, QgsProject::instance() );
area2str = mUtils->formatAreaInProjectUnit( -0.22, 0, project );
QVERIFY( area2str == "-0 m²" );

area2str = mUtils->formatAreaInProjectUnit( 1.222234, 2, Qgis::AreaUnit::SquareKilometers, QgsProject::instance() );
// Change project area units to square kilometers
project->setAreaUnits( Qgis::AreaUnit::SquareKilometers );

area2str = mUtils->formatAreaInProjectUnit( 1.222234, 2, project );
QVERIFY( area2str == "0.00 km²" );

area2str = mUtils->formatAreaInProjectUnit( 6000, 1, Qgis::AreaUnit::Acres, QgsProject::instance() );
// Change project area units to acres
project->setAreaUnits( Qgis::AreaUnit::Acres );

area2str = mUtils->formatAreaInProjectUnit( 6000, 1, project );
QVERIFY( area2str == "1.5 ac" );

area2str = mUtils->formatAreaInProjectUnit( 5, 1, Qgis::AreaUnit::Acres, QgsProject::instance() );
area2str = mUtils->formatAreaInProjectUnit( 5, 1, project );
QVERIFY( area2str == "0.0 ac" );

area2str = mUtils->formatAreaInProjectUnit( 7000, 1, Qgis::AreaUnit::Acres, QgsProject::instance() );
area2str = mUtils->formatAreaInProjectUnit( 7000, 1, project );
QVERIFY( area2str == "1.7 ac" );
}
2 changes: 1 addition & 1 deletion app/test/testutilsfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestUtilsFunctions: public QObject
void testInvalidGeometryWarning();
void testAttribution();
void testParsePositionUpdates();
void testFormatDistanceInDistanceUnit();
void testFormatDistanceInProjectUnit();
void testFormatAreaInProjectUnit();

private:
Expand Down
1 change: 0 additions & 1 deletion gallery/qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,5 @@
<file>../app/qml/dialogs/MMDiscardGeometryChangesDialog.qml</file>
<file>../app/qml/dialogs/MMProviderRemoveReceiverDialog.qml</file>
<file>../app/qml/dialogs/components/MMDialogAdditionalText.qml</file>
<file>../app/qml/dialogs/MMFinishMeasurementDialog.qml</file>
</qresource>
</RCC>

1 comment on commit 8a6e67b

@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 24.10.669811 just submitted!

Please sign in to comment.