Skip to content

Commit

Permalink
improved model edition in modeler
Browse files Browse the repository at this point in the history
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@189 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com committed May 25, 2012
1 parent 3ff9a30 commit debbe5d
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 68 deletions.
11 changes: 8 additions & 3 deletions src/sextante/modeler/CalculatorModelerParametersDialog.py
Expand Up @@ -59,6 +59,7 @@ def getNumbers(self):
dependent = []
else:
dependent = self.model.getDependentAlgorithms(self.algIndex)
dependent.append(self.algIndex)

i=0
for alg in self.model.algs:
Expand All @@ -74,7 +75,7 @@ def setParamValues(self):
self.values = {}
self.outputs = {}

name = self.model.getSafeNameForHarcodedParameter(self.alg.getParameterFromName(self.alg.FORMULA))
name = self.getSafeNameForHarcodedParameter(self.alg.getParameterFromName(self.alg.FORMULA))
value = AlgorithmAndParameter(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM, name)
self.params[self.alg.FORMULA] = value
formula = str(self.formulaText.text())
Expand All @@ -92,7 +93,7 @@ def setParamValues(self):
i += 1
#we create a dummy harcoded value for all unused variable slots
paramname = self.alg.NUMBER + str(i)
name = self.model.getSafeNameForHarcodedParameter(self.alg.getParameterFromName(paramname))
name = self.getSafeNameForHarcodedParameter(self.alg.getParameterFromName(paramname))
value = AlgorithmAndParameter(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM, name)
self.values[name] = 0
for i in range(len(used), self.alg.AVAILABLE_VARIABLES):
Expand All @@ -102,7 +103,11 @@ def setParamValues(self):
self.outputs[self.alg.RESULT] = None
return True


def getSafeNameForHarcodedParameter(self, param):
if self.algIndex is None:
return "HARDCODEDPARAMVALUE_" + param.name + "_" + str(self.alg)
else:
return "HARDCODEDPARAMVALUE_" + param.name + "_" + str(len(self.model.algs))

def okPressed(self):
if self.setParamValues():
Expand Down
52 changes: 29 additions & 23 deletions src/sextante/modeler/ModelerAlgorithm.py
Expand Up @@ -145,6 +145,7 @@ def updateAlgorithm(self, algIndex, parametersMap, valuesMap, outputsMap):
self.algOutputs[algIndex] = outputsMap
for value in valuesMap.keys():
self.paramValues[value] = valuesMap[value]
self.updateModelerView()


def removeAlgorithm(self, index):
Expand Down Expand Up @@ -224,7 +225,7 @@ def getDependsOnAlgorithms(self, algIndex):
'''This method returns a list with the indexes of algorithm a given one depends on'''
algs = []
for aap in self.algParameters[algIndex].values():
if aap.alg not in algs:
if aap.alg != AlgorithmAndParameter.PARENT_MODEL_ALGORITHM and aap.alg not in algs:
algs.append(aap.alg)
return algs

Expand Down Expand Up @@ -252,9 +253,6 @@ def getPositionForParameterItem(self):
BOX_HEIGHT = 80
return QtCore.QPointF(MARGIN + BOX_WIDTH / 2 + len(self.paramPos) * (BOX_WIDTH + MARGIN), MARGIN + BOX_HEIGHT / 2)

def getSafeNameForHarcodedParameter(self, param):
return "HARDCODEDPARAMVALUE_" + param.name + "_" + str(len(self.algs))

def serialize(self):
s="NAME:" + str(self.name) + "\n"
s +="GROUP:" + str(self.group) + "\n"
Expand Down Expand Up @@ -333,26 +331,34 @@ def getValueFromAlgorithmAndParameter(self, aap):
return self.producedOutputs[int(aap.alg)][aap.param]

def processAlgorithm(self, progress):
self.producedOutputs = []
iAlg = 0
for alg in self.algs:
if iAlg in self.deactivated:
self.producedOutputs = {}
executed = []
while len(executed) < len(self.algs) - len(self.deactivated):
iAlg = 0
for alg in self.algs:
if iAlg not in self.deactivated and iAlg not in executed:
canExecute = True
required = self.getDependsOnAlgorithms(iAlg)
for requiredAlg in required:
if requiredAlg not in executed:
canExecute = False
break
if canExecute:
try:
alg = alg.getCopy()
self.prepareAlgorithm(alg, iAlg)
progress.setText("Running " + alg.name + " [" + str(iAlg+1) + "/"
+ str(len(self.algs) - len(self.deactivated)) +"]")
outputs = {}
alg.execute(progress)
for out in alg.outputs:
outputs[out.name] = out.value
self.producedOutputs[iAlg] = outputs
executed.append(iAlg)

except GeoAlgorithmExecutionException, e :
raise GeoAlgorithmExecutionException("Error executing algorithm " + str(iAlg) + "\n" + e.msg)
iAlg += 1
else:
try:
alg = alg.getCopy()#copy.deepcopy(alg)
self.prepareAlgorithm(alg, iAlg)
progress.setText("Running " + alg.name + " [" + str(iAlg+1) + "/"
+ str(len(self.algs) - len(self.deactivated)) +"]")
outputs = {}
alg.execute(progress)
for out in alg.outputs:
outputs[out.name] = out.value
self.producedOutputs.append(outputs)
iAlg += 1
except GeoAlgorithmExecutionException, e :
raise GeoAlgorithmExecutionException("Error executing algorithm " + str(iAlg) + "\n" + e.msg)


def getOutputType(self, i, outname):
for out in self.algs[i].outputs:
Expand Down
56 changes: 30 additions & 26 deletions src/sextante/modeler/ModelerDialog.py
Expand Up @@ -87,25 +87,27 @@ def setupUi(self):
self.canvasTabWidget.setMinimumWidth(300)
self.view = QtGui.QGraphicsView(self.scene)

self.canvasTabWidget.addTab(self.view, "Design")
self.pythonText = QtGui.QTextEdit()
self.createScriptButton = QtGui.QPushButton()
self.createScriptButton.setText("Create script from model code")
self.createScriptButton.clicked.connect(self.createScript)
self.verticalLayoutPython = QtGui.QVBoxLayout()
self.verticalLayoutPython.setSpacing(2)
self.verticalLayoutPython.setMargin(0)
self.verticalLayoutPython.addWidget(self.pythonText)
self.verticalLayoutPython.addWidget(self.createScriptButton)
self.pythonWidget = QtGui.QWidget()
self.pythonWidget.setLayout(self.verticalLayoutPython)
self.canvasTabWidget.addTab(self.pythonWidget, "Python code")
#=======================================================================
# self.canvasTabWidget.addTab(self.view, "Design")
# self.pythonText = QtGui.QTextEdit()
# self.createScriptButton = QtGui.QPushButton()
# self.createScriptButton.setText("Create script from model code")
# self.createScriptButton.clicked.connect(self.createScript)
# self.verticalLayoutPython = QtGui.QVBoxLayout()
# self.verticalLayoutPython.setSpacing(2)
# self.verticalLayoutPython.setMargin(0)
# self.verticalLayoutPython.addWidget(self.pythonText)
# self.verticalLayoutPython.addWidget(self.createScriptButton)
# self.pythonWidget = QtGui.QWidget()
# self.pythonWidget.setLayout(self.verticalLayoutPython)
# self.canvasTabWidget.addTab(self.pythonWidget, "Python code")
#=======================================================================

self.canvasLayout = QtGui.QVBoxLayout()
self.canvasLayout.setSpacing(2)
self.canvasLayout.setMargin(0)
self.canvasLayout.addLayout(self.horizontalLayoutNames)
self.canvasLayout.addWidget(self.canvasTabWidget)
self.canvasLayout.addWidget(self.view)#canvasTabWidget)

#upper part, putting the two previous parts together
#===================================================
Expand Down Expand Up @@ -162,17 +164,19 @@ def editHelp(self):
if self.alg.descriptionFile is None and dlg.descriptions:
self.help = dlg.descriptions

def createScript(self):
if str(self.textGroup.text()).strip() == "":
QMessageBox.warning(self, "Warning", "Please enter group name before saving")
return
filename = QtGui.QFileDialog.getSaveFileName(self, "Save Script", ScriptUtils.scriptsFolder(), "Python scripts (*.py)")
if filename:
fout = open(filename, "w")
fout.write(str(self.textGroup.text()) + "=group")
fout.write(self.alg.getAsPythonCode())
fout.close()
self.update = True
#===========================================================================
# def createScript(self):
# if str(self.textGroup.text()).strip() == "":
# QMessageBox.warning(self, "Warning", "Please enter group name before saving")
# return
# filename = QtGui.QFileDialog.getSaveFileName(self, "Save Script", ScriptUtils.scriptsFolder(), "Python scripts (*.py)")
# if filename:
# fout = open(filename, "w")
# fout.write(str(self.textGroup.text()) + "=group")
# fout.write(self.alg.getAsPythonCode())
# fout.close()
# self.update = True
#===========================================================================

def runModel(self):
##TODO: enable alg cloning without saving to file
Expand Down Expand Up @@ -243,7 +247,7 @@ def repaintModel(self):
self.scene.setSceneRect(QtCore.QRectF(0, 0, 1000, 1000))
self.scene.paintModel(self.alg)
self.view.setScene(self.scene)
self.pythonText.setText(self.alg.getAsPythonCode())
#self.pythonText.setText(self.alg.getAsPythonCode())


def addInput(self):
Expand Down
1 change: 0 additions & 1 deletion src/sextante/modeler/ModelerGraphicItem.py
Expand Up @@ -71,7 +71,6 @@ def editElement(self):
dlg.exec_()
if dlg.params != None:
self.model.updateAlgorithm(self.elementIndex, dlg.params, dlg.values, dlg.outputs)
self.model.updateModelerView()

def removeElement(self):
if isinstance(self.element, Parameter):
Expand Down

0 comments on commit debbe5d

Please sign in to comment.