Skip to content
Permalink
Browse files
Avoid showing "0.000 deg" in measure dialog for small degree measurem…
…ents

Instead auto add decimal places so that a usable value is shown
when the unit is degrees and the setting for decimal places isn't
sufficient to show a value.
  • Loading branch information
nyalldawson committed Apr 7, 2017
1 parent 4180837 commit 29fd5ccd868cd4f1c876f98259a48a6bf16d4516
Showing with 30 additions and 1 deletion.
  1. +10 −1 src/app/qgsmeasuredialog.cpp
  2. +20 −0 tests/src/app/testqgsmeasuretool.cpp
@@ -274,7 +274,16 @@ QString QgsMeasureDialog::formatDistance( double distance, bool convertUnits ) c

if ( convertUnits )
distance = convertLength( distance, mDistanceUnits );
return QgsDistanceArea::formatDistance( distance, mDecimalPlaces, mDistanceUnits, baseUnit );

int decimals = mDecimalPlaces;
if ( mDistanceUnits == QgsUnitTypes::DistanceDegrees && distance < 1 )
{
// special handling for degrees - because we can't use smaller units (eg m->mm), we need to make sure there's
// enough decimal places to show a usable measurement value
int minPlaces = qRound( log10( 1.0 / distance ) ) + 1;
decimals = qMax( decimals, minPlaces );
}
return QgsDistanceArea::formatDistance( distance, decimals, mDistanceUnits, baseUnit );
}

QString QgsMeasureDialog::formatArea( double area, bool convertUnits ) const
@@ -43,6 +43,7 @@ class TestQgsMeasureTool : public QObject
void testLengthCalculation();
void testLengthCalculationNoCrs();
void testAreaCalculation();
void degreeDecimalPlaces();

private:
QgisApp *mQgisApp = nullptr;
@@ -227,5 +228,24 @@ void TestQgsMeasureTool::testAreaCalculation()
QGSCOMPARENEAR( measured, expected, 0.001 );
}

void TestQgsMeasureTool::degreeDecimalPlaces()
{
QgsProject::instance()->setDistanceUnits( QgsUnitTypes::DistanceDegrees );

QgsSettings s;
s.setValue( QStringLiteral( "qgis/measure/decimalplaces" ), 3 );

std::unique_ptr< QgsMeasureTool > tool( new QgsMeasureTool( mCanvas, true ) );
std::unique_ptr< QgsMeasureDialog > dlg( new QgsMeasureDialog( tool.get() ) );

QCOMPARE( dlg->formatDistance( 11, false ), QString( "11.000 deg" ) );
QCOMPARE( dlg->formatDistance( 0.005, false ), QString( "0.005 deg" ) );
QCOMPARE( dlg->formatDistance( 0.002, false ), QString( "0.0020 deg" ) );
QCOMPARE( dlg->formatDistance( 0.001, false ), QString( "0.0010 deg" ) );
QCOMPARE( dlg->formatDistance( 0.0001, false ), QString( "0.00010 deg" ) );
QCOMPARE( dlg->formatDistance( 0.00001, false ), QString( "0.000010 deg" ) );

}

QGSTEST_MAIN( TestQgsMeasureTool )
#include "testqgsmeasuretool.moc"

0 comments on commit 29fd5cc

Please sign in to comment.