mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-18 00:06:00 -04:00
[processing] Add snippet collection to script editor
The list of snippets is still very short, but the snippet management logic is added in this commit
This commit is contained in:
parent
51de0caeb8
commit
ac0eddaea1
@ -16,7 +16,6 @@
|
|||||||
* *
|
* *
|
||||||
***************************************************************************
|
***************************************************************************
|
||||||
"""
|
"""
|
||||||
from processing.modeler.ModelerUtils import ModelerUtils
|
|
||||||
|
|
||||||
__author__ = 'Alexander Bruy'
|
__author__ = 'Alexander Bruy'
|
||||||
__date__ = 'December 2012'
|
__date__ = 'December 2012'
|
||||||
@ -29,6 +28,7 @@ __revision__ = '$Format:%H$'
|
|||||||
import codecs
|
import codecs
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
@ -37,12 +37,13 @@ from PyQt4.Qsci import *
|
|||||||
from qgis.core import *
|
from qgis.core import *
|
||||||
from qgis.utils import iface
|
from qgis.utils import iface
|
||||||
|
|
||||||
|
from processing.modeler.ModelerUtils import ModelerUtils
|
||||||
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
||||||
from processing.gui.HelpEditionDialog import HelpEditionDialog
|
from processing.gui.HelpEditionDialog import HelpEditionDialog
|
||||||
from processing.algs.r.RAlgorithm import RAlgorithm
|
from processing.algs.r.RAlgorithm import RAlgorithm
|
||||||
from processing.algs.r.RUtils import RUtils
|
from processing.algs.r.RUtils import RUtils
|
||||||
from processing.script.ScriptAlgorithm import ScriptAlgorithm
|
from processing.script.ScriptAlgorithm import ScriptAlgorithm
|
||||||
from processing.script.ScriptUtils import ScriptUtils
|
from processing.script import ScriptUtils
|
||||||
from processing.ui.ui_DlgScriptEditor import Ui_DlgScriptEditor
|
from processing.ui.ui_DlgScriptEditor import Ui_DlgScriptEditor
|
||||||
|
|
||||||
import processing.resources_rc
|
import processing.resources_rc
|
||||||
@ -62,7 +63,6 @@ class ScriptEditorDialog(QDialog, Ui_DlgScriptEditor):
|
|||||||
self.setWindowFlags(Qt.WindowMinimizeButtonHint |
|
self.setWindowFlags(Qt.WindowMinimizeButtonHint |
|
||||||
Qt.WindowMaximizeButtonHint |
|
Qt.WindowMaximizeButtonHint |
|
||||||
Qt.WindowCloseButtonHint)
|
Qt.WindowCloseButtonHint)
|
||||||
|
|
||||||
# Set icons
|
# Set icons
|
||||||
self.btnOpen.setIcon(
|
self.btnOpen.setIcon(
|
||||||
QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
|
QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
|
||||||
@ -79,6 +79,7 @@ class ScriptEditorDialog(QDialog, Ui_DlgScriptEditor):
|
|||||||
QgsApplication.getThemeIcon('/mActionEditPaste.png'))
|
QgsApplication.getThemeIcon('/mActionEditPaste.png'))
|
||||||
self.btnUndo.setIcon(QgsApplication.getThemeIcon('/mActionUndo.png'))
|
self.btnUndo.setIcon(QgsApplication.getThemeIcon('/mActionUndo.png'))
|
||||||
self.btnRedo.setIcon(QgsApplication.getThemeIcon('/mActionRedo.png'))
|
self.btnRedo.setIcon(QgsApplication.getThemeIcon('/mActionRedo.png'))
|
||||||
|
self.btnSnippets.setIcon(QgsApplication.getThemeIcon('/mActionHelpAPI.png'))
|
||||||
|
|
||||||
# Connect signals and slots
|
# Connect signals and slots
|
||||||
self.btnOpen.clicked.connect(self.openScript)
|
self.btnOpen.clicked.connect(self.openScript)
|
||||||
@ -86,6 +87,7 @@ class ScriptEditorDialog(QDialog, Ui_DlgScriptEditor):
|
|||||||
self.btnSaveAs.clicked.connect(self.saveAs)
|
self.btnSaveAs.clicked.connect(self.saveAs)
|
||||||
self.btnEditHelp.clicked.connect(self.editHelp)
|
self.btnEditHelp.clicked.connect(self.editHelp)
|
||||||
self.btnRun.clicked.connect(self.runAlgorithm)
|
self.btnRun.clicked.connect(self.runAlgorithm)
|
||||||
|
self.btnSnippets.clicked.connect(self.showSnippets)
|
||||||
self.btnCut.clicked.connect(self.editor.cut)
|
self.btnCut.clicked.connect(self.editor.cut)
|
||||||
self.btnCopy.clicked.connect(self.editor.copy)
|
self.btnCopy.clicked.connect(self.editor.copy)
|
||||||
self.btnPaste.clicked.connect(self.editor.paste)
|
self.btnPaste.clicked.connect(self.editor.paste)
|
||||||
@ -96,6 +98,27 @@ class ScriptEditorDialog(QDialog, Ui_DlgScriptEditor):
|
|||||||
self.alg = alg
|
self.alg = alg
|
||||||
self.algType = algType
|
self.algType = algType
|
||||||
|
|
||||||
|
self.snippets = {}
|
||||||
|
if self.algType == self.SCRIPT_PYTHON:
|
||||||
|
path = os.path.join(os.path.dirname(ScriptUtils.__file__), "snippets.py")
|
||||||
|
with open(path) as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
snippetlines = []
|
||||||
|
name = None
|
||||||
|
for line in lines:
|
||||||
|
if line.startswith("##"):
|
||||||
|
if snippetlines:
|
||||||
|
self.snippets[name] = "".join(snippetlines)
|
||||||
|
name = line[2:]
|
||||||
|
snippetlines = []
|
||||||
|
else:
|
||||||
|
snippetlines.append(line)
|
||||||
|
if snippetlines:
|
||||||
|
self.snippets[name] = "".join(snippetlines)
|
||||||
|
|
||||||
|
if not self.snippets:
|
||||||
|
self.btnSnippets.setVisible(False)
|
||||||
|
|
||||||
if self.alg is not None:
|
if self.alg is not None:
|
||||||
self.filename = self.alg.descriptionFile
|
self.filename = self.alg.descriptionFile
|
||||||
self.editor.setText(self.alg.script)
|
self.editor.setText(self.alg.script)
|
||||||
@ -109,6 +132,14 @@ class ScriptEditorDialog(QDialog, Ui_DlgScriptEditor):
|
|||||||
|
|
||||||
self.editor.setLexerType(self.algType)
|
self.editor.setLexerType(self.algType)
|
||||||
|
|
||||||
|
def showSnippets(self, evt):
|
||||||
|
popupmenu = QMenu()
|
||||||
|
for name, snippet in self.snippets.iteritems():
|
||||||
|
action = QAction(self.tr(name), self.btnSnippets)
|
||||||
|
action.triggered[()].connect(lambda snippet=snippet: self.editor.insert(snippet))
|
||||||
|
popupmenu.addAction(action)
|
||||||
|
popupmenu.exec_(QCursor.pos())
|
||||||
|
|
||||||
def closeEvent(self, evt):
|
def closeEvent(self, evt):
|
||||||
if self.hasChanged:
|
if self.hasChanged:
|
||||||
ret = QMessageBox.question(self, self.tr('Unsaved changes'),
|
ret = QMessageBox.question(self, self.tr('Unsaved changes'),
|
||||||
|
16
python/plugins/processing/script/snippets.py
Normal file
16
python/plugins/processing/script/snippets.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
##Iterate over the features of a layer.
|
||||||
|
feats = processing.features(layer)
|
||||||
|
n = len(feats)
|
||||||
|
for i, feat in enumerate(feats):
|
||||||
|
progress.setPercentage(int(100 * i / n))
|
||||||
|
#do something with 'feat'
|
||||||
|
|
||||||
|
##Create a new layer from another one, with an extra field
|
||||||
|
fields = processing.fields(layer)
|
||||||
|
# int, float and bool can be used as well as types
|
||||||
|
fields.append(('NEW_FIELD', str))
|
||||||
|
writer = processing.VectorWriter(output_file, None, fields,
|
||||||
|
processing.geomtype(layer), layer.crs())
|
||||||
|
|
||||||
|
##Create a new table
|
||||||
|
writer = processing.TableWriter(output_file, None, ['field1', 'field2'])
|
@ -219,6 +219,23 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="Line" name="line_5">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Vertical</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QToolButton" name="btnSnippets">
|
||||||
|
<property name="text">
|
||||||
|
<string>...</string>
|
||||||
|
</property>
|
||||||
|
<property name="autoRaise">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<spacer name="horizontalSpacer">
|
<spacer name="horizontalSpacer">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user