Skip to content
Permalink
Browse files
QgsUnitTypes::FormatAngle show appropriate number of decimals
  • Loading branch information
domi4484 committed Sep 1, 2021
1 parent 51a945c commit 07e49fdabe5a8f398231494f50498c9905776f2b
@@ -583,17 +583,7 @@ Returns the conversion factor between the specified angular units.
Returns an angle formatted as a friendly string.

:param angle: angle to format
:param decimals: number of decimal places to show
:param unit: unit of angle

:return: formatted angle string
%End

static QString formatAngle( double angle, QgsUnitTypes::AngleUnit unit );
%Docstring
Returns an angle formatted as a friendly string.

:param angle: angle to format
:param decimals: number of decimal places to show. By -1 it show an appropriate number of decimal places
:param unit: unit of angle

:return: formatted angle string
@@ -81,8 +81,7 @@ void QgsPointRotationItem::paint( QPainter *painter )
label.addText( mPixmap.width(),
mPixmap.height() / 2.0 + fm.height() / 2.0,
mFont,
QgsUnitTypes::formatAngle( rotationText,
mRotationUnit ) );
QgsUnitTypes::formatAngle( rotationText, -1, mRotationUnit ) );
painter->setPen( bufferPen );
painter->setBrush( Qt::NoBrush );
painter->drawPath( label );
@@ -2538,87 +2538,52 @@ double QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AngleUnit fromUnit, Qgs
QString QgsUnitTypes::formatAngle( double angle, int decimals, QgsUnitTypes::AngleUnit unit )
{
QString unitLabel;
int decimalPlaces = 2;

switch ( unit )
{
case AngleDegrees:
unitLabel = QObject::tr( "°", "angle" );
decimalPlaces = 0;
break;
case AngleRadians:
unitLabel = QObject::tr( " rad", "angle" );
decimalPlaces = 2;
break;
case AngleGon:
unitLabel = QObject::tr( " gon", "angle" );
decimalPlaces = 0;
break;
case AngleMinutesOfArc:
unitLabel = QObject::tr( "", "angle minutes" );
decimalPlaces = 0;
break;
case AngleSecondsOfArc:
unitLabel = QObject::tr( "", "angle seconds" );
decimalPlaces = 0;
break;
case AngleTurn:
unitLabel = QObject::tr( " tr", "angle turn" );
decimalPlaces = 3;
break;
case AngleMilliradiansSI:
unitLabel = QObject::tr( " millirad", "angular mil SI" );
decimalPlaces = 0;
break;
case AngleMilNATO:
unitLabel = QObject::tr( " mil", "angular mil NATO" );
decimalPlaces = 0;
break;
case AngleUnknownUnit:
break;
}

return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimals ).arg( unitLabel );
}
if ( decimals >= 0 )
decimalPlaces = decimals;

QString QgsUnitTypes::formatAngle( double angle, AngleUnit unit )
{
QString unitLabel;
int decimals = 0;

switch ( unit )
{
case AngleDegrees:
unitLabel = QObject::tr( "°", "angle" );
decimals = 0;
break;
case AngleRadians:
unitLabel = QObject::tr( " rad", "angle" );
decimals = 2;
break;
case AngleGon:
unitLabel = QObject::tr( " gon", "angle" );
decimals = 0;
break;
case AngleMinutesOfArc:
unitLabel = QObject::tr( "", "angle minutes" );
decimals = 0;
break;
case AngleSecondsOfArc:
unitLabel = QObject::tr( "", "angle seconds" );
decimals = 0;
break;
case AngleTurn:
unitLabel = QObject::tr( " tr", "angle turn" );
decimals = 3;
break;
case AngleMilliradiansSI:
unitLabel = QObject::tr( " millirad", "angular mil SI" );
decimals = 0;
break;
case AngleMilNATO:
unitLabel = QObject::tr( " mil", "angular mil NATO" );
decimals = 0;
break;
case AngleUnknownUnit:
break;
}

return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimals ).arg( unitLabel );
return QStringLiteral( "%L1%2" ).arg( angle, 0, 'f', decimalPlaces ).arg( unitLabel );
}


QgsUnitTypes::DistanceValue QgsUnitTypes::scaledDistance( double distance, QgsUnitTypes::DistanceUnit unit, int decimals, bool keepBaseUnit )
{
DistanceValue result;
@@ -557,20 +557,12 @@ class CORE_EXPORT QgsUnitTypes
/**
* Returns an angle formatted as a friendly string.
* \param angle angle to format
* \param decimals number of decimal places to show
* \param decimals number of decimal places to show. By -1 it show an appropriate number of decimal places
* \param unit unit of angle
* \returns formatted angle string
*/
Q_INVOKABLE static QString formatAngle( double angle, int decimals, QgsUnitTypes::AngleUnit unit );

/**
* Returns an angle formatted as a friendly string.
* \param angle angle to format
* \param unit unit of angle
* \returns formatted angle string
*/
Q_INVOKABLE static QString formatAngle( double angle, QgsUnitTypes::AngleUnit unit );

/**
* Will convert a \a distance with a given \a unit to a distance value which is nice to display.
* It will convert between different units (e.g. from meters to kilometers or millimeters)
@@ -1173,6 +1173,16 @@ def testFormatAngle(self):
self.assertEqual(QgsUnitTypes.formatAngle(1, 2, QgsUnitTypes.AngleMilNATO), '1.00 mil')
self.assertEqual(QgsUnitTypes.formatAngle(1, 2, QgsUnitTypes.AngleUnknownUnit), '1.00')

self.assertEqual(QgsUnitTypes.formatAngle(45, -1, QgsUnitTypes.AngleDegrees), '45°')
self.assertEqual(QgsUnitTypes.formatAngle(1, -1, QgsUnitTypes.AngleRadians), '1.00 rad')
self.assertEqual(QgsUnitTypes.formatAngle(1, -1, QgsUnitTypes.AngleGon), '1 gon')
self.assertEqual(QgsUnitTypes.formatAngle(1.11111111, -1, QgsUnitTypes.AngleMinutesOfArc), '1′')
self.assertEqual(QgsUnitTypes.formatAngle(1.99999999, -1, QgsUnitTypes.AngleSecondsOfArc), '2″')
self.assertEqual(QgsUnitTypes.formatAngle(1, -1, QgsUnitTypes.AngleTurn), '1.000 tr')
self.assertEqual(QgsUnitTypes.formatAngle(1, -1, QgsUnitTypes.AngleMilliradiansSI), '1 millirad')
self.assertEqual(QgsUnitTypes.formatAngle(1, -1, QgsUnitTypes.AngleMilNATO), '1 mil')
self.assertEqual(QgsUnitTypes.formatAngle(1, -1, QgsUnitTypes.AngleUnknownUnit), '1.00')

def testEncodeDecodeLayoutUnits(self):
"""Test encoding and decoding layout units"""
units = [QgsUnitTypes.LayoutMillimeters,

0 comments on commit 07e49fd

Please sign in to comment.