Skip to content

Commit

Permalink
[processing] Fix incorrect parameter definition used when evaluating
Browse files Browse the repository at this point in the history
parent layer parameter in the gui for dynamic properties data defined
buttons
  • Loading branch information
nyalldawson committed Feb 11, 2019
1 parent 2a917f4 commit d3bd137
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/gui/processing/qgsprocessingwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/processing/qgsprocessingwidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 26 additions & 5 deletions tests/src/gui/testprocessinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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() );

Expand All @@ -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 );
Expand Down

0 comments on commit d3bd137

Please sign in to comment.