Simplify a few more functions

This commit is contained in:
Veronica Berglyd Olsen
2024-03-17 16:10:27 +01:00
parent c26cfd5372
commit 6bb9ec0dc1
4 changed files with 53 additions and 71 deletions
+4 -9
View File
@@ -1192,7 +1192,10 @@ class GuiDocEditor(QPlainTextEdit):
if time() - self._lastEdit < 25.0: if time() - self._lastEdit < 25.0:
logger.debug("Running word counter") logger.debug("Running word counter")
SHARED.runInThreadPool(self.wCounterDoc) SHARED.runInThreadPool(self.wCounterDoc)
self._updateOutline() self.docHeader.setOutline({
block.blockNumber(): block.text()
for block in self._qDocument.iterBlockByType(BLOCK_TITLE)
})
return return
@@ -1837,14 +1840,6 @@ class GuiDocEditor(QPlainTextEdit):
# Internal Functions # Internal Functions
## ##
def _updateOutline(self) -> None:
"""Scan the text for headings and update the outline."""
self.docHeader.setOutline({
block.blockNumber(): block.text()
for block in self._qDocument.iterBlockByType(BLOCK_TITLE)
})
return
def _processTag(self, cursor: QTextCursor | None = None, def _processTag(self, cursor: QTextCursor | None = None,
follow: bool = True, create: bool = False) -> nwTrinary: follow: bool = True, create: bool = False) -> nwTrinary:
"""Activated by Ctrl+Enter. Checks that we're in a block """Activated by Ctrl+Enter. Checks that we're in a block
+19 -29
View File
@@ -524,24 +524,19 @@ class GuiMain(QMainWindow):
# Document Actions # Document Actions
## ##
def closeDocument(self, beforeOpen: bool = False) -> bool: def closeDocument(self, beforeOpen: bool = False) -> None:
"""Close the document and clear the editor and title field.""" """Close the document and clear the editor and title field."""
if not SHARED.hasProject: if SHARED.hasProject:
logger.error("No project open")
return False
# Disable focus mode if it is active # Disable focus mode if it is active
if SHARED.focusMode: if SHARED.focusMode:
SHARED.setFocusMode(False) SHARED.setFocusMode(False)
self.docEditor.saveCursorPosition() self.docEditor.saveCursorPosition()
if self.docEditor.docChanged: if self.docEditor.docChanged:
self.saveDocument() self.saveDocument()
self.docEditor.clearEditor() self.docEditor.clearEditor()
if not beforeOpen: if not beforeOpen:
self.novelView.setActiveHandle(None) self.novelView.setActiveHandle(None)
return
return True
def openDocument(self, tHandle: str | None, tLine: int | None = None, def openDocument(self, tHandle: str | None, tLine: int | None = None,
changeFocus: bool = True, doScroll: bool = False) -> bool: changeFocus: bool = True, doScroll: bool = False) -> bool:
@@ -732,36 +727,32 @@ class GuiMain(QMainWindow):
tHandle, sTitle = self.outlineView.getSelectedHandle() tHandle, sTitle = self.outlineView.getSelectedHandle()
else: else:
logger.warning("No item selected") logger.warning("No item selected")
return False
if tHandle is not None and sTitle is not None:
hItem = SHARED.project.index.getItemHeading(tHandle, sTitle)
if hItem is not None:
tLine = hItem.line
if tHandle is not None:
self.openDocument(tHandle, tLine=tLine, changeFocus=False, doScroll=False)
return return
def editItemLabel(self, tHandle: str | None = None) -> bool: if tHandle and sTitle:
if hItem := SHARED.project.index.getItemHeading(tHandle, sTitle):
tLine = hItem.line
if tHandle:
self.openDocument(tHandle, tLine=tLine, changeFocus=False, doScroll=False)
return
def editItemLabel(self, tHandle: str | None = None) -> None:
"""Open the edit item dialog.""" """Open the edit item dialog."""
if not SHARED.hasProject: if SHARED.hasProject:
logger.error("No project open")
return False
if tHandle is None and (self.docEditor.anyFocus() or SHARED.focusMode): if tHandle is None and (self.docEditor.anyFocus() or SHARED.focusMode):
tHandle = self.docEditor.docHandle tHandle = self.docEditor.docHandle
self.projView.renameTreeItem(tHandle) self.projView.renameTreeItem(tHandle)
return True return
def rebuildTrees(self) -> None: def rebuildTrees(self) -> None:
"""Rebuild the project tree.""" """Rebuild the project tree."""
self.projView.populateTree() self.projView.populateTree()
return return
def rebuildIndex(self, beQuiet: bool = False) -> bool: def rebuildIndex(self, beQuiet: bool = False) -> None:
"""Rebuild the entire index.""" """Rebuild the entire index."""
if not SHARED.hasProject: if SHARED.hasProject:
logger.error("No project open")
return False
logger.info("Rebuilding index ...") logger.info("Rebuilding index ...")
qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
tStart = time() tStart = time()
@@ -782,7 +773,7 @@ class GuiMain(QMainWindow):
if not beQuiet: if not beQuiet:
SHARED.info(self.tr("The project index has been successfully rebuilt.")) SHARED.info(self.tr("The project index has been successfully rebuilt."))
return True return
## ##
# Main Dialogs # Main Dialogs
@@ -892,15 +883,14 @@ class GuiMain(QMainWindow):
SHARED.error(self.tr("Could not initialise the dialog.")) SHARED.error(self.tr("Could not initialise the dialog."))
return return
def reportConfErr(self) -> bool: def reportConfErr(self) -> None:
"""Checks if the Config module has any errors to report, and let """Checks if the Config module has any errors to report, and let
the user know if this is the case. The Config module caches the user know if this is the case. The Config module caches
errors since it is initialised before the GUI itself. errors since it is initialised before the GUI itself.
""" """
if CONFIG.hasError: if CONFIG.hasError:
SHARED.error(CONFIG.errorText()) SHARED.error(CONFIG.errorText())
return True return
return False
## ##
# Main Window Actions # Main Window Actions
+1 -1
View File
@@ -125,7 +125,7 @@ def testGuiEditor_LoadText(qtbot, nwGUI, projPath, ipsumText, mockRnd):
longText = "### Lorem Ipsum\n\n%s" % "\n\n".join(ipsumText*20) longText = "### Lorem Ipsum\n\n%s" % "\n\n".join(ipsumText*20)
nwGUI.docEditor.replaceText(longText) nwGUI.docEditor.replaceText(longText)
nwGUI.saveDocument() nwGUI.saveDocument()
assert nwGUI.closeDocument() is True nwGUI.closeDocument()
# Invalid handle # Invalid handle
assert nwGUI.docEditor.loadText("abcdefghijklm") is False assert nwGUI.docEditor.loadText("abcdefghijklm") is False
+3 -6
View File
@@ -49,13 +49,10 @@ def testGuiMain_ProjectBlocker(nwGUI):
# Test no-project blocking # Test no-project blocking
assert nwGUI.closeProject() is True assert nwGUI.closeProject() is True
assert nwGUI.saveProject() is False assert nwGUI.saveProject() is False
assert nwGUI.closeDocument() is False
assert nwGUI.openDocument(None) is False assert nwGUI.openDocument(None) is False
assert nwGUI.openNextDocument(None) is False assert nwGUI.openNextDocument(None) is False
assert nwGUI.viewDocument(None) is False assert nwGUI.viewDocument(None) is False
assert nwGUI.importDocument() is False assert nwGUI.importDocument() is False
assert nwGUI.editItemLabel() is False
assert nwGUI.rebuildIndex() is False
# END Test testGuiMain_ProjectBlocker # END Test testGuiMain_ProjectBlocker
@@ -120,7 +117,7 @@ def testGuiMain_ProjectTreeItems(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
nwGUI.projView.projTree._getTreeItem(sHandle).setSelected(True) nwGUI.projView.projTree._getTreeItem(sHandle).setSelected(True)
nwGUI._keyPressReturn() nwGUI._keyPressReturn()
assert nwGUI.docEditor.docHandle == sHandle assert nwGUI.docEditor.docHandle == sHandle
assert nwGUI.closeDocument() is True nwGUI.closeDocument()
# Novel Tree has focus # Novel Tree has focus
nwGUI._changeView(nwView.NOVEL) nwGUI._changeView(nwView.NOVEL)
@@ -132,7 +129,7 @@ def testGuiMain_ProjectTreeItems(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
nwGUI.novelView.novelTree.setCurrentItem(selItem) nwGUI.novelView.novelTree.setCurrentItem(selItem)
nwGUI._keyPressReturn() nwGUI._keyPressReturn()
assert nwGUI.docEditor.docHandle == sHandle assert nwGUI.docEditor.docHandle == sHandle
assert nwGUI.closeDocument() is True nwGUI.closeDocument()
# Project Outline has focus # Project Outline has focus
nwGUI._changeView(nwView.OUTLINE) nwGUI._changeView(nwView.OUTLINE)
@@ -144,7 +141,7 @@ def testGuiMain_ProjectTreeItems(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
nwGUI.outlineView.outlineTree.setCurrentItem(selItem) nwGUI.outlineView.outlineTree.setCurrentItem(selItem)
nwGUI._keyPressReturn() nwGUI._keyPressReturn()
assert nwGUI.docEditor.docHandle == sHandle assert nwGUI.docEditor.docHandle == sHandle
assert nwGUI.closeDocument() is True nwGUI.closeDocument()
# qtbot.stop() # qtbot.stop()