Skip to content

Commit

Permalink
[processing] ressurect delete and edit actions for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 5, 2018
1 parent 422d804 commit b747a6b
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 72 deletions.
18 changes: 10 additions & 8 deletions python/plugins/processing/script/DeleteScriptAction.py
Expand Up @@ -29,26 +29,28 @@

from qgis.PyQt.QtWidgets import QMessageBox

from qgis.core import QgsApplication
from qgis.core import QgsApplication, QgsProcessingAlgorithm

from processing.gui.ContextAction import ContextAction

from processing.script.ScriptAlgorithm import ScriptAlgorithm
from processing.script import ScriptUtils


class DeleteScriptAction(ContextAction):

def __init__(self):
self.name = self.tr('Delete script')
self.name = self.tr("Delete script")

def isEnabled(self):
return isinstance(self.itemData, ScriptAlgorithm) and self.itemData.allowEdit
return isinstance(self.itemData, QgsProcessingAlgorithm) and self.itemData.provider().id() == "script"

def execute(self):
reply = QMessageBox.question(None,
self.tr('Confirmation'),
self.tr('Are you sure you want to delete this script?'),
self.tr("Confirmation"),
self.tr("Are you sure you want to delete this script?"),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if reply == QMessageBox.Yes:
os.remove(self.itemData.descriptionFile)
QgsApplication.processingRegistry().providerById('script').refreshAlgorithms()
filePath = ScriptUtils.findAlgorithmSource(self.itemData.__class__.__name__)
os.remove(filePath)
QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
15 changes: 11 additions & 4 deletions python/plugins/processing/script/EditScriptAction.py
Expand Up @@ -25,19 +25,26 @@

__revision__ = '$Format:%H$'

import inspect

from qgis.core import QgsProcessingAlgorithm
from qgis.utils import iface

from processing.gui.ContextAction import ContextAction

from processing.script.ScriptEditorDialog import ScriptEditorDialog
from processing.script.ScriptAlgorithm import ScriptAlgorithm
from processing.script import ScriptUtils


class EditScriptAction(ContextAction):

def __init__(self):
self.name = self.tr('Edit script')
self.name = self.tr("Edit script")

def isEnabled(self):
return isinstance(self.itemData, ScriptAlgorithm) and self.itemData.allowEdit
return isinstance(self.itemData, QgsProcessingAlgorithm) and self.itemData.provider().id() == "script"

def execute(self):
dlg = ScriptEditorDialog(self.itemData)
filePath = ScriptUtils.findAlgorithmSource(self.itemData.__class__.__name__)
dlg = ScriptEditorDialog(filePath, iface.mainWindow())
dlg.show()
131 changes: 71 additions & 60 deletions python/plugins/processing/script/ScriptEditorDialog.py
Expand Up @@ -54,13 +54,13 @@
class ScriptEditorDialog(BASE, WIDGET):
hasChanged = False

def __init__(self, alg):
super(ScriptEditorDialog, self).__init__(None)
def __init__(self, filePath=None, parent=None):
super(ScriptEditorDialog, self).__init__(parent)
self.setupUi(self)

self.setWindowFlags(Qt.WindowMinimizeButtonHint |
Qt.WindowMaximizeButtonHint |
Qt.WindowCloseButtonHint)
#~ self.setWindowFlags(Qt.WindowMinimizeButtonHint |
#~ Qt.WindowMaximizeButtonHint |
#~ Qt.WindowCloseButtonHint)

self.searchWidget.setVisible(False)

Expand Down Expand Up @@ -117,7 +117,10 @@ def __init__(self, alg):
self.btnReplace.clicked.connect(self.replace)
self.lastSearch = None

self.alg = alg
self.filePath = filePath
if self.filePath is not None:
self._loadFile(self.filePath)
#self.alg = alg

self.snippets = {}
path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "script", "snippets.py")
Expand All @@ -139,11 +142,12 @@ def __init__(self, alg):
#if self.snippets:
# self.btnSnippets.setVisible(False)

if self.alg is not None:
self.filename = self.alg.descriptionFile
self.editor.setText(self.alg.script)
else:
self.filename = None
#if self.alg is not None:
# pass
# self.filename = self.alg.descriptionFile
# self.editor.setText(self.alg.script)
#else:
# self.filename = None

self.update = False
self.help = None
Expand Down Expand Up @@ -178,41 +182,37 @@ def updateProviders(self):
QgsApplication.processingRegistry().providerById('script').refreshAlgorithms()

def editHelp(self):
if self.alg is None:
alg = ScriptAlgorithm(None, self.editor.text())
else:
alg = self.alg

dlg = HelpEditionDialog(alg)
dlg.exec_()
if dlg.descriptions:
self.help = dlg.descriptions
self.setHasChanged(True)
#if self.alg is None:
# alg = ScriptAlgorithm(None, self.editor.text())
#else:
# alg = self.alg
#
#dlg = HelpEditionDialog(alg)
#dlg.exec_()
#if dlg.descriptions:
# self.help = dlg.descriptions
# self.setHasChanged(True)
pass

def openScript(self):
if self.hasChanged:
ret = QMessageBox.warning(self, self.tr('Unsaved changes'),
self.tr('There are unsaved changes in script. Continue?'),
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

scriptDir = ScriptUtils.scriptsFolders()[0]
filterName = self.tr('Python scripts (*.py)')
filterName = self.tr("Processing scripts (*.py *.PY)")
self.filename, fileFilter = QFileDialog.getOpenFileName(
self, self.tr('Open script'), scriptDir, filterName)
self, self.tr("Open script"), scriptDir, filterName)

if self.filename == '':
if self.filePath == "":
return

with OverrideCursor(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()
self._loadFile(self.filePath)

def save(self):
self.saveScript(False)
Expand All @@ -232,11 +232,12 @@ def saveScript(self, saveAs):
self.filename += '.py'

text = self.editor.text()
if self.alg is not None:
self.alg.script = text
#if self.alg is not None:
# self.alg.script = text

try:
with codecs.open(self.filename, 'w', encoding='utf-8') as fout:
fout.write(text)
with codecs.open(self.filePath, 'w', encoding='utf-8') as f:
f.write(text)
except IOError:
QMessageBox.warning(self,
self.tr('I/O error'),
Expand All @@ -247,37 +248,38 @@ def saveScript(self, saveAs):

# If help strings were defined before saving the script for
# the first time, we do it here
if self.help:
with codecs.open(self.filename + '.help', 'w', encoding='utf-8') as f:
json.dump(self.help, f)
self.help = None
#if self.help:
# with codecs.open(self.filename + '.help', 'w', encoding='utf-8') as f:
# json.dump(self.help, f)
# self.help = None
self.setHasChanged(False)
else:
self.filename = None
self.filePath = None

def setHasChanged(self, hasChanged):
self.hasChanged = hasChanged
self.actionSaveScript.setEnabled(hasChanged)

def runAlgorithm(self):
alg = ScriptAlgorithm(None, script=self.editor.text())
alg.setProvider(QgsApplication.processingRegistry().providerById('script'))

dlg = alg.createCustomParametersWidget(self)
if not dlg:
dlg = AlgorithmDialog(alg)

canvas = iface.mapCanvas()
prevMapTool = canvas.mapTool()

dlg.show()

if canvas.mapTool() != prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
#alg = ScriptAlgorithm(None, script=self.editor.text())
#alg.setProvider(QgsApplication.processingRegistry().providerById('script'))
#
#dlg = alg.createCustomParametersWidget(self)
#if not dlg:
# dlg = AlgorithmDialog(alg)
#
#canvas = iface.mapCanvas()
#prevMapTool = canvas.mapTool()
#
#dlg.show()
#
#if canvas.mapTool() != prevMapTool:
# try:
# canvas.mapTool().reset()
# except:
# pass
# canvas.setMapTool(prevMapTool)
pass

def find(self):
textToFind = self.leFindText.text()
Expand All @@ -294,3 +296,12 @@ def replace(self):

def toggleSearchBox(self, checked):
self.searchWidget.setVisible(checked)

def _loadFile(self, filePath):
with codecs.open(filePath, "r", encoding="utf-8") as f:
txt = f.read()

self.editor.setText(txt)
self.hasChanged = False
self.editor.setModified(False)
self.editor.recolor()
12 changes: 12 additions & 0 deletions python/plugins/processing/script/ScriptUtils.py
Expand Up @@ -71,3 +71,15 @@ def loadAlgorithm(moduleName, filePath):
QgsMessageLog.logMessage("Could not import script algorithm '{}' from '{}'\n{}".format(moduleName, filePath, str(e)),
"Processing",
QgsMessageLog.CRITICAL)


def findAlgorithmSource(className):
fileName = "{}.py".format(className)
folders = scriptsFolders()
for folder in folders:
items = os.scandir(folder)
for entry in items:
if entry.is_file() and entry.name == fileName:
return os.path.abspath(os.path.join(folder, fileName))

return None

0 comments on commit b747a6b

Please sign in to comment.