Skip to content

Commit ac0edda

Browse files
committedNov 21, 2014
[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
1 parent 51de0ca commit ac0edda

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed
 

‎python/plugins/processing/gui/ScriptEditorDialog.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
from processing.modeler.ModelerUtils import ModelerUtils
2019

2120
__author__ = 'Alexander Bruy'
2221
__date__ = 'December 2012'
@@ -29,6 +28,7 @@
2928
import codecs
3029
import sys
3130
import json
31+
import os
3232

3333
from PyQt4.QtCore import *
3434
from PyQt4.QtGui import *
@@ -37,12 +37,13 @@
3737
from qgis.core import *
3838
from qgis.utils import iface
3939

40+
from processing.modeler.ModelerUtils import ModelerUtils
4041
from processing.gui.AlgorithmDialog import AlgorithmDialog
4142
from processing.gui.HelpEditionDialog import HelpEditionDialog
4243
from processing.algs.r.RAlgorithm import RAlgorithm
4344
from processing.algs.r.RUtils import RUtils
4445
from processing.script.ScriptAlgorithm import ScriptAlgorithm
45-
from processing.script.ScriptUtils import ScriptUtils
46+
from processing.script import ScriptUtils
4647
from processing.ui.ui_DlgScriptEditor import Ui_DlgScriptEditor
4748

4849
import processing.resources_rc
@@ -62,7 +63,6 @@ def __init__(self, algType, alg):
6263
self.setWindowFlags(Qt.WindowMinimizeButtonHint |
6364
Qt.WindowMaximizeButtonHint |
6465
Qt.WindowCloseButtonHint)
65-
6666
# Set icons
6767
self.btnOpen.setIcon(
6868
QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
@@ -79,13 +79,15 @@ def __init__(self, algType, alg):
7979
QgsApplication.getThemeIcon('/mActionEditPaste.png'))
8080
self.btnUndo.setIcon(QgsApplication.getThemeIcon('/mActionUndo.png'))
8181
self.btnRedo.setIcon(QgsApplication.getThemeIcon('/mActionRedo.png'))
82+
self.btnSnippets.setIcon(QgsApplication.getThemeIcon('/mActionHelpAPI.png'))
8283

8384
# Connect signals and slots
8485
self.btnOpen.clicked.connect(self.openScript)
8586
self.btnSave.clicked.connect(self.save)
8687
self.btnSaveAs.clicked.connect(self.saveAs)
8788
self.btnEditHelp.clicked.connect(self.editHelp)
8889
self.btnRun.clicked.connect(self.runAlgorithm)
90+
self.btnSnippets.clicked.connect(self.showSnippets)
8991
self.btnCut.clicked.connect(self.editor.cut)
9092
self.btnCopy.clicked.connect(self.editor.copy)
9193
self.btnPaste.clicked.connect(self.editor.paste)
@@ -96,6 +98,27 @@ def __init__(self, algType, alg):
9698
self.alg = alg
9799
self.algType = algType
98100

101+
self.snippets = {}
102+
if self.algType == self.SCRIPT_PYTHON:
103+
path = os.path.join(os.path.dirname(ScriptUtils.__file__), "snippets.py")
104+
with open(path) as f:
105+
lines = f.readlines()
106+
snippetlines = []
107+
name = None
108+
for line in lines:
109+
if line.startswith("##"):
110+
if snippetlines:
111+
self.snippets[name] = "".join(snippetlines)
112+
name = line[2:]
113+
snippetlines = []
114+
else:
115+
snippetlines.append(line)
116+
if snippetlines:
117+
self.snippets[name] = "".join(snippetlines)
118+
119+
if not self.snippets:
120+
self.btnSnippets.setVisible(False)
121+
99122
if self.alg is not None:
100123
self.filename = self.alg.descriptionFile
101124
self.editor.setText(self.alg.script)
@@ -109,6 +132,14 @@ def __init__(self, algType, alg):
109132

110133
self.editor.setLexerType(self.algType)
111134

135+
def showSnippets(self, evt):
136+
popupmenu = QMenu()
137+
for name, snippet in self.snippets.iteritems():
138+
action = QAction(self.tr(name), self.btnSnippets)
139+
action.triggered[()].connect(lambda snippet=snippet: self.editor.insert(snippet))
140+
popupmenu.addAction(action)
141+
popupmenu.exec_(QCursor.pos())
142+
112143
def closeEvent(self, evt):
113144
if self.hasChanged:
114145
ret = QMessageBox.question(self, self.tr('Unsaved changes'),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
##Iterate over the features of a layer.
2+
feats = processing.features(layer)
3+
n = len(feats)
4+
for i, feat in enumerate(feats):
5+
progress.setPercentage(int(100 * i / n))
6+
#do something with 'feat'
7+
8+
##Create a new layer from another one, with an extra field
9+
fields = processing.fields(layer)
10+
# int, float and bool can be used as well as types
11+
fields.append(('NEW_FIELD', str))
12+
writer = processing.VectorWriter(output_file, None, fields,
13+
processing.geomtype(layer), layer.crs())
14+
15+
##Create a new table
16+
writer = processing.TableWriter(output_file, None, ['field1', 'field2'])

‎python/plugins/processing/ui/DlgScriptEditor.ui

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@
219219
</property>
220220
</widget>
221221
</item>
222+
<item>
223+
<widget class="Line" name="line_5">
224+
<property name="orientation">
225+
<enum>Qt::Vertical</enum>
226+
</property>
227+
</widget>
228+
</item>
229+
<item>
230+
<widget class="QToolButton" name="btnSnippets">
231+
<property name="text">
232+
<string>...</string>
233+
</property>
234+
<property name="autoRaise">
235+
<bool>true</bool>
236+
</property>
237+
</widget>
238+
</item>
222239
<item>
223240
<spacer name="horizontalSpacer">
224241
<property name="orientation">

0 commit comments

Comments
 (0)
Please sign in to comment.