Skip to content

Commit

Permalink
Allow adding manual layers in modeler multi layer alg parameters
Browse files Browse the repository at this point in the history
This allows creation of models with child algorithms which do
things like merge a selected layer with a predefined static
layer, or create a vrt with a mix of static and user selected
layers.
  • Loading branch information
nyalldawson committed Aug 16, 2017
1 parent aefd5cc commit 8139786
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
10 changes: 7 additions & 3 deletions python/plugins/processing/gui/MultipleInputDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
from qgis.core import (QgsSettings,
QgsProcessing,
QgsVectorFileWriter,
QgsProviderRegistry)
QgsProviderRegistry,
QgsProcessingModelChildParameterSource)
from qgis.PyQt import uic
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtCore import QByteArray
Expand Down Expand Up @@ -105,8 +106,11 @@ def populateList(self):

# add extra options (e.g. manually added layers)
for t in [o for o in self.selectedoptions if not isinstance(o, int)]:
item = QStandardItem(t)
item.setData(t, Qt.UserRole)
if isinstance(t, QgsProcessingModelChildParameterSource):
item = QStandardItem(t.staticValue())
else:
item = QStandardItem(t)
item.setData(item.text(), Qt.UserRole)
item.setCheckState(Qt.Checked)
item.setCheckable(True)
self.model.appendRow(item)
Expand Down
28 changes: 17 additions & 11 deletions python/plugins/processing/gui/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
QgsProcessingOutputVectorLayer,
QgsProcessingOutputString,
QgsProcessingOutputNumber,
QgsProcessingModelChildParameterSource,
QgsProcessingModelAlgorithm)

from qgis.PyQt.QtWidgets import (
Expand Down Expand Up @@ -562,7 +563,7 @@ def createWidget(self):
return widget
else:
options = [self.dialog.resolveValueDescription(opt) for opt in self._getOptions()]
return MultipleInputPanel(options)
return MultipleInputPanel(options, datatype=self.param.layerType())

def refresh(self):
if self.param.layerType() != QgsProcessing.TypeFile:
Expand All @@ -582,15 +583,20 @@ def setValue(self, value):
return self.widget.setText(value)
else:
options = self._getOptions()
selected = []
for i, opt in enumerate(options):
try:
if opt in value:
selected.append(i)
except TypeError:
if opt == value:
selected.append(i)
self.widget.setSelectedItems(selected)

if not isinstance(value, (tuple, list)):
value = [value]

selected_options = []
for sel in value:
if sel in options:
selected_options.append(options.index(sel))
elif isinstance(sel, QgsProcessingModelChildParameterSource):
selected_options.append(sel.staticValue())
else:
selected_options.append(sel)

self.widget.setSelectedItems(selected_options)

def value(self):
if self.dialogType == DIALOG_STANDARD:
Expand All @@ -608,7 +614,7 @@ def value(self):
return self.widget.getText()
else:
options = self._getOptions()
values = [options[i] for i in self.widget.selectedoptions]
values = [options[i] if isinstance(i, int) else QgsProcessingModelChildParameterSource.fromStaticValue(i) for i in self.widget.selectedoptions]
if len(values) == 0 and not self.param.flags() & QgsProcessing.FlagOptional:
raise InvalidParameterValue()
return values
Expand Down

0 comments on commit 8139786

Please sign in to comment.