Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 14, 2017
1 parent b3a9e46 commit b26e681
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/FieldsMapper.py
Expand Up @@ -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:
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions python/plugins/processing/algs/qgis/ui/FieldsMappingPanel.py
Expand Up @@ -54,6 +54,7 @@
from qgis.core import (
QgsApplication,
QgsExpression,
QgsMapLayerProxyModel,
QgsProcessingFeatureSourceDefinition,
QgsProcessingUtils,
QgsProject,
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
28 changes: 13 additions & 15 deletions python/plugins/processing/modeler/ModelerParametersDialog.py
Expand Up @@ -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():
Expand Down

0 comments on commit b26e681

Please sign in to comment.