31
31
import os
32
32
33
33
from qgis .PyQt import uic
34
- from qgis .PyQt .QtCore import Qt
34
+ from qgis .PyQt .QtCore import Qt , QSize , QByteArray , QSettings
35
35
from qgis .PyQt .QtGui import QIcon , QCursor
36
- from qgis .PyQt .QtWidgets import QMenu , QAction , QMessageBox , QFileDialog , QApplication
36
+ from qgis .PyQt .QtWidgets import (QMenu ,
37
+ QAction ,
38
+ QToolButton ,
39
+ QMessageBox ,
40
+ QFileDialog ,
41
+ QApplication )
37
42
38
43
from qgis .core import QgsApplication
39
44
from qgis .utils import iface
@@ -65,40 +70,52 @@ def __init__(self, algType, alg):
65
70
self .setWindowFlags (Qt .WindowMinimizeButtonHint |
66
71
Qt .WindowMaximizeButtonHint |
67
72
Qt .WindowCloseButtonHint )
68
- # Set icons
69
- self .btnOpen .setIcon (
73
+
74
+ settings = QSettings ()
75
+ self .restoreState (settings .value ("/Processing/stateScriptEditor" , QByteArray ()))
76
+ self .restoreGeometry (settings .value ("/Processing/geometryScriptEditor" , QByteArray ()))
77
+
78
+ iconSize = settings .value ("iconsize" , 24 )
79
+ self .toolBar .setIconSize (QSize (iconSize , iconSize ))
80
+
81
+ self .actionOpenScript .setIcon (
70
82
QgsApplication .getThemeIcon ('/mActionFileOpen.svg' ))
71
- self .btnSave .setIcon (
83
+ self .actionSaveScript .setIcon (
72
84
QgsApplication .getThemeIcon ('/mActionFileSave.svg' ))
73
- self .btnSaveAs .setIcon (
85
+ self .actionSaveScriptAs .setIcon (
74
86
QgsApplication .getThemeIcon ('/mActionFileSaveAs.svg' ))
75
- self .btnEditHelp .setIcon (
76
- QIcon (os .path .join (pluginPath , 'images' , 'edithelp.png' )))
77
- self .btnRun .setIcon (
78
- QIcon (os .path .join (pluginPath , 'images' , 'runalgorithm.png' )))
79
- self .btnCut .setIcon (QgsApplication .getThemeIcon ('/mActionEditCut.svg' ))
80
- self .btnCopy .setIcon (
87
+ self .actionEditScriptHelp .setIcon (
88
+ QgsApplication .getThemeIcon ('/mActionEditHelpContent.svg' ))
89
+ self .actionRunScript .setIcon (
90
+ QgsApplication .getThemeIcon ('/mActionStart.svg' ))
91
+ self .actionCut .setIcon (
92
+ QgsApplication .getThemeIcon ('/mActionEditCut.svg' ))
93
+ self .actionCopy .setIcon (
81
94
QgsApplication .getThemeIcon ('/mActionEditCopy.svg' ))
82
- self .btnPaste .setIcon (
95
+ self .actionPaste .setIcon (
83
96
QgsApplication .getThemeIcon ('/mActionEditPaste.svg' ))
84
- self .btnUndo .setIcon (QgsApplication .getThemeIcon ('/mActionUndo.svg' ))
85
- self .btnRedo .setIcon (QgsApplication .getThemeIcon ('/mActionRedo.svg' ))
86
- self .btnSnippets .setIcon (QgsApplication .getThemeIcon ('/mActionHelpAPI.png' ))
97
+ self .actionUndo .setIcon (
98
+ QgsApplication .getThemeIcon ('/mActionUndo.svg' ))
99
+ self .actionRedo .setIcon (
100
+ QgsApplication .getThemeIcon ('/mActionRedo.svg' ))
101
+ self .actionIncreaseFontSize .setIcon (
102
+ QgsApplication .getThemeIcon ('/mActionIncreaseFont.svg' ))
103
+ self .actionDecreaseFontSize .setIcon (
104
+ QgsApplication .getThemeIcon ('/mActionDecreaseFont.svg' ))
87
105
88
106
# Connect signals and slots
89
- self .btnOpen .clicked .connect (self .openScript )
90
- self .btnSave .clicked .connect (self .save )
91
- self .btnSaveAs .clicked .connect (self .saveAs )
92
- self .btnEditHelp .clicked .connect (self .editHelp )
93
- self .btnRun .clicked .connect (self .runAlgorithm )
94
- self .btnSnippets .clicked .connect (self .showSnippets )
95
- self .btnCut .clicked .connect (self .editor .cut )
96
- self .btnCopy .clicked .connect (self .editor .copy )
97
- self .btnPaste .clicked .connect (self .editor .paste )
98
- self .btnUndo .clicked .connect (self .editor .undo )
99
- self .btnRedo .clicked .connect (self .editor .redo )
100
- self .btnIncreaseFont .clicked .connect (self .editor .zoomIn )
101
- self .btnDecreaseFont .clicked .connect (self .editor .zoomOut )
107
+ self .actionOpenScript .triggered .connect (self .openScript )
108
+ self .actionSaveScript .triggered .connect (self .save )
109
+ self .actionSaveScriptAs .triggered .connect (self .saveAs )
110
+ self .actionEditScriptHelp .triggered .connect (self .editHelp )
111
+ self .actionRunScript .triggered .connect (self .runAlgorithm )
112
+ self .actionCut .triggered .connect (self .editor .cut )
113
+ self .actionCopy .triggered .connect (self .editor .copy )
114
+ self .actionPaste .triggered .connect (self .editor .paste )
115
+ self .actionUndo .triggered .connect (self .editor .undo )
116
+ self .actionRedo .triggered .connect (self .editor .redo )
117
+ self .actionIncreaseFontSize .triggered .connect (self .editor .zoomIn )
118
+ self .actionDecreaseFontSize .triggered .connect (self .editor .zoomOut )
102
119
self .editor .textChanged .connect (lambda : self .setHasChanged (True ))
103
120
104
121
self .alg = alg
@@ -107,7 +124,7 @@ def __init__(self, algType, alg):
107
124
self .snippets = {}
108
125
if self .algType == self .SCRIPT_PYTHON :
109
126
path = os .path .join (os .path .dirname (os .path .dirname (__file__ )), "script" , "snippets.py" )
110
- with open (path ) as f :
127
+ with codecs . open (path , 'r' , encoding = 'utf-8' ) as f :
111
128
lines = f .readlines ()
112
129
snippetlines = []
113
130
name = None
@@ -122,8 +139,8 @@ def __init__(self, algType, alg):
122
139
if snippetlines :
123
140
self .snippets [name ] = "" .join (snippetlines )
124
141
125
- if not self .snippets :
126
- self .btnSnippets .setVisible (False )
142
+ #if self.snippets:
143
+ # self.btnSnippets.setVisible(False)
127
144
128
145
if self .alg is not None :
129
146
self .filename = self .alg .descriptionFile
@@ -138,13 +155,13 @@ def __init__(self, algType, alg):
138
155
139
156
self .editor .setLexerType (self .algType )
140
157
141
- def showSnippets (self , evt ):
142
- popupmenu = QMenu ()
143
- for name , snippet in list (self .snippets .items ()):
144
- action = QAction (self .tr (name ), self .btnSnippets )
145
- action .triggered [()].connect (lambda snippet = snippet : self .editor .insert (snippet ))
146
- popupmenu .addAction (action )
147
- popupmenu .exec_ (QCursor .pos ())
158
+ # def showSnippets(self, evt):
159
+ # popupmenu = QMenu()
160
+ # for name, snippet in list(self.snippets.items()):
161
+ # action = QAction(self.tr(name), self.btnSnippets)
162
+ # action.triggered[()].connect(lambda snippet=snippet: self.editor.insert(snippet))
163
+ # popupmenu.addAction(action)
164
+ # popupmenu.exec_(QCursor.pos())
148
165
149
166
def closeEvent (self , evt ):
150
167
if self .hasChanged :
@@ -189,7 +206,7 @@ def openScript(self):
189
206
scriptDir = RUtils .RScriptsFolders ()[0 ]
190
207
filterName = self .tr ('Processing R script (*.rsx)' )
191
208
192
- self .filename = QFileDialog .getOpenFileName (
209
+ self .filename , fileFilter = QFileDialog .getOpenFileName (
193
210
self , self .tr ('Open script' ), scriptDir , filterName )
194
211
195
212
if self .filename == '' :
@@ -220,9 +237,8 @@ def saveScript(self, saveAs):
220
237
scriptDir = RUtils .RScriptsFolders ()[0 ]
221
238
filterName = self .tr ('Processing R script (*.rsx)' )
222
239
223
- self .filename = str (QFileDialog .getSaveFileName (self ,
224
- self .tr ('Save script' ), scriptDir ,
225
- filterName ))
240
+ self .filename , fileFilter = QFileDialog .getSaveFileName (
241
+ self , self .tr ('Save script' ), scriptDir , filterName )
226
242
227
243
if self .filename :
228
244
if self .algType == self .SCRIPT_PYTHON and \
@@ -239,17 +255,17 @@ def saveScript(self, saveAs):
239
255
with codecs .open (self .filename , 'w' , encoding = 'utf-8' ) as fout :
240
256
fout .write (text )
241
257
except IOError :
242
- QMessageBox .warning (self , self . tr ( 'I/O error' ),
243
- self .tr ('Unable to save edits. Reason: \n %s' )
244
- % str (sys .exc_info ()[1 ])
258
+ QMessageBox .warning (self ,
259
+ self .tr ('I/O error' ),
260
+ self . tr ( 'Unable to save edits. Reason: \n {}' ). format (sys .exc_info ()[1 ])
245
261
)
246
262
return
247
263
self .update = True
248
264
249
265
# If help strings were defined before saving the script for
250
266
# the first time, we do it here
251
267
if self .help :
252
- with open (self .filename + '.help' , 'w' ) as f :
268
+ with codecs . open (self .filename + '.help' , 'w' , encoding = 'utf-8 ' ) as f :
253
269
json .dump (self .help , f )
254
270
self .help = None
255
271
self .setHasChanged (False )
@@ -258,7 +274,7 @@ def saveScript(self, saveAs):
258
274
259
275
def setHasChanged (self , hasChanged ):
260
276
self .hasChanged = hasChanged
261
- self .btnSave .setEnabled (hasChanged )
277
+ self .actionSaveScript .setEnabled (hasChanged )
262
278
263
279
def runAlgorithm (self ):
264
280
if self .algType == self .SCRIPT_PYTHON :
0 commit comments