16
16
* *
17
17
***************************************************************************
18
18
"""
19
- from processing .modeler .ModelerUtils import ModelerUtils
20
19
21
20
__author__ = 'Alexander Bruy'
22
21
__date__ = 'December 2012'
29
28
import codecs
30
29
import sys
31
30
import json
31
+ import os
32
32
33
33
from PyQt4 .QtCore import *
34
34
from PyQt4 .QtGui import *
37
37
from qgis .core import *
38
38
from qgis .utils import iface
39
39
40
+ from processing .modeler .ModelerUtils import ModelerUtils
40
41
from processing .gui .AlgorithmDialog import AlgorithmDialog
41
42
from processing .gui .HelpEditionDialog import HelpEditionDialog
42
43
from processing .algs .r .RAlgorithm import RAlgorithm
43
44
from processing .algs .r .RUtils import RUtils
44
45
from processing .script .ScriptAlgorithm import ScriptAlgorithm
45
- from processing .script . ScriptUtils import ScriptUtils
46
+ from processing .script import ScriptUtils
46
47
from processing .ui .ui_DlgScriptEditor import Ui_DlgScriptEditor
47
48
48
49
import processing .resources_rc
@@ -62,7 +63,6 @@ def __init__(self, algType, alg):
62
63
self .setWindowFlags (Qt .WindowMinimizeButtonHint |
63
64
Qt .WindowMaximizeButtonHint |
64
65
Qt .WindowCloseButtonHint )
65
-
66
66
# Set icons
67
67
self .btnOpen .setIcon (
68
68
QgsApplication .getThemeIcon ('/mActionFileOpen.svg' ))
@@ -79,13 +79,15 @@ def __init__(self, algType, alg):
79
79
QgsApplication .getThemeIcon ('/mActionEditPaste.png' ))
80
80
self .btnUndo .setIcon (QgsApplication .getThemeIcon ('/mActionUndo.png' ))
81
81
self .btnRedo .setIcon (QgsApplication .getThemeIcon ('/mActionRedo.png' ))
82
+ self .btnSnippets .setIcon (QgsApplication .getThemeIcon ('/mActionHelpAPI.png' ))
82
83
83
84
# Connect signals and slots
84
85
self .btnOpen .clicked .connect (self .openScript )
85
86
self .btnSave .clicked .connect (self .save )
86
87
self .btnSaveAs .clicked .connect (self .saveAs )
87
88
self .btnEditHelp .clicked .connect (self .editHelp )
88
89
self .btnRun .clicked .connect (self .runAlgorithm )
90
+ self .btnSnippets .clicked .connect (self .showSnippets )
89
91
self .btnCut .clicked .connect (self .editor .cut )
90
92
self .btnCopy .clicked .connect (self .editor .copy )
91
93
self .btnPaste .clicked .connect (self .editor .paste )
@@ -96,6 +98,27 @@ def __init__(self, algType, alg):
96
98
self .alg = alg
97
99
self .algType = algType
98
100
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
+
99
122
if self .alg is not None :
100
123
self .filename = self .alg .descriptionFile
101
124
self .editor .setText (self .alg .script )
@@ -109,6 +132,14 @@ def __init__(self, algType, alg):
109
132
110
133
self .editor .setLexerType (self .algType )
111
134
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
+
112
143
def closeEvent (self , evt ):
113
144
if self .hasChanged :
114
145
ret = QMessageBox .question (self , self .tr ('Unsaved changes' ),
0 commit comments