Skip to content

Commit

Permalink
[processing] Add snippet collection to script editor
Browse files Browse the repository at this point in the history
The list of snippets is still very short, but the snippet management logic is added in this commit
  • Loading branch information
volaya committed Nov 21, 2014
1 parent 51de0ca commit ac0edda
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
37 changes: 34 additions & 3 deletions python/plugins/processing/gui/ScriptEditorDialog.py
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from processing.modeler.ModelerUtils import ModelerUtils

__author__ = 'Alexander Bruy'
__date__ = 'December 2012'
Expand All @@ -29,6 +28,7 @@
import codecs
import sys
import json
import os

from PyQt4.QtCore import *
from PyQt4.QtGui import *
Expand All @@ -37,12 +37,13 @@
from qgis.core import *
from qgis.utils import iface

from processing.modeler.ModelerUtils import ModelerUtils
from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.gui.HelpEditionDialog import HelpEditionDialog
from processing.algs.r.RAlgorithm import RAlgorithm
from processing.algs.r.RUtils import RUtils
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

import processing.resources_rc
Expand All @@ -62,7 +63,6 @@ def __init__(self, algType, alg):
self.setWindowFlags(Qt.WindowMinimizeButtonHint |
Qt.WindowMaximizeButtonHint |
Qt.WindowCloseButtonHint)

# Set icons
self.btnOpen.setIcon(
QgsApplication.getThemeIcon('/mActionFileOpen.svg'))
Expand All @@ -79,13 +79,15 @@ def __init__(self, algType, alg):
QgsApplication.getThemeIcon('/mActionEditPaste.png'))
self.btnUndo.setIcon(QgsApplication.getThemeIcon('/mActionUndo.png'))
self.btnRedo.setIcon(QgsApplication.getThemeIcon('/mActionRedo.png'))
self.btnSnippets.setIcon(QgsApplication.getThemeIcon('/mActionHelpAPI.png'))

# Connect signals and slots
self.btnOpen.clicked.connect(self.openScript)
self.btnSave.clicked.connect(self.save)
self.btnSaveAs.clicked.connect(self.saveAs)
self.btnEditHelp.clicked.connect(self.editHelp)
self.btnRun.clicked.connect(self.runAlgorithm)
self.btnSnippets.clicked.connect(self.showSnippets)
self.btnCut.clicked.connect(self.editor.cut)
self.btnCopy.clicked.connect(self.editor.copy)
self.btnPaste.clicked.connect(self.editor.paste)
Expand All @@ -96,6 +98,27 @@ def __init__(self, algType, alg):
self.alg = alg
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:
self.filename = self.alg.descriptionFile
self.editor.setText(self.alg.script)
Expand All @@ -109,6 +132,14 @@ def __init__(self, algType, alg):

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):
if self.hasChanged:
ret = QMessageBox.question(self, self.tr('Unsaved changes'),
Expand Down
16 changes: 16 additions & 0 deletions python/plugins/processing/script/snippets.py
@@ -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'])
17 changes: 17 additions & 0 deletions python/plugins/processing/ui/DlgScriptEditor.ui
Expand Up @@ -219,6 +219,23 @@
</property>
</widget>
</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>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down

0 comments on commit ac0edda

Please sign in to comment.