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

(cherry picked from commit 5acd7ff)
  • Loading branch information
nyalldawson committed Dec 18, 2018
1 parent 5f8870a commit 7a366a6413bf2690fd9517d8db3ba5c86b2ea7e0
@@ -1528,6 +1528,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;
@@ -4584,12 +4584,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;
}
@@ -1511,12 +1511,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;

};

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

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

QWidget *spin = QgsProcessingNumericWidgetWrapper::createWidget();
switch ( type() )
{
@@ -699,7 +701,7 @@ QWidget *QgsProcessingDistanceWidgetWrapper::createWidget()
mWarningLabel->setLayout( warningLayout );
layout->insertWidget( 4, mWarningLabel );

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

QWidget *w = new QWidget();
layout->setMargin( 0 );
@@ -3251,6 +3251,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" ) );
@@ -3308,6 +3315,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() ) );

@@ -1214,6 +1214,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" ) );
@@ -1293,6 +1295,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 7a366a6

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