Skip to content

Commit e6e3917

Browse files
committed
[FEATURE][processing] In batch mode, allow population of file/layer
input columns by searching for files matching a specified pattern With optional recursive search!
1 parent 3f4c3d0 commit e6e3917

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

python/plugins/processing/gui/BatchAlgorithmDialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def runAlgorithm(self):
7878
load_layers = self.mainWidget().checkLoadLayersOnCompletion.isChecked()
7979
project = QgsProject.instance() if load_layers else None
8080

81-
for row in range(len(self.mainWidget().batchRowCount())):
81+
for row in range(self.mainWidget().batchRowCount()):
8282
col = 0
8383
parameters = {}
8484
for param in self.algorithm().parameterDefinitions():

python/plugins/processing/gui/BatchPanel.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,21 @@
4949
from qgis.core import (Qgis,
5050
QgsApplication,
5151
QgsSettings,
52-
QgsProperty,
52+
QgsProperty, # NOQA - must be here for saved file evaluation
5353
QgsProject,
54-
QgsProcessingFeatureSourceDefinition,
55-
QgsCoordinateReferenceSystem,
54+
QgsProcessingFeatureSourceDefinition, # NOQA - must be here for saved file evaluation
55+
QgsCoordinateReferenceSystem, # NOQA - must be here for saved file evaluation
5656
QgsProcessingParameterDefinition,
57-
QgsProcessingModelAlgorithm)
57+
QgsProcessingModelAlgorithm,
58+
QgsProcessingParameterFile,
59+
QgsProcessingParameterMapLayer,
60+
QgsProcessingParameterRasterLayer,
61+
QgsProcessingParameterMeshLayer,
62+
QgsProcessingParameterVectorLayer,
63+
QgsProcessingParameterFeatureSource)
5864
from qgis.gui import (QgsProcessingParameterWidgetContext,
59-
QgsProcessingContextGenerator)
65+
QgsProcessingContextGenerator,
66+
QgsFindFilesByPatternDialog)
6067
from qgis.utils import iface
6168

6269
from processing.gui.wrappers import WidgetWrapperFactory, WidgetWrapper
@@ -103,9 +110,20 @@ def createMenu(self):
103110
fill_down_action = QAction(self.tr('Fill Down'), self.menu)
104111
fill_down_action.triggered.connect(self.fillDown)
105112
fill_down_action.setToolTip(self.tr('Copy the first value down to all other rows'))
106-
107113
self.menu.addAction(fill_down_action)
108114

115+
if isinstance(self.parameterDefinition, (QgsProcessingParameterFile,
116+
QgsProcessingParameterMapLayer,
117+
QgsProcessingParameterRasterLayer,
118+
QgsProcessingParameterMeshLayer,
119+
QgsProcessingParameterVectorLayer,
120+
QgsProcessingParameterFeatureSource)):
121+
find_by_pattern_action = QAction(QCoreApplication.translate('BatchPanel', 'Add Files by Pattern…'),
122+
self.menu)
123+
find_by_pattern_action.triggered.connect(self.addFilesByPattern)
124+
find_by_pattern_action.setToolTip(self.tr('Adds files by a file pattern match'))
125+
self.menu.addAction(find_by_pattern_action)
126+
109127
def fillDown(self):
110128
"""
111129
Copy the top value down
@@ -127,13 +145,30 @@ def setRowValue(self, row, value, context):
127145
"""
128146
Sets the value for a row, in the current column
129147
"""
148+
if self.panel.batchRowCount() <= row:
149+
self.panel.addRow()
150+
130151
wrapper = self.panel.wrappers[row][self.column]
131152
if wrapper is None:
132153
# e.g. destination header
133154
self.panel.tblParameters.cellWidget(row + 1, self.column).setValue(str(value))
134155
else:
135156
wrapper.setParameterValue(value, context)
136157

158+
def addFilesByPattern(self):
159+
"""
160+
Populates the dialog using a file pattern match
161+
"""
162+
dlg = QgsFindFilesByPatternDialog()
163+
dlg.setWindowTitle(self.tr("Add Files by Pattern"))
164+
if dlg.exec_():
165+
files = dlg.files()
166+
context = dataobjects.createContext()
167+
168+
first_row = self.panel.batchRowCount() if self.panel.batchRowCount() > 1 else 0
169+
for row, file in enumerate(files):
170+
self.setRowValue(first_row + row, file, context)
171+
137172

138173
class BatchPanel(BASE, WIDGET):
139174

0 commit comments

Comments
 (0)