Skip to content
Permalink
Browse files

[processing] add open script action to script editor

  • Loading branch information
alexbruy committed Nov 19, 2014
1 parent 1c6aa9b commit 4c24666c87289e3cd55ecf71aba0a7fc3ca1e6b4
@@ -64,6 +64,8 @@ def __init__(self, algType, alg):
Qt.WindowCloseButtonHint)

# Set icons
self.btnOpen.setIcon(
QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
self.btnSave.setIcon(
QgsApplication.getThemeIcon('/mActionFileSave.svg'))
self.btnSaveAs.setIcon(
@@ -79,6 +81,7 @@ def __init__(self, algType, alg):
self.btnRedo.setIcon(QgsApplication.getThemeIcon('/mActionRedo.png'))

# Connect signals and slots
self.btnOpen.clicked.connect(self.openScript)
self.btnSave.clicked.connect(self.save)
self.btnSaveAs.clicked.connect(self.saveAs)
self.btnEditHelp.clicked.connect(self.editHelp)
@@ -106,6 +109,18 @@ def __init__(self, algType, alg):

self.editor.setLexerType(self.algType)

def closeEvent(self, evt):
if self.hasChanged:
ret = QMessageBox.question(self, self.tr('Unsaved changes'),
self.tr('There are unsaved changes in script. Continue?'),
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if ret == QMessageBox.Yes:
evt.accept()
else:
evt.ignore()
else:
evt.accept()

def editHelp(self):
if self.alg is None:
if self.algType == self.SCRIPT_PYTHON:
@@ -123,6 +138,34 @@ def editHelp(self):
if self.alg is None and dlg.descriptions:
self.help = dlg.descriptions

def openScript(self):
if self.hasChanged:
ret = QMessageBox.warning(self, self.tr('Unsaved changes'),
self.tr('There are unsaved changes in script. Continue?'),
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if ret == QMessageBox.No:
return

if self.algType == self.SCRIPT_PYTHON:
scriptDir = ScriptUtils.scriptsFolder()
filterName = self.tr('Python scripts (*.py)')
elif self.algType == self.SCRIPT_R:
scriptDir = RUtils.RScriptsFolder()
filterName = self.tr('Processing R script (*.rsx)')

self.filename = QFileDialog.getOpenFileName(
self, self.tr('Save script'), scriptDir, filterName)

QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
with codecs.open(self.filename, 'r', encoding='utf-8') as f:
txt = f.read()

self.editor.setText(txt)
self.hasChanged = False
self.editor.setModified(False)
self.editor.recolor()
QApplication.restoreOverrideCursor()

def save(self):
self.saveScript(False)

@@ -203,11 +203,9 @@ def _mimeDataAlgorithm(items):

def closeEvent(self, evt):
if self.hasChanged:
ret = QMessageBox.question(self, self.tr('Message'),
self.tr('There are unsaved changes in model. Close '
'modeler without saving?'),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
ret = QMessageBox.question(self, self.tr('Unsaved changes'),
self.tr('There are unsaved changes in model. Continue?'),
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

if ret == QMessageBox.Yes:
evt.accept()
@@ -34,6 +34,22 @@
<property name="rightMargin">
<number>3</number>
</property>
<item>
<widget class="QToolButton" name="btnOpen">
<property name="toolTip">
<string>Open script</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="shortcut">
<string>Ctrl+O, Return</string>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnSave">
<property name="toolTip">

1 comment on commit 4c24666

@TADickerson

This comment has been minimized.

Copy link

@TADickerson TADickerson commented on 4c24666 May 6, 2016

In the openScript function, should there be logic to handle the case where a user cancels out of the "Open script" dialog? Currently, the returned value from QFileDialog.getOpenFileName(...) is handed directly to self.filename, but if the user canceled out of the "Open script" dialog, then that returned value is an empty string (u''), which caused me to experience a python error a few lines down when attempting to open self.filename (which, in this case, would now be an empty string).

(Sorry for the comment here; I would have used the main QGIS Redmine bug tracker to report this, but the OSGeo account creation tool is down for maintenance today. Also, sorry for not just providing a proposed fix for this...there are probably multiple ways to resolve this, and I'm not confident about which approach would be more consistent with the rest of this plugin....try / except error handling, versus just putting some conditional logic to check the value returned from the file picker dialog before passing to self.filename?)

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