From 74910a6b56311d98ef19989e7a3f1f715aa4a300 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Mon, 22 Mar 2021 18:57:57 +0100 Subject: [PATCH] Use an enum for switching focus instead of hard coded numbers --- nw/constants/__init__.py | 3 ++- nw/constants/enum.py | 9 +++++++++ nw/gui/mainmenu.py | 10 +++++----- nw/guimain.py | 14 +++++++------- tests/test_gui/test_gui_doceditor.py | 20 ++++++++++---------- tests/test_gui/test_gui_mainmenu.py | 6 ++++-- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/nw/constants/__init__.py b/nw/constants/__init__.py index 1a6748a1..b3590f65 100644 --- a/nw/constants/__init__.py +++ b/nw/constants/__init__.py @@ -6,7 +6,7 @@ from nw.constants.constants import ( ) from nw.constants.enum import ( nwAlert, nwDocAction, nwItemClass, nwItemLayout, nwItemType, nwOutline, - nwDocInsert + nwDocInsert, nwWidget ) __all__ = [ @@ -28,4 +28,5 @@ __all__ = [ "nwItemType", "nwOutline", "nwDocInsert", + "nwWidget", ] diff --git a/nw/constants/enum.py b/nw/constants/enum.py index 4e800666..7136377d 100644 --- a/nw/constants/enum.py +++ b/nw/constants/enum.py @@ -112,6 +112,15 @@ class nwAlert(Enum): # END Enum nwAlert +class nwWidget(Enum): + + TREE = 1 + EDITOR = 2 + VIEWER = 3 + OUTLINE = 4 + +# END Enum nwWidget + class nwOutline(Enum): TITLE = 0 diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 40b3fcff..97d92042 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -33,7 +33,7 @@ from PyQt5.QtWidgets import QMenuBar, QAction from nw.constants import ( nwItemType, nwItemClass, nwDocAction, nwDocInsert, nwKeyWords, nwLabels, - nwUnicode + nwUnicode, nwWidget ) logger = logging.getLogger(__name__) @@ -469,28 +469,28 @@ class GuiMainMenu(QMenuBar): self.aFocusTree = QAction("Focus Project Tree", self) self.aFocusTree.setStatusTip("Move focus to project tree") self.aFocusTree.setShortcut("Alt+1") - self.aFocusTree.triggered.connect(lambda: self.theParent.switchFocus(1)) + self.aFocusTree.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.TREE)) self.viewMenu.addAction(self.aFocusTree) # View > Document Pane 1 self.aFocusEditor = QAction("Focus Document Editor", self) self.aFocusEditor.setStatusTip("Move focus to left document pane") self.aFocusEditor.setShortcut("Alt+2") - self.aFocusEditor.triggered.connect(lambda: self.theParent.switchFocus(2)) + self.aFocusEditor.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.EDITOR)) self.viewMenu.addAction(self.aFocusEditor) # View > Document Pane 2 self.aFocusView = QAction("Focus Document Viewer", self) self.aFocusView.setStatusTip("Move focus to right document pane") self.aFocusView.setShortcut("Alt+3") - self.aFocusView.triggered.connect(lambda: self.theParent.switchFocus(3)) + self.aFocusView.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.VIEWER)) self.viewMenu.addAction(self.aFocusView) # View > Outline self.aFocusOutline = QAction("Focus Outline", self) self.aFocusOutline.setStatusTip("Move focus to outline") self.aFocusOutline.setShortcut("Alt+4") - self.aFocusOutline.triggered.connect(lambda: self.theParent.switchFocus(4)) + self.aFocusOutline.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.OUTLINE)) self.viewMenu.addAction(self.aFocusOutline) # View > Separator diff --git a/nw/guimain.py b/nw/guimain.py index 4edd38d7..c11657a2 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -46,7 +46,7 @@ from nw.gui import ( GuiProjectTree, GuiProjectWizard, GuiTheme, GuiWordList, GuiWritingStats ) from nw.core import NWProject, NWDoc, NWIndex -from nw.constants import nwItemType, nwItemClass, nwAlert, nwLists +from nw.constants import nwItemType, nwItemClass, nwAlert, nwLists, nwWidget from nw.common import getGuiItem, hexToInt logger = logging.getLogger(__name__) @@ -810,7 +810,7 @@ class GuiMain(QMainWindow): if tHandle is None: if self.docEditor.anyFocus() or self.isFocusMode: tHandle = self.docEditor.theHandle - elif self.treeView.hasFocus(): + else: tHandle = self.treeView.getSelectedHandle() if tHandle is None: @@ -1180,15 +1180,15 @@ class GuiMain(QMainWindow): def switchFocus(self, paneNo): """Switch focus between main GUI views. """ - if paneNo == 1: + if paneNo == nwWidget.TREE: self.treeView.setFocus() - elif paneNo == 2: + elif paneNo == nwWidget.EDITOR: self.mainTabs.setCurrentWidget(self.splitDocs) self.docEditor.setFocus() - elif paneNo == 3: + elif paneNo == nwWidget.VIEWER: self.mainTabs.setCurrentWidget(self.splitDocs) self.docViewer.setFocus() - elif paneNo == 4: + elif paneNo == nwWidget.OUTLINE: self.mainTabs.setCurrentWidget(self.splitOutline) self.projView.setFocus() return @@ -1225,7 +1225,7 @@ class GuiMain(QMainWindow): if self.isFocusMode: logger.debug("Activating Focus Mode") self.mainTabs.setCurrentWidget(self.splitDocs) - self.switchFocus(2) + self.switchFocus(nwWidget.EDITOR) else: logger.debug("Deactivating Focus Mode") diff --git a/tests/test_gui/test_gui_doceditor.py b/tests/test_gui/test_gui_doceditor.py index 5b07f44a..b53da662 100644 --- a/tests/test_gui/test_gui_doceditor.py +++ b/tests/test_gui/test_gui_doceditor.py @@ -33,7 +33,7 @@ from PyQt5.QtWidgets import QAction, QMessageBox, QDialog from nw.gui.itemeditor import GuiItemEditor from nw.gui.doceditor import GuiDocEditor from nw.gui.projtree import GuiProjectTree -from nw.constants import nwItemType, nwDocAction +from nw.constants import nwItemType, nwDocAction, nwWidget keyDelay = 2 typeDelay = 1 @@ -117,14 +117,14 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi nwGUI.mainConf.autoScroll = True # Add a Character File - nwGUI.switchFocus(1) + nwGUI.switchFocus(nwWidget.TREE) nwGUI.treeView.clearSelection() nwGUI.treeView._getTreeItem("71ee45a3c0db9").setSelected(True) nwGUI.treeView.newTreeItem(nwItemType.FILE, None) assert nwGUI.openSelectedItem() # Type something into the document - nwGUI.switchFocus(2) + nwGUI.switchFocus(nwWidget.EDITOR) qtbot.keyClick(nwGUI.docEditor, "a", modifier=Qt.ControlModifier, delay=keyDelay) for c in "# Jane Doe": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -139,14 +139,14 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi qtbot.keyClick(nwGUI.docEditor, Qt.Key_Return, delay=keyDelay) # Add a Plot File - nwGUI.switchFocus(1) + nwGUI.switchFocus(nwWidget.TREE) nwGUI.treeView.clearSelection() nwGUI.treeView._getTreeItem("44cb730c42048").setSelected(True) nwGUI.treeView.newTreeItem(nwItemType.FILE, None) assert nwGUI.openSelectedItem() # Type something into the document - nwGUI.switchFocus(2) + nwGUI.switchFocus(nwWidget.EDITOR) qtbot.keyClick(nwGUI.docEditor, "a", modifier=Qt.ControlModifier, delay=keyDelay) for c in "# Main Plot": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -161,7 +161,7 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi qtbot.keyClick(nwGUI.docEditor, Qt.Key_Return, delay=keyDelay) # Add a World File - nwGUI.switchFocus(1) + nwGUI.switchFocus(nwWidget.TREE) nwGUI.treeView.clearSelection() nwGUI.treeView._getTreeItem("811786ad1ae74").setSelected(True) nwGUI.treeView.newTreeItem(nwItemType.FILE, None) @@ -173,7 +173,7 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi nwGUI.docEditor.replaceText("") # Type something into the document - nwGUI.switchFocus(2) + nwGUI.switchFocus(nwWidget.EDITOR) qtbot.keyClick(nwGUI.docEditor, "a", modifier=Qt.ControlModifier, delay=keyDelay) for c in "# Main Location": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -192,7 +192,7 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi nwGUI._autoSaveProject() # Select the 'New Scene' file - nwGUI.switchFocus(1) + nwGUI.switchFocus(nwWidget.TREE) nwGUI.treeView.clearSelection() nwGUI.treeView._getTreeItem("73475cb40a568").setExpanded(True) nwGUI.treeView._getTreeItem("31489056e0916").setExpanded(True) @@ -200,7 +200,7 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi assert nwGUI.openSelectedItem() # Type something into the document - nwGUI.switchFocus(2) + nwGUI.switchFocus(nwWidget.EDITOR) qtbot.keyClick(nwGUI.docEditor, "a", modifier=Qt.ControlModifier, delay=keyDelay) for c in "# Novel": qtbot.keyClick(nwGUI.docEditor, c, delay=typeDelay) @@ -300,7 +300,7 @@ def testGuiEditor_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj, refDir, outDi qtbot.wait(stepDelay) # Open and view the edited document - nwGUI.switchFocus(3) + nwGUI.switchFocus(nwWidget.VIEWER) assert nwGUI.openDocument("0e17daca5f3e1") assert nwGUI.viewDocument("0e17daca5f3e1") qtbot.wait(stepDelay) diff --git a/tests/test_gui/test_gui_mainmenu.py b/tests/test_gui/test_gui_mainmenu.py index 94bb8480..409b3dac 100644 --- a/tests/test_gui/test_gui_mainmenu.py +++ b/tests/test_gui/test_gui_mainmenu.py @@ -28,7 +28,9 @@ from PyQt5.QtGui import QTextCursor, QTextBlock from PyQt5.QtWidgets import QAction, QFileDialog, QMessageBox from nw.gui.doceditor import GuiDocEditor -from nw.constants import nwUnicode, nwDocAction, nwDocInsert, nwKeyWords +from nw.constants import ( + nwUnicode, nwDocAction, nwDocInsert, nwKeyWords, nwWidget +) keyDelay = 2 typeDelay = 1 @@ -366,7 +368,7 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncDir, fncProj): assert nwGUI.treeView._getTreeItem("0e17daca5f3e1") is not None - nwGUI.switchFocus(1) + nwGUI.switchFocus(nwWidget.TREE) nwGUI.treeView.clearSelection() nwGUI.treeView._getTreeItem("0e17daca5f3e1").setSelected(True) assert nwGUI.openSelectedItem()