Skip to content

Commit

Permalink
Added run button to modeler
Browse files Browse the repository at this point in the history
Added tool to define grass region on canvas

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@147 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Apr 24, 2012
1 parent 88053cc commit 0023d04
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/sextante/core/GeoAlgorithm.py
Expand Up @@ -230,7 +230,8 @@ def getOutputValue(self, name):
return None

def getAsCommand(self):
'''Returns the command that would run this same algorithm from the console'''
'''Returns the command that would run this same algorithm from the console.
Should return null if the algorithm can be run from the console.'''
s="Sextante.runalg(\"" + self.commandLineName() + "\","
for param in self.parameters:
s+=param.getValueAsCommandLineParameter() + ","
Expand Down
39 changes: 39 additions & 0 deletions src/sextante/grass/DefineGrassRegionAction.py
@@ -0,0 +1,39 @@
import os
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.gui.ToolboxAction import ToolboxAction
from sextante.core.QGisLayers import QGisLayers
from sextante.gui.RectangleMapTool import RectangleMapTool
from sextante.core.SextanteConfig import SextanteConfig
from sextante.grass.GrassUtils import GrassUtils

class DefineGrassRegionAction(ToolboxAction):

def __init__(self):
self.name="Define GRASS region on canvas"
self.group="Tools"
canvas = QGisLayers.iface.mapCanvas()
self.prevMapTool = canvas.mapTool()
self.tool = RectangleMapTool(canvas)
QtCore.QObject.connect(self.tool, SIGNAL("rectangleCreated()"), self.fillCoords)

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/grass.png")

def execute(self):
QtGui.QMessageBox.information(None, "GRASS Region", "Click and drag onto map canvas to define GRASS region")
canvas = QGisLayers.iface.mapCanvas()
canvas.setMapTool(self.tool)

def fillCoords(self):
r = self.tool.rectangle()
SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMIN, r.xMinimum())
SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMIN, r.yMinimum())
SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_XMAX, r.xMaximum())
SextanteConfig.setSettingValue(GrassUtils.GRASS_REGION_YMAX, r.yMaximum())
s = str(r.xMinimum()) + "," + str(r.xMaximum()) + "," + str(r.yMinimum()) + "," + str(r.yMaximum())
self.tool.reset()
canvas = QGisLayers.iface.mapCanvas()
canvas.setMapTool(self.prevMapTool)
QtGui.QMessageBox.information(None, "GRASS Region", "GRASS region set to:\n" + s)
2 changes: 2 additions & 0 deletions src/sextante/grass/GrassAlgorithmProvider.py
Expand Up @@ -7,11 +7,13 @@
from sextante.grass.GrassUtils import GrassUtils
from sextante.grass.GrassAlgorithm import GrassAlgorithm
from sextante.core.SextanteUtils import SextanteUtils
from sextante.grass.DefineGrassRegionAction import DefineGrassRegionAction

class GrassAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.actions.append(DefineGrassRegionAction())
self.createAlgsList() #preloading algorithms to speed up

def initializeSettings(self):
Expand Down
4 changes: 3 additions & 1 deletion src/sextante/gui/ParametersDialog.py
Expand Up @@ -151,7 +151,9 @@ def accept(self):
QApplication.restoreOverrideCursor()
else:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, self.alg.getAsCommand())
command = self.alg.getAsCommand()
if command:
SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, command)
ret = AlgorithmExecutor.runalg(self.alg, self)
QApplication.restoreOverrideCursor()
if ret:
Expand Down
10 changes: 8 additions & 2 deletions src/sextante/modeler/ModelerAlgorithm.py
Expand Up @@ -153,8 +153,8 @@ def getSafeNameForHarcodedParameter(self, param):
return "HARDCODEDPARAMVALUE_" + param.name + "_" + str(len(self.algs))

def serialize(self):
s="NAME:" + self.name + "\n"
s +="GROUP:" + self.group + "\n"
s="NAME:" + str(self.name) + "\n"
s +="GROUP:" + str(self.group) + "\n"

i = 0
for param in self.parameters:
Expand Down Expand Up @@ -326,6 +326,12 @@ def ismodelparam(self, paramname):
return False


def getAsCommand(self):
if self.descriptionFile:
return GeoAlgorithm.getAsCommand(self)
else:
return None

def commandLineName(self):
return "modeler:" + os.path.basename(self.descriptionFile)[:-5].lower()

Expand Down
23 changes: 23 additions & 0 deletions src/sextante/modeler/ModelerDialog.py
Expand Up @@ -12,6 +12,8 @@
from sextante.script.ScriptUtils import ScriptUtils
from sextante.gui.HelpEditionDialog import HelpEditionDialog
import pickle
from sextante.gui.ParametersDialog import ParametersDialog
from sextante.core.SextanteUtils import SextanteUtils

class ModelerDialog(QtGui.QDialog):
def __init__(self, alg=None):
Expand Down Expand Up @@ -121,6 +123,9 @@ def setupUi(self):
self.editHelpButton = QtGui.QPushButton()
self.editHelpButton.setText("Edit model help")
self.buttonBox.addButton(self.editHelpButton, QtGui.QDialogButtonBox.ActionRole)
self.runButton = QtGui.QPushButton()
self.runButton.setText("Run")
self.buttonBox.addButton(self.runButton, QtGui.QDialogButtonBox.ActionRole)
self.openButton = QtGui.QPushButton()
self.openButton.setText("Open")
self.buttonBox.addButton(self.openButton, QtGui.QDialogButtonBox.ActionRole)
Expand All @@ -133,6 +138,7 @@ def setupUi(self):
QObject.connect(self.openButton, QtCore.SIGNAL("clicked()"), self.openModel)
QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveModel)
QObject.connect(self.closeButton, QtCore.SIGNAL("clicked()"), self.closeWindow)
QObject.connect(self.runButton, QtCore.SIGNAL("clicked()"), self.runModel)
QObject.connect(self.editHelpButton, QtCore.SIGNAL("clicked()"), self.editHelp)

self.globalLayout = QtGui.QVBoxLayout()
Expand Down Expand Up @@ -168,6 +174,23 @@ def createScript(self):
fout.close()
self.update = True

def runModel(self):
##TODO: enable alg cloning without saving to file
if self.alg.descriptionFile is None:
self.alg.descriptionFile = SextanteUtils.getTempFilename("model")
text = self.alg.serialize()
fout = open(self.alg.descriptionFile, "w")
fout.write(text)
fout.close()
self.alg.provider = Providers.providers["Modeler"]
alg = copy.deepcopy(self.alg)
self.alg.descriptionFile = None
alg.descriptionFile = None
else:
alg = copy.deepcopy(self.alg)
dlg = ParametersDialog(alg)
dlg.exec_()

def saveModel(self):
if str(self.textGroup.text()).strip() == "" or str(self.textName.text()).strip() == "":
QMessageBox.warning(self, "Warning", "Please enter group and model names before saving")
Expand Down

0 comments on commit 0023d04

Please sign in to comment.