Skip to content
Permalink
Browse files

fixed problem when editing help and saving R scripts

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@230 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf
volayaf committed Jun 7, 2012
1 parent 9c84243 commit 15dfeff9f8a802979172ca9a5916a0292d0ae965
@@ -1,70 +1,88 @@
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.r.RUtils import RUtils
import pickle
from sextante.gui.HelpEditionDialog import HelpEditionDialog
import pickle
from sextante.r.RAlgorithm import RAlgorithm
from sextante.r.RUtils import RUtils

class EditRScriptDialog(QtGui.QDialog):
def __init__(self, alg):
self.alg = alg
if self.alg is not None:
self.filename = self.alg.descriptionFile
else:
self.filename = None
QtGui.QDialog.__init__(self)
self.setModal(True)
self.setupUi()
self.update = False
self.help = None

def setupUi(self):
self.resize(600,400)
self.setWindowTitle("Edit script")
self.resize(600, 350)
layout = QVBoxLayout()
self.text = QtGui.QTextEdit()
self.text.setObjectName("text")
self.text.setEnabled(True)
self.buttonBox = QtGui.QDialogButtonBox()
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
if self.alg != None:
self.text.setText(self.alg.script)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Close)
self.editHelpButton = QtGui.QPushButton()
self.editHelpButton.setText("Edit model help")
self.editHelpButton.setText("Edit script help")
self.buttonBox.addButton(self.editHelpButton, QtGui.QDialogButtonBox.ActionRole)
QObject.connect(self.editHelpButton, QtCore.SIGNAL("clicked()"), self.editHelp)
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)
QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveAlgorithm)
QObject.connect(self.closeButton, QtCore.SIGNAL("clicked()"), self.cancelPressed)
layout.addWidget(self.text)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
self.connect(self.buttonBox, SIGNAL("accepted()"), self.saveAlgorithm)
self.connect(self.buttonBox, SIGNAL("rejected()"), self.cancelPressed)
self.connect(self.editHelpButton, SIGNAL("clicked()"), self.editHelp)
QtCore.QMetaObject.connectSlotsByName(self)


def editHelp(self):
dlg = HelpEditionDialog(self.alg)
if self.alg is None:
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
else:
alg = self.alg
dlg = HelpEditionDialog(alg)
dlg.exec_()
#We store the description string in case there were not saved because there was no
#filename defined yet
if self.alg.descriptionFile is None and dlg.descriptions:
if self.alg is None and dlg.descriptions:
self.help = dlg.descriptions


def saveAlgorithm(self):
if self.alg!=None:
filename = self.alg.descriptionFile
else:
filename = QtGui.QFileDialog.getSaveFileName(self, "Save Script", RUtils.RScriptsFolder(), "R-SEXTANTE scripts (*.rsx)")
if self.filename is None:
self.filename = QtGui.QFileDialog.getSaveFileName(self, "Save Script", RUtils.RScriptsFolder(), "SEXTANTE R script (*.rsx)")

if filename:
#self.alg.descriptionFile = filename
text = self.text.toPlainText()
fout = open(filename, "w")
if self.filename:
text = str(self.text.toPlainText())
if self.alg is not None:
self.alg.script = text
fout = open(self.filename, "w")
fout.write(text)
fout.close()
self.update = True
#if help strings were defined before saving the model for the first time, we do it here
if self.help:
f = open(filename + ".help", "wb")
f = open(self.filename + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
QtGui.QMessageBox.information(self, "Script saving", "Script was correctly saved.")
self.close()
else:
self.filename = None

def cancelPressed(self):
self.update = False
#self.update = False
self.close()
@@ -37,14 +37,30 @@ def getCopy(self):
newone.provider = self.provider
return newone

def __init__(self, descriptionfile):
def __init__(self, descriptionFile, script=None):
GeoAlgorithm.__init__(self)
self.descriptionFile = descriptionfile
self.defineCharacteristicsFromFile()
self.script = script
self.descriptionFile = descriptionFile
if script is not None:
self.defineCharacteristicsFromScript()
if descriptionFile is not None:
self.defineCharacteristicsFromFile()

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

def defineCharacteristicsFromScript(self):
lines = self.script.split("\n")
self.silentOutputs = []
self.name = "[Unnamed algorithm]"
self.group = "User R scripts"
for line in lines:
if line.startswith("##"):
try:
self.processParameterLine(line.strip("\n"))
except:
pass

def defineCharacteristicsFromFile(self):
self.script = ""
self.commands=[]
@@ -1,3 +1,4 @@
##layer = raster
##[Example scripts]=group
##layer = raster
##showplots
hist(as.matrix(layer))
@@ -31,7 +31,7 @@ def setupUi(self):
if self.alg != None:
self.text.setText(self.alg.script)
self.editHelpButton = QtGui.QPushButton()
self.editHelpButton.setText("Edit model help")
self.editHelpButton.setText("Edit script help")
self.buttonBox.addButton(self.editHelpButton, QtGui.QDialogButtonBox.ActionRole)
QObject.connect(self.editHelpButton, QtCore.SIGNAL("clicked()"), self.editHelp)
self.saveButton = QtGui.QPushButton()

0 comments on commit 15dfeff

Please sign in to comment.
You can’t perform that action at this time.