Skip to content

Commit 71b7ea9

Browse files
committed
[processing] allow saving and loading batch processing configurations
1 parent ad1fd2a commit 71b7ea9

File tree

3 files changed

+180
-38
lines changed

3 files changed

+180
-38
lines changed

python/plugins/processing/gui/BatchAlgorithmDialog.py

+2-26
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,6 @@ def __init__(self, alg):
6969

7070
self.textShortHelp.setVisible(False)
7171

72-
def setParamValue(self, param, widget, alg=None):
73-
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable,
74-
ParameterMultipleInput)):
75-
value = widget.getText()
76-
if unicode(value).strip() == '':
77-
value = None
78-
return param.setValue(value)
79-
elif isinstance(param, ParameterBoolean):
80-
return param.setValue(widget.currentIndex() == 0)
81-
elif isinstance(param, ParameterSelection):
82-
return param.setValue(widget.currentIndex())
83-
elif isinstance(param, ParameterFixedTable):
84-
return param.setValue(widget.table)
85-
elif isinstance(param, ParameterExtent):
86-
if alg is not None:
87-
widget.useNewAlg(alg)
88-
return param.setValue(widget.getValue())
89-
elif isinstance(param, (ParameterCrs, ParameterFile)):
90-
return param.setValue(widget.getValue())
91-
elif isinstance(param, ParameterGeometryPredicate):
92-
return param.setValue(widget.value())
93-
else:
94-
return param.setValue(widget.text())
95-
9672
def accept(self):
9773
self.algs = []
9874
self.load = []
@@ -108,7 +84,7 @@ def accept(self):
10884
col += 1
10985
continue
11086
widget = self.mainWidget.tblParameters.cellWidget(row, col)
111-
if not self.setParamValue(param, widget, alg):
87+
if not self.mainWidget.setParamValue(param, widget, alg):
11288
self.lblProgress.setText(
11389
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
11490
self.algs = None
@@ -120,7 +96,7 @@ def accept(self):
12096
continue
12197
if isinstance(param, ParameterExtent):
12298
widget = self.mainWidget.tblParameters.cellWidget(row, col)
123-
if not self.setParamValue(param, widget, alg):
99+
if not self.mainWidget.setParamValue(param, widget, alg):
124100
self.lblProgress.setText(
125101
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
126102
self.algs = None

python/plugins/processing/gui/BatchPanel.py

+141-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
__revision__ = '$Format:%H$'
2727

2828
import os
29+
import json
2930

3031
from PyQt4 import uic
31-
from PyQt4.QtGui import QWidget, QIcon, QTableWidgetItem, QComboBox, QLineEdit, QHeaderView
32+
from PyQt4.QtGui import (QWidget, QIcon, QTableWidgetItem, QComboBox, QLineEdit,
33+
QHeaderView, QFileDialog)
3234

3335
from qgis.core import QgsApplication
3436

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

6062
class BatchPanel(BASE, WIDGET):
6163

64+
PARAMETERS = "PARAMETERS"
65+
OUTPUTS = "OUTPUTS"
66+
6267
def __init__(self, parent, alg):
6368
super(BatchPanel, self).__init__(None)
6469
self.setupUi(self)
@@ -68,13 +73,17 @@ def __init__(self, parent, alg):
6873
# Set icons
6974
self.btnAdd.setIcon(QgsApplication.getThemeIcon('/symbologyAdd.svg'))
7075
self.btnRemove.setIcon(QgsApplication.getThemeIcon('/symbologyRemove.svg'))
76+
self.btnOpen.setIcon(QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
77+
self.btnSave.setIcon(QgsApplication.getThemeIcon('/mActionFileSave.svg'))
7178
self.btnAdvanced.setIcon(QIcon(os.path.join(pluginPath, 'images', 'alg.png')))
7279

7380
self.alg = alg
7481
self.parent = parent
7582

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

165174
return item
166175

176+
def load(self):
177+
filename = unicode(QFileDialog.getOpenFileName(self,
178+
self.tr('Open batch'), None,
179+
self.tr('JSON files (*.json)')))
180+
if filename:
181+
with open(filename) as f:
182+
values = json.load(f)
183+
184+
self.tblParameters.setRowCount(0)
185+
for row, alg in enumerate(values):
186+
self.addRow()
187+
params = alg[self.PARAMETERS]
188+
outputs = alg[self.OUTPUTS]
189+
column = 0
190+
for param in self.alg.parameters:
191+
if param.hidden:
192+
continue
193+
widget = self.tblParameters.cellWidget(row, column)
194+
if param.name in params:
195+
value = params[param.name]
196+
self.setValueInWidget(widget, value)
197+
column += 1
198+
199+
for out in self.alg.outputs:
200+
if out.hidden:
201+
continue
202+
widget = self.tblParameters.cellWidget(row, column)
203+
if out.name in outputs:
204+
value = outputs[out.name]
205+
self.setValueInWidget(widget, value)
206+
column += 1
207+
208+
def setValueInWidget(self, widget, value):
209+
if isinstance(widget, (BatchInputSelectionPanel, QLineEdit, FileSelectionPanel)):
210+
widget.setText(unicode(value))
211+
elif isinstance(widget, (BatchOutputSelectionPanel, GeometryPredicateSelectionPanel)):
212+
widget.setValue(unicode(value))
213+
214+
elif isinstance(widget, QComboBox):
215+
idx = widget.findText(unicode(value))
216+
if idx != -1:
217+
widget.setCurrentIndex(idx)
218+
elif isinstance(widget, ExtentSelectionPanel):
219+
if value is not None:
220+
widget.setExtentFromString(value)
221+
else:
222+
widget.setExtentFromString('')
223+
elif isinstance(widget, CrsSelectionPanel):
224+
widget.setAuthId(value)
225+
226+
227+
def save(self):
228+
toSave = []
229+
for row in range(self.tblParameters.rowCount()):
230+
algParams = {}
231+
algOutputs = {}
232+
col = 0
233+
alg = self.alg.getCopy()
234+
for param in alg.parameters:
235+
if param.hidden:
236+
continue
237+
if isinstance(param, ParameterExtent):
238+
col += 1
239+
continue
240+
widget = self.tblParameters.cellWidget(row, col)
241+
if not self.setParamValue(param, widget, alg):
242+
self.parent.lblProgress.setText(
243+
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
244+
return
245+
algParams[param.name] = param.getValueAsCommandLineParameter()
246+
col += 1
247+
col = 0
248+
for param in alg.parameters:
249+
if param.hidden:
250+
continue
251+
if isinstance(param, ParameterExtent):
252+
widget = self.tblParameters.cellWidget(row, col)
253+
if not self.setParamValue(param, widget, alg):
254+
self.parent.lblProgress.setText(
255+
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
256+
return
257+
algParams[param.name] = unicode(param.value())
258+
col += 1
259+
for out in alg.outputs:
260+
if out.hidden:
261+
continue
262+
widget = self.tblParameters.cellWidget(row, col)
263+
text = widget.getValue()
264+
if text.strip() != '':
265+
algOutputs[out.name] = text.strip()
266+
col += 1
267+
else:
268+
self.parent.lblProgress.setText(
269+
self.tr('<b>Wrong or missing parameter value: %s (row %d)</b>') % (out.description, row + 1))
270+
return
271+
toSave.append({self.PARAMETERS: algParams, self.OUTPUTS: algOutputs})
272+
273+
filename = unicode(QFileDialog.getSaveFileName(self,
274+
self.tr('Save batch'),
275+
None,
276+
self.tr('JSON files (*.json)')))
277+
if filename:
278+
if not filename.endswith('.json'):
279+
filename += '.json'
280+
with open(filename, 'w') as f:
281+
json.dump(toSave, f)
282+
283+
def setParamValue(self, param, widget, alg=None):
284+
if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable,
285+
ParameterMultipleInput)):
286+
value = widget.getText()
287+
if unicode(value).strip() == '':
288+
value = None
289+
return param.setValue(value)
290+
elif isinstance(param, ParameterBoolean):
291+
return param.setValue(widget.currentIndex() == 0)
292+
elif isinstance(param, ParameterSelection):
293+
return param.setValue(widget.currentIndex())
294+
elif isinstance(param, ParameterFixedTable):
295+
return param.setValue(widget.table)
296+
elif isinstance(param, ParameterExtent):
297+
if alg is not None:
298+
widget.useNewAlg(alg)
299+
return param.setValue(widget.getValue())
300+
elif isinstance(param, (ParameterCrs, ParameterFile)):
301+
return param.setValue(widget.getValue())
302+
elif isinstance(param, ParameterGeometryPredicate):
303+
return param.setValue(widget.value())
304+
else:
305+
return param.setValue(widget.text())
306+
167307
def addRow(self):
168308
self.tblParameters.setRowCount(self.tblParameters.rowCount() + 1)
169309

python/plugins/processing/ui/widgetBatchPanel.ui

+37-11
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,30 @@
2020
<property name="spacing">
2121
<number>2</number>
2222
</property>
23-
<item row="0" column="0">
24-
<widget class="QToolButton" name="btnAdvanced">
23+
<item row="0" column="1">
24+
<widget class="QToolButton" name="btnAdd">
2525
<property name="toolTip">
26-
<string>Toggle advanced mode</string>
26+
<string>Add row</string>
2727
</property>
2828
<property name="text">
2929
<string>...</string>
3030
</property>
31-
<property name="checkable">
32-
<bool>true</bool>
33-
</property>
3431
<property name="autoRaise">
3532
<bool>true</bool>
3633
</property>
3734
</widget>
3835
</item>
39-
<item row="0" column="1">
40-
<widget class="QToolButton" name="btnAdd">
36+
<item row="0" column="0">
37+
<widget class="QToolButton" name="btnAdvanced">
4138
<property name="toolTip">
42-
<string>Add row</string>
39+
<string>Toggle advanced mode</string>
4340
</property>
4441
<property name="text">
4542
<string>...</string>
4643
</property>
44+
<property name="checkable">
45+
<bool>true</bool>
46+
</property>
4747
<property name="autoRaise">
4848
<bool>true</bool>
4949
</property>
@@ -62,7 +62,7 @@
6262
</property>
6363
</widget>
6464
</item>
65-
<item row="0" column="3">
65+
<item row="0" column="5">
6666
<spacer name="horizontalSpacer">
6767
<property name="orientation">
6868
<enum>Qt::Horizontal</enum>
@@ -75,7 +75,7 @@
7575
</property>
7676
</spacer>
7777
</item>
78-
<item row="1" column="0" colspan="4">
78+
<item row="1" column="0" colspan="6">
7979
<widget class="QTableWidget" name="tblParameters">
8080
<property name="selectionBehavior">
8181
<enum>QAbstractItemView::SelectRows</enum>
@@ -94,6 +94,32 @@
9494
</attribute>
9595
</widget>
9696
</item>
97+
<item row="0" column="3">
98+
<widget class="QToolButton" name="btnOpen">
99+
<property name="toolTip">
100+
<string>Open</string>
101+
</property>
102+
<property name="text">
103+
<string>...</string>
104+
</property>
105+
<property name="autoRaise">
106+
<bool>true</bool>
107+
</property>
108+
</widget>
109+
</item>
110+
<item row="0" column="4">
111+
<widget class="QToolButton" name="btnSave">
112+
<property name="toolTip">
113+
<string>Save</string>
114+
</property>
115+
<property name="text">
116+
<string>...</string>
117+
</property>
118+
<property name="autoRaise">
119+
<bool>true</bool>
120+
</property>
121+
</widget>
122+
</item>
97123
</layout>
98124
</widget>
99125
<resources/>

0 commit comments

Comments
 (0)