diff --git a/novelwriter/gui/mainmenu.py b/novelwriter/gui/mainmenu.py index 449b9b70..4714547d 100644 --- a/novelwriter/gui/mainmenu.py +++ b/novelwriter/gui/mainmenu.py @@ -34,7 +34,10 @@ from PyQt5.QtWidgets import QAction, QMenuBar from novelwriter import CONFIG, SHARED from novelwriter.common import openExternalPath, qtLambda -from novelwriter.constants import nwConst, nwKeyWords, nwLabels, nwStyles, nwUnicode, trConst +from novelwriter.constants import ( + nwConst, nwKeyWords, nwLabels, nwShortcode, nwStats, nwStyles, nwUnicode, + trConst +) from novelwriter.enum import nwDocAction, nwDocInsert, nwFocus, nwView from novelwriter.extensions.eventfilters import StatusTipFilter @@ -571,25 +574,11 @@ class GuiMainMenu(QMenuBar): # Insert > Tags and References self.mInsKeywords = self.insMenu.addMenu(self.tr("Tags and References")) - 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.FOCUS_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, F") - 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") - self.mInsKWItems[nwKeyWords.MENTION_KEY] = (QAction(self.mInsKeywords), "Ctrl+K, M") - for key in self.mInsKWItems: - action = self.mInsKWItems[key][0] - action.setText(trConst(nwLabels.KEY_NAME[key])) - action.setShortcut(self.mInsKWItems[key][1]) + for key in nwKeyWords.ALL_KEYS: + action = self.mInsKeywords.addAction(trConst(nwLabels.KEY_NAME[key])) + action.setShortcut(nwLabels.KEY_SHORTCUT[key]) action.triggered.connect(qtLambda(self.requestDocKeyWordInsert.emit, key)) - self.mInsKeywords.addAction(action) - self.mainGui.addAction(self.mInsKWItems[key][0]) + self.mainGui.addAction(action) # Insert > Special Comments self.mInsComments = self.insMenu.addMenu(self.tr("Special Comments")) @@ -610,6 +599,13 @@ class GuiMainMenu(QMenuBar): ) self.mainGui.addAction(self.aInsShort) + # Insert > Word/Character Count + self.mInsField = self.insMenu.addMenu(self.tr("Word/Character Count")) + for field in nwStats.ALL_FIELDS: + value = nwShortcode.FIELD_VALUE.format(field) + action = self.mInsField.addAction(trConst(nwLabels.STATS_NAME[field])) + action.triggered.connect(qtLambda(self.requestDocInsertText.emit, value)) + # Insert > Breaks and Vertical Space self.mInsBreaks = self.insMenu.addMenu(self.tr("Breaks and Vertical Space")) diff --git a/tests/test_gui/test_gui_mainmenu.py b/tests/test_gui/test_gui_mainmenu.py index 32a13d55..34d6901b 100644 --- a/tests/test_gui/test_gui_mainmenu.py +++ b/tests/test_gui/test_gui_mainmenu.py @@ -29,7 +29,7 @@ from PyQt5.QtGui import QDesktopServices, QTextBlock, QTextCursor from PyQt5.QtWidgets import QAction, QFileDialog, QMessageBox from novelwriter import CONFIG, SHARED -from novelwriter.constants import nwKeyWords, nwUnicode +from novelwriter.constants import nwKeyWords, nwShortcode, nwStats, nwUnicode from novelwriter.enum import nwDocAction, nwDocInsert from novelwriter.gui.doceditor import GuiDocEditor from novelwriter.types import QtKeepAnchor, QtMoveRight @@ -70,161 +70,163 @@ def testGuiMainMenu_Slots(qtbot, monkeypatch, nwGUI, projPath): def testGuiMainMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum): """Test the main menu Edit and Format entries.""" monkeypatch.setattr(GuiDocEditor, "hasFocus", lambda *a: True) + mainMenu = nwGUI.mainMenu + docEditor = nwGUI.docEditor # Test Document Action with No Project - assert nwGUI.docEditor.docAction(nwDocAction.COPY) is False + assert docEditor.docAction(nwDocAction.COPY) is False assert nwGUI.openProject(prjLipsum) is True # Split By Chapter assert nwGUI.openDocument("4c4f28287af27") is True - nwGUI.docEditor.setCursorPosition(57) - cleanText = nwGUI.docEditor.getText()[54:101] + docEditor.setCursorPosition(57) + cleanText = docEditor.getText()[54:101] # Bold - nwGUI.mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) fmtStr = "**Pellentesque** nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:105] == fmtStr - nwGUI.mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + assert docEditor.getText()[54:105] == fmtStr + mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Italic - nwGUI.mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) fmtStr = "_Pellentesque_ nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:103] == fmtStr - nwGUI.mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + assert docEditor.getText()[54:103] == fmtStr + mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Strikethrough - nwGUI.mainMenu.aFmtStrike.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtStrike.activate(QAction.ActionEvent.Trigger) fmtStr = "~~Pellentesque~~ nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:105] == fmtStr - nwGUI.mainMenu.aFmtStrike.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + assert docEditor.getText()[54:105] == fmtStr + mainMenu.aFmtStrike.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Should get us back to plain - nwGUI.mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) - nwGUI.mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) - nwGUI.mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) - nwGUI.mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtItalic.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtBold.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Double Quotes - nwGUI.mainMenu.aFmtDQuote.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtDQuote.activate(QAction.ActionEvent.Trigger) fmtStr = "“Pellentesque” nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:103] == fmtStr - nwGUI.mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + assert docEditor.getText()[54:103] == fmtStr + mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Single Quotes - nwGUI.mainMenu.aFmtSQuote.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtSQuote.activate(QAction.ActionEvent.Trigger) fmtStr = "‘Pellentesque’ nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:103] == fmtStr - nwGUI.mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + assert docEditor.getText()[54:103] == fmtStr + mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Block Formats # ============= # cSpell:ignore Pellentesque erat nulla posuere commodo - nwGUI.docEditor.setCursorPosition(57) + docEditor.setCursorPosition(57) # Header 1 - nwGUI.mainMenu.aFmtHead1.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtHead1.activate(QAction.ActionEvent.Trigger) fmtStr = "# Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:103] == fmtStr + assert docEditor.getText()[54:103] == fmtStr # Header 2 - nwGUI.mainMenu.aFmtHead2.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtHead2.activate(QAction.ActionEvent.Trigger) fmtStr = "## Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:104] == fmtStr + assert docEditor.getText()[54:104] == fmtStr # Header 3 - nwGUI.mainMenu.aFmtHead3.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtHead3.activate(QAction.ActionEvent.Trigger) fmtStr = "### Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:105] == fmtStr + assert docEditor.getText()[54:105] == fmtStr # Header 4 - nwGUI.mainMenu.aFmtHead4.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtHead4.activate(QAction.ActionEvent.Trigger) fmtStr = "#### Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:106] == fmtStr + assert docEditor.getText()[54:106] == fmtStr # Title Format - nwGUI.mainMenu.aFmtTitle.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtTitle.activate(QAction.ActionEvent.Trigger) fmtStr = "#! Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:104] == fmtStr + assert docEditor.getText()[54:104] == fmtStr # Unnumbered Chapter - nwGUI.mainMenu.aFmtUnNum.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtUnNum.activate(QAction.ActionEvent.Trigger) fmtStr = "##! Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:105] == fmtStr + assert docEditor.getText()[54:105] == fmtStr # Hard Scene - nwGUI.mainMenu.aFmtHardSc.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtHardSc.activate(QAction.ActionEvent.Trigger) fmtStr = "###! Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:106] == fmtStr + assert docEditor.getText()[54:106] == fmtStr # Clear Format - nwGUI.mainMenu.aFmtNoFormat.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + mainMenu.aFmtNoFormat.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Comment On - nwGUI.mainMenu.aFmtComment.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtComment.activate(QAction.ActionEvent.Trigger) fmtStr = "% Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:103] == fmtStr + assert docEditor.getText()[54:103] == fmtStr # Comment Off - nwGUI.mainMenu.aFmtComment.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + mainMenu.aFmtComment.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Check comment with no space before text - nwGUI.docEditor.setCursorPosition(54) - nwGUI.docEditor.insertText("%") + docEditor.setCursorPosition(54) + docEditor.insertText("%") fmtStr = "%Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:102] == fmtStr + assert docEditor.getText()[54:102] == fmtStr - nwGUI.mainMenu.aFmtNoFormat.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + mainMenu.aFmtNoFormat.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Undo/Redo - nwGUI.mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) + mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) fmtStr = "%Pellentesque nec erat ut nulla posuere commodo." - assert nwGUI.docEditor.getText()[54:102] == fmtStr - nwGUI.mainMenu.aEditRedo.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:101] == cleanText + assert docEditor.getText()[54:102] == fmtStr + mainMenu.aEditRedo.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:101] == cleanText # Cut, Copy and Paste - nwGUI.docEditor.setCursorPosition(54) - nwGUI.docEditor._makeSelection(QTextCursor.WordUnderCursor) + docEditor.setCursorPosition(54) + docEditor._makeSelection(QTextCursor.WordUnderCursor) - nwGUI.mainMenu.aEditCut.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:104] == ( + mainMenu.aEditCut.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:104] == ( " nec erat ut nulla posuere commodo. Curabitur nisi" ) - nwGUI.mainMenu.aEditPaste.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:104] == ( + mainMenu.aEditPaste.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:104] == ( "Pellentesque nec erat ut nulla posuere commodo. Cu" ) - nwGUI.docEditor.setCursorPosition(54) - nwGUI.docEditor._makeSelection(QTextCursor.WordUnderCursor) + docEditor.setCursorPosition(54) + docEditor._makeSelection(QTextCursor.WordUnderCursor) - nwGUI.mainMenu.aEditCopy.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:104] == ( + mainMenu.aEditCopy.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:104] == ( "Pellentesque nec erat ut nulla posuere commodo. Cu" ) - nwGUI.docEditor.setCursorPosition(54) - nwGUI.mainMenu.aEditPaste.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[54:104] == ( + docEditor.setCursorPosition(54) + mainMenu.aEditPaste.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[54:104] == ( "PellentesquePellentesque nec erat ut nulla posuere" ) - nwGUI.mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) + mainMenu.aEditUndo.activate(QAction.ActionEvent.Trigger) # Select Paragraph/All - nwGUI.docEditor.setCursorPosition(57) - nwGUI.mainMenu.aSelectPar.activate(QAction.ActionEvent.Trigger) - cursor = nwGUI.docEditor.textCursor() + docEditor.setCursorPosition(57) + mainMenu.aSelectPar.activate(QAction.ActionEvent.Trigger) + cursor = docEditor.textCursor() assert cursor.selectedText() == ( "Pellentesque nec erat ut nulla posuere commodo. Curabitur nisi augue, imperdiet et porta " "imperdiet, efficitur id leo. Cras finibus arcu at nibh commodo congue. Proin suscipit " @@ -235,78 +237,78 @@ def testGuiMainMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum): "nunc lacus, imperdiet nec posuere ac, interdum non lectus." ) - nwGUI.docEditor.setCursorPosition(57) - nwGUI.mainMenu.aSelectAll.activate(QAction.ActionEvent.Trigger) - cursor = nwGUI.docEditor.textCursor() + docEditor.setCursorPosition(57) + mainMenu.aSelectAll.activate(QAction.ActionEvent.Trigger) + cursor = docEditor.textCursor() assert len(cursor.selectedText()) == 1910 # Clear the Text - nwGUI.docEditor.clear() - assert nwGUI.docEditor.isEmpty + docEditor.clear() + assert docEditor.isEmpty # Alignment & Indent # ================== cleanText = "A single, short paragraph.\n\n" - nwGUI.docEditor.setPlainText(cleanText) - nwGUI.docEditor.setCursorPosition(0) + docEditor.setPlainText(cleanText) + docEditor.setCursorPosition(0) # Left Align - nwGUI.mainMenu.aFmtAlignLeft.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtAlignLeft.activate(QAction.ActionEvent.Trigger) fmtStr = "A single, short paragraph. <<" - assert nwGUI.docEditor.getText()[:29] == fmtStr + assert docEditor.getText()[:29] == fmtStr # Right Align - nwGUI.mainMenu.aFmtAlignRight.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtAlignRight.activate(QAction.ActionEvent.Trigger) fmtStr = ">> A single, short paragraph." - assert nwGUI.docEditor.getText()[:29] == fmtStr + assert docEditor.getText()[:29] == fmtStr # Centre Align - nwGUI.mainMenu.aFmtAlignCentre.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtAlignCentre.activate(QAction.ActionEvent.Trigger) fmtStr = ">> A single, short paragraph. <<" - assert nwGUI.docEditor.getText()[:32] == fmtStr + assert docEditor.getText()[:32] == fmtStr # Left Indent - nwGUI.mainMenu.aFmtIndentLeft.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtIndentLeft.activate(QAction.ActionEvent.Trigger) fmtStr = "> A single, short paragraph." - assert nwGUI.docEditor.getText()[:28] == fmtStr + assert docEditor.getText()[:28] == fmtStr # Right Indent - nwGUI.mainMenu.aFmtIndentRight.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtIndentRight.activate(QAction.ActionEvent.Trigger) fmtStr = "> A single, short paragraph. <" - assert nwGUI.docEditor.getText()[:30] == fmtStr + assert docEditor.getText()[:30] == fmtStr # No Format - nwGUI.mainMenu.aFmtNoFormat.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText()[:30] == cleanText + mainMenu.aFmtNoFormat.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText()[:30] == cleanText # Other Checks # Replace Quotes - nwGUI.docEditor.setPlainText(( + docEditor.setPlainText(( "### New Text\n\n" "Text with 'single' quotes and 'tricky stuff's'.\n\n" "Also text with \"double\" quotes which are \"less tricky\".\n\n" )) - nwGUI.mainMenu.aSelectAll.activate(QAction.ActionEvent.Trigger) - nwGUI.mainMenu.aFmtReplSng.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == ( + mainMenu.aSelectAll.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtReplSng.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == ( "### New Text\n\n" "Text with ‘single’ quotes and ‘tricky stuff’s’.\n\n" "Also text with \"double\" quotes which are \"less tricky\".\n\n" ) - nwGUI.mainMenu.aSelectAll.activate(QAction.ActionEvent.Trigger) - nwGUI.mainMenu.aFmtReplDbl.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == ( + mainMenu.aSelectAll.activate(QAction.ActionEvent.Trigger) + mainMenu.aFmtReplDbl.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == ( "### New Text\n\n" "Text with ‘single’ quotes and ‘tricky stuff’s’.\n\n" "Also text with “double” quotes which are “less tricky”.\n\n" ) # Remove in-paragraph line breaks - nwGUI.docEditor.setPlainText(( + docEditor.setPlainText(( "### New Text\n\n" "@char: Someone\n" "@location: Somewhere\n\n" @@ -314,8 +316,8 @@ def testGuiMainMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum): "Here is some text\non multiple\nlines.\n\n" "With another paragraph\nhere." )) - nwGUI.mainMenu.aFmtRmBreaks.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == ( + mainMenu.aFmtRmBreaks.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == ( "### New Text\n\n" "@char: Someone\n" "@location: Somewhere\n\n" @@ -324,7 +326,7 @@ def testGuiMainMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum): "With another paragraph here.\n" ) - nwGUI.docEditor.setPlainText(( + docEditor.setPlainText(( "### New Text\n\n" "@char: Someone\n" "@location: Somewhere\n\n" @@ -332,12 +334,12 @@ def testGuiMainMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum): "Here is some text\non multiple\nlines.\n\n" "With another paragraph\nhere." )) - cursor = nwGUI.docEditor.textCursor() + cursor = docEditor.textCursor() cursor.setPosition(74) cursor.movePosition(QtMoveRight, QtKeepAnchor, 29) - nwGUI.docEditor.setTextCursor(cursor) - nwGUI.mainMenu.aFmtRmBreaks.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == ( + docEditor.setTextCursor(cursor) + mainMenu.aFmtRmBreaks.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == ( "### New Text\n\n" "@char: Someone\n" "@location: Somewhere\n\n" @@ -347,10 +349,10 @@ def testGuiMainMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum): ) # Test Invalid Document Action - assert not nwGUI.docEditor.docAction(nwDocAction.NO_ACTION) + assert not docEditor.docAction(nwDocAction.NO_ACTION) # Test Invalid Formats - nwGUI.docEditor.setPlainText(( + docEditor.setPlainText(( "### New Text\n\n" "@tag: Bod\n\n" "Text with 'single' quotes and 'tricky stuff's'.\n\n" @@ -358,15 +360,15 @@ def testGuiMainMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum): )) # Cannot Format Tag - nwGUI.docEditor.setCursorPosition(17) - assert not nwGUI.docEditor._formatBlock(nwDocAction.BLOCK_TXT) + docEditor.setCursorPosition(17) + assert not docEditor._formatBlock(nwDocAction.BLOCK_TXT) # Invalid Action - nwGUI.docEditor.setCursorPosition(30) - assert not nwGUI.docEditor._formatBlock(nwDocAction.NO_ACTION) + docEditor.setCursorPosition(30) + assert not docEditor._formatBlock(nwDocAction.NO_ACTION) # Ensure No Changes - assert nwGUI.docEditor.getText() == ( + assert docEditor.getText() == ( "### New Text\n\n" "@tag: Bod\n\n" "Text with 'single' quotes and 'tricky stuff's'.\n\n" @@ -383,198 +385,175 @@ def testGuiMainMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd assert nwGUI.projView.projTree._getTreeItem(C.hSceneDoc) is not None assert nwGUI.openDocument(C.hSceneDoc) is True - nwGUI.docEditor.clear() + mainMenu = nwGUI.mainMenu + docEditor = nwGUI.docEditor + docEditor.clear() # Test Faulty Inserts - nwGUI.docEditor.insertText("hello world") - assert nwGUI.docEditor.getText() == "hello world" - nwGUI.docEditor.clear() + docEditor.insertText("hello world") + assert docEditor.getText() == "hello world" + docEditor.clear() - nwGUI.docEditor.insertText(nwDocInsert.NO_INSERT) - assert nwGUI.docEditor.isEmpty + docEditor.insertText(nwDocInsert.NO_INSERT) + assert docEditor.isEmpty - nwGUI.docEditor.insertText(None) - assert nwGUI.docEditor.isEmpty + docEditor.insertText(None) + assert docEditor.isEmpty # qtbot.stop() - nwGUI.docEditor.clear() + docEditor.clear() # Check Menu Entries - nwGUI.mainMenu.aInsENDash.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_ENDASH - nwGUI.docEditor.clear() + mainMenu.aInsENDash.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_ENDASH + docEditor.clear() - nwGUI.mainMenu.aInsEMDash.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_EMDASH - nwGUI.docEditor.clear() + mainMenu.aInsEMDash.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_EMDASH + docEditor.clear() - nwGUI.mainMenu.aInsHorBar.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_HBAR - nwGUI.docEditor.clear() + mainMenu.aInsHorBar.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_HBAR + docEditor.clear() - nwGUI.mainMenu.aInsFigDash.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_FGDASH - nwGUI.docEditor.clear() + mainMenu.aInsFigDash.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_FGDASH + docEditor.clear() - nwGUI.mainMenu.aInsQuoteLS.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == CONFIG.fmtSQuoteOpen - nwGUI.docEditor.clear() + mainMenu.aInsQuoteLS.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == CONFIG.fmtSQuoteOpen + docEditor.clear() - nwGUI.mainMenu.aInsQuoteRS.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == CONFIG.fmtSQuoteClose - nwGUI.docEditor.clear() + mainMenu.aInsQuoteRS.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == CONFIG.fmtSQuoteClose + docEditor.clear() - nwGUI.mainMenu.aInsQuoteLD.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == CONFIG.fmtDQuoteOpen - nwGUI.docEditor.clear() + mainMenu.aInsQuoteLD.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == CONFIG.fmtDQuoteOpen + docEditor.clear() - nwGUI.mainMenu.aInsQuoteRD.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == CONFIG.fmtDQuoteClose - nwGUI.docEditor.clear() + mainMenu.aInsQuoteRD.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == CONFIG.fmtDQuoteClose + docEditor.clear() - nwGUI.mainMenu.aInsMSApos.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_MAPOS - nwGUI.docEditor.clear() + mainMenu.aInsMSApos.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_MAPOS + docEditor.clear() - nwGUI.mainMenu.aInsEllipsis.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_HELLIP - nwGUI.docEditor.clear() + mainMenu.aInsEllipsis.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_HELLIP + docEditor.clear() - nwGUI.mainMenu.aInsPrime.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_PRIME - nwGUI.docEditor.clear() + mainMenu.aInsPrime.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_PRIME + docEditor.clear() - nwGUI.mainMenu.aInsDPrime.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_DPRIME - nwGUI.docEditor.clear() + mainMenu.aInsDPrime.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_DPRIME + docEditor.clear() - nwGUI.mainMenu.aInsBullet.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_BULL - nwGUI.docEditor.clear() + mainMenu.aInsBullet.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_BULL + docEditor.clear() - nwGUI.mainMenu.aInsHyBull.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_HYBULL - nwGUI.docEditor.clear() + mainMenu.aInsHyBull.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_HYBULL + docEditor.clear() - nwGUI.mainMenu.aInsFlower.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_FLOWER - nwGUI.docEditor.clear() + mainMenu.aInsFlower.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_FLOWER + docEditor.clear() - nwGUI.mainMenu.aInsPerMille.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_PERMIL - nwGUI.docEditor.clear() + mainMenu.aInsPerMille.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_PERMIL + docEditor.clear() - nwGUI.mainMenu.aInsDegree.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_DEGREE - nwGUI.docEditor.clear() + mainMenu.aInsDegree.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_DEGREE + docEditor.clear() - nwGUI.mainMenu.aInsMinus.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_MINUS - nwGUI.docEditor.clear() + mainMenu.aInsMinus.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_MINUS + docEditor.clear() - nwGUI.mainMenu.aInsTimes.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_TIMES - nwGUI.docEditor.clear() + mainMenu.aInsTimes.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_TIMES + docEditor.clear() - nwGUI.mainMenu.aInsDivide.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_DIVIDE - nwGUI.docEditor.clear() + mainMenu.aInsDivide.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_DIVIDE + docEditor.clear() - nwGUI.mainMenu.aInsNBSpace.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_NBSP - nwGUI.docEditor.clear() + mainMenu.aInsNBSpace.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_NBSP + docEditor.clear() - nwGUI.mainMenu.aInsThinSpace.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_THSP - nwGUI.docEditor.clear() + mainMenu.aInsThinSpace.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_THSP + docEditor.clear() - nwGUI.mainMenu.aInsThinNBSpace.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == nwUnicode.U_THNBSP - nwGUI.docEditor.clear() + mainMenu.aInsThinNBSpace.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == nwUnicode.U_THNBSP + docEditor.clear() # Insert Keywords # =============== - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.TAG_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.TAG_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.POV_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.POV_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.FOCUS_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.FOCUS_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.CHAR_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.CHAR_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.PLOT_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.PLOT_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.TIME_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.TIME_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.WORLD_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.WORLD_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.OBJECT_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.OBJECT_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.ENTITY_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.ENTITY_KEY - - nwGUI.docEditor.setPlainText("Stuff") - nwGUI.mainMenu.mInsKWItems[nwKeyWords.CUSTOM_KEY][0].activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.CUSTOM_KEY + for action, key in zip(mainMenu.mInsKeywords.actions(), nwKeyWords.ALL_KEYS): + docEditor.setPlainText("Stuff") + action.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == f"Stuff\n{key}: " # Faulty Keyword Inserts - assert not nwGUI.docEditor.insertKeyWord("blabla") + assert not docEditor.insertKeyWord("blabla") with monkeypatch.context() as mp: mp.setattr(QTextBlock, "isValid", lambda *a, **k: False) - assert not nwGUI.docEditor.insertKeyWord(nwKeyWords.TAG_KEY) + assert not docEditor.insertKeyWord(nwKeyWords.TAG_KEY) - nwGUI.docEditor.clear() + # Insert Fields + # ============= + + for action, field in zip(mainMenu.mInsField.actions(), nwStats.ALL_FIELDS): + value = nwShortcode.FIELD_VALUE.format(field) + docEditor.setPlainText("Stuff ") + docEditor.setCursorPosition(6) + action.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == f"Stuff {value}" + + docEditor.clear() # Insert Special Comments # ======================= - nwGUI.docEditor.setPlainText("Stuff\n") - nwGUI.mainMenu.aInsSynopsis.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%Synopsis: \n" + docEditor.setPlainText("Stuff\n") + mainMenu.aInsSynopsis.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == "Stuff\n%Synopsis: \n" - nwGUI.docEditor.setPlainText("Stuff\n") - nwGUI.mainMenu.aInsShort.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Stuff\n%Short: \n" + docEditor.setPlainText("Stuff\n") + mainMenu.aInsShort.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == "Stuff\n%Short: \n" # Breaks and Vertical Space # ========================= - nwGUI.docEditor.setPlainText("### Stuff\n") - nwGUI.mainMenu.aInsNewPage.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "[newpage]\n### Stuff\n" + docEditor.setPlainText("### Stuff\n") + mainMenu.aInsNewPage.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == "[newpage]\n### Stuff\n" - nwGUI.docEditor.setPlainText("Line OneLine Two\n") - nwGUI.docEditor.setCursorPosition(8) - nwGUI.mainMenu.aInsLineBreak.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Line One[br]Line Two\n" + docEditor.setPlainText("Line OneLine Two\n") + docEditor.setCursorPosition(8) + mainMenu.aInsLineBreak.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == "Line One[br]Line Two\n" - nwGUI.docEditor.setPlainText("### Stuff\n") - nwGUI.mainMenu.aInsVSpaceS.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "[vspace]\n### Stuff\n" + docEditor.setPlainText("### Stuff\n") + mainMenu.aInsVSpaceS.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == "[vspace]\n### Stuff\n" - nwGUI.docEditor.setPlainText("### Stuff\n") - nwGUI.mainMenu.aInsVSpaceM.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "[vspace:2]\n### Stuff\n" + docEditor.setPlainText("### Stuff\n") + mainMenu.aInsVSpaceM.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == "[vspace:2]\n### Stuff\n" - nwGUI.docEditor.clear() + docEditor.clear() # Insert Text from File # ===================== @@ -600,23 +579,23 @@ def testGuiMainMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd # Open the document from before, and add some text to it nwGUI.openDocument(C.hSceneDoc) - nwGUI.docEditor.setPlainText("Bar") - assert nwGUI.docEditor.getText() == "Bar" + docEditor.setPlainText("Bar") + assert docEditor.getText() == "Bar" # The document isn't empty, so the message box should pop with monkeypatch.context() as mp: mp.setattr(QMessageBox, "result", lambda *a, **k: QMessageBox.StandardButton.No) assert not nwGUI.importDocument() - assert nwGUI.docEditor.getText() == "Bar" + assert docEditor.getText() == "Bar" # Finally, accept the replaced text, this time we use the menu entry to trigger it - nwGUI.mainMenu.aImportFile.activate(QAction.ActionEvent.Trigger) - assert nwGUI.docEditor.getText() == "Foo" + mainMenu.aImportFile.activate(QAction.ActionEvent.Trigger) + assert docEditor.getText() == "Foo" # Reveal File Location # ==================== - nwGUI.mainMenu.aFileDetails.activate(QAction.ActionEvent.Trigger) + mainMenu.aFileDetails.activate(QAction.ActionEvent.Trigger) path = str(projPath / "content" / "000000000000f.nwd") assert SHARED.lastAlert.endswith(f"File Location: {path}")