-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkflowCreatorDialog.py
346 lines (301 loc) · 15.8 KB
/
WorkflowCreatorDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
***************************************************************************
WorkflowCreatorDialog.py
-------------------------------------
Copyright (C) 2014 TIGER-NET (www.tiger-net.org)
***************************************************************************
* This plugin is part of the Water Observation Information System (WOIS) *
* developed under the TIGER-NET project funded by the European Space *
* Agency as part of the long-term TIGER initiative aiming at promoting *
* the use of Earth Observation (EO) for improved Integrated Water *
* Resources Management (IWRM) in Africa. *
* *
* WOIS is a free software i.e. you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation, either version 3 of the License, *
* or (at your option) any later version. *
* *
* WOIS is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************
"""
from PyQt4 import QtCore, QtGui
import codecs
from processing.modeler.ModelerUtils import ModelerUtils
try:
ModelerUtils.allAlgs = ModelerUtils.getAlgorithms()
except:
pass
try:
from processing.modeler.Providers import Providers
except:
from processing.modeler.ModelerUtils import ModelerUtils
try:
from processing.parameters.ParameterString import ParameterString
except:
from processing.core.parameters import ParameterString
try:
from processing.parameters.ParameterBoolean import ParameterBoolean
except:
from processing.core.parameters import ParameterBoolean
try:
from processing.parameters.ParameterSelection import ParameterSelection
except:
from processing.core.parameters import ParameterSelection
try:
from processing.parameters.ParameterNumber import ParameterNumber
except:
from processing.core.parameters import ParameterNumber
from processing.gui.ParametersDialog import ParametersDialog
from processing_workflow.Workflow import Workflow
from processing_workflow.StepDialog import StepDialog, NORMAL_MODE, BATCH_MODE
from processing_workflow.WorkflowUtils import WorkflowUtils
from processing.gui.AlgorithmExecutionDialog import AlgorithmExecutionDialog
from processing_workflow.WrongWorkflowException import WrongWorkflowException
# Dialog for creating new workflows from all the available SEXTANTE algorithms
class WorkflowCreatorDialog(ParametersDialog):
def __init__(self, workflow):
QtGui.QDialog.__init__(self)
self.setupUi()
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowSystemMenuHint |
QtCore.Qt.WindowMinMaxButtonsHint)
# set as window modal
self.setWindowModality(1)
self.workflow = Workflow()
self.help = None
self.update = True #indicates whether to update or not the toolbox after closing this dialog
self.executed = False
if workflow:
self.openWorkflow(workflow.descriptionFile)
def setupUi(self):
self.resize(1200, 600)
self.setWindowTitle("WOIS Workflow Creator")
self.setWindowIcon(WorkflowUtils.workflowIcon())
self.tabWidget = QtGui.QTabWidget()
self.tabWidget.setMaximumSize(QtCore.QSize(350, 10000))
self.tabWidget.setMinimumWidth(300)
#left hand side part
#==================================
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setSpacing(2)
self.verticalLayout.setMargin(0)
self.searchBox = QtGui.QLineEdit()
self.searchBox.textChanged.connect(self.fillAlgorithmTree)
self.verticalLayout.addWidget(self.searchBox)
self.algorithmTree = QtGui.QTreeWidget()
self.algorithmTree.setHeaderHidden(True)
self.fillAlgorithmTree()
self.verticalLayout.addWidget(self.algorithmTree)
self.algorithmTree.doubleClicked.connect(self.addAlgorithm)
self.algorithmsTab = QtGui.QWidget()
self.algorithmsTab.setLayout(self.verticalLayout)
self.tabWidget.addTab(self.algorithmsTab, "Algorithms")
#right hand side part
#==================================
self.textName = QtGui.QLineEdit()
if hasattr(self.textName, 'setPlaceholderText'):
self.textName.setPlaceholderText("[Enter workflow name here]")
self.textGroup = QtGui.QLineEdit()
if hasattr(self.textGroup, 'setPlaceholderText'):
self.textGroup.setPlaceholderText("[Enter group name here]")
self.horizontalLayoutNames = QtGui.QHBoxLayout()
self.horizontalLayoutNames.setSpacing(2)
self.horizontalLayoutNames.setMargin(0)
self.horizontalLayoutNames.addWidget(self.textName)
self.horizontalLayoutNames.addWidget(self.textGroup)
self.canvasTabWidget = QtGui.QTabWidget()
self.canvasTabWidget.setMinimumWidth(300)
self.canvasTabWidget.setMovable(True)
self.canvasLayout = QtGui.QVBoxLayout()
self.canvasLayout.setSpacing(2)
self.canvasLayout.setMargin(0)
self.canvasLayout.addLayout(self.horizontalLayoutNames)
self.canvasLayout.addWidget(self.canvasTabWidget)
#upper part, putting the two previous parts together
#===================================================
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setSpacing(2)
self.horizontalLayout.setMargin(0)
self.horizontalLayout.addWidget(self.tabWidget)
self.horizontalLayout.addLayout(self.canvasLayout)
#And the whole layout
#==========================
self.progress = QtGui.QProgressBar()
self.progress.setMinimum(0)
self.progress.setMaximum(100)
self.progress.setValue(0)
self.buttonBox = QtGui.QDialogButtonBox()
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.runButton = QtGui.QPushButton()
self.runButton.setText("Test")
self.buttonBox.addButton(self.runButton, QtGui.QDialogButtonBox.ActionRole)
self.removeButton = QtGui.QPushButton()
self.removeButton.setText("Remove step")
self.buttonBox.addButton(self.removeButton, QtGui.QDialogButtonBox.ActionRole)
self.openButton = QtGui.QPushButton()
self.openButton.setText("Open")
self.buttonBox.addButton(self.openButton, QtGui.QDialogButtonBox.ActionRole)
self.saveButton = QtGui.QPushButton()
self.saveButton.setText("Save")
self.buttonBox.addButton(self.saveButton, QtGui.QDialogButtonBox.ActionRole)
self.closeButton = QtGui.QPushButton()
self.closeButton.setText("Close")
self.buttonBox.addButton(self.closeButton, QtGui.QDialogButtonBox.ActionRole)
QtCore.QObject.connect(self.openButton, QtCore.SIGNAL("clicked()"), self.openWorkflow)
QtCore.QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveWorkflow)
QtCore.QObject.connect(self.closeButton, QtCore.SIGNAL("clicked()"), self.closeWindow)
QtCore.QObject.connect(self.runButton, QtCore.SIGNAL("clicked()"), self.runWorkflow)
QtCore.QObject.connect(self.removeButton, QtCore.SIGNAL("clicked()"), self.removeStep)
self.globalLayout = QtGui.QVBoxLayout()
self.globalLayout.setSpacing(2)
self.globalLayout.setMargin(0)
self.globalLayout.addLayout(self.horizontalLayout)
self.globalLayout.addWidget(self.progress)
self.globalLayout.addWidget(self.buttonBox)
self.setLayout(self.globalLayout)
QtCore.QMetaObject.connectSlotsByName(self)
def closeWindow(self):
self.close()
# For running (testing) the workflow without saving it and closing the creator window
def runWorkflow(self):
for i in range(0, self.canvasTabWidget.count()):
self.updateStep(i)
self.workflow.run()
def removeStep(self):
removeIndex = self.canvasTabWidget.currentIndex()
self.canvasTabWidget.removeTab(removeIndex)
self.workflow.removeStep(removeIndex)
def updateStep(self, stepNumber):
stepDialog = self.canvasTabWidget.widget(stepNumber)
# get customised default values for some parameter types
if stepDialog.getMode() == NORMAL_MODE:
if isinstance(stepDialog.normalModeDialog, AlgorithmExecutionDialog):
for param in stepDialog.alg.parameters:
if isinstance(param, ParameterBoolean) or isinstance(param, ParameterNumber) or isinstance(param, ParameterString) or isinstance(param, ParameterSelection):
# this is not very nice going so deep into step dialog but there seems to be no other way right now
stepDialog.normalModeDialog.setParamValue(param, stepDialog.normalModeDialog.paramTable.valueItems[param.name])
elif stepDialog.getMode() == BATCH_MODE:
col = 0
for param in stepDialog.alg.parameters:
if isinstance(param, ParameterBoolean) or isinstance(param, ParameterNumber) or isinstance(param, ParameterString) or isinstance(param, ParameterSelection):
stepDialog.batchModeDialog.setParameterValueFromWidget(param, stepDialog.batchModeDialog.table.cellWidget(0, col))
col += 1
# update the step in the workflow
self.workflow.changeStep(stepNumber, stepDialog.alg, stepDialog.getMode(), stepDialog.getInstructions())
# Save workflow to text file
def saveWorkflow(self):
if unicode(self.textGroup.text()).strip() == "" or unicode(self.textName.text()).strip() == "":
QtGui.QMessageBox.warning(self, "Warning", "Please enter group and model names before saving")
return
self.workflow.name = unicode(self.textName.text())
self.workflow.group = unicode(self.textGroup.text())
# save the instructions for all the steps in the workflow
for i in range(0, self.canvasTabWidget.count()):
self.updateStep(i)
if self.workflow.descriptionFile != None:
filename = self.workflow.descriptionFile
else:
filename = unicode(QtGui.QFileDialog.getSaveFileName(self, "Save Workflow", WorkflowUtils.workflowPath(), "QGIS Processing workflows (*.workflow)"))
if filename:
if not filename.endswith(".workflow"):
filename += ".workflow"
self.workflow.descriptionFile = filename
if filename:
text = self.workflow.serialize()
fout = codecs.open(filename, 'w', encoding='utf-8') #open(filename, "w")
fout.write(text)
fout.close()
self.update = True
QtGui.QMessageBox.information(self, "Workflow saving", "Workflow was correctly saved.")
# Open workflow from text file
def openWorkflow(self, filename = None):
if not filename:
filename = QtGui.QFileDialog.getOpenFileName(self, "Open Workflow", WorkflowUtils.workflowPath(), "SEXTANTE workflows (*.workflow)")
if filename:
try:
workflow = Workflow()
workflow.openWorkflow(filename)
self.workflow = workflow
self.canvasTabWidget.clear()
for i in range(0, self.workflow.getLength()):
# create a dialog for this algorithm
stepDialog = StepDialog(self.workflow.getAlgorithm(i), self)
stepDialog.setMode(self.workflow.getMode(i))
stepDialog.setInstructions(self.workflow.getInstructions(i))
# create new tab for it
self.canvasTabWidget.addTab(stepDialog, self.workflow.getAlgorithm(i).name)
self.textGroup.setText(workflow.group)
self.textName.setText(workflow.name)
except WrongWorkflowException, e:
QtGui.QMessageBox.critical(self, "Could not open workflowl", "The selected workflow could not be loaded\nWrong line:" + e.msg)
# Change the mode (normal or batch execution) for the currently open StepDialog
# This is a slot for a StepDialog signal
def changeAlgMode(self, mode):
# change the mode in the dialog
self.canvasTabWidget.currentWidget().setMode(mode)
# and in the step object
self.workflow.changeMode(self.canvasTabWidget.currentIndex(),mode)
# Add new step (algorithm) to the workflow
def addAlgorithm(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = ModelerUtils.getAlgorithm(item.alg.commandLineName())
alg = alg.getCopy()#copy.deepcopy(alg)
# create a tab for this algorithm
stepDialog = StepDialog(alg, self)
self.canvasTabWidget.addTab(stepDialog, alg.name)
# add this step to the workflow
self.workflow.addStep(alg, stepDialog.getMode(), stepDialog.getInstructions())
# List all the available algorithms in SEXTANTE
def fillAlgorithmTree(self):
self.algorithmTree.clear()
text = unicode(self.searchBox.text())
allAlgs = ModelerUtils.allAlgs
# Account for old (pre 2.4) and new (2.4 and up) Processing API
try:
providers = Providers.providers
except:
providers = ModelerUtils.providers
for providerName in allAlgs.keys():
# don't show workflows in available algorithms
if providerName == "workflow":
continue
groups = {}
provider = allAlgs[providerName]
algs = provider.values()
#add algorithms
for alg in algs:
if not alg.showInModeler:
continue
if text == "" or text.lower() in alg.name.lower():
if alg.group in groups:
groupItem = groups[alg.group]
else:
groupItem = QtGui.QTreeWidgetItem()
groupItem.setText(0, alg.group)
groups[alg.group] = groupItem
algItem = TreeAlgorithmItem(alg)
groupItem.addChild(algItem)
if len(groups) > 0:
providerItem = QtGui.QTreeWidgetItem()
providerItem.setText(0, providers[providerName].getDescription())
providerItem.setIcon(0, providers[providerName].getIcon())
for groupItem in groups.values():
providerItem.addChild(groupItem)
self.algorithmTree.addTopLevelItem(providerItem)
providerItem.setExpanded(True)
for groupItem in groups.values():
if text != "":
groupItem.setExpanded(True)
self.algorithmTree.sortItems(0, QtCore.Qt.AscendingOrder)
class TreeAlgorithmItem(QtGui.QTreeWidgetItem):
def __init__(self, alg):
QtGui.QTreeWidgetItem.__init__(self)
self.alg = alg
self.setText(0, alg.name)
self.setIcon(0, alg.getIcon())