Skip to content
Permalink
Browse files

Fix batch panel uses layer ids when auto populating output names base…

…d on a layer parameter
  • Loading branch information
github-actions authored and nyalldawson committed Jul 5, 2020
1 parent 7da0f63 commit 66f72cdc64251edf833fa3e648db0049e3d20b41
@@ -33,6 +33,7 @@
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterBoolean,
QgsProcessingParameterEnum,
QgsProject,
QgsProcessingParameterMatrix)
from qgis.PyQt.QtWidgets import QWidget, QPushButton, QLineEdit, QHBoxLayout, QSizePolicy, QFileDialog

@@ -108,8 +109,19 @@ def showSelectionDialog(self):
if isinstance(v, QgsMapLayer):
s = v.name()
else:
s = os.path.basename(v)
s = os.path.splitext(s)[0]
if v in QgsProject.instance().mapLayers():
layer = QgsProject.instance().mapLayer(v)
# value is a layer ID, but we'd prefer to show a layer name if it's unique in the project
if len([l for _, l in QgsProject.instance().mapLayers().items() if l.name().lower() == layer.name().lower()]) == 1:
s = layer.name()
else:
# otherwise fall back to layer id
s = v
else:
# else try to use file base name
# TODO: this is bad for database sources!!
s = os.path.basename(v)
s = os.path.splitext(s)[0]
elif isinstance(param, QgsProcessingParameterBoolean):
s = 'true' if v else 'false'
elif isinstance(param, QgsProcessingParameterEnum):
@@ -304,14 +304,6 @@ def showLayerSelectionDialog(self):
dlg = MultipleInputDialog([layer.name() for layer in layers])
dlg.exec_()

def generate_layer_id(layer):
# prefer layer name if unique
if len([l for l in layers if l.name().lower() == layer.name().lower()]) == 1:
return layer.name()
else:
# otherwise fall back to layer id
return layer.id()

if not dlg.selectedoptions:
return

@@ -321,8 +313,7 @@ def generate_layer_id(layer):

first_row = self.panel.batchRowCount() if self.panel.batchRowCount() > 1 else 0
for row, selected_idx in enumerate(selected):
layer = layers[selected_idx]
value = generate_layer_id(layer)
value = layers[selected_idx].id()
self.setRowValue(first_row + row, value, context)

def calculateByExpression(self):

0 comments on commit 66f72cd

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