Skip to content

Commit b28814c

Browse files
committedFeb 24, 2020
[processing] Fix incorrect error about "cannot load dependant fields"
when a field parameter is linked to a feature source parameter which has the "selected features only" checked
1 parent 9a6cb46 commit b28814c

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed
 

‎src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3329,41 +3329,65 @@ void QgsProcessingFieldWidgetWrapper::setParentLayerWrapperValue( const QgsAbstr
33293329
context = tmpContext.get();
33303330
}
33313331

3332-
const QVariant value = parentWrapper->parameterValue();
3332+
QVariant value = parentWrapper->parameterValue();
3333+
3334+
if ( value.canConvert<QgsProcessingFeatureSourceDefinition>() )
3335+
{
3336+
// input is a QgsProcessingFeatureSourceDefinition - source from it.
3337+
// this is normally discouraged, and algorithms should NEVER do this -- but in this case we can make
3338+
// certain assumptions due to the fact that we're calling this outside of algorithm/model execution and all sources
3339+
// should be real map layers at this stage
3340+
QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( value );
3341+
value = fromVar.source;
3342+
}
33333343
QgsVectorLayer *layer = QgsProcessingParameters::parameterAsVectorLayer( parentWrapper->parameterDefinition(), value, *context );
3334-
if ( !layer || !layer->isValid() )
3344+
if ( layer && layer->isValid() )
33353345
{
3336-
if ( mComboBox )
3337-
mComboBox->setLayer( nullptr );
3338-
else if ( mPanel )
3339-
mPanel->setFields( QgsFields() );
3340-
3341-
if ( value.isValid() && widgetContext().messageBar() )
3346+
// need to grab ownership of layer if required - otherwise layer may be deleted when context
3347+
// goes out of scope
3348+
std::unique_ptr< QgsMapLayer > ownedLayer( context->takeResultLayer( layer->id() ) );
3349+
if ( ownedLayer && ownedLayer->type() == QgsMapLayerType::VectorLayer )
33423350
{
3343-
widgetContext().messageBar()->clearWidgets();
3344-
widgetContext().messageBar()->pushMessage( QString(), QObject::tr( "Could not load selected layer/table. Dependent field could not be populated" ),
3345-
Qgis::Warning, 5 );
3351+
mParentLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
3352+
layer = mParentLayer.get();
3353+
}
3354+
else
3355+
{
3356+
// don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
33463357
}
3347-
return;
3348-
}
33493358

3350-
// need to grab ownership of layer if required - otherwise layer may be deleted when context
3351-
// goes out of scope
3352-
std::unique_ptr< QgsMapLayer > ownedLayer( context->takeResultLayer( layer->id() ) );
3353-
if ( ownedLayer && ownedLayer->type() == QgsMapLayerType::VectorLayer )
3354-
{
3355-
mParentLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
3356-
layer = mParentLayer.get();
3359+
if ( mComboBox )
3360+
mComboBox->setLayer( layer );
3361+
else if ( mPanel )
3362+
mPanel->setFields( filterFields( layer->fields() ) );
33573363
}
33583364
else
33593365
{
3360-
// don't need ownership of this layer - it wasn't owned by context (so e.g. is owned by the project)
3361-
}
3366+
std::unique_ptr< QgsProcessingFeatureSource > source( QgsProcessingParameters::parameterAsSource( parentWrapper->parameterDefinition(), value, *context ) );
3367+
if ( source )
3368+
{
3369+
const QgsFields fields = source->fields();
3370+
if ( mComboBox )
3371+
mComboBox->setFields( fields );
3372+
else if ( mPanel )
3373+
mPanel->setFields( filterFields( fields ) );
3374+
}
3375+
else
3376+
{
3377+
if ( mComboBox )
3378+
mComboBox->setLayer( nullptr );
3379+
else if ( mPanel )
3380+
mPanel->setFields( QgsFields() );
33623381

3363-
if ( mComboBox )
3364-
mComboBox->setLayer( layer );
3365-
else if ( mPanel )
3366-
mPanel->setFields( filterFields( layer->fields() ) );
3382+
if ( value.isValid() && widgetContext().messageBar() )
3383+
{
3384+
widgetContext().messageBar()->clearWidgets();
3385+
widgetContext().messageBar()->pushMessage( QString(), QObject::tr( "Could not load selected layer/table. Dependent field could not be populated" ),
3386+
Qgis::Warning, 5 );
3387+
}
3388+
}
3389+
return;
3390+
}
33673391

33683392
const QgsProcessingParameterField *fieldParam = static_cast< const QgsProcessingParameterField * >( parameterDefinition() );
33693393
if ( mPanel && fieldParam->defaultToAllFields() )

0 commit comments

Comments
 (0)
Please sign in to comment.