Skip to content

Commit 38b3212

Browse files
author
volayaf@gmail.com
committed
fixed #5566
added deactivation of algorithms in modeler git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@182 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent fa8c9e6 commit 38b3212

File tree

4 files changed

+134
-61
lines changed

4 files changed

+134
-61
lines changed

src/sextante/modeler/ModelerAlgorithm.py

+109-58
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def __init__(self):
5050
self.algPos = []
5151
self.paramPos = []
5252

53+
#deactivated algorithms that should not be executed
54+
self.deactivated = []
55+
5356

5457
def getIcon(self):
5558
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/model.png")
@@ -137,6 +140,97 @@ def addAlgorithm(self, alg, parametersMap, valuesMap, outputsMap):
137140
self.paramValues[value] = valuesMap[value]
138141
self.algPos.append(self.getPositionForAlgorithmItem())
139142

143+
def removeAlgorithm(self, index):
144+
if self.hasDependencies(self.algs[index], index):
145+
return False
146+
for out in self.algs[index].outputs:
147+
val = self.algOutputs[index][out.name]
148+
if val:
149+
name = self.getSafeNameForOutput(index, out)
150+
self.removeOutputFromName(name)
151+
del self.algs[index]
152+
del self.algParameters[index]
153+
del self.algOutputs[index]
154+
del self.algPos[index]
155+
self.updateModelerView()
156+
return True
157+
158+
def removeParameter(self, index):
159+
if self.hasDependencies(self.parameters[index], index):
160+
return False
161+
del self.parameters[index]
162+
del self.paramPos[index]
163+
self.updateModelerView()
164+
return True
165+
166+
def hasDependencies(self, element, elementIndex):
167+
'''This method returns true if some other element depends on the passed one'''
168+
if isinstance(element, Parameter):
169+
for alg in self.algParameters:
170+
for aap in alg.values():
171+
if aap.alg == AlgorithmAndParameter.PARENT_MODEL_ALGORITHM:
172+
if aap.param == element.name:
173+
return True
174+
elif aap.param in self.paramValues: #check for multiple inputs
175+
aap2 = self.paramValues[aap.param]
176+
if element.name in aap2:
177+
return True
178+
if isinstance(element, ParameterVector):
179+
for param in self.parameters:
180+
if isinstance(param, ParameterTableField):
181+
if param.parent == element.name:
182+
return True
183+
else:
184+
for alg in self.algParameters:
185+
for aap in alg.values():
186+
if aap.alg == elementIndex:
187+
return True
188+
189+
return False
190+
191+
def deactivateAlgorithm(self, algIndex, update = False):
192+
if algIndex not in self.deactivated:
193+
self.deactivated.append(algIndex)
194+
dependent = self.getDependentAlgorithms(algIndex)
195+
for alg in dependent:
196+
self.deactivateAlgorithm(alg)
197+
if update:
198+
self.updateModelerView()
199+
200+
def activateAlgorithm(self, algIndex, update = False):
201+
if algIndex in self.deactivated:
202+
dependsOn = self.getDependsOnAlgorithms(algIndex)
203+
for alg in dependsOn:
204+
if alg in self.deactivated:
205+
return False
206+
self.deactivated.remove(algIndex)
207+
dependent = self.getDependentAlgorithms(algIndex)
208+
for alg in dependent:
209+
self.activateAlgorithm(alg)
210+
if update:
211+
self.updateModelerView()
212+
return True
213+
214+
def getDependsOnAlgorithms(self, algIndex):
215+
'''This method returns a list with the indexes of algorithm a given one depends on'''
216+
algs = []
217+
for aap in self.algParameters[algIndex].values():
218+
if aap.alg not in algs:
219+
algs.append(aap.alg)
220+
return algs
221+
222+
def getDependentAlgorithms(self, algIndex):
223+
'''This method returns a list with the indexes of algorithm depending on a given one'''
224+
dependent = []
225+
index = -1
226+
for alg in self.algParameters:
227+
index += 1
228+
for aap in alg.values():
229+
if aap.alg == algIndex:
230+
dependent.append(index)
231+
break
232+
return dependent
233+
140234
def getPositionForAlgorithmItem(self):
141235
MARGIN = 20
142236
BOX_WIDTH = 200
@@ -233,18 +327,22 @@ def processAlgorithm(self, progress):
233327
self.producedOutputs = []
234328
iAlg = 0
235329
for alg in self.algs:
236-
try:
237-
alg = alg.getCopy()#copy.deepcopy(alg)
238-
self.prepareAlgorithm(alg, iAlg)
239-
progress.setText("Running " + alg.name + " [" + str(iAlg+1) + "/" + str(len(self.algs)) +"]")
240-
outputs = {}
241-
alg.execute(progress)
242-
for out in alg.outputs:
243-
outputs[out.name] = out.value
244-
self.producedOutputs.append(outputs)
330+
if iAlg in self.deactivated:
245331
iAlg += 1
246-
except GeoAlgorithmExecutionException, e :
247-
raise GeoAlgorithmExecutionException("Error executing algorithm " + str(iAlg) + "\n" + e.msg)
332+
else:
333+
try:
334+
alg = alg.getCopy()#copy.deepcopy(alg)
335+
self.prepareAlgorithm(alg, iAlg)
336+
progress.setText("Running " + alg.name + " [" + str(iAlg+1) + "/"
337+
+ str(len(self.algs) - len(self.deactivated)) +"]")
338+
outputs = {}
339+
alg.execute(progress)
340+
for out in alg.outputs:
341+
outputs[out.name] = out.value
342+
self.producedOutputs.append(outputs)
343+
iAlg += 1
344+
except GeoAlgorithmExecutionException, e :
345+
raise GeoAlgorithmExecutionException("Error executing algorithm " + str(iAlg) + "\n" + e.msg)
248346

