Skip to content

Commit

Permalink
Avoid showing "0.000 deg" in measure dialog for small degree measurem…
Browse files Browse the repository at this point in the history
…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 29fd5cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/app/qgsmeasuredialog.cpp
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/src/app/testqgsmeasuretool.cpp
Expand Up @@ -43,6 +43,7 @@ class TestQgsMeasureTool : public QObject
void testLengthCalculation();
void testLengthCalculationNoCrs();
void testAreaCalculation();
void degreeDecimalPlaces();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -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.