Skip to content

Commit 1c1de3a

Browse files
committed
[processing] Fix optional numeric parameters cannot be cleared
Without this change optional numeric parameters have no way to be cleared in the GUI - they are always forced to have a value Fixes #17471 - but I've noticed that many optional numeric GRASS parameters have a non-null default value. These may need to be investigated and manually changed to None defaults in the description files.
1 parent db85502 commit 1c1de3a

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

python/plugins/processing/gui/NumberInputPanel.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from qgis.core import (QgsExpression,
3737
QgsProcessingParameterNumber,
3838
QgsProcessingOutputNumber,
39+
QgsProcessingParameterDefinition,
3940
QgsProcessingModelChildParameterSource)
4041
from qgis.gui import QgsExpressionBuilderDialog
4142
from processing.tools.dataobjects import createExpressionContext, createContext
@@ -154,20 +155,31 @@ def __init__(self, param):
154155
else:
155156
self.spnValue.setMinimum(-999999999)
156157

158+
self.allowing_null = False
157159
# set default value
160+
if param.flags() & QgsProcessingParameterDefinition.FlagOptional:
161+
self.spnValue.setShowClearButton(True)
162+
min = self.spnValue.minimum() - 1
163+
self.spnValue.setMinimum(min)
164+
self.spnValue.setValue(min)
165+
self.spnValue.setSpecialValueText(self.tr('Not set'))
166+
self.allowing_null = True
167+
158168
if param.defaultValue() is not None:
159169
self.setValue(param.defaultValue())
160-
try:
161-
self.spnValue.setClearValue(float(param.defaultValue()))
162-
except:
163-
pass
170+
if not self.allowing_null:
171+
try:
172+
self.spnValue.setClearValue(float(param.defaultValue()))
173+
except:
174+
pass
164175
elif self.param.minimum() is not None:
165176
try:
166177
self.setValue(float(self.param.minimum()))
167-
self.spnValue.setClearValue(float(self.param.minimum()))
178+
if not self.allowing_null:
179+
self.spnValue.setClearValue(float(self.param.minimum()))
168180
except:
169181
pass
170-
else:
182+
elif not self.allowing_null:
171183
self.setValue(0)
172184
self.spnValue.setClearValue(0)
173185

@@ -179,7 +191,10 @@ def __init__(self, param):
179191
self.spnValue.valueChanged.connect(lambda: self.hasChanged.emit())
180192

181193
def getValue(self):
182-
return self.spnValue.value()
194+
if self.allowing_null and self.spnValue.value() == self.spnValue.minimum():
195+
return None
196+
else:
197+
return self.spnValue.value()
183198

184199
def setValue(self, value):
185200
try:

0 commit comments

Comments
 (0)