Skip to content
Permalink
Browse files

[processing] Add API to set default unit to distance parameters

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 5acd7ffaefff4a0936e14e1be9da5e8445acd82e
@@ -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;
@@ -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;
}
@@ -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;

};

@@ -739,6 +739,8 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingDistanceWidgetWrapper:

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

QWidget *spin = QgsProcessingNumericWidgetWrapper::createWidget();
switch ( type() )
{
@@ -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 );
@@ -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" ) );
@@ -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() ) );

@@ -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" ) );
@@ -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;

0 comments on commit 5acd7ff

Please sign in to comment.
You can’t perform that action at this time.