Skip to content

Commit

Permalink
[processing] add open script action to script editor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Nov 19, 2014
1 parent 1c6aa9b commit 4c24666
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
43 changes: 43 additions & 0 deletions python/plugins/processing/gui/ScriptEditorDialog.py
Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand Down
8 changes: 3 additions & 5 deletions python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions python/plugins/processing/ui/DlgScriptEditor.ui
Expand Up @@ -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">
Expand Down

1 comment on commit 4c24666

@TADickerson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.