Skip to content

Commit

Permalink
Work on resurrecting modeler
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 20, 2017
1 parent 5d8f2d9 commit efe8bba
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 119 deletions.
29 changes: 16 additions & 13 deletions python/plugins/processing/gui/NumberInputPanel.py
Expand Up @@ -34,10 +34,13 @@
from qgis.PyQt.QtWidgets import QDialog

from qgis.core import (QgsExpression,
QgsProcessingParameterNumber)
QgsProcessingParameterNumber,
QgsProcessingOutputNumber,
QgsProcessingOutputVectorLayer,
QgsProcessingOutputRasterLayer,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterRasterLayer)
from qgis.gui import QgsExpressionBuilderDialog
from processing.core.parameters import ParameterNumber, ParameterVector, ParameterRaster
from processing.core.outputs import OutputNumber, OutputVector, OutputRaster
from processing.modeler.ModelerAlgorithm import ValueFromInput, ValueFromOutput, CompoundValue
from processing.tools.dataobjects import createExpressionContext

Expand Down Expand Up @@ -76,7 +79,7 @@ def showExpressionsBuilder(self):
dlg = QgsExpressionBuilderDialog(None, str(self.leText.text()), self, 'generic', context)

context.popScope()
values = self.modelParametersDialog.getAvailableValuesOfType(ParameterNumber, OutputNumber)
values = self.modelParametersDialog.getAvailableValuesOfType(QgsProcessingParameterNumber, QgsProcessingOutputNumber)
variables = {}
for value in values:
if isinstance(value, ValueFromInput):
Expand All @@ -86,11 +89,11 @@ def showExpressionsBuilder(self):
else:
name = "%s_%s" % (value.alg, value.output)
alg = self.modelParametersDialog.model.algs[value.alg]
out = alg.algorithm.getOutputFromName(value.output)
out = alg.algorithm.outputDefinition(value.output)
desc = self.tr("Output '{0}' from algorithm '{1}'").format(out.description(), alg.description)
variables[name] = desc
values = self.modelParametersDialog.getAvailableValuesOfType(ParameterVector, OutputVector)
values.extend(self.modelParametersDialog.getAvailableValuesOfType(ParameterRaster, OutputRaster))
values = self.modelParametersDialog.getAvailableValuesOfType([QgsProcessingParameterFeatureSource, QgsProcessingParameterRasterLayer],
[QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer])
for value in values:
if isinstance(value, ValueFromInput):
name = value.name
Expand All @@ -99,13 +102,13 @@ def showExpressionsBuilder(self):
else:
name = "%s_%s" % (value.alg, value.output)
alg = self.modelParametersDialog.model.algs[value.alg]
element = alg.algorithm.getOutputFromName(value.output)
element = alg.algorithm.outputDefinition(value.output)
desc = self.tr("Output '{0}' from algorithm '{1}'").format(element.description(), alg.description)
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_maxx' % name] = self.tr("Maximum X of {0}").format(desc)
variables['%s_maxy' % name] = self.tr("Maximum Y of {0}").format(desc)
if isinstance(element, (ParameterRaster, OutputRaster)):
if isinstance(element, (QgsProcessingParameterRasterLayer, QgsProcessingOutputRasterLayer)):
variables['%s_min' % name] = self.tr("Minimum value of {0}").format(desc)
variables['%s_max' % name] = self.tr("Maximum value of {0}").format(desc)
variables['%s_avg' % name] = self.tr("Mean value of {0}").format(desc)
Expand All @@ -123,13 +126,13 @@ def getValue(self):
value = self.leText.text()
values = []
for param in self.modelParametersDialog.model.parameters:
if isinstance(param, ParameterNumber):
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.outputs:
if isinstance(out, OutputNumber) and "@%s_%s" % (alg.name(), out.name) in value:
values.append(ValueFromOutput(alg.name(), out.name))
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()))
if values:
return CompoundValue(values, value)
else:
Expand Down
58 changes: 31 additions & 27 deletions python/plugins/processing/gui/wrappers.py
Expand Up @@ -61,7 +61,11 @@
QgsProcessingParameterTable,
QgsProcessingParameterTableField,
QgsProcessingParameterFeatureSource,
QgsProcessingFeatureSourceDefinition)
QgsProcessingFeatureSourceDefinition,
QgsProcessingOutputRasterLayer,
QgsProcessingOutputVectorLayer,
QgsProcessingOutputString,
QgsProcessingOutputNumber)

