From 86ed75918fb2d436d212b3cbd4742e7d1c10c179 Mon Sep 17 00:00:00 2001 From: Lubos Date: Tue, 3 Oct 2023 11:29:59 +0200 Subject: [PATCH 1/3] Use distance unit from project's CRS for Stakeout panel --- app/inpututils.cpp | 29 +++++++++++++++++++++++++++++ app/inpututils.h | 1 + app/qml/StakeoutPanel.qml | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/inpututils.cpp b/app/inpututils.cpp index 48a295773..75a9c79b5 100644 --- a/app/inpututils.cpp +++ b/app/inpututils.cpp @@ -160,6 +160,35 @@ QString InputUtils::formatNumber( const double number, int precision ) return QString::number( number, 'f', precision ); } +QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision ) +{ + QgsCoordinateReferenceSystem projectCrs = QgsProject::instance()->crs(); + + switch ( projectCrs.mapUnits() ) + { + case Qgis::DistanceUnit::Meters: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters, 'f', precision ), "m" ); + case Qgis::DistanceUnit::Kilometers: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters / 1000, 'f', precision ), "km" ); + case Qgis::DistanceUnit::Feet: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 3.28084, 'f', precision ), "feet" ); + case Qgis::DistanceUnit::NauticalMiles: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters / 1852, 'f', precision ), "nautical miles" ); + case Qgis::DistanceUnit::Yards: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 1.09361, 'f', precision ), "yards" ); + case Qgis::DistanceUnit::Miles: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters / 1609.34, 'f', precision ), "miles" ); + case Qgis::DistanceUnit::Centimeters: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 100, 'f', precision ), "cm" ); + case Qgis::DistanceUnit::Millimeters: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 1000, 'f', precision ), "mm" ); + case Qgis::DistanceUnit::Inches: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 39.3701, 'f', precision ), "inches" ); + default: + return QString( "%1 %2" ).arg( QString::number( distanceInMeters, 'f', precision ), "m" ); + } +} + QString InputUtils::formatDateTimeDiff( const QDateTime &tMin, const QDateTime &tMax ) { qint64 daysDiff = tMin.daysTo( tMax ); diff --git a/app/inpututils.h b/app/inpututils.h index 78a9d1001..e930a2fff 100644 --- a/app/inpututils.h +++ b/app/inpututils.h @@ -75,6 +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 void setExtentToFeature( const FeatureLayerPair &pair, InputMapSettings *mapSettings, double panelOffsetRatio ); // utility functions to extract information from map settings diff --git a/app/qml/StakeoutPanel.qml b/app/qml/StakeoutPanel.qml index 49dc46fdf..73c9edeee 100644 --- a/app/qml/StakeoutPanel.qml +++ b/app/qml/StakeoutPanel.qml @@ -240,7 +240,7 @@ Item { width: parent.width / 2 titleText: qsTr( "Distance" ) - text: remainingDistance >= 0 ?__inputUtils.formatNumber( remainingDistance, 2 ) + " m" : "N/A m" + text: remainingDistance >= 0 ?__inputUtils.formatDistanceInProjectUnit( remainingDistance, 2 ) : "N/A" titleComponent.wrapMode: Text.NoWrap titleComponent.elide: Text.ElideRight From ae5c2fcaf8f455b45e63d1d6365e7750eeec9f42 Mon Sep 17 00:00:00 2001 From: Lubos Date: Tue, 3 Oct 2023 15:28:25 +0200 Subject: [PATCH 2/3] Use QGis functions for converting distance units --- app/inpututils.cpp | 39 +++++++++++++-------------------------- app/inpututils.h | 2 +- app/qml/StakeoutPanel.qml | 2 +- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/app/inpututils.cpp b/app/inpututils.cpp index 75a9c79b5..cfb472890 100644 --- a/app/inpututils.cpp +++ b/app/inpututils.cpp @@ -160,33 +160,20 @@ QString InputUtils::formatNumber( const double number, int precision ) return QString::number( number, 'f', precision ); } -QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision ) -{ - QgsCoordinateReferenceSystem projectCrs = QgsProject::instance()->crs(); - - switch ( projectCrs.mapUnits() ) - { - case Qgis::DistanceUnit::Meters: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters, 'f', precision ), "m" ); - case Qgis::DistanceUnit::Kilometers: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters / 1000, 'f', precision ), "km" ); - case Qgis::DistanceUnit::Feet: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 3.28084, 'f', precision ), "feet" ); - case Qgis::DistanceUnit::NauticalMiles: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters / 1852, 'f', precision ), "nautical miles" ); - case Qgis::DistanceUnit::Yards: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 1.09361, 'f', precision ), "yards" ); - case Qgis::DistanceUnit::Miles: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters / 1609.34, 'f', precision ), "miles" ); - case Qgis::DistanceUnit::Centimeters: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 100, 'f', precision ), "cm" ); - case Qgis::DistanceUnit::Millimeters: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 1000, 'f', precision ), "mm" ); - case Qgis::DistanceUnit::Inches: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters * 39.3701, 'f', precision ), "inches" ); - default: - return QString( "%1 %2" ).arg( QString::number( distanceInMeters, 'f', precision ), "m" ); +QString InputUtils::formatDistanceInProjectUnit( InputMapSettings *mapSettings, const double distanceInMeters, int precision ) +{ + QgsCoordinateReferenceSystem projectCrs = mapSettings->destinationCrs(); + + if ( !projectCrs.isValid() || projectCrs.mapUnits() == Qgis::DistanceUnit::Unknown ) + { + return QString::number( distanceInMeters, 'f', precision ); } + + double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Meters, projectCrs.mapUnits() ); + double distance = distanceInMeters * factor; + QString abbreviation = QgsUnitTypes::toAbbreviatedString( projectCrs.mapUnits() ); + + return QString( "%1 %2" ).arg( QString::number( distance, 'f', precision ), abbreviation ); } QString InputUtils::formatDateTimeDiff( const QDateTime &tMin, const QDateTime &tMax ) diff --git a/app/inpututils.h b/app/inpututils.h index e930a2fff..01d2dd981 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( InputMapSettings *mapSettings, const double distanceInMeters, int precision = 1 ); Q_INVOKABLE void setExtentToFeature( const FeatureLayerPair &pair, InputMapSettings *mapSettings, double panelOffsetRatio ); // utility functions to extract information from map settings diff --git a/app/qml/StakeoutPanel.qml b/app/qml/StakeoutPanel.qml index 73c9edeee..7bbe74904 100644 --- a/app/qml/StakeoutPanel.qml +++ b/app/qml/StakeoutPanel.qml @@ -240,7 +240,7 @@ Item { width: parent.width / 2 titleText: qsTr( "Distance" ) - text: remainingDistance >= 0 ?__inputUtils.formatDistanceInProjectUnit( remainingDistance, 2 ) : "N/A" + text: remainingDistance >= 0 ?__inputUtils.formatDistanceInProjectUnit( root.mapCanvas.mapSettings, remainingDistance, 2 ) : "N/A" titleComponent.wrapMode: Text.NoWrap titleComponent.elide: Text.ElideRight From 9cf63a2f5d5bb32f128b02d21d361b9c27843d50 Mon Sep 17 00:00:00 2001 From: Lubos Date: Tue, 3 Oct 2023 16:08:37 +0200 Subject: [PATCH 3/3] Use distance unit from QGis projekt in Stakeout panel --- app/inpututils.cpp | 10 +++++----- app/inpututils.h | 2 +- app/qml/StakeoutPanel.qml | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/inpututils.cpp b/app/inpututils.cpp index cfb472890..b5333c2ad 100644 --- a/app/inpututils.cpp +++ b/app/inpututils.cpp @@ -160,18 +160,18 @@ QString InputUtils::formatNumber( const double number, int precision ) return QString::number( number, 'f', precision ); } -QString InputUtils::formatDistanceInProjectUnit( InputMapSettings *mapSettings, const double distanceInMeters, int precision ) +QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision ) { - QgsCoordinateReferenceSystem projectCrs = mapSettings->destinationCrs(); + Qgis::DistanceUnit distUnit = QgsProject::instance()->distanceUnits(); - if ( !projectCrs.isValid() || projectCrs.mapUnits() == Qgis::DistanceUnit::Unknown ) + if ( distUnit == Qgis::DistanceUnit::Unknown ) { return QString::number( distanceInMeters, 'f', precision ); } - double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Meters, projectCrs.mapUnits() ); + double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Meters, distUnit ); double distance = distanceInMeters * factor; - QString abbreviation = QgsUnitTypes::toAbbreviatedString( projectCrs.mapUnits() ); + QString abbreviation = QgsUnitTypes::toAbbreviatedString( distUnit ); return QString( "%1 %2" ).arg( QString::number( distance, 'f', precision ), abbreviation ); } diff --git a/app/inpututils.h b/app/inpututils.h index 01d2dd981..e930a2fff 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( InputMapSettings *mapSettings, const double distanceInMeters, int precision = 1 ); + Q_INVOKABLE QString formatDistanceInProjectUnit( const double distanceInMeters, int precision = 1 ); Q_INVOKABLE void setExtentToFeature( const FeatureLayerPair &pair, InputMapSettings *mapSettings, double panelOffsetRatio ); // utility functions to extract information from map settings diff --git a/app/qml/StakeoutPanel.qml b/app/qml/StakeoutPanel.qml index 7bbe74904..73c9edeee 100644 --- a/app/qml/StakeoutPanel.qml +++ b/app/qml/StakeoutPanel.qml @@ -240,7 +240,7 @@ Item { width: parent.width / 2 titleText: qsTr( "Distance" ) - text: remainingDistance >= 0 ?__inputUtils.formatDistanceInProjectUnit( root.mapCanvas.mapSettings, remainingDistance, 2 ) : "N/A" + text: remainingDistance >= 0 ?__inputUtils.formatDistanceInProjectUnit( remainingDistance, 2 ) : "N/A" titleComponent.wrapMode: Text.NoWrap titleComponent.elide: Text.ElideRight