Skip to content

Commit a73cb7d

Browse files
committed
make snippets dialog modal and also disable edit fields until a new snippet is created
1 parent a242888 commit a73cb7d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Snippets(QDialog):
8181
def __init__(self, parent=None):
8282
super(Snippets, self).__init__(parent)
8383
# Create widgets
84-
self.setWindowModality(Qt.NonModal)
84+
self.setWindowModality(Qt.ApplicationModal)
8585
self.title = QLabel(self.tr("Snippet Editor"))
8686
self.saveButton = QPushButton(self.tr("Save"))
8787
self.revertButton = QPushButton(self.tr("Revert"))
@@ -182,6 +182,9 @@ def __init__(self, parent=None):
182182
self.deleteSnippetButton.clicked.connect(self.deleteSnippet)
183183
self.newFolderButton.clicked.connect(self.newFolder)
184184

185+
#Read-only until new snippet
186+
self.readOnly(True)
187+
185188
@staticmethod
186189
def registerAllSnippets():
187190
for action in list(filter(lambda x: x.startswith("Snippets\\"), UIAction.getAllRegisteredActions())):
@@ -239,6 +242,7 @@ def selectFile(self, new, old):
239242
return
240243
newSelection = self.files.filePath(new.indexes()[0])
241244
if QFileInfo(newSelection).isDir():
245+
self.readOnly(True)
242246
self.clearSelection()
243247
return
244248

@@ -260,6 +264,7 @@ def loadSnippet(self):
260264
self.snippetDescription.setText(snippetDescription) if snippetDescription else self.snippetDescription.setText("")
261265
self.keySequenceEdit.setKeySequence(snippetKeys) if snippetKeys else self.keySequenceEdit.setKeySequence(QKeySequence(""))
262266
self.edit.setPlainText(snippetCode) if snippetCode else self.edit.setPlainText("")
267+
self.readOnly(False)
263268

264269
def newFileDialog(self):
265270
(snippetName, ok) = QInputDialog.getText(self, self.tr("Snippet Name"), self.tr("Snippet Name: "))
@@ -272,8 +277,20 @@ def newFileDialog(self):
272277
open(os.path.join(selection, snippetName), "w").close()
273278
else:
274279
open(os.path.join(snippetPath, snippetName), "w").close()
280+
self.readOnly(False)
275281
log_debug("Snippet %s created." % snippetName)
276282

283+
def readOnly(self, flag):
284+
self.keySequenceEdit.setEnabled(not flag)
285+
self.snippetDescription.setReadOnly(flag)
286+
self.edit.setReadOnly(flag)
287+
if flag:
288+
self.snippetDescription.setDisabled(True)
289+
self.edit.setDisabled(True)
290+
else:
291+
self.snippetDescription.setEnabled(True)
292+
self.edit.setEnabled(True)
293+
277294
def deleteSnippet(self):
278295
selection = self.tree.selectedIndexes()[::self.columns][0] #treeview returns each selected element in the row
279296
snippetName = self.files.fileName(selection)
@@ -282,6 +299,7 @@ def deleteSnippet(self):
282299
log_debug("Deleting snippet %s." % snippetName)
283300
self.clearSelection()
284301
self.files.remove(selection)
302+
self.registerAllSnippets()
285303

286304
def snippetChanged(self):
287305
if (self.currentFile == "" or QFileInfo(self.currentFile).isDir()):

0 commit comments

Comments
 (0)