@@ -102,13 +102,14 @@ def __init__(self, parent=None):
102
102
self .setWindowModality (Qt .ApplicationModal )
103
103
self .title = QLabel (self .tr ("Snippet Editor" ))
104
104
self .saveButton = QPushButton (self .tr ("Save" ))
105
- self .revertButton = QPushButton (self .tr ("Revert " ))
105
+ self .closeButton = QPushButton (self .tr ("Close " ))
106
106
self .clearHotkeyButton = QPushButton (self .tr ("Clear Hotkey" ))
107
107
self .setWindowTitle (self .title .text ())
108
108
self .newFolderButton = QPushButton ("New Folder" )
109
109
self .deleteSnippetButton = QPushButton ("Delete" )
110
110
self .newSnippetButton = QPushButton ("New Snippet" )
111
111
self .edit = QPlainTextEdit ()
112
+ self .edit .setPlaceholderText ("python code" )
112
113
self .resetting = False
113
114
self .columns = 3
114
115
@@ -118,9 +119,7 @@ def __init__(self, parent=None):
118
119
self .currentFileLabel = QLabel ()
119
120
self .currentFile = ""
120
121
self .snippetDescription = QLineEdit ()
121
- self .snippetEditsPending = False
122
-
123
- self .clearSelection ()
122
+ self .snippetDescription .setPlaceholderText ("optional description" )
124
123
125
124
#Set Editbox Size
126
125
font = getMonospaceFont (self )
@@ -158,7 +157,7 @@ def __init__(self, parent=None):
158
157
buttons .addWidget (self .clearHotkeyButton )
159
158
buttons .addWidget (self .keySequenceEdit )
160
159
buttons .addWidget (self .currentHotkeyLabel )
161
- buttons .addWidget (self .revertButton )
160
+ buttons .addWidget (self .closeButton )
162
161
buttons .addWidget (self .saveButton )
163
162
164
163
description = QHBoxLayout ()
@@ -167,9 +166,8 @@ def __init__(self, parent=None):
167
166
168
167
vlayoutWidget = QWidget ()
169
168
vlayout = QVBoxLayout ()
170
- vlayout .addWidget (self .currentFileLabel )
171
- vlayout .addWidget (self .edit )
172
169
vlayout .addLayout (description )
170
+ vlayout .addWidget (self .edit )
173
171
vlayout .addLayout (buttons )
174
172
vlayoutWidget .setLayout (vlayout )
175
173
@@ -193,15 +191,27 @@ def __init__(self, parent=None):
193
191
194
192
# Add signals
195
193
self .saveButton .clicked .connect (self .save )
196
- self .revertButton .clicked .connect (self .loadSnippet )
194
+ self .closeButton .clicked .connect (self .close )
197
195
self .clearHotkeyButton .clicked .connect (self .clearHotkey )
198
196
self .tree .selectionModel ().selectionChanged .connect (self .selectFile )
199
197
self .newSnippetButton .clicked .connect (self .newFileDialog )
200
198
self .deleteSnippetButton .clicked .connect (self .deleteSnippet )
201
199
self .newFolderButton .clicked .connect (self .newFolder )
202
200
203
- #Read-only until new snippet
204
- self .readOnly (True )
201
+ if self .settings .contains ("ui/snippeteditor/selected" ):
202
+ selectedName = self .settings .value ("ui/snippeteditor/selected" )
203
+ self .tree .selectionModel ().select (self .files .index (selectedName ), QItemSelectionModel .ClearAndSelect | QItemSelectionModel .Rows )
204
+ if self .tree .selectionModel ().hasSelection ():
205
+ self .selectFile (self .tree .selectionModel ().selection (), None )
206
+ self .edit .setFocus ()
207
+ cursor = self .edit .textCursor ()
208
+ cursor .setPosition (self .edit .document ().characterCount ()- 1 )
209
+ self .edit .setTextCursor (cursor )
210
+ else :
211
+ self .readOnly (True )
212
+ else :
213
+ self .readOnly (True )
214
+
205
215
206
216
@staticmethod
207
217
def registerAllSnippets ():
@@ -216,7 +226,7 @@ def registerAllSnippets():
216
226
snippetKeys = None
217
227
(snippetDescription , snippetKeys , snippetCode ) = loadSnippetFromFile (snippet )
218
228
if not snippetDescription :
219
- actionText = "Snippets\\ " + snippet
229
+ actionText = "Snippets\\ " + os . path . basename ( snippet ). rstrip ( ".py" )
220
230
else :
221
231
actionText = "Snippets\\ " + snippetDescription
222
232
if snippetCode :
@@ -234,6 +244,7 @@ def clearSelection(self):
234
244
self .currentFileLabel .setText ("" )
235
245
self .snippetDescription .setText ("" )
236
246
self .edit .setPlainText ("" )
247
+ self .currentFile = ""
237
248
238
249
def reject (self ):
239
250
self .settings .setValue ("ui/snippeteditor/geometry" , self .saveGeometry ())
@@ -252,19 +263,21 @@ def newFolder(self):
252
263
if QFileInfo (selection ).isDir ():
253
264
QDir (selection ).mkdir (folderName )
254
265
else :
255
- QDir (snippetPath ).mkdir (folderName )
266
+ QDir (snippetPath ).mkdir (folderName )
256
267
257
268
def selectFile (self , new , old ):
258
269
if (self .resetting ):
259
270
self .resetting = False
260
271
return
261
272
newSelection = self .files .filePath (new .indexes ()[0 ])
273
+ self .settings .setValue ("ui/snippeteditor/selected" , newSelection )
262
274
if QFileInfo (newSelection ).isDir ():
263
275
self .readOnly (True )
264
- self .clearSelection ()
276
+ self .tree .clearSelection ()
277
+ self .currentFile = ""
265
278
return
266
279
267
- if old .length () > 0 :
280
+ if old and old .length () > 0 :
268
281
oldSelection = self .files .filePath (old .indexes ()[0 ])
269
282
if not QFileInfo (oldSelection ).isDir () and self .snippetChanged ():
270
283
question = QMessageBox .question (self , self .tr ("Discard" ), self .tr ("Snippet changed. Discard changes?" ))
@@ -292,10 +305,12 @@ def newFileDialog(self):
292
305
index = self .tree .selectionModel ().currentIndex ()
293
306
selection = self .files .filePath (index )
294
307
if QFileInfo (selection ).isDir ():
295
- open ( os .path .join (selection , snippetName ), "w" ). close ( )
308
+ path = os .path .join (selection , snippetName )
296
309
else :
297
- open ( os .path .join (snippetPath , snippetName ), "w" ). close ( )
310
+ path = os .path .join (snippetPath , snippetName )
298
311
self .readOnly (False )
312
+ open (path , "w" ).close ()
313
+ self .tree .setCurrentIndex (self .files .index (path ))
299
314
log_debug ("Snippet %s created." % snippetName )
300
315
301
316
def readOnly (self , flag ):
@@ -323,8 +338,6 @@ def snippetChanged(self):
323
338
if (self .currentFile == "" or QFileInfo (self .currentFile ).isDir ()):
324
339
return False
325
340
(snippetDescription , snippetKeys , snippetCode ) = loadSnippetFromFile (self .currentFile )
326
- if (not snippetCode ):
327
- return False
328
341
if snippetKeys == None and not self .keySequenceEdit .keySequence ().isEmpty ():
329
342
return True
330
343
if snippetKeys != None and snippetKeys != self .keySequenceEdit .keySequence ().toString ():
0 commit comments