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
+45 -55
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") # Disable focus mode if it is active
return False if SHARED.focusMode:
SHARED.setFocusMode(False)
# Disable focus mode if it is active self.docEditor.saveCursorPosition()
if SHARED.focusMode: if self.docEditor.docChanged:
SHARED.setFocusMode(False) self.saveDocument()
self.docEditor.clearEditor()
self.docEditor.saveCursorPosition() if not beforeOpen:
if self.docEditor.docChanged: self.novelView.setActiveHandle(None)
self.saveDocument() return
self.docEditor.clearEditor()
if not beforeOpen:
self.novelView.setActiveHandle(None)
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,57 +727,53 @@ 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 return
if tHandle is not None and sTitle is not None:
hItem = SHARED.project.index.getItemHeading(tHandle, sTitle) if tHandle and sTitle:
if hItem is not None: if hItem := SHARED.project.index.getItemHeading(tHandle, sTitle):
tLine = hItem.line tLine = hItem.line
if tHandle is not None: if tHandle:
self.openDocument(tHandle, tLine=tLine, changeFocus=False, doScroll=False) self.openDocument(tHandle, tLine=tLine, changeFocus=False, doScroll=False)
return return
def editItemLabel(self, tHandle: str | None = None) -> bool: 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") if tHandle is None and (self.docEditor.anyFocus() or SHARED.focusMode):
return False tHandle = self.docEditor.docHandle
if tHandle is None and (self.docEditor.anyFocus() or SHARED.focusMode): self.projView.renameTreeItem(tHandle)
tHandle = self.docEditor.docHandle return
self.projView.renameTreeItem(tHandle)
return True
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") logger.info("Rebuilding index ...")
return False qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
tStart = time()
logger.info("Rebuilding index ...") self.projView.saveProjectTasks()
qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) SHARED.project.index.rebuildIndex()
tStart = time() self.projView.populateTree()
self.novelView.refreshTree()
self.projView.saveProjectTasks() tEnd = time()
SHARED.project.index.rebuildIndex() self.mainStatus.setStatusMessage(
self.projView.populateTree() self.tr("Indexing completed in {0} ms").format(f"{(tEnd - tStart)*1000.0:.1f}")
self.novelView.refreshTree() )
self.docEditor.updateTagHighLighting()
self._updateStatusWordCount()
qApp.restoreOverrideCursor()
tEnd = time() if not beQuiet:
self.mainStatus.setStatusMessage( SHARED.info(self.tr("The project index has been successfully rebuilt."))
self.tr("Indexing completed in {0} ms").format(f"{(tEnd - tStart)*1000.0:.1f}")
)
self.docEditor.updateTagHighLighting()
self._updateStatusWordCount()
qApp.restoreOverrideCursor()
if not beQuiet: return
SHARED.info(self.tr("The project index has been successfully rebuilt."))
return True
## ##
# 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()