249347

250348
def getOutputType(self, i, outname):
@@ -341,53 +439,6 @@ def updateModelerView(self):
341439
if self.modelerdialog:
342440
self.modelerdialog.repaintModel()
343441

344-
def removeAlgorithm(self, index):
345-
if self.hasDependencies(self.algs[index], index):
346-
return False
347-
for out in self.algs[index].outputs:
348-
val = self.algOutputs[index][out.name]
349-
if val:
350-
name = self.getSafeNameForOutput(index, out)
351-
self.removeOutputFromName(name)
352-
del self.algs[index]
353-
del self.algParameters[index]
354-
del self.algOutputs[index]
355-
del self.algPos[index]
356-
self.updateModelerView()
357-
return True
358-
359-
def removeParameter(self, index):
360-
if self.hasDependencies(self.parameters[index], index):
361-
return False
362-
del self.parameters[index]
363-
del self.paramPos[index]
364-
self.updateModelerView()
365-
return True
366-
367-
def hasDependencies(self, element, elementIndex):
368-
'''returns true if some other element depends on the passed one'''
369-
if isinstance(element, Parameter):
370-
for alg in self.algParameters:
371-
for aap in alg.values():
372-
if aap.alg == AlgorithmAndParameter.PARENT_MODEL_ALGORITHM:
373-
if aap.param == element.name:
374-
return True
375-
elif aap.param in self.paramValues: #check for multiple inputs
376-
aap2 = self.paramValues[aap.param]
377-
if element.name in aap2:
378-
return True
379-
if isinstance(element, ParameterVector):
380-
for param in self.parameters:
381-
if isinstance(param, ParameterTableField):
382-
if param.parent == element.name:
383-
return True
384-
else:
385-
for alg in self.algParameters:
386-
for aap in alg.values():
387-
if aap.alg == elementIndex:
388-
return True
389-
390-
return False
391442

392443
def helpfile(self):
393444
helpfile = self.descriptionFile + ".help"

src/sextante/modeler/ModelerArrowItem.py

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def paint(self, painter, option, widget=None):
106106
self.setLine(QtCore.QLineF(intersectPoint, myStartItem.pos()))
107107
line = self.line()
108108

109+
if line.length() == 0: #division by zero might occur if arrow has no length
110+
return
109111
angle = math.acos(line.dx() / line.length())
110112
if line.dy() >= 0:
111113
angle = (math.pi * 2.0) - angle

src/sextante/modeler/ModelerGraphicItem.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from PyQt4 import QtCore, QtGui
22
from sextante.parameters.Parameter import Parameter
33
import os
4+
from sextante.core.GeoAlgorithm import GeoAlgorithm
45

56
class ModelerGraphicItem(QtGui.QGraphicsItem):
67

@@ -36,17 +37,31 @@ def contextMenuEvent(self, event):
3637
popupmenu = QtGui.QMenu()
3738
removeAction = popupmenu.addAction("Remove")
3839
removeAction.triggered.connect(self.removeElement)
40+
if isinstance(self.element, GeoAlgorithm):
41+
if self.elementIndex in self.model.deactivated:
42+
removeAction = popupmenu.addAction("Activate")
43+
removeAction.triggered.connect(self.activateAlgorithm)
44+
else:
45+
deactivateAction = popupmenu.addAction("Deactivate")
46+
deactivateAction.triggered.connect(self.deactivateAlgorithm)
3947
popupmenu.exec_(event.screenPos())
4048

49+
def deactivateAlgorithm(self):
50+
self.model.deactivateAlgorithm(self.elementIndex, True)
51+
52+
def activateAlgorithm(self):
53+
if not self.model.activateAlgorithm(self.elementIndex, True):
54+
QtGui.QMessageBox.warning(None, "Could not activate Algorithm",
55+
"The selected algorithm depends on other currently non-active algorithms.\nActivate them them before trying to activate it.")
4156
def removeElement(self):
4257
if isinstance(self.element, Parameter):
4358
if not self.model.removeParameter(self.elementIndex):
4459
QtGui.QMessageBox.warning(None, "Could not remove element",
45-
"Other elements depend on this one.\nRemove them before trying to remove it.")
60+
"Other elements depend on the selected one.\nRemove them before trying to remove it.")
4661
else:
4762
if not self.model.removeAlgorithm(self.elementIndex):
4863
QtGui.QMessageBox.warning(None, "Could not remove element",
49-
"Other elements depend on this one.\nRemove them before trying to remove it.")
64+
"Other elements depend on the selected one.\nRemove them before trying to remove it.")
5065

5166
def getAdjustedText(self, text):
5267
return text
@@ -66,6 +81,12 @@ def paint(self, painter, option, widget=None):
6681
h = fm.height()
6782
pt = QtCore.QPointF(-w/2, h/2)
6883
painter.drawText(pt, self.text)
84+
if isinstance(self.element, GeoAlgorithm):
85+
if self.elementIndex in self.model.deactivated:
86+
painter.setPen(QtGui.QPen(QtCore.Qt.red))
87+
w = fm.width(QtCore.QString("[deactivated]"))
88+
pt = QtCore.QPointF(-w/2, h+h/2)
89+
painter.drawText(pt, "[deactivated]")
6990
painter.drawPixmap(-10 , -(ModelerGraphicItem.BOX_HEIGHT )/3,self.pixmap)
7091

7192

src/sextante/modeler/ModelerScene.py

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def getLastAlgorithmItem(self):
3434
else:
3535
return None
3636

37-
3837
def getItemsFromAAP(self, aap, isMultiple):
3938
items = []
4039
start = int(aap.alg)

0 commit comments

Comments
 (0)