Skip to content
Permalink
Browse files

[processing] Fix incorrect parameter definition used when evaluating

parent layer parameter in the gui for dynamic properties data defined
buttons
  • Loading branch information
nyalldawson committed Feb 11, 2019
1 parent 2a917f4 commit d3bd1370d6bff3108df51834d635a11bc42e5a61
@@ -200,7 +200,7 @@ void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<Qg
{
if ( wrapper->parameterDefinition()->name() == parameterDefinition()->dynamicLayerParameterName() )
{
setDynamicParentLayerParameter( wrapper->parameterValue() );
setDynamicParentLayerParameter( wrapper );
connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, &QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged );
break;
}
@@ -259,11 +259,11 @@ void QgsAbstractProcessingParameterWidgetWrapper::parentLayerChanged( QgsAbstrac
{
if ( wrapper )
{
setDynamicParentLayerParameter( wrapper->parameterValue() );
setDynamicParentLayerParameter( wrapper );
}
}

void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter( const QVariant &value )
void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
{
if ( mPropertyButton )
{
@@ -279,7 +279,7 @@ void QgsAbstractProcessingParameterWidgetWrapper::setDynamicParentLayerParameter
context = tmpContext.get();
}

QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( parameterDefinition(), value, *context );
QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( parentWrapper->parameterDefinition(), parentWrapper->parameterValue(), *context );
if ( !layer )
{
mPropertyButton->setVectorLayer( nullptr );
@@ -334,7 +334,7 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject, p
QgsProcessingGui::WidgetType mType = QgsProcessingGui::Standard;
const QgsProcessingParameterDefinition *mParameterDefinition = nullptr;

void setDynamicParentLayerParameter( const QVariant &value );
void setDynamicParentLayerParameter( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper );

QPointer< QWidget > mWidget;
QPointer< QgsPropertyOverrideButton > mPropertyButton;
@@ -408,6 +408,20 @@ class TestProcessingContextGenerator : public QgsProcessingContextGenerator
QgsProcessingContext &mContext;
};


class TestLayerWrapper : public QgsAbstractProcessingParameterWidgetWrapper
{
public:
TestLayerWrapper( const QgsProcessingParameterDefinition *parameter = nullptr )
: QgsAbstractProcessingParameterWidgetWrapper( parameter )
{}
QWidget *createWidget() override { return nullptr; }
void setWidgetValue( const QVariant &val, QgsProcessingContext & ) override { v = val;}
QVariant widgetValue() const override { return v; }

QVariant v;
};

void TestProcessingGui::testWrapperDynamic()
{
const QgsProcessingAlgorithm *centroidAlg = QgsApplication::processingRegistry()->algorithmById( QStringLiteral( "native:centroids" ) );
@@ -441,15 +455,20 @@ void TestProcessingGui::testWrapperDynamic()
QgsVectorLayer *vl = new QgsVectorLayer( QStringLiteral( "LineString" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
p.addMapLayer( vl );

TestLayerWrapper layerWrapper( layerDef );

QVERIFY( !allPartsWrapper.mPropertyButton->vectorLayer() );
allPartsWrapper.setDynamicParentLayerParameter( QVariant::fromValue( vl ) );
layerWrapper.setWidgetValue( QVariant::fromValue( vl ), context );
allPartsWrapper.setDynamicParentLayerParameter( &layerWrapper );
QCOMPARE( allPartsWrapper.mPropertyButton->vectorLayer(), vl );
// should not be owned by wrapper
QVERIFY( !allPartsWrapper.mDynamicLayer.get() );
allPartsWrapper.setDynamicParentLayerParameter( QVariant() );
layerWrapper.setWidgetValue( QVariant(), context );
allPartsWrapper.setDynamicParentLayerParameter( &layerWrapper );
QVERIFY( !allPartsWrapper.mPropertyButton->vectorLayer() );

allPartsWrapper.setDynamicParentLayerParameter( vl->id() );
layerWrapper.setWidgetValue( vl->id(), context );
allPartsWrapper.setDynamicParentLayerParameter( &layerWrapper );
QVERIFY( !allPartsWrapper.mPropertyButton->vectorLayer() );
QVERIFY( !allPartsWrapper.mDynamicLayer.get() );

@@ -458,13 +477,15 @@ void TestProcessingGui::testWrapperDynamic()
TestProcessingContextGenerator generator( context );
allPartsWrapper.registerProcessingContextGenerator( &generator );

allPartsWrapper.setDynamicParentLayerParameter( vl->id() );
layerWrapper.setWidgetValue( vl->id(), context );
allPartsWrapper.setDynamicParentLayerParameter( &layerWrapper );
QCOMPARE( allPartsWrapper.mPropertyButton->vectorLayer(), vl );
QVERIFY( !allPartsWrapper.mDynamicLayer.get() );

// non-project layer
QString pointFileName = TEST_DATA_DIR + QStringLiteral( "/points.shp" );
allPartsWrapper.setDynamicParentLayerParameter( pointFileName );
layerWrapper.setWidgetValue( pointFileName, context );
allPartsWrapper.setDynamicParentLayerParameter( &layerWrapper );
QCOMPARE( allPartsWrapper.mPropertyButton->vectorLayer()->publicSource(), pointFileName );
// must be owned by wrapper, or layer may be deleted while still required by wrapper
QCOMPARE( allPartsWrapper.mDynamicLayer->publicSource(), pointFileName );

0 comments on commit d3bd137

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