From 2b89a364f970c6a24ebfe067bb305ddc5778c679 Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Mon, 2 Mar 2020 08:14:25 -0500 Subject: [PATCH] use close instead of revert as button name, remember last snippet edited --- __init__.py | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/__init__.py b/__init__.py index 1df863e..fe881bd 100644 --- a/__init__.py +++ b/__init__.py @@ -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 @@ -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) @@ -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() @@ -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) @@ -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(): @@ -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: @@ -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()) @@ -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?")) @@ -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): @@ -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():