from qgis.PyQt.QtWidgets import (
QCheckBox,
Expand Down Expand Up @@ -271,7 +275,7 @@ def createWidget(self):
widget = QComboBox()
widget.addItem(self.tr('Yes'), True)
widget.addItem(self.tr('No'), False)
bools = self.dialog.getAvailableValuesOfType(ParameterBoolean, None)
bools = self.dialog.getAvailableValuesOfType(QgsProcessingParameterBoolean, None)
for b in bools:
widget.addItem(self.dialog.resolveValueDescription(b), b)
return widget
Expand Down Expand Up @@ -308,11 +312,11 @@ def createWidget(self):

widget.setLayout(layout)
self.combo.setEditable(True)
crss = self.dialog.getAvailableValuesOfType(ParameterCrs)
crss = self.dialog.getAvailableValuesOfType(QgsProcessingParameterCrs)
for crs in crss:
self.combo.addItem(self.dialog.resolveValueDescription(crs), crs)
raster = self.dialog.getAvailableValuesOfType(ParameterRaster, OutputRaster)
vector = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector)
raster = self.dialog.getAvailableValuesOfType(QgsProcessingParameterRasterLayer, QgsProcessingOutputRasterLayer)
vector = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer)
for r in raster:
self.combo.addItem("Crs of layer " + self.dialog.resolveValueDescription(r), r)
for v in vector:
Expand Down Expand Up @@ -374,11 +378,11 @@ def createWidget(self):
else:
widget = QComboBox()
widget.setEditable(True)
extents = self.dialog.getAvailableValuesOfType(ParameterExtent, OutputExtent)
extents = self.dialog.getAvailableValuesOfType(QgsProcessingParameterExtent, OutputExtent)
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
widget.addItem(self.USE_MIN_COVERING_EXTENT, None)
raster = self.dialog.getAvailableValuesOfType(ParameterRaster, OutputRaster)
vector = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector)
raster = self.dialog.getAvailableValuesOfType(QgsProcessingParameterRasterLayer, QgsProcessingOutputRasterLayer)
vector = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer)
for ex in extents:
widget.addItem(self.dialog.resolveValueDescription(ex), ex)
for r in raster:
Expand Down Expand Up @@ -428,7 +432,7 @@ def createWidget(self):
else:
item = QComboBox()
item.setEditable(True)
points = self.dialog.getAvailableValuesOfType(ParameterPoint)
points = self.dialog.getAvailableValuesOfType(QgsProcessingParameterPoint)
for p in points:
item.addItem(self.dialog.resolveValueDescription(p), p)
item.setEditText(str(self.param.defaultValue()))
Expand Down Expand Up @@ -473,7 +477,7 @@ def createWidget(self):
else:
widget = QComboBox()
widget.setEditable(True)
files = self.dialog.getAvailableValuesOfType(ParameterFile, OutputFile)
files = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFile, OutputFile)
for f in files:
widget.addItem(self.dialog.resolveValueDescription(f), f)
return widget
Expand Down Expand Up @@ -513,22 +517,22 @@ class MultipleInputWidgetWrapper(WidgetWrapper):

def _getOptions(self):
if self.param.layerType() == QgsProcessingParameterDefinition.TypeVectorAny:
options = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector)
options = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer)
elif self.param.layerType() == QgsProcessingParameterDefinition.TypeVectorPoint:
options = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector,
options = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer,
[QgsProcessingParameterDefinition.TypeVectorPoint, QgsProcessingParameterDefinition.TypeVectorAny])
elif self.param.layerType() == QgsProcessingParameterDefinition.TypeVectorLine:
options = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector,
options = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer,
[QgsProcessingParameterDefinition.TypeVectorLine, QgsProcessingParameterDefinition.TypeVectorAny])
elif self.param.layerType() == QgsProcessingParameterDefinition.TypeVectorPolygon:
options = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector,
options = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer,
[QgsProcessingParameterDefinition.TypeVectorPolygon, QgsProcessingParameterDefinition.TypeVectorAny])
elif self.param.layerType() == QgsProcessingParameterDefinition.TypeRaster:
options = self.dialog.getAvailableValuesOfType(ParameterRaster, OutputRaster)
options = self.dialog.getAvailableValuesOfType(QgsProcessingParameterRasterLayer, QgsProcessingOutputRasterLayer)
elif self.param.layerType() == QgsProcessingParameterDefinition.TypeTable:
options = self.dialog.getAvailableValuesOfType(ParameterTable, OutputTable)
options = self.dialog.getAvailableValuesOfType(QgsProcessingParameterTable, OutputTable)
else:
options = self.dialog.getAvailableValuesOfType(ParameterFile, OutputFile)
options = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFile, OutputFile)
options = sorted(options, key=lambda opt: self.dialog.resolveValueDescription(opt))
return options

Expand Down Expand Up @@ -656,7 +660,7 @@ def createWidget(self):
return BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
else:
self.combo = QComboBox()
layers = self.dialog.getAvailableValuesOfType(ParameterRaster, OutputRaster)
layers = self.dialog.getAvailableValuesOfType(QgsProcessingParameterRasterLayer, QgsProcessingOutputRasterLayer)
self.combo.setEditable(True)
for layer in layers:
self.combo.addItem(self.dialog.resolveValueDescription(layer), layer)
Expand Down Expand Up @@ -812,7 +816,7 @@ def createWidget(self):
return widget
else:
self.combo = QComboBox()
layers = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector)
layers = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer)
self.combo.setEditable(True)
for layer in layers:
self.combo.addItem(self.dialog.resolveValueDescription(layer), layer)
Expand Down Expand Up @@ -901,8 +905,8 @@ def createWidget(self):

else:
# strings, numbers, files and table fields are all allowed input types
strings = self.dialog.getAvailableValuesOfType([ParameterString, ParameterNumber, ParameterFile,
ParameterTableField, ParameterExpression], OutputString)
strings = self.dialog.getAvailableValuesOfType([QgsProcessingParameterString, QgsProcessingParameterNumber, QgsProcessingParameterFile,
QgsProcessingParameterTableField, QgsProcessingParameterExpression], QgsProcessingOutputString)
options = [(self.dialog.resolveValueDescription(s), s) for s in strings]
if self.param.multiLine():
widget = MultilineTextPanel(options)
Expand Down Expand Up @@ -986,13 +990,13 @@ def createWidget(self):
if self.param.defaultValue():
widget.setExpression(self.param.defaultValue())
else:
strings = self.dialog.getAvailableValuesOfType([ParameterExpression, ParameterString, ParameterNumber], OutputString)
strings = self.dialog.getAvailableValuesOfType([QgsProcessingParameterExpression, QgsProcessingParameterString, QgsProcessingParameterNumber], QgsProcessingOutputString)
options = [(self.dialog.resolveValueDescription(s), s) for s in strings]
widget = QComboBox()
widget.setEditable(True)
for desc, val in options:
widget.addItem(desc, val)
widget.setEditText(self.param.default or "")
widget.setEditText(self.param.defaultValue() or "")
return widget

def postInitialize(self, wrappers):
Expand Down Expand Up @@ -1072,10 +1076,10 @@ def createWidget(self):
return BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
else:
self.combo = QComboBox()
layers = self.dialog.getAvailableValuesOfType(ParameterRaster, OutputRaster)
layers = self.dialog.getAvailableValuesOfType(QgsProcessingParameterRasterLayer, QgsProcessingOutputRasterLayer)
self.combo.setEditable(True)
tables = self.dialog.getAvailableValuesOfType(ParameterTable, OutputTable)
layers = self.dialog.getAvailableValuesOfType(ParameterVector, OutputVector)
tables = self.dialog.getAvailableValuesOfType(QgsProcessingParameterTable, OutputTable)
layers = self.dialog.getAvailableValuesOfType(QgsProcessingParameterFeatureSource, QgsProcessingOutputVectorLayer)
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
self.combo.addItem(self.NOT_SELECTED, None)
for table in tables:
Expand Down Expand Up @@ -1159,7 +1163,7 @@ def createWidget(self):
else:
widget = QComboBox()
widget.setEditable(True)
fields = self.dialog.getAvailableValuesOfType(ParameterTableField, None)
fields = self.dialog.getAvailableValuesOfType([QgsProcessingParameterTableField, QgsProcessingParameterString], [QgsProcessingOutputString])
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
widget.addItem(self.NOT_SET, None)
for f in fields:
Expand Down
30 changes: 19 additions & 11 deletions python/plugins/processing/modeler/ModelerAlgorithm.py
Expand Up @@ -36,20 +36,29 @@
from operator import attrgetter

from qgis.core import (QgsApplication,
QgsProcessingParameterDefinition)
QgsProcessingParameterDefinition,
QgsProcessingParameterBoolean,
QgsProcessingParameterCrs,
QgsProcessingParameterMapLayer,
QgsProcessingParameterExtent,
QgsProcessingParameterPoint,
QgsProcessingParameterFile,
QgsProcessingParameterMatrix,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterNumber,
QgsProcessingParameterRange,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingParameterExpression,
QgsProcessingParameterTable,
QgsProcessingParameterTableField,
QgsProcessingParameterFeatureSource)
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.modeler.WrongModelException import WrongModelException
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import (Parameter,
ParameterRaster,
ParameterVector,
ParameterTable,
ParameterTableField,
ParameterBoolean,
ParameterString,
ParameterNumber)

from processing.gui.Help2Html import getHtmlFromDescriptionsDict

Expand Down Expand Up @@ -240,8 +249,7 @@ def __init__(self):
self.inputs = {}
GeoAlgorithm.__init__(self)

classes = [ParameterRaster, ParameterVector, ParameterTable, ParameterTableField,
ParameterBoolean, ParameterString, ParameterNumber]
classes = [c for c in QgsProcessingParameterDefinition.__subclasses__()]
self.parameters = []
for c in classes:
for inp in list(self.inputs.values()):
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -552,7 +552,11 @@ def addAlgorithm(self):
self._addAlgorithm(alg)

def _addAlgorithm(self, alg, pos=None):
dlg = alg.getCustomModelerParametersDialog(self.alg)
dlg = None
try:
dlg = alg.getCustomModelerParametersDialog(self.alg)
except:
pass
if not dlg:
dlg = ModelerParametersDialog(alg, self.alg)
dlg.exec_()
Expand Down

0 comments on commit efe8bba

Please sign in to comment.