mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
[processing] ressurect delete and edit actions for scripts
This commit is contained in:
parent
422d804db1
commit
b747a6b50c
@ -29,26 +29,28 @@ import os
|
|||||||
|
|
||||||
from qgis.PyQt.QtWidgets import QMessageBox
|
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.gui.ContextAction import ContextAction
|
||||||
|
|
||||||
from processing.script.ScriptAlgorithm import ScriptAlgorithm
|
from processing.script import ScriptUtils
|
||||||
|
|
||||||
|
|
||||||
class DeleteScriptAction(ContextAction):
|
class DeleteScriptAction(ContextAction):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.name = self.tr('Delete script')
|
self.name = self.tr("Delete script")
|
||||||
|
|
||||||
def isEnabled(self):
|
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):
|
def execute(self):
|
||||||
reply = QMessageBox.question(None,
|
reply = QMessageBox.question(None,
|
||||||
self.tr('Confirmation'),
|
self.tr("Confirmation"),
|
||||||
self.tr('Are you sure you want to delete this script?'),
|
self.tr("Are you sure you want to delete this script?"),
|
||||||
QMessageBox.Yes | QMessageBox.No,
|
QMessageBox.Yes | QMessageBox.No,
|
||||||
QMessageBox.No)
|
QMessageBox.No)
|
||||||
if reply == QMessageBox.Yes:
|
if reply == QMessageBox.Yes:
|
||||||
os.remove(self.itemData.descriptionFile)
|
filePath = ScriptUtils.findAlgorithmSource(self.itemData.__class__.__name__)
|
||||||
QgsApplication.processingRegistry().providerById('script').refreshAlgorithms()
|
os.remove(filePath)
|
||||||
|
QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
|
||||||
|
@ -25,19 +25,26 @@ __copyright__ = '(C) 2012, Victor Olaya'
|
|||||||
|
|
||||||
__revision__ = '$Format:%H$'
|
__revision__ = '$Format:%H$'
|
||||||
|
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
from qgis.core import QgsProcessingAlgorithm
|
||||||
|
from qgis.utils import iface
|
||||||
|
|
||||||
from processing.gui.ContextAction import ContextAction
|
from processing.gui.ContextAction import ContextAction
|
||||||
|
|
||||||
from processing.script.ScriptEditorDialog import ScriptEditorDialog
|
from processing.script.ScriptEditorDialog import ScriptEditorDialog
|
||||||
from processing.script.ScriptAlgorithm import ScriptAlgorithm
|
from processing.script import ScriptUtils
|
||||||
|
|
||||||
|
|
||||||
class EditScriptAction(ContextAction):
|
class EditScriptAction(ContextAction):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.name = self.tr('Edit script')
|
self.name = self.tr("Edit script")
|
||||||
|
|
||||||
def isEnabled(self):
|
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):
|
def execute(self):
|
||||||
dlg = ScriptEditorDialog(self.itemData)
|
filePath = ScriptUtils.findAlgorithmSource(self.itemData.__class__.__name__)
|
||||||
|
dlg = ScriptEditorDialog(filePath, iface.mainWindow())
|
||||||
dlg.show()
|
dlg.show()
|
||||||
|
@ -54,13 +54,13 @@ WIDGET, BASE = uic.loadUiType(
|
|||||||
class ScriptEditorDialog(BASE, WIDGET):
|
class ScriptEditorDialog(BASE, WIDGET):
|
||||||
hasChanged = False
|
hasChanged = False
|
||||||
|
|
||||||
def __init__(self, alg):
|
def __init__(self, filePath=None, parent=None):
|
||||||
super(ScriptEditorDialog, self).__init__(None)
|
super(ScriptEditorDialog, self).__init__(parent)
|
||||||
self.setupUi(self)
|
self.setupUi(self)
|
||||||
|
|
||||||
self.setWindowFlags(Qt.WindowMinimizeButtonHint |
|
#~ self.setWindowFlags(Qt.WindowMinimizeButtonHint |
|
||||||
Qt.WindowMaximizeButtonHint |
|
#~ Qt.WindowMaximizeButtonHint |
|
||||||
Qt.WindowCloseButtonHint)
|
#~ Qt.WindowCloseButtonHint)
|
||||||
|
|
||||||
self.searchWidget.setVisible(False)
|
self.searchWidget.setVisible(False)
|
||||||
|
|
||||||
@ -117,7 +117,10 @@ class ScriptEditorDialog(BASE, WIDGET):
|
|||||||
self.btnReplace.clicked.connect(self.replace)
|
self.btnReplace.clicked.connect(self.replace)
|
||||||
self.lastSearch = None
|
self.lastSearch = None
|
||||||
|
|
||||||
self.alg = alg
|
self.filePath = filePath
|
||||||
|
if self.filePath is not None:
|
||||||
|
self._loadFile(self.filePath)
|
||||||
|
#self.alg = alg
|
||||||
|
|
||||||
self.snippets = {}
|
self.snippets = {}
|
||||||
path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "script", "snippets.py")
|
path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "script", "snippets.py")
|
||||||
@ -139,11 +142,12 @@ class ScriptEditorDialog(BASE, WIDGET):
|
|||||||
#if self.snippets:
|
#if self.snippets:
|
||||||
# self.btnSnippets.setVisible(False)
|
# self.btnSnippets.setVisible(False)
|
||||||
|
|
||||||
if self.alg is not None:
|
#if self.alg is not None:
|
||||||
self.filename = self.alg.descriptionFile
|
# pass
|
||||||
self.editor.setText(self.alg.script)
|
# self.filename = self.alg.descriptionFile
|
||||||
else:
|
# self.editor.setText(self.alg.script)
|
||||||
self.filename = None
|
#else:
|
||||||
|
# self.filename = None
|
||||||
|
|
||||||
self.update = False
|
self.update = False
|
||||||
self.help = None
|
self.help = None
|
||||||
@ -178,41 +182,37 @@ class ScriptEditorDialog(BASE, WIDGET):
|
|||||||
QgsApplication.processingRegistry().providerById('script').refreshAlgorithms()
|
QgsApplication.processingRegistry().providerById('script').refreshAlgorithms()
|
||||||
|
|
||||||
def editHelp(self):
|
def editHelp(self):
|
||||||
if self.alg is None:
|
#if self.alg is None:
|
||||||
alg = ScriptAlgorithm(None, self.editor.text())
|
# alg = ScriptAlgorithm(None, self.editor.text())
|
||||||
else:
|
#else:
|
||||||
alg = self.alg
|
# alg = self.alg
|
||||||
|
#
|
||||||
dlg = HelpEditionDialog(alg)
|
#dlg = HelpEditionDialog(alg)
|
||||||
dlg.exec_()
|
#dlg.exec_()
|
||||||
if dlg.descriptions:
|
#if dlg.descriptions:
|
||||||
self.help = dlg.descriptions
|
# self.help = dlg.descriptions
|
||||||
self.setHasChanged(True)
|
# self.setHasChanged(True)
|
||||||
|
pass
|
||||||
|
|
||||||
def openScript(self):
|
def openScript(self):
|
||||||
if self.hasChanged:
|
if self.hasChanged:
|
||||||
ret = QMessageBox.warning(self, self.tr('Unsaved changes'),
|
ret = QMessageBox.warning(self,
|
||||||
self.tr('There are unsaved changes in script. Continue?'),
|
self.tr("Unsaved changes"),
|
||||||
|
self.tr("There are unsaved changes in script. Continue?"),
|
||||||
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
|
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
|
||||||
if ret == QMessageBox.No:
|
if ret == QMessageBox.No:
|
||||||
return
|
return
|
||||||
|
|
||||||
scriptDir = ScriptUtils.scriptsFolders()[0]
|
scriptDir = ScriptUtils.scriptsFolders()[0]
|
||||||
filterName = self.tr('Python scripts (*.py)')
|
filterName = self.tr("Processing scripts (*.py *.PY)")
|
||||||
self.filename, fileFilter = QFileDialog.getOpenFileName(
|
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
|
return
|
||||||
|
|
||||||
with OverrideCursor(Qt.WaitCursor):
|
with OverrideCursor(Qt.WaitCursor):
|
||||||
with codecs.open(self.filename, 'r', encoding='utf-8') as f:
|
self._loadFile(self.filePath)
|
||||||
txt = f.read()
|
|
||||||
|
|
||||||
self.editor.setText(txt)
|
|
||||||
self.hasChanged = False
|
|
||||||
self.editor.setModified(False)
|
|
||||||
self.editor.recolor()
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
self.saveScript(False)
|
self.saveScript(False)
|
||||||
@ -232,11 +232,12 @@ class ScriptEditorDialog(BASE, WIDGET):
|
|||||||
self.filename += '.py'
|
self.filename += '.py'
|
||||||
|
|
||||||
text = self.editor.text()
|
text = self.editor.text()
|
||||||
if self.alg is not None:
|
#if self.alg is not None:
|
||||||
self.alg.script = text
|
# self.alg.script = text
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with codecs.open(self.filename, 'w', encoding='utf-8') as fout:
|
with codecs.open(self.filePath, 'w', encoding='utf-8') as f:
|
||||||
fout.write(text)
|
f.write(text)
|
||||||
except IOError:
|
except IOError:
|
||||||
QMessageBox.warning(self,
|
QMessageBox.warning(self,
|
||||||
self.tr('I/O error'),
|
self.tr('I/O error'),
|
||||||
@ -247,37 +248,38 @@ class ScriptEditorDialog(BASE, WIDGET):
|
|||||||
|
|
||||||
# If help strings were defined before saving the script for
|
# If help strings were defined before saving the script for
|
||||||
# the first time, we do it here
|
# the first time, we do it here
|
||||||
if self.help:
|
#if self.help:
|
||||||
with codecs.open(self.filename + '.help', 'w', encoding='utf-8') as f:
|
# with codecs.open(self.filename + '.help', 'w', encoding='utf-8') as f:
|
||||||
json.dump(self.help, f)
|
# json.dump(self.help, f)
|
||||||
self.help = None
|
# self.help = None
|
||||||
self.setHasChanged(False)
|
self.setHasChanged(False)
|
||||||
else:
|
else:
|
||||||
self.filename = None
|
self.filePath = None
|
||||||
|
|
||||||
def setHasChanged(self, hasChanged):
|
def setHasChanged(self, hasChanged):
|
||||||
self.hasChanged = hasChanged
|
self.hasChanged = hasChanged
|
||||||
self.actionSaveScript.setEnabled(hasChanged)
|
self.actionSaveScript.setEnabled(hasChanged)
|
||||||
|
|
||||||
def runAlgorithm(self):
|
def runAlgorithm(self):
|
||||||
alg = ScriptAlgorithm(None, script=self.editor.text())
|
#alg = ScriptAlgorithm(None, script=self.editor.text())
|
||||||
alg.setProvider(QgsApplication.processingRegistry().providerById('script'))
|
#alg.setProvider(QgsApplication.processingRegistry().providerById('script'))
|
||||||
|
#
|
||||||
dlg = alg.createCustomParametersWidget(self)
|
#dlg = alg.createCustomParametersWidget(self)
|
||||||
if not dlg:
|
#if not dlg:
|
||||||
dlg = AlgorithmDialog(alg)
|
# dlg = AlgorithmDialog(alg)
|
||||||
|
#
|
||||||
canvas = iface.mapCanvas()
|
#canvas = iface.mapCanvas()
|
||||||
prevMapTool = canvas.mapTool()
|
#prevMapTool = canvas.mapTool()
|
||||||
|
#
|
||||||
dlg.show()
|
#dlg.show()
|
||||||
|
#
|
||||||
if canvas.mapTool() != prevMapTool:
|
#if canvas.mapTool() != prevMapTool:
|
||||||
try:
|
# try:
|
||||||
canvas.mapTool().reset()
|
# canvas.mapTool().reset()
|
||||||
except:
|
# except:
|
||||||
pass
|
# pass
|
||||||
canvas.setMapTool(prevMapTool)
|
# canvas.setMapTool(prevMapTool)
|
||||||
|
pass
|
||||||
|
|
||||||
def find(self):
|
def find(self):
|
||||||
textToFind = self.leFindText.text()
|
textToFind = self.leFindText.text()
|
||||||
@ -294,3 +296,12 @@ class ScriptEditorDialog(BASE, WIDGET):
|
|||||||
|
|
||||||
def toggleSearchBox(self, checked):
|
def toggleSearchBox(self, checked):
|
||||||
self.searchWidget.setVisible(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()
|
||||||
|
@ -71,3 +71,15 @@ def loadAlgorithm(moduleName, filePath):
|
|||||||
QgsMessageLog.logMessage("Could not import script algorithm '{}' from '{}'\n{}".format(moduleName, filePath, str(e)),
|
QgsMessageLog.logMessage("Could not import script algorithm '{}' from '{}'\n{}".format(moduleName, filePath, str(e)),
|
||||||
"Processing",
|
"Processing",
|
||||||
QgsMessageLog.CRITICAL)
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user