Skip to content

Commit

Permalink
[FEATURE][processing] In batch mode, allow population of file/layer
Browse files Browse the repository at this point in the history
input columns by searching for files matching a specified pattern

With optional recursive search!
  • Loading branch information
nyalldawson committed Apr 30, 2019
1 parent 3f4c3d0 commit e6e3917
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/BatchAlgorithmDialog.py
Expand Up @@ -78,7 +78,7 @@ def runAlgorithm(self):
load_layers = self.mainWidget().checkLoadLayersOnCompletion.isChecked()
project = QgsProject.instance() if load_layers else None

for row in range(len(self.mainWidget().batchRowCount())):
for row in range(self.mainWidget().batchRowCount()):
col = 0
parameters = {}
for param in self.algorithm().parameterDefinitions():
Expand Down
47 changes: 41 additions & 6 deletions python/plugins/processing/gui/BatchPanel.py
Expand Up @@ -49,14 +49,21 @@
from qgis.core import (Qgis,
QgsApplication,
QgsSettings,
QgsProperty,
QgsProperty, # NOQA - must be here for saved file evaluation
QgsProject,
QgsProcessingFeatureSourceDefinition,
QgsCoordinateReferenceSystem,
QgsProcessingFeatureSourceDefinition, # NOQA - must be here for saved file evaluation
QgsCoordinateReferenceSystem, # NOQA - must be here for saved file evaluation
QgsProcessingParameterDefinition,
QgsProcessingModelAlgorithm)
QgsProcessingModelAlgorithm,
QgsProcessingParameterFile,
QgsProcessingParameterMapLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMeshLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource)
from qgis.gui import (QgsProcessingParameterWidgetContext,
QgsProcessingContextGenerator)
QgsProcessingContextGenerator,
QgsFindFilesByPatternDialog)
from qgis.utils import iface

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

self.menu.addAction(fill_down_action)

if isinstance(self.parameterDefinition, (QgsProcessingParameterFile,
QgsProcessingParameterMapLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMeshLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource)):
find_by_pattern_action = QAction(QCoreApplication.translate('BatchPanel', 'Add Files by Pattern…'),
self.menu)
find_by_pattern_action.triggered.connect(self.addFilesByPattern)
find_by_pattern_action.setToolTip(self.tr('Adds files by a file pattern match'))
self.menu.addAction(find_by_pattern_action)

def fillDown(self):
"""
Copy the top value down
Expand All @@ -127,13 +145,30 @@ def setRowValue(self, row, value, context):
"""
Sets the value for a row, in the current column
"""
if self.panel.batchRowCount() <= row:
self.panel.addRow()

wrapper = self.panel.wrappers[row][self.column]
if wrapper is None:
# e.g. destination header
self.panel.tblParameters.cellWidget(row + 1, self.column).setValue(str(value))
else:
wrapper.setParameterValue(value, context)

def addFilesByPattern(self):
"""
Populates the dialog using a file pattern match
"""
dlg = QgsFindFilesByPatternDialog()
dlg.setWindowTitle(self.tr("Add Files by Pattern"))
if dlg.exec_():
files = dlg.files()
context = dataobjects.createContext()

first_row = self.panel.batchRowCount() if self.panel.batchRowCount() > 1 else 0
for row, file in enumerate(files):
self.setRowValue(first_row + row, file, context)


class BatchPanel(BASE, WIDGET):

Expand Down

0 comments on commit e6e3917

Please sign in to comment.