Navigation Menu

Skip to content

Commit

Permalink
Refactor creation of batch dialog row algorithm parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 30, 2019
1 parent e6e3917 commit 185172a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
37 changes: 1 addition & 36 deletions python/plugins/processing/gui/BatchAlgorithmDialog.py
Expand Up @@ -79,42 +79,7 @@ def runAlgorithm(self):
project = QgsProject.instance() if load_layers else None

for row in range(self.mainWidget().batchRowCount()):
col = 0
parameters = {}
for param in self.algorithm().parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagHidden or param.isDestination():
continue
wrapper = self.mainWidget().wrappers[row][col]
parameters[param.name()] = wrapper.parameterValue()
if not param.checkValueIsAcceptable(wrapper.parameterValue()):
self.messageBar().pushMessage("", self.tr('Wrong or missing parameter value: {0} (row {1})').format(
param.description(), row + 1),
level=Qgis.Warning, duration=5)
return
col += 1
count_visible_outputs = 0
for out in self.algorithm().destinationParameterDefinitions():
if out.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

count_visible_outputs += 1
widget = self.mainWidget().tblParameters.cellWidget(row + 1, col)
text = widget.getValue()
if out.checkValueIsAcceptable(text):
if isinstance(out, (QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterFeatureSink)):
# load rasters and sinks on completion
parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, project)
else:
parameters[out.name()] = text
col += 1
else:
self.messageBar().pushMessage("", self.tr('Wrong or missing output value: {0} (row {1})').format(
out.description(), row + 1),
level=Qgis.Warning, duration=5)
return

parameters = self.mainWidget().parametersForRow(row, destinationProject=project, warnOnInvalid=True)
alg_parameters.append(parameters)

task = QgsScopedProxyProgressTask(self.tr('Batch Processing - {0}').format(self.algorithm().displayName()))
Expand Down
87 changes: 68 additions & 19 deletions python/plugins/processing/gui/BatchPanel.py
Expand Up @@ -46,24 +46,32 @@
QFileInfo,
QCoreApplication
)
from qgis.core import (Qgis,
QgsApplication,
QgsSettings,
QgsProperty, # NOQA - must be here for saved file evaluation
QgsProject,
QgsProcessingFeatureSourceDefinition, # NOQA - must be here for saved file evaluation
QgsCoordinateReferenceSystem, # NOQA - must be here for saved file evaluation
QgsProcessingParameterDefinition,
QgsProcessingModelAlgorithm,
QgsProcessingParameterFile,
QgsProcessingParameterMapLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMeshLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource)
from qgis.gui import (QgsProcessingParameterWidgetContext,
QgsProcessingContextGenerator,
QgsFindFilesByPatternDialog)
from qgis.core import (
Qgis,
QgsApplication,
QgsSettings,
QgsProperty, # NOQA - must be here for saved file evaluation
QgsProject,
QgsProcessingFeatureSourceDefinition, # NOQA - must be here for saved file evaluation
QgsCoordinateReferenceSystem, # NOQA - must be here for saved file evaluation
QgsProcessingParameterDefinition,
QgsProcessingModelAlgorithm,
QgsProcessingParameterFile,
QgsProcessingParameterMapLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMeshLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputLayerDefinition
)
from qgis.gui import (
QgsProcessingParameterWidgetContext,
QgsProcessingContextGenerator,
QgsFindFilesByPatternDialog
)
from qgis.utils import iface

from processing.gui.wrappers import WidgetWrapperFactory, WidgetWrapper
Expand Down Expand Up @@ -171,7 +179,6 @@ def addFilesByPattern(self):


class BatchPanel(BASE, WIDGET):

PARAMETERS = "PARAMETERS"
OUTPUTS = "OUTPUTS"

Expand Down Expand Up @@ -457,3 +464,45 @@ def toggleAdvancedMode(self, checked):
for column, param in enumerate(self.alg.parameterDefinitions()):
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.tblParameters.setColumnHidden(column, not checked)

def parametersForRow(self, row, destinationProject=None, warnOnInvalid=True):
"""
Returns the parameters dictionary corresponding to a row in the batch table
"""
col = 0
parameters = {}
for param in self.alg.parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagHidden or param.isDestination():
continue
wrapper = self.wrappers[row][col]
parameters[param.name()] = wrapper.parameterValue()
if warnOnInvalid and not param.checkValueIsAcceptable(wrapper.parameterValue()):
self.parent.messageBar().pushMessage("",
self.tr('Wrong or missing parameter value: {0} (row {1})').format(
param.description(), row + 1),
level=Qgis.Warning, duration=5)
return {}
col += 1
count_visible_outputs = 0
for out in self.alg.destinationParameterDefinitions():
if out.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

count_visible_outputs += 1
widget = self.tblParameters.cellWidget(row + 1, col)
text = widget.getValue()
if not warnOnInvalid or out.checkValueIsAcceptable(text):
if isinstance(out, (QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterFeatureSink)):
# load rasters and sinks on completion
parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, destinationProject)
else:
parameters[out.name()] = text
col += 1
else:
self.parent.messageBar().pushMessage("", self.tr('Wrong or missing output value: {0} (row {1})').format(
out.description(), row + 1),
level=Qgis.Warning, duration=5)
return {}
return parameters

0 comments on commit 185172a

Please sign in to comment.