Skip to content
Permalink
Browse files

Allow setting default value for field parameters in models

  • Loading branch information
nyalldawson committed Sep 22, 2017
1 parent 9caa722 commit 94530a9efc25104ca95083bb44a90307f71a47e0
@@ -1269,6 +1269,8 @@ def createWidget(self):
widget.addItem(self.NOT_SET, None)
for f in fields:
widget.addItem(self.dialog.resolveValueDescription(f), f)
widget.setToolTip(
self.tr('Input parameter, or name of field (separate field names with ; for multiple field parameters)'))
return widget

def postInitialize(self, wrappers):
@@ -1297,6 +1299,8 @@ def refreshItems(self):
else:
self.widget.setLayer(self._layer)
self.widget.setCurrentIndex(0)
if self.param.defaultValue() is not None:
self.setValue(self.param.defaultValue())

def getFields(self):
if self._layer is None:
@@ -1322,7 +1326,7 @@ def setValue(self, value):
options = self.widget.options
selected = []
for i, opt in enumerate(options):
if opt in value:
if opt in value or opt==value:
selected.append(i)
self.widget.setSelectedItems(selected)
else:
@@ -173,6 +173,16 @@ def setupUi(self):
if self.param is not None:
self.multipleCheck.setChecked(self.param.allowMultiple())
self.verticalLayout.addWidget(self.multipleCheck)

self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
self.defaultTextBox = QLineEdit()
self.defaultTextBox.setToolTip(self.tr('Default field name, or ; separated list of field names for multiple field parameters'))
if self.param is not None:
default = self.param.defaultValue()
if default is not None:
self.defaultTextBox.setText(str(default))
self.verticalLayout.addWidget(self.defaultTextBox)

elif self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BAND or \
isinstance(self.param, QgsProcessingParameterBand):
self.verticalLayout.addWidget(QLabel(self.tr('Parent layer')))
@@ -339,7 +349,10 @@ def accept(self):
return
parent = self.parentCombo.currentData()
datatype = self.datatypeCombo.currentData()
self.param = QgsProcessingParameterField(name, description, None, parent, datatype, self.multipleCheck.isChecked())
default = self.defaultTextBox.text()
if not default:
default = None
self.param = QgsProcessingParameterField(name, description, defaultValue=default, parentLayerParameterName=parent, type=datatype, allowMultiple=self.multipleCheck.isChecked())
elif (self.paramType == ModelerParameterDefinitionDialog.PARAMETER_BAND or
isinstance(self.param, QgsProcessingParameterBand)):
if self.parentCombo.currentIndex() < 0:

0 comments on commit 94530a9

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