From c1fe78ead6a2621694db67a95335ad34217eaba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubo=C5=A1?= Date: Thu, 5 Oct 2023 15:37:23 +0200 Subject: [PATCH] Use distance unit from project's CRS for Stakeout panel (#2826) * Use distance unit from project's CRS for Stakeout panel * Use QGis functions for converting distance units * Use distance unit from QGis projekt in Stakeout panel --- app/inpututils.cpp | 16 ++++++++++++++++ app/inpututils.h | 1 + app/qml/StakeoutPanel.qml | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/inpututils.cpp b/app/inpututils.cpp index 48a295773..b5333c2ad 100644 --- a/app/inpututils.cpp +++ b/app/inpututils.cpp @@ -160,6 +160,22 @@ QString InputUtils::formatNumber( const double number, int precision ) return QString::number( number, 'f', precision ); } +QString InputUtils::formatDistanceInProjectUnit( const double distanceInMeters, int precision ) +{ + Qgis::DistanceUnit distUnit = QgsProject::instance()->distanceUnits(); + + if ( distUnit == Qgis::DistanceUnit::Unknown ) + { + return QString::number( distanceInMeters, 'f', precision ); + } + + double factor = QgsUnitTypes::fromUnitToUnitFactor( Qgis::DistanceUnit::Meters, distUnit ); + double distance = distanceInMeters * factor; + QString abbreviation = QgsUnitTypes::toAbbreviatedString( distUnit ); + + return QString( "%1 %2" ).arg( QString::number( distance, 'f', precision ), abbreviation ); +} + 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