Skip to content

Commit

Permalink
use close instead of revert as button name, remember last snippet edited
Browse files Browse the repository at this point in the history
  • Loading branch information
psifertex committed Mar 2, 2020
1 parent 380205c commit 2b89a36
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,14 @@ def __init__(self, parent=None):
self.setWindowModality(Qt.ApplicationModal)
self.title = QLabel(self.tr("Snippet Editor"))
self.saveButton = QPushButton(self.tr("Save"))
self.revertButton = QPushButton(self.tr("Revert"))
self.closeButton = QPushButton(self.tr("Close"))
self.clearHotkeyButton = QPushButton(self.tr("Clear Hotkey"))
self.setWindowTitle(self.title.text())
self.newFolderButton = QPushButton("New Folder")
self.deleteSnippetButton = QPushButton("Delete")
self.newSnippetButton = QPushButton("New Snippet")
self.edit = QPlainTextEdit()
self.edit.setPlaceholderText("python code")
self.resetting = False
self.columns = 3

Expand All @@ -118,9 +119,7 @@ def __init__(self, parent=None):
self.currentFileLabel = QLabel()
self.currentFile = ""
self.snippetDescription = QLineEdit()
self.snippetEditsPending = False

self.clearSelection()
self.snippetDescription.setPlaceholderText("optional description")

#Set Editbox Size
font = getMonospaceFont(self)
Expand Down Expand Up @@ -158,7 +157,7 @@ def __init__(self, parent=None):
buttons.addWidget(self.clearHotkeyButton)
buttons.addWidget(self.keySequenceEdit)
buttons.addWidget(self.currentHotkeyLabel)
buttons.addWidget(self.revertButton)
buttons.addWidget(self.closeButton)
buttons.addWidget(self.saveButton)

description = QHBoxLayout()
Expand All @@ -167,9 +166,8 @@ def __init__(self, parent=None):

vlayoutWidget = QWidget()
vlayout = QVBoxLayout()
vlayout.addWidget(self.currentFileLabel)
vlayout.addWidget(self.edit)
vlayout.addLayout(description)
vlayout.addWidget(self.edit)
vlayout.addLayout(buttons)
vlayoutWidget.setLayout(vlayout)

Expand All @@ -193,15 +191,27 @@ def __init__(self, parent=None):

# Add signals
self.saveButton.clicked.connect(self.save)
self.revertButton.clicked.connect(self.loadSnippet)
self.closeButton.clicked.connect(self.close)
self.clearHotkeyButton.clicked.connect(self.clearHotkey)
self.tree.selectionModel().selectionChanged.connect(self.selectFile)
self.newSnippetButton.clicked.connect(self.newFileDialog)
self.deleteSnippetButton.clicked.connect(self.deleteSnippet)
self.newFolderButton.clicked.connect(self.newFolder)

#Read-only until new snippet
self.readOnly(True)
if self.settings.contains("ui/snippeteditor/selected"):
selectedName = self.settings.value("ui/snippeteditor/selected")
self.tree.selectionModel().select(self.files.index(selectedName), QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows)
if self.tree.selectionModel().hasSelection():
self.selectFile(self.tree.selectionModel().selection(), None)
self.edit.setFocus()
cursor = self.edit.textCursor()
cursor.setPosition(self.edit.document().characterCount()-1)
self.edit.setTextCursor(cursor)
else:
self.readOnly(True)
else:
self.readOnly(True)


@staticmethod
def registerAllSnippets():
Expand All @@ -216,7 +226,7 @@ def registerAllSnippets():
snippetKeys = None
(snippetDescription, snippetKeys, snippetCode) = loadSnippetFromFile(snippet)
if not snippetDescription:
actionText = "Snippets\\" + snippet
actionText = "Snippets\\" + os.path.basename(snippet).rstrip(".py")
else:
actionText = "Snippets\\" + snippetDescription
if snippetCode:
Expand All @@ -234,6 +244,7 @@ def clearSelection(self):
self.currentFileLabel.setText("")
self.snippetDescription.setText("")
self.edit.setPlainText("")
self.currentFile = ""

def reject(self):
self.settings.setValue("ui/snippeteditor/geometry", self.saveGeometry())
Expand All @@ -252,19 +263,21 @@ def newFolder(self):
if QFileInfo(selection).isDir():
QDir(selection).mkdir(folderName)
else:
QDir(snippetPath).mkdir(folderName)
QDir(snippetPath).mkdir(folderName)

def selectFile(self, new, old):
if (self.resetting):
self.resetting = False
return
newSelection = self.files.filePath(new.indexes()[0])
self.settings.setValue("ui/snippeteditor/selected", newSelection)
if QFileInfo(newSelection).isDir():
self.readOnly(True)
self.clearSelection()
self.tree.clearSelection()
self.currentFile = ""
return

if old.length() > 0:
if old and old.length() > 0:
oldSelection = self.files.filePath(old.indexes()[0])
if not QFileInfo(oldSelection).isDir() and self.snippetChanged():
question = QMessageBox.question(self, self.tr("Discard"), self.tr("Snippet changed. Discard changes?"))
Expand Down Expand Up @@ -292,10 +305,12 @@ def newFileDialog(self):
index = self.tree.selectionModel().currentIndex()
selection = self.files.filePath(index)
if QFileInfo(selection).isDir():
open(os.path.join(selection, snippetName), "w").close()
path = os.path.join(selection, snippetName)
else:
open(os.path.join(snippetPath, snippetName), "w").close()
path = os.path.join(snippetPath, snippetName)
self.readOnly(False)
open(path, "w").close()
self.tree.setCurrentIndex(self.files.index(path))
log_debug("Snippet %s created." % snippetName)

def readOnly(self, flag):
Expand Down Expand Up @@ -323,8 +338,6 @@ def snippetChanged(self):
if (self.currentFile == "" or QFileInfo(self.currentFile).isDir()):
return False
(snippetDescription, snippetKeys, snippetCode) = loadSnippetFromFile(self.currentFile)
if (not snippetCode):
return False
if snippetKeys == None and not self.keySequenceEdit.keySequence().isEmpty():
return True
if snippetKeys != None and snippetKeys != self.keySequenceEdit.keySequence().toString():
Expand Down

0 comments on commit 2b89a36

Please sign in to comment.