Fix setting number inputs to algorithms in modeler

This commit is contained in:
Nyall Dawson 2017-06-26 16:06:07 +10:00
parent 53e9538ad3
commit 11d35821c0
2 changed files with 48 additions and 28 deletions

View File

@ -39,6 +39,7 @@ from qgis.core import (QgsExpression,
QgsProcessingOutputVectorLayer, QgsProcessingOutputVectorLayer,
QgsProcessingOutputRasterLayer, QgsProcessingOutputRasterLayer,
QgsProcessingParameterFeatureSource, QgsProcessingParameterFeatureSource,
QgsProcessingModelAlgorithm,
QgsProcessingParameterRasterLayer) QgsProcessingParameterRasterLayer)
from qgis.gui import QgsExpressionBuilderDialog from qgis.gui import QgsExpressionBuilderDialog
from processing.modeler.ModelerAlgorithm import ValueFromInput, ValueFromOutput, CompoundValue from processing.modeler.ModelerAlgorithm import ValueFromInput, ValueFromOutput, CompoundValue
@ -82,28 +83,30 @@ class ModellerNumberInputPanel(BASE, WIDGET):
values = self.modelParametersDialog.getAvailableValuesOfType(QgsProcessingParameterNumber, QgsProcessingOutputNumber) values = self.modelParametersDialog.getAvailableValuesOfType(QgsProcessingParameterNumber, QgsProcessingOutputNumber)
variables = {} variables = {}
for value in values: for value in values:
if isinstance(value, ValueFromInput): if isinstance(value, QgsProcessingModelAlgorithm.ChildParameterSource):
name = value.name if value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ModelParameter:
element = self.modelParametersDialog.model.inputs[name].param name = value.parameterName()
desc = element.description element = self.modelParametersDialog.model.parameterDefinition(name)
else: desc = element.description()
name = "%s_%s" % (value.alg, value.output) elif value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ChildOutput:
alg = self.modelParametersDialog.model.algs[value.alg] name = "%s_%s" % (value.outputChildId(), value.outputName())
out = alg.algorithm.outputDefinition(value.output) alg = self.modelParametersDialog.model.childAlgorithm(value.outputChildId())
desc = self.tr("Output '{0}' from algorithm '{1}'").format(out.description(), alg.description) out = alg.algorithm().outputDefinition(value.outputName())
desc = self.tr("Output '{0}' from algorithm '{1}'").format(out.description(), alg.description())
variables[name] = desc variables[name] = desc
values = self.modelParametersDialog.getAvailableValuesOfType([QgsProcessingParameterFeatureSource, QgsProcessingParameterRasterLayer], values = self.modelParametersDialog.getAvailableValuesOfType([QgsProcessingParameterFeatureSource, QgsProcessingParameterRasterLayer],
[QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer]) [QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer])
for value in values: for value in values:
if isinstance(value, ValueFromInput): if isinstance(value, QgsProcessingModelAlgorithm.ChildParameterSource):
name = value.name if value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ModelParameter:
element = self.modelParametersDialog.model.inputs[name].param name = value.parameterName()
desc = element.description element = self.modelParametersDialog.model.parameterDefinition(name)
else: desc = element.description()
name = "%s_%s" % (value.alg, value.output) elif value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ChildOutput:
alg = self.modelParametersDialog.model.algs[value.alg] name = "%s_%s" % (value.outputChildId(), value.outputName())
element = alg.algorithm.outputDefinition(value.output) alg = self.modelParametersDialog.model.childAlgorithm(value.outputChildId())
desc = self.tr("Output '{0}' from algorithm '{1}'").format(element.description(), alg.description) out = alg.algorithm().outputDefinition(value.outputName())
desc = self.tr("Output '{0}' from algorithm '{1}'").format(out.description(), alg.description())
variables['%s_minx' % name] = self.tr("Minimum X of {0}").format(desc) variables['%s_minx' % name] = self.tr("Minimum X of {0}").format(desc)
variables['%s_miny' % name] = self.tr("Minimum Y of {0}").format(desc) variables['%s_miny' % name] = self.tr("Minimum Y of {0}").format(desc)
variables['%s_maxx' % name] = self.tr("Maximum X of {0}").format(desc) variables['%s_maxx' % name] = self.tr("Maximum X of {0}").format(desc)
@ -125,21 +128,41 @@ class ModellerNumberInputPanel(BASE, WIDGET):
def getValue(self): def getValue(self):
value = self.leText.text() value = self.leText.text()
values = [] values = []
#for param in self.modelParametersDialog.model.parameterDefinitions():
# if isinstance(param, QgsProcessingParameterNumber):
# if "@" + param.name() in value:
# values.append(ValueFromInput(param.name()))
#for alg in list(self.modelParametersDialog.model.algs.values()):
# for out in alg.algorithm.outputDefinitions():
# if isinstance(out, QgsProcessingOutputNumber) and "@%s_%s" % (alg.modeler_name, out.name) in value:
# values.append(ValueFromOutput(alg.modeler_name, out.name()))
for param in self.modelParametersDialog.model.parameterDefinitions(): for param in self.modelParametersDialog.model.parameterDefinitions():
if isinstance(param, QgsProcessingParameterNumber): if isinstance(param, QgsProcessingParameterNumber):
if "@" + param.name() in value: if "@" + param.name() == value:
values.append(ValueFromInput(param.name())) return QgsProcessingModelAlgorithm.ChildParameterSource.fromModelParameter(param.name())
for alg in list(self.modelParametersDialog.model.algs.values()):
for out in alg.algorithm.outputDefinitions(): for alg in list(self.modelParametersDialog.model.childAlgorithms().values()):
if isinstance(out, QgsProcessingOutputNumber) and "@%s_%s" % (alg.modeler_name, out.name) in value: for out in alg.algorithm().outputDefinitions():
values.append(ValueFromOutput(alg.modeler_name, out.name())) if isinstance(out, QgsProcessingOutputNumber) and "@%s_%s" % (alg.childId(), out.name()) == value:
return QgsProcessingModelAlgorithm.ChildParameterSource.fromChildOutput(alg.childId(), out.outputName())
if values: if values:
return CompoundValue(values, value) return CompoundValue(values, value)
else: else:
return value return value
def setValue(self, value): def setValue(self, value):
self.leText.setText(str(value)) if isinstance(value, QgsProcessingModelAlgorithm.ChildParameterSource):
if value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ModelParameter:
self.leText.setText('@' + value.parameterName())
elif value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ChildOutput:
name = "%s_%s" % (value.outputChildId(), value.outputName())
self.leText.setText(name)
else:
self.leText.setText(str(value.staticValue()))
else:
self.leText.setText(str(value))
class NumberInputPanel(NUMBER_BASE, NUMBER_WIDGET): class NumberInputPanel(NUMBER_BASE, NUMBER_WIDGET):

View File

@ -318,9 +318,6 @@ class ModelerParametersDialog(QDialog):
if (isinstance(val, if (isinstance(val,
QgsProcessingModelAlgorithm.ChildParameterSource) and val.source() == QgsProcessingModelAlgorithm.ChildParameterSource.StaticValue and not param.checkValueIsAcceptable( QgsProcessingModelAlgorithm.ChildParameterSource) and val.source() == QgsProcessingModelAlgorithm.ChildParameterSource.StaticValue and not param.checkValueIsAcceptable(
val.staticValue())) \ val.staticValue())) \
or (not isinstance(val,
QgsProcessingModelAlgorithm.ChildParameterSource) and not param.checkValueIsAcceptable(
val))\
or (val is None and not param.flags() & QgsProcessingParameterDefinition.FlagOptional): or (val is None and not param.flags() & QgsProcessingParameterDefinition.FlagOptional):
self.bar.pushMessage("Error", "Wrong or missing value for parameter '%s'" % param.description(), self.bar.pushMessage("Error", "Wrong or missing value for parameter '%s'" % param.description(),
level=QgsMessageBar.WARNING) level=QgsMessageBar.WARNING)