/
EditRScriptDialog.py
140 lines (127 loc) · 5.46 KB
/
EditRScriptDialog.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
# -*- coding: utf-8 -*-
"""
***************************************************************************
EditRScriptDialog.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from sextante.gui.ParametersDialog import ParametersDialog
from sextante.core.QGisLayers import QGisLayers
from sextante.modeler.Providers import Providers
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
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(False)
self.setupUi()
self.update = False
self.help = None
def setupUi(self):
self.resize(600,400)
self.setWindowTitle("Edit script")
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.editHelpButton = QtGui.QPushButton()
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.runButton = QtGui.QPushButton()
self.runButton.setText("Run")
self.buttonBox.addButton(self.runButton, QtGui.QDialogButtonBox.ActionRole)
self.closeButton = QtGui.QPushButton()
self.closeButton.setText("Close")
self.buttonBox.addButton(self.closeButton, QtGui.QDialogButtonBox.ActionRole)
QObject.connect(self.runButton, QtCore.SIGNAL("clicked()"), self.runAlgorithm)
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)
QtCore.QMetaObject.connectSlotsByName(self)
def editHelp(self):
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 is None and dlg.descriptions:
self.help = dlg.descriptions
def runAlgorithm(self):
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
alg.provider = Providers.providers['r']
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = ParametersDialog(alg)
canvas = QGisLayers.iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool()!=prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
def saveAlgorithm(self):
if self.filename is None:
self.filename = str(QtGui.QFileDialog.getSaveFileName(self, "Save Script", RUtils.RScriptsFolder(), "SEXTANTE R script (*.rsx)"))
if self.filename:
if not self.filename.endswith(".rsx"):
self.filename += ".rsx"
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(self.filename + ".help", "wb")
pickle.dump(self.help, f)
f.close()
self.help = None
QtGui.QMessageBox.information(self, "Script saving", "Script was correctly saved.")
else:
self.filename = None
def cancelPressed(self):
#self.update = False
self.close()