Skip to content

Commit

Permalink
[processing] Add API to set default unit to distance parameters
Browse files Browse the repository at this point in the history
Otherwise the API forces use of a corresponding CRS parameter in
order to determine the relevant distance unit, but this is not
always desirable
  • Loading branch information
nyalldawson committed Dec 17, 2018
1 parent 9e1bf68 commit 5acd7ff
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,24 @@ Returns the name of the parent parameter, or an empty string if this is not set.
Sets the name of the parent layer parameter. Use an empty string if this is not required.

.. seealso:: :py:func:`parentParameterName`
%End

QgsUnitTypes::DistanceUnit defaultUnit() const;
%Docstring
Returns the default distance unit for the parameter.

.. seealso:: :py:func:`setDefaultUnit`

.. versionadded:: 3.4.3
%End

void setDefaultUnit( QgsUnitTypes::DistanceUnit unit );
%Docstring
Sets the default distance ``unit`` for the parameter.

.. seealso:: :py:func:`defaultUnit`

.. versionadded:: 3.4.3
%End

virtual QVariantMap toVariantMap() const;
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4734,12 +4734,14 @@ QVariantMap QgsProcessingParameterDistance::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterNumber::toVariantMap();
map.insert( QStringLiteral( "parent" ), mParentParameterName );
map.insert( QStringLiteral( "default_unit" ), static_cast< int >( mDefaultUnit ) );
return map;
}

bool QgsProcessingParameterDistance::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterNumber::fromVariantMap( map );
mParentParameterName = map.value( QStringLiteral( "parent" ) ).toString();
mDefaultUnit = static_cast< QgsUnitTypes::DistanceUnit>( map.value( QStringLiteral( "default_unit" ), QgsUnitTypes::DistanceUnknownUnit ).toInt() );
return true;
}
17 changes: 17 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -1539,12 +1539,29 @@ class CORE_EXPORT QgsProcessingParameterDistance : public QgsProcessingParameter
*/
void setParentParameterName( const QString &parentParameterName );

/**
* Returns the default distance unit for the parameter.
*
* \see setDefaultUnit()
* \since QGIS 3.4.3
*/
QgsUnitTypes::DistanceUnit defaultUnit() const { return mDefaultUnit; }

/**
* Sets the default distance \a unit for the parameter.
*
* \see defaultUnit()
* \since QGIS 3.4.3
*/
void setDefaultUnit( QgsUnitTypes::DistanceUnit unit ) { mDefaultUnit = unit; }

QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;

private:

QString mParentParameterName;
QgsUnitTypes::DistanceUnit mDefaultUnit = QgsUnitTypes::DistanceUnknownUnit;

};

Expand Down
4 changes: 3 additions & 1 deletion src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingDistanceWidgetWrapper:

QWidget *QgsProcessingDistanceWidgetWrapper::createWidget()
{
const QgsProcessingParameterDistance *distanceDef = static_cast< const QgsProcessingParameterDistance * >( parameterDefinition() );

QWidget *spin = QgsProcessingNumericWidgetWrapper::createWidget();
switch ( type() )
{
Expand Down Expand Up @@ -777,7 +779,7 @@ QWidget *QgsProcessingDistanceWidgetWrapper::createWidget()
mWarningLabel->setLayout( warningLayout );
layout->insertWidget( 4, mWarningLabel );

setUnits( QgsUnitTypes::DistanceUnknownUnit );
setUnits( distanceDef->defaultUnit() );

QWidget *w = new QWidget();
layout->setMargin( 0 );
Expand Down
8 changes: 8 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3274,6 +3274,13 @@ void TestQgsProcessing::parameterDistance()
std::unique_ptr< QgsProcessingParameterDistance > def( new QgsProcessingParameterDistance( "non_optional", QString(), 5, QStringLiteral( "parent" ), false ) );
QCOMPARE( def->parentParameterName(), QStringLiteral( "parent" ) );
def->setParentParameterName( QStringLiteral( "parent2" ) );
QCOMPARE( def->defaultUnit(), QgsUnitTypes::DistanceUnknownUnit );
def->setDefaultUnit( QgsUnitTypes::DistanceFeet );
QCOMPARE( def->defaultUnit(), QgsUnitTypes::DistanceFeet );
std::unique_ptr< QgsProcessingParameterDistance > clone( def->clone() );
QCOMPARE( clone->parentParameterName(), QStringLiteral( "parent2" ) );
QCOMPARE( clone->defaultUnit(), QgsUnitTypes::DistanceFeet );

QCOMPARE( def->parentParameterName(), QStringLiteral( "parent2" ) );
QVERIFY( def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "1.1" ) );
Expand Down Expand Up @@ -3331,6 +3338,7 @@ void TestQgsProcessing::parameterDistance()
QCOMPARE( fromMap.maximum(), def->maximum() );
QCOMPARE( fromMap.dataType(), def->dataType() );
QCOMPARE( fromMap.parentParameterName(), QStringLiteral( "parent2" ) );
QCOMPARE( fromMap.defaultUnit(), QgsUnitTypes::DistanceFeet );
def.reset( dynamic_cast< QgsProcessingParameterDistance *>( QgsProcessingParameters::parameterFromVariantMap( map ) ) );
QVERIFY( dynamic_cast< QgsProcessingParameterDistance *>( def.get() ) );

Expand Down
11 changes: 11 additions & 0 deletions tests/src/gui/testprocessinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,8 @@ void TestProcessingGui::testDistanceWrapper()
// test unit handling
w->show();

QCOMPARE( wrapper.mLabel->text(), QStringLiteral( "<unknown>" ) );

// crs values
wrapper.setUnitParameterValue( QStringLiteral( "EPSG:3111" ) );
QCOMPARE( wrapper.mLabel->text(), QStringLiteral( "meters" ) );
Expand Down Expand Up @@ -1475,6 +1477,15 @@ void TestProcessingGui::testDistanceWrapper()

delete w;

// with default unit
QgsProcessingParameterDistance paramDefaultUnit( QStringLiteral( "num" ), QStringLiteral( "num" ) );
paramDefaultUnit.setDefaultUnit( QgsUnitTypes::DistanceFeet );
QgsProcessingDistanceWidgetWrapper wrapperDefaultUnit( &paramDefaultUnit, QgsProcessingGui::Standard );
w = wrapperDefaultUnit.createWrappedWidget( context );
w->show();
QCOMPARE( wrapperDefaultUnit.mLabel->text(), QStringLiteral( "feet" ) );
delete w;

// with decimals
QgsProcessingParameterDistance paramDecimals( QStringLiteral( "num" ), QStringLiteral( "num" ), QVariant(), QString(), true, 1, 1.02 );
QVariantMap metadata;
Expand Down

0 comments on commit 5acd7ff

Please sign in to comment.