From 67063e089ce48892f41e273f53548dea77ea4646 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 21 Nov 2020 14:46:01 +0100 Subject: [PATCH] Added insert menu entries for keywords --- nw/constants/constants.py | 2 +- nw/gui/doceditor.py | 27 ++++++++++++++++++++++++++- nw/gui/mainmenu.py | 33 ++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/nw/constants/constants.py b/nw/constants/constants.py index a32e60c4..a768da9d 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -167,7 +167,7 @@ class nwLabels(): nwKeyWords.POV_KEY : "Point of View", nwKeyWords.CHAR_KEY : "Characters", nwKeyWords.PLOT_KEY : "Plot", - nwKeyWords.TIME_KEY : "Time", + nwKeyWords.TIME_KEY : "Timeline", nwKeyWords.WORLD_KEY : "Locations", nwKeyWords.OBJECT_KEY : "Objects", nwKeyWords.ENTITY_KEY : "Entities", diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 522177fd..5f12b77a 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -54,7 +54,8 @@ from nw.core import NWDoc, NWSpellSimple, countWords from nw.gui.dochighlight import GuiDocHighlighter from nw.common import transferCase from nw.constants import ( - nwConst, nwAlert, nwUnicode, nwDocAction, nwDocInsert, nwItemClass + nwConst, nwAlert, nwUnicode, nwDocAction, nwDocInsert, nwItemClass, + nwKeyWords ) logger = logging.getLogger(__name__) @@ -755,6 +756,30 @@ class GuiDocEditor(QTextEdit): return True + def insertKeyWord(self, keyWord): + """Insert a keyword in the text editor, at the cursor position. + If the insert line is not blank, a new line is started. + """ + if keyWord not in nwKeyWords.VALID_KEYS: + logger.error("Invalid keyword '%s'" % keyWord) + return False + + logger.verbose("Inserting keyword '%s'" % keyWord) + + theCursor = self.textCursor() + theBlock = theCursor.block() + if not theBlock.isValid(): + logger.error("Filed to insert keyword '%s'" % keyWord) + return False + + theCursor.beginEditBlock() + if theBlock.length() > 1: + theCursor.insertText("\n") + theCursor.insertText("%s: " % keyWord) + theCursor.endEditBlock() + + return True + def closeSearch(self): """Close the search box. """ diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 706b5bba..bfa69441 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -32,7 +32,9 @@ from PyQt5.QtCore import QUrl, QProcess from PyQt5.QtGui import QDesktopServices from PyQt5.QtWidgets import QMenuBar, QAction -from nw.constants import nwItemType, nwItemClass, nwDocAction, nwDocInsert +from nw.constants import ( + nwItemType, nwItemClass, nwDocAction, nwDocInsert, nwKeyWords, nwLabels +) logger = logging.getLogger(__name__) @@ -61,10 +63,11 @@ class GuiMainMenu(QMenuBar): self._buildHelpMenu() # Function Pointers - self._docAction = self.theParent.passDocumentAction - self._moveTreeItem = self.theParent.treeView.moveTreeItem - self._newTreeItem = self.theParent.treeView.newTreeItem - self._docInsert = self.theParent.docEditor.insertText + self._docAction = self.theParent.passDocumentAction + self._moveTreeItem = self.theParent.treeView.moveTreeItem + self._newTreeItem = self.theParent.treeView.newTreeItem + self._docInsert = self.theParent.docEditor.insertText + self._insertKeyWord = self.theParent.docEditor.insertKeyWord logger.debug("GuiMainMenu initialisation complete") @@ -499,6 +502,26 @@ class GuiMainMenu(QMenuBar): # Insert self.insertMenu = self.addMenu("&Insert") + # Insert > Keywords and Tags + self.mInsKeywords = self.insertMenu.addMenu("Keywords and Tags") + self.mInsKWItems = {} + self.mInsKWItems[nwKeyWords.TAG_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, G") + self.mInsKWItems[nwKeyWords.POV_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, V") + self.mInsKWItems[nwKeyWords.CHAR_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, C") + self.mInsKWItems[nwKeyWords.PLOT_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, P") + self.mInsKWItems[nwKeyWords.TIME_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, T") + self.mInsKWItems[nwKeyWords.WORLD_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, L") + self.mInsKWItems[nwKeyWords.OBJECT_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, O") + self.mInsKWItems[nwKeyWords.ENTITY_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, E") + self.mInsKWItems[nwKeyWords.CUSTOM_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, X") + for n, keyWord in enumerate(self.mInsKWItems): + self.mInsKWItems[keyWord][0].setText(nwLabels.KEY_NAME[keyWord]) + self.mInsKWItems[keyWord][0].setShortcut(self.mInsKWItems[keyWord][1]) + self.mInsKWItems[keyWord][0].triggered.connect( + lambda n, keyWord=keyWord: self._insertKeyWord(keyWord) + ) + self.mInsKeywords.addAction(self.mInsKWItems[keyWord][0]) + # Insert > Short Dash self.aInsENDash = QAction("Short Dash", self) self.aInsENDash.setStatusTip("Insert short dash")