2012-12-08 19:42:43 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
EditScriptDialog.py
|
|
|
|
---------------------
|
|
|
|
Date : December 2012
|
|
|
|
Copyright : (C) 2012 by Alexander Bruy
|
|
|
|
Email : alexander dot bruy 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'December 2012'
|
|
|
|
__copyright__ = '(C) 2012, Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2014-11-21 15:01:23 +01:00
|
|
|
import os
|
2018-01-30 08:12:01 +02:00
|
|
|
import codecs
|
2018-01-30 14:12:49 +02:00
|
|
|
import inspect
|
2018-02-01 12:50:04 +02:00
|
|
|
import traceback
|
2018-05-21 14:23:26 +10:00
|
|
|
import warnings
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2016-04-29 11:39:26 +02:00
|
|
|
from qgis.PyQt import uic
|
2018-01-30 08:12:01 +02:00
|
|
|
from qgis.PyQt.QtCore import Qt
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.PyQt.QtGui import QCursor
|
|
|
|
from qgis.PyQt.QtWidgets import (QMessageBox,
|
2018-01-30 08:12:01 +02:00
|
|
|
QFileDialog)
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2018-02-01 12:50:04 +02:00
|
|
|
from qgis.gui import QgsGui, QgsErrorDialog
|
2018-02-02 08:34:47 +02:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsSettings,
|
|
|
|
QgsError,
|
|
|
|
QgsProcessingAlgorithm,
|
|
|
|
QgsProcessingFeatureBasedAlgorithm)
|
2017-07-30 22:36:51 +02:00
|
|
|
from qgis.utils import iface, OverrideCursor
|
2018-12-10 16:35:52 +10:00
|
|
|
from qgis.processing import alg as algfactory
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2014-11-12 11:42:29 +02:00
|
|
|
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
2018-01-29 16:46:48 +02:00
|
|
|
from processing.script import ScriptUtils
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
2018-05-21 14:23:26 +10:00
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
WIDGET, BASE = uic.loadUiType(
|
|
|
|
os.path.join(pluginPath, "ui", "DlgScriptEditor.ui"))
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
class ScriptEditorDialog(BASE, WIDGET):
|
2014-04-24 17:26:00 +02:00
|
|
|
hasChanged = False
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2018-01-29 18:54:26 +02:00
|
|
|
def __init__(self, filePath=None, parent=None):
|
|
|
|
super(ScriptEditorDialog, self).__init__(parent)
|
2012-12-08 19:42:43 +02:00
|
|
|
self.setupUi(self)
|
|
|
|
|
2018-01-30 08:12:01 +02:00
|
|
|
QgsGui.instance().enableAutoGeometryRestore(self)
|
|
|
|
|
2018-01-30 17:31:18 +02:00
|
|
|
self.editor.initLexer()
|
2017-12-15 11:07:15 +02:00
|
|
|
self.searchWidget.setVisible(False)
|
|
|
|
|
2018-08-15 19:06:48 +10:00
|
|
|
if iface is not None:
|
|
|
|
self.toolBar.setIconSize(iface.iconSize())
|
|
|
|
self.setStyleSheet(iface.mainWindow().styleSheet())
|
2016-11-24 12:34:59 +02:00
|
|
|
|
|
|
|
self.actionOpenScript.setIcon(
|
2018-05-20 15:09:53 +07:00
|
|
|
QgsApplication.getThemeIcon('/mActionScriptOpen.svg'))
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionSaveScript.setIcon(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
QgsApplication.getThemeIcon('/mActionFileSave.svg'))
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionSaveScriptAs.setIcon(
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
QgsApplication.getThemeIcon('/mActionFileSaveAs.svg'))
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionRunScript.setIcon(
|
|
|
|
QgsApplication.getThemeIcon('/mActionStart.svg'))
|
|
|
|
self.actionCut.setIcon(
|
|
|
|
QgsApplication.getThemeIcon('/mActionEditCut.svg'))
|
|
|
|
self.actionCopy.setIcon(
|
2016-09-11 20:41:21 +02:00
|
|
|
QgsApplication.getThemeIcon('/mActionEditCopy.svg'))
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionPaste.setIcon(
|
2016-09-11 20:41:21 +02:00
|
|
|
QgsApplication.getThemeIcon('/mActionEditPaste.svg'))
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionUndo.setIcon(
|
|
|
|
QgsApplication.getThemeIcon('/mActionUndo.svg'))
|
|
|
|
self.actionRedo.setIcon(
|
|
|
|
QgsApplication.getThemeIcon('/mActionRedo.svg'))
|
2017-12-15 11:07:15 +02:00
|
|
|
self.actionFindReplace.setIcon(
|
|
|
|
QgsApplication.getThemeIcon('/mActionFindReplace.svg'))
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionIncreaseFontSize.setIcon(
|
|
|
|
QgsApplication.getThemeIcon('/mActionIncreaseFont.svg'))
|
|
|
|
self.actionDecreaseFontSize.setIcon(
|
|
|
|
QgsApplication.getThemeIcon('/mActionDecreaseFont.svg'))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
# Connect signals and slots
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionOpenScript.triggered.connect(self.openScript)
|
|
|
|
self.actionSaveScript.triggered.connect(self.save)
|
|
|
|
self.actionSaveScriptAs.triggered.connect(self.saveAs)
|
|
|
|
self.actionRunScript.triggered.connect(self.runAlgorithm)
|
|
|
|
self.actionCut.triggered.connect(self.editor.cut)
|
|
|
|
self.actionCopy.triggered.connect(self.editor.copy)
|
|
|
|
self.actionPaste.triggered.connect(self.editor.paste)
|
|
|
|
self.actionUndo.triggered.connect(self.editor.undo)
|
|
|
|
self.actionRedo.triggered.connect(self.editor.redo)
|
2017-12-15 11:07:15 +02:00
|
|
|
self.actionFindReplace.toggled.connect(self.toggleSearchBox)
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionIncreaseFontSize.triggered.connect(self.editor.zoomIn)
|
|
|
|
self.actionDecreaseFontSize.triggered.connect(self.editor.zoomOut)
|
2014-04-24 17:26:00 +02:00
|
|
|
self.editor.textChanged.connect(lambda: self.setHasChanged(True))
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2018-05-19 17:17:41 +07:00
|
|
|
self.leFindText.returnPressed.connect(self.find)
|
2017-12-15 11:07:15 +02:00
|
|
|
self.btnFind.clicked.connect(self.find)
|
|
|
|
self.btnReplace.clicked.connect(self.replace)
|
|
|
|
self.lastSearch = None
|
|
|
|
|
2018-05-21 09:57:33 +10:00
|
|
|
self.filePath = None
|
|
|
|
if filePath is not None:
|
|
|
|
self._loadFile(filePath)
|
2014-11-21 15:01:23 +01:00
|
|
|
|
2018-01-30 08:12:01 +02:00
|
|
|
self.setHasChanged(False)
|
|
|
|
|
2018-05-21 09:57:33 +10:00
|
|
|
def update_dialog_title(self):
|
|
|
|
"""
|
|
|
|
Updates the script editor dialog title
|
|
|
|
"""
|
|
|
|
if self.filePath:
|
|
|
|
path, file_name = os.path.split(self.filePath)
|
|
|
|
else:
|
|
|
|
file_name = self.tr('Untitled Script')
|
|
|
|
|
|
|
|
if self.hasChanged:
|
|
|
|
file_name = '*' + file_name
|
|
|
|
|
|
|
|
self.setWindowTitle(self.tr('{} - Processing Script Editor').format(file_name))
|
|
|
|
|
2018-01-30 08:12:01 +02:00
|
|
|
def closeEvent(self, event):
|
2018-05-20 12:57:08 +07:00
|
|
|
settings = QgsSettings()
|
|
|
|
settings.setValue("/Processing/stateScriptEditor", self.saveState())
|
|
|
|
settings.setValue("/Processing/geometryScriptEditor", self.saveGeometry())
|
|
|
|
|
2014-11-19 11:31:54 +02:00
|
|
|
if self.hasChanged:
|
2018-05-20 12:57:08 +07:00
|
|
|
ret = QMessageBox.question(
|
|
|
|
self, self.tr('Save Script?'),
|
|
|
|
self.tr('There are unsaved changes in this script. Do you want to keep those?'),
|
|
|
|
QMessageBox.Save | QMessageBox.Cancel | QMessageBox.Discard, QMessageBox.Cancel)
|
|
|
|
|
|
|
|
if ret == QMessageBox.Save:
|
|
|
|
self.saveScript(False)
|
|
|
|
event.accept()
|
|
|
|
elif ret == QMessageBox.Discard:
|
2018-01-30 08:12:01 +02:00
|
|
|
event.accept()
|
2014-11-19 11:31:54 +02:00
|
|
|
else:
|
2018-01-30 08:12:01 +02:00
|
|
|
event.ignore()
|
2014-11-19 11:31:54 +02:00
|
|
|
else:
|
2018-01-30 08:12:01 +02:00
|
|
|
event.accept()
|
2017-03-02 11:59:23 +01:00
|
|
|
|
2014-11-19 11:31:54 +02:00
|
|
|
def openScript(self):
|
|
|
|
if self.hasChanged:
|
2018-01-29 18:54:26 +02:00
|
|
|
ret = QMessageBox.warning(self,
|
|
|
|
self.tr("Unsaved changes"),
|
2018-01-30 08:12:01 +02:00
|
|
|
self.tr("There are unsaved changes in the script. Continue?"),
|
2015-08-22 14:29:41 +02:00
|
|
|
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
|
2014-11-19 11:31:54 +02:00
|
|
|
if ret == QMessageBox.No:
|
|
|
|
return
|
|
|
|
|
2018-01-29 15:50:16 +02:00
|
|
|
scriptDir = ScriptUtils.scriptsFolders()[0]
|
2018-01-30 08:12:01 +02:00
|
|
|
fileName, _ = QFileDialog.getOpenFileName(self,
|
|
|
|
self.tr("Open script"),
|
|
|
|
scriptDir,
|
2018-08-03 08:34:05 +03:00
|
|
|
self.tr("Processing scripts (*.py *.PY)"))
|
2014-11-19 11:31:54 +02:00
|
|
|
|
2018-01-30 08:12:01 +02:00
|
|
|
if fileName == "":
|
2016-05-09 11:44:52 +03:00
|
|
|
return
|
|
|
|
|
2017-07-30 22:36:51 +02:00
|
|
|
with OverrideCursor(Qt.WaitCursor):
|
2018-01-30 08:12:01 +02:00
|
|
|
self._loadFile(fileName)
|
2014-11-19 11:31:54 +02:00
|
|
|
|
2012-12-08 19:42:43 +02:00
|
|
|
def save(self):
|
|
|
|
self.saveScript(False)
|
|
|
|
|
|
|
|
def saveAs(self):
|
|
|
|
self.saveScript(True)
|
|
|
|
|
|
|
|
def saveScript(self, saveAs):
|
2018-02-01 12:50:04 +02:00
|
|
|
newPath = None
|
2018-01-30 08:12:01 +02:00
|
|
|
if self.filePath is None or saveAs:
|
2018-01-29 15:50:16 +02:00
|
|
|
scriptDir = ScriptUtils.scriptsFolders()[0]
|
2018-01-30 08:12:01 +02:00
|
|
|
newPath, _ = QFileDialog.getSaveFileName(self,
|
|
|
|
self.tr("Save script"),
|
|
|
|
scriptDir,
|
2018-08-03 08:34:05 +03:00
|
|
|
self.tr("Processing scripts (*.py *.PY)"))
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2018-02-01 12:50:04 +02:00
|
|
|
if newPath:
|
|
|
|
if not newPath.lower().endswith(".py"):
|
|
|
|
newPath += ".py"
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2018-02-01 12:50:04 +02:00
|
|
|
self.filePath = newPath
|
2018-01-29 18:54:26 +02:00
|
|
|
|
2018-02-01 12:50:04 +02:00
|
|
|
if self.filePath:
|
2018-01-30 08:12:01 +02:00
|
|
|
text = self.editor.text()
|
2012-12-08 19:42:43 +02:00
|
|
|
try:
|
2018-01-30 08:12:01 +02:00
|
|
|
with codecs.open(self.filePath, "w", encoding="utf-8") as f:
|
2018-01-29 18:54:26 +02:00
|
|
|
f.write(text)
|
2018-01-30 08:12:01 +02:00
|
|
|
except IOError as e:
|
2016-11-24 12:34:59 +02:00
|
|
|
QMessageBox.warning(self,
|
2018-01-30 08:12:01 +02:00
|
|
|
self.tr("I/O error"),
|
|
|
|
self.tr("Unable to save edits:\n{}").format(str(e))
|
2015-08-22 14:29:41 +02:00
|
|
|
)
|
2012-12-08 19:42:43 +02:00
|
|
|
return
|
2018-09-28 15:52:32 +10:00
|
|
|
|
2014-04-24 17:26:00 +02:00
|
|
|
self.setHasChanged(False)
|
2012-12-08 19:42:43 +02:00
|
|
|
|
2018-09-28 15:52:32 +10:00
|
|
|
QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
|
|
|
|
|
2014-04-24 17:26:00 +02:00
|
|
|
def setHasChanged(self, hasChanged):
|
|
|
|
self.hasChanged = hasChanged
|
2016-11-24 12:34:59 +02:00
|
|
|
self.actionSaveScript.setEnabled(hasChanged)
|
2018-05-21 09:57:33 +10:00
|
|
|
self.update_dialog_title()
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2012-12-08 19:42:43 +02:00
|
|
|
def runAlgorithm(self):
|
2018-12-10 16:35:52 +10:00
|
|
|
_locals = {}
|
2018-02-01 12:50:04 +02:00
|
|
|
try:
|
2018-12-10 16:35:52 +10:00
|
|
|
exec(self.editor.text(), _locals)
|
2018-02-01 12:50:04 +02:00
|
|
|
except Exception as e:
|
|
|
|
error = QgsError(traceback.format_exc(), "Processing")
|
|
|
|
QgsErrorDialog.show(error,
|
|
|
|
self.tr("Execution error")
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
alg = None
|
2018-12-10 16:35:52 +10:00
|
|
|
try:
|
|
|
|
alg = algfactory.instances.pop().createInstance()
|
|
|
|
except IndexError:
|
|
|
|
for name, attr in _locals.items():
|
|
|
|
if inspect.isclass(attr) and issubclass(attr, (QgsProcessingAlgorithm, QgsProcessingFeatureBasedAlgorithm)) and attr.__name__ not in ("QgsProcessingAlgorithm", "QgsProcessingFeatureBasedAlgorithm"):
|
|
|
|
alg = attr()
|
|
|
|
break
|
2018-02-01 12:50:04 +02:00
|
|
|
|
|
|
|
if alg is None:
|
|
|
|
QMessageBox.warning(self,
|
|
|
|
self.tr("No script found"),
|
|
|
|
self.tr("Seems there is no valid script in the file.")
|
|
|
|
)
|
|
|
|
return
|
2018-01-30 14:12:49 +02:00
|
|
|
|
2018-01-30 08:12:01 +02:00
|
|
|
alg.setProvider(QgsApplication.processingRegistry().providerById("script"))
|
2018-01-30 14:12:49 +02:00
|
|
|
alg.initAlgorithm()
|
2018-01-30 08:12:01 +02:00
|
|
|
|
2018-11-23 06:06:33 +10:00
|
|
|
dlg = alg.createCustomParametersWidget(iface.mainWindow())
|
2018-01-30 08:12:01 +02:00
|
|
|
if not dlg:
|
2018-11-23 06:06:33 +10:00
|
|
|
dlg = AlgorithmDialog(alg, parent=iface.mainWindow())
|
2018-01-30 08:12:01 +02:00
|
|
|
|
|
|
|
canvas = iface.mapCanvas()
|
|
|
|
prevMapTool = canvas.mapTool()
|
|
|
|
|
|
|
|
dlg.show()
|
|
|
|
|
|
|
|
if canvas.mapTool() != prevMapTool:
|
|
|
|
try:
|
|
|
|
canvas.mapTool().reset()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
canvas.setMapTool(prevMapTool)
|
2017-12-15 11:07:15 +02:00
|
|
|
|
|
|
|
def find(self):
|
|
|
|
textToFind = self.leFindText.text()
|
|
|
|
caseSensitive = self.chkCaseSensitive.isChecked()
|
|
|
|
wholeWord = self.chkWholeWord.isChecked()
|
|
|
|
if self.lastSearch is None or textToFind != self.lastSearch:
|
|
|
|
self.editor.findFirst(textToFind, False, caseSensitive, wholeWord, True)
|
|
|
|
else:
|
|
|
|
self.editor.findNext()
|
|
|
|
|
|
|
|
def replace(self):
|
|
|
|
textToReplace = self.leReplaceText.text()
|
|
|
|
self.editor.replaceSelectedText(textToReplace)
|
|
|
|
|
|
|
|
def toggleSearchBox(self, checked):
|
|
|
|
self.searchWidget.setVisible(checked)
|
2018-05-19 17:17:41 +07:00
|
|
|
if (checked):
|
|
|
|
self.leFindText.setFocus()
|
2018-01-29 18:54:26 +02:00
|
|
|
|
|
|
|
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()
|
2018-05-21 09:57:33 +10:00
|
|
|
|
|
|
|
self.filePath = filePath
|
|
|
|
self.update_dialog_title()
|