Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properly get layer from a QgsProcessingFeatureSourceDefinition #41457

Merged
merged 2 commits into from
Feb 11, 2021

Conversation

troopa81
Copy link
Contributor

@troopa81 troopa81 commented Feb 9, 2021

Fixes #40055 : Complete the QgsProcessingParameters::parameterAsLayer method to deal with QgsProcessingFeatureSourceDefinition

@troopa81 troopa81 added backport release-3_16 Bug Either a bug report, or a bug fix. Let's hope for the latter! Processing Relating to QGIS Processing framework or individual Processing algorithms labels Feb 9, 2021
@github-actions github-actions bot added this to the 3.18.0 milestone Feb 9, 2021
@nyalldawson
Copy link
Collaborator

I'm -1 to this approach -- a feature source isn't the same thing as a vector layer, and we run into risks of api abuse if we allow users to treat them as the same.

I think a better approach would be to create a new QgsProcessingParameters::parameterAsQgsFields method which can take a parameter definition and value and return the corresponding QgsFields associated with the value. So if the parameter definition is a QgsProcessingParameterVectorLayer then it gets the layer's fields, but if the definition is a feature source definition then it evaluates the source and gets the fields from the QgsProcessingFeatureSource instead.

That would give you a means of retrieving the fields for the parent parameter, and you could use them in the widget wrapper to update the widget accordingly.

@troopa81
Copy link
Contributor Author

@nyalldawson I understand your point. parameterAsLayer is indeed not the best place to fix the issue.

I should have been more complete on the issue description. The issue is because of the QgsMapLayerComboBox::value methods which returns a QgsProcessingFeatureSourceDefinition when the only selected feature is checked.

I changed my PR to fix QgsProcessingExpressionWidgetWrapper::setParentLayerWrapperValue. If we agree on this I will add some tests and squash commits then.

That would give you a means of retrieving the fields for the parent parameter, and you could use them in the widget wrapper to update the widget accordingly.

I need the layer object and not just the fields, so the expression builder could have the layer specific variable (layer_id, layer_name...)

@nyalldawson
Copy link
Collaborator

Ok, this approach looks good, thanks!

@cefect
Copy link

cefect commented May 17, 2021

@troopa81 Did this change the 'source' behavior in Q3.16.6?

This was working before in pyqgis:
QgsProcessingFeatureSourceDefinition(source=vlay.id())

and is now throwing 'Could not load source layer' when passed to processing.run

@troopa81
Copy link
Contributor Author

@troopa81 Did this change the 'source' behavior in Q3.16.6?

I'm pretty sure your issue is not related to this PR because it doesn't change the QgsProcessingFeatureSourceDefinition behavior, only the way QgsProcessingParameterExpression deal with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Processing Relating to QGIS Processing framework or individual Processing algorithms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expression widget does not list fields when Selected features only is checked
3 participants