Skip to content

Commit

Permalink
[processing] Avoid losing static, user-entered parameter values
Browse files Browse the repository at this point in the history
in model child algorithms

Prior to this change if you edited an algorithm in a model and
tried to enter a preset string for certain parameter types, this
string would get silently discarded on closing the dialog. E.g.
with a dissolve algorithm it was not possible to have a fixed
field name within the model to dissolve by.

This was caused by WidgetWrapper.comboValue  returning the customData
for these manually entered values in the parameters combo box,
yet manually entered values never have custom data.

To work around this we only return the custom data if its
set for the selected item - otherwise we return the text unchanged.
In order to handle the "[not set]" options, a new static custom
data value of WidgetWrapper.NOT_SET_OPTION is added to that
comboValue can detect this and return the appropriate None value.
  • Loading branch information
nyalldawson committed Feb 15, 2018
1 parent 12fcfac commit 46dc2f6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions python/plugins/processing/gui/wrappers.py
Expand Up @@ -140,6 +140,8 @@ def getExtendedLayerName(layer):
class WidgetWrapper(QObject):
widgetValueHasChanged = pyqtSignal(object)

NOT_SET_OPTION = '~~~~!!!!NOT SET!!!!~~~~~~~'

def __init__(self, param, dialog, row=0, col=0, **kwargs):
QObject.__init__(self)
self.param = param
Expand All @@ -160,7 +162,12 @@ def comboValue(self, validator=None, combobox=None):
if validator is not None and not validator(v):
raise InvalidParameterValue()
return v
return combobox.currentData()
if combobox.currentData() == self.NOT_SET_OPTION:
return None
elif combobox.currentData() is not None:
return combobox.currentData()
else:
return combobox.currentText()

def createWidget(self, **kwargs):
pass
Expand Down Expand Up @@ -1303,7 +1310,7 @@ def createWidget(self):
tables = self.dialog.getAvailableValuesOfType((QgsProcessingParameterVectorLayer, QgsProcessingParameterString),
(QgsProcessingOutputVectorLayer, QgsProcessingOutputMapLayer, QgsProcessingOutputFile, QgsProcessingOutputString))
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
self.combo.addItem(self.NOT_SELECTED, None)
self.combo.addItem(self.NOT_SELECTED, self.NOT_SET_OPTION)
for table in tables:
self.combo.addItem(self.dialog.resolveValueDescription(table), table)

Expand Down Expand Up @@ -1395,7 +1402,7 @@ def createWidget(self):
fields = self.dialog.getAvailableValuesOfType([QgsProcessingParameterField, QgsProcessingParameterString],
[QgsProcessingOutputString])
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
widget.addItem(self.NOT_SET, None)
widget.addItem(self.NOT_SET, self.NOT_SET_OPTION)
for f in fields:
widget.addItem(self.dialog.resolveValueDescription(f), f)
widget.setToolTip(
Expand Down Expand Up @@ -1508,7 +1515,7 @@ def createWidget(self):
fields = self.dialog.getAvailableValuesOfType([QgsProcessingParameterBand, QgsProcessingParameterNumber],
[QgsProcessingOutputNumber])
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
widget.addItem(self.NOT_SET, None)
widget.addItem(self.NOT_SET, self.NOT_SET_OPTION)
for f in fields:
widget.addItem(self.dialog.resolveValueDescription(f), f)
return widget
Expand Down

0 comments on commit 46dc2f6

Please sign in to comment.