Skip to content
Permalink
Browse files

[processing] Check all parameter's values in ModelerParametersDialog

Check that all parameter's values pass the validity check,
even if not returned as QgsProcessingModelChildParameterSource.

In case of list, tests that it is really a QgsProcessingModelChildParameterSource list,
and create a QgsProcessingModelChildParameterSource from the list if it is not the case
(useful for custom parameters that return lists as ParameterFieldsMapping).
  • Loading branch information
arnaud-morvan authored and m-kuhn committed Aug 5, 2017
1 parent b3a9e46 commit b26e6812d439842540369ba67f7b679a4f0c2b89
@@ -62,7 +62,7 @@ def __init__(self, name, description, parentLayerParameterName='INPUT'):
def type(self):
return 'fields_mapping'

def checkValueIsAcceptable(self, value, context):
def checkValueIsAcceptable(self, value, context=None):
if not isinstance(value, list):
return False
for field_def in value:
@@ -68,6 +68,7 @@
from .ExtentFromLayer import ExtentFromLayer
from .ExtractNodes import ExtractNodes
from .ExtractSpecificNodes import ExtractSpecificNodes
from .FieldsMapper import FieldsMapper
from .FixedDistanceBuffer import FixedDistanceBuffer
from .FixGeometry import FixGeometry
from .GeometryByExpression import GeometryByExpression
@@ -160,7 +161,6 @@
# from .SetRasterStyle import SetRasterStyle
# from .SelectByAttributeSum import SelectByAttributeSum
# from .HypsometricCurves import HypsometricCurves
from .FieldsMapper import FieldsMapper
# from .Datasources2Vrt import Datasources2Vrt
# from .OrientedMinimumBoundingBox import OrientedMinimumBoundingBox
# from .DefineProjection import DefineProjection
@@ -54,6 +54,7 @@
from qgis.core import (
QgsApplication,
QgsExpression,
QgsMapLayerProxyModel,
QgsProcessingFeatureSourceDefinition,
QgsProcessingUtils,
QgsProject,
@@ -66,7 +67,7 @@

pluginPath = os.path.dirname(__file__)
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'widgetFieldsMapping.ui'))
os.path.join(pluginPath, 'fieldsmappingpanelbase.ui'))


class FieldsMappingModel(QAbstractTableModel):
@@ -303,7 +304,8 @@ def __init__(self, parent=None):
self.model.modelReset.connect(self.on_model_modelReset)
self.model.rowsInserted.connect(self.on_model_rowsInserted)

self.updateLayerCombo()
self.layerCombo.setAllowEmptyLayer(True)
self.layerCombo.setFilters(QgsMapLayerProxyModel.VectorLayer)

def configure(self):
self.model = FieldsMappingModel()
@@ -458,14 +460,9 @@ def on_model_rowsInserted(self, parent, start, end):
for row in range(start, end + 1):
self.openPersistentEditors(row)

def updateLayerCombo(self):
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
for layer in layers:
self.layerCombo.addItem(layer.name(), layer)

@pyqtSlot(bool, name='on_loadLayerFieldsButton_clicked')
def on_loadLayerFieldsButton_clicked(self, checked=False):
layer = self.layerCombo.currentData()
layer = self.layerCombo.currentLayer()
if layer is None:
return
self.model.loadLayerFields(layer)
@@ -114,7 +114,7 @@
</widget>
</item>
<item>
<widget class="QComboBox" name="layerCombo">
<widget class="QgsMapLayerComboBox" name="layerCombo">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
@@ -143,6 +143,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsMapLayerComboBox</class>
<extends>QComboBox</extends>
<header>qgis.gui</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
@@ -303,21 +303,19 @@ def createAlgorithm(self):
if param.isDestination() or param.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue
val = self.wrappers[param.name()].value()
if (isinstance(val,
QgsProcessingModelChildParameterSource) and val.source() == QgsProcessingModelChildParameterSource.StaticValue and not param.checkValueIsAcceptable(
val.staticValue())) \
or (val is None and not param.flags() & QgsProcessingParameterDefinition.FlagOptional):
self.bar.pushMessage("Error", "Wrong or missing value for parameter '%s'" % param.description(),
level=QgsMessageBar.WARNING)
return None
if val is None:
continue
elif isinstance(val, QgsProcessingModelChildParameterSource):
alg.addParameterSources(param.name(), [val])
elif isinstance(val, list):
alg.addParameterSources(param.name(), val)
else:
alg.addParameterSources(param.name(), [QgsProcessingModelChildParameterSource.fromStaticValue(val)])
if isinstance(val, QgsProcessingModelChildParameterSource):
val = [val]
elif not (isinstance(val, list) and all([isinstance(subval, QgsProcessingModelChildParameterSource) for subval in val])):
val = [QgsProcessingModelChildParameterSource.fromStaticValue(val)]
for subval in val:
if (isinstance(subval, QgsProcessingModelChildParameterSource) and
subval.source() == QgsProcessingModelChildParameterSource.StaticValue and
not param.checkValueIsAcceptable(subval.staticValue())) \
or (subval is None and not param.flags() & QgsProcessingParameterDefinition.FlagOptional):
self.bar.pushMessage("Error", "Wrong or missing value for parameter '%s'" % param.description(),
level=QgsMessageBar.WARNING)
return None
alg.addParameterSources(param.name(), val)

outputs = {}
for dest in self._alg.destinationParameterDefinitions():

0 comments on commit b26e681

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