Skip to content
Permalink
Browse files

[processing] allow saving and loading batch processing configurations

  • Loading branch information
volaya committed Jan 14, 2016
1 parent ad1fd2a commit 71b7ea9f937a8c9696f48afb3e3236a25bd5ef5f
@@ -69,30 +69,6 @@ def __init__(self, alg):

self.textShortHelp.setVisible(False)

def setParamValue(self, param, widget, alg=None):
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable,
ParameterMultipleInput)):
value = widget.getText()
if unicode(value).strip() == '':
value = None
return param.setValue(value)
elif isinstance(param, ParameterBoolean):
return param.setValue(widget.currentIndex() == 0)
elif isinstance(param, ParameterSelection):
return param.setValue(widget.currentIndex())
elif isinstance(param, ParameterFixedTable):
return param.setValue(widget.table)
elif isinstance(param, ParameterExtent):
if alg is not None:
widget.useNewAlg(alg)
return param.setValue(widget.getValue())
elif isinstance(param, (ParameterCrs, ParameterFile)):
return param.setValue(widget.getValue())
elif isinstance(param, ParameterGeometryPredicate):
return param.setValue(widget.value())
else:
return param.setValue(widget.text())

def accept(self):
self.algs = []
self.load = []
@@ -108,7 +84,7 @@ def accept(self):
col += 1
continue
widget = self.mainWidget.tblParameters.cellWidget(row, col)
if not self.setParamValue(param, widget, alg):
if not self.mainWidget.setParamValue(param, widget, alg):
self.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
self.algs = None
@@ -120,7 +96,7 @@ def accept(self):
continue
if isinstance(param, ParameterExtent):
widget = self.mainWidget.tblParameters.cellWidget(row, col)
if not self.setParamValue(param, widget, alg):
if not self.mainWidget.setParamValue(param, widget, alg):
self.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
self.algs = None
@@ -26,9 +26,11 @@
__revision__ = '$Format:%H$'

import os
import json

from PyQt4 import uic
from PyQt4.QtGui import QWidget, QIcon, QTableWidgetItem, QComboBox, QLineEdit, QHeaderView
from PyQt4.QtGui import (QWidget, QIcon, QTableWidgetItem, QComboBox, QLineEdit,
QHeaderView, QFileDialog)

from qgis.core import QgsApplication

@@ -59,6 +61,9 @@

class BatchPanel(BASE, WIDGET):

PARAMETERS = "PARAMETERS"
OUTPUTS = "OUTPUTS"

def __init__(self, parent, alg):
super(BatchPanel, self).__init__(None)
self.setupUi(self)
@@ -68,13 +73,17 @@ def __init__(self, parent, alg):
# Set icons
self.btnAdd.setIcon(QgsApplication.getThemeIcon('/symbologyAdd.svg'))
self.btnRemove.setIcon(QgsApplication.getThemeIcon('/symbologyRemove.svg'))
self.btnOpen.setIcon(QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
self.btnSave.setIcon(QgsApplication.getThemeIcon('/mActionFileSave.svg'))
self.btnAdvanced.setIcon(QIcon(os.path.join(pluginPath, 'images', 'alg.png')))

self.alg = alg
self.parent = parent

self.btnAdd.clicked.connect(self.addRow)
self.btnRemove.clicked.connect(self.removeRows)
self.btnOpen.clicked.connect(self.load)
self.btnSave.clicked.connect(self.save)
self.btnAdvanced.toggled.connect(self.toggleAdvancedMode)
self.tblParameters.horizontalHeader().sectionDoubleClicked.connect(
self.fillParameterValues)
@@ -164,6 +173,137 @@ def getWidgetFromParameter(self, param, row, col):

return item

def load(self):
filename = unicode(QFileDialog.getOpenFileName(self,
self.tr('Open batch'), None,
self.tr('JSON files (*.json)')))
if filename:
with open(filename) as f:
values = json.load(f)

self.tblParameters.setRowCount(0)
for row, alg in enumerate(values):
self.addRow()
params = alg[self.PARAMETERS]
outputs = alg[self.OUTPUTS]
column = 0
for param in self.alg.parameters:
if param.hidden:
continue
widget = self.tblParameters.cellWidget(row, column)
if param.name in params:
value = params[param.name]
self.setValueInWidget(widget, value)
column += 1

for out in self.alg.outputs:
if out.hidden:
continue
widget = self.tblParameters.cellWidget(row, column)
if out.name in outputs:
value = outputs[out.name]
self.setValueInWidget(widget, value)
column += 1

def setValueInWidget(self, widget, value):
if isinstance(widget, (BatchInputSelectionPanel, QLineEdit, FileSelectionPanel)):
widget.setText(unicode(value))
elif isinstance(widget, (BatchOutputSelectionPanel, GeometryPredicateSelectionPanel)):
widget.setValue(unicode(value))

elif isinstance(widget, QComboBox):
idx = widget.findText(unicode(value))
if idx != -1:
widget.setCurrentIndex(idx)
elif isinstance(widget, ExtentSelectionPanel):
if value is not None:
widget.setExtentFromString(value)
else:
widget.setExtentFromString('')
elif isinstance(widget, CrsSelectionPanel):
widget.setAuthId(value)


def save(self):
toSave = []
for row in range(self.tblParameters.rowCount()):
algParams = {}
algOutputs = {}
col = 0
alg = self.alg.getCopy()
for param in alg.parameters:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
col += 1
continue
widget = self.tblParameters.cellWidget(row, col)
if not self.setParamValue(param, widget, alg):
self.parent.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
return
algParams[param.name] = param.getValueAsCommandLineParameter()
col += 1
col = 0
for param in alg.parameters:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
widget = self.tblParameters.cellWidget(row, col)
if not self.setParamValue(param, widget, alg):
self.parent.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
return
algParams[param.name] = unicode(param.value())
col += 1
for out in alg.outputs:
if out.hidden:
continue
widget = self.tblParameters.cellWidget(row, col)
text = widget.getValue()
if text.strip() != '':
algOutputs[out.name] = text.strip()
col += 1
else:
self.parent.lblProgress.setText(
self.tr('<b>Wrong or missing parameter value: %s (row %d)</b>') % (out.description, row + 1))
return
toSave.append({self.PARAMETERS: algParams, self.OUTPUTS: algOutputs})

filename = unicode(QFileDialog.getSaveFileName(self,
self.tr('Save batch'),
None,
self.tr('JSON files (*.json)')))
if filename:
if not filename.endswith('.json'):
filename += '.json'
with open(filename, 'w') as f:
json.dump(toSave, f)

def setParamValue(self, param, widget, alg=None):
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable,
ParameterMultipleInput)):
value = widget.getText()
if unicode(value).strip() == '':
value = None
return param.setValue(value)
elif isinstance(param, ParameterBoolean):
return param.setValue(widget.currentIndex() == 0)
elif isinstance(param, ParameterSelection):
return param.setValue(widget.currentIndex())
elif isinstance(param, ParameterFixedTable):
return param.setValue(widget.table)
elif isinstance(param, ParameterExtent):
if alg is not None:
widget.useNewAlg(alg)
return param.setValue(widget.getValue())
elif isinstance(param, (ParameterCrs, ParameterFile)):
return param.setValue(widget.getValue())
elif isinstance(param, ParameterGeometryPredicate):
return param.setValue(widget.value())
else:
return param.setValue(widget.text())

def addRow(self):
self.tblParameters.setRowCount(self.tblParameters.rowCount() + 1)

@@ -20,30 +20,30 @@
<property name="spacing">
<number>2</number>
</property>
<item row="0" column="0">
<widget class="QToolButton" name="btnAdvanced">
<item row="0" column="1">
<widget class="QToolButton" name="btnAdd">
<property name="toolTip">
<string>Toggle advanced mode</string>
<string>Add row</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="btnAdd">
<item row="0" column="0">
<widget class="QToolButton" name="btnAdvanced">
<property name="toolTip">
<string>Add row</string>
<string>Toggle advanced mode</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
@@ -62,7 +62,7 @@
</property>
</widget>
</item>
<item row="0" column="3">
<item row="0" column="5">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -75,7 +75,7 @@
</property>
</spacer>
</item>
<item row="1" column="0" colspan="4">
<item row="1" column="0" colspan="6">
<widget class="QTableWidget" name="tblParameters">
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
@@ -94,6 +94,32 @@
</attribute>
</widget>
</item>
<item row="0" column="3">
<widget class="QToolButton" name="btnOpen">
<property name="toolTip">
<string>Open</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="QToolButton" name="btnSave">
<property name="toolTip">
<string>Save</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>

0 comments on commit 71b7ea9

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