From 7c33c949cbdeae8923d6d9e02801f269faf3b1d8 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 16 Jun 2024 15:23:22 +0200 Subject: [PATCH] Merge the two methods in the shared class to save editor content --- novelwriter/gui/projtree.py | 6 +++--- novelwriter/gui/search.py | 2 +- novelwriter/shared.py | 27 ++++++++++----------------- novelwriter/tools/manusbuild.py | 2 +- novelwriter/tools/manuscript.py | 2 +- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 551ad6e6..f6e897cb 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -997,7 +997,7 @@ class GuiProjectTree(QTreeWidget): trItemP.takeChild(tIndex) for dHandle in reversed(self.getTreeFromHandle(tHandle)): - SHARED.closeDocument(dHandle) + SHARED.closeEditor(dHandle) SHARED.project.removeItem(dHandle) self._treeMap.pop(dHandle, None) @@ -1404,7 +1404,7 @@ class GuiProjectTree(QTreeWidget): return False # Save the open document first, in case it's part of merge - SHARED.saveDocument() + SHARED.saveEditor() # Create merge object, and append docs docMerger = DocMerger(SHARED.project) @@ -1805,7 +1805,7 @@ class _TreeContextMenu(QMenu): def _itemHeader(self) -> None: """Check if there is a header that can be used for rename.""" - SHARED.ensureEditorSaved(self._handle) + SHARED.saveEditor() if hItem := SHARED.project.index.getItemHeading(self._handle, "T0001"): action = self.addAction(self.tr("Rename to Heading")) action.triggered.connect( diff --git a/novelwriter/gui/search.py b/novelwriter/gui/search.py index f19c419d..d89d04eb 100644 --- a/novelwriter/gui/search.py +++ b/novelwriter/gui/search.py @@ -259,7 +259,7 @@ class GuiProjectSearch(QWidget): if not self._blocked: QApplication.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor)) start = time() - SHARED.saveDocument() + SHARED.saveEditor() self._blocked = True self._map = {} self.searchResult.clear() diff --git a/novelwriter/shared.py b/novelwriter/shared.py index 95c59068..130019d2 100644 --- a/novelwriter/shared.py +++ b/novelwriter/shared.py @@ -172,15 +172,21 @@ class SharedData(QObject): logger.debug("Thread Pool Max Count: %d", QThreadPool.globalInstance().maxThreadCount()) return - def closeDocument(self, tHandle: str | None = None) -> None: + def closeEditor(self, tHandle: str | None = None) -> None: """Close the document editor, optionally a specific document.""" if tHandle is None or tHandle == self.mainGui.docEditor.docHandle: self.mainGui.closeDocument() return - def saveDocument(self) -> None: - """Forward save document call to main GUI.""" - self.mainGui.saveDocument() + def saveEditor(self, tHandle: str | None = None) -> None: + """Save editor content, optionally a specific document.""" + docEditor = self.mainGui.docEditor + if ( + self.hasProject and docEditor.docHandle + and (tHandle is None or tHandle == docEditor.docHandle) + ): + logger.debug("Saving editor document before action") + docEditor.saveText() return def openProject(self, path: str | Path, clearLock: bool = False) -> bool: @@ -216,19 +222,6 @@ class SharedData(QObject): self._resetIdleTimer() return - def ensureEditorSaved(self, tHandle: str | None) -> None: - """Ensure that the editor content is saved. Optionally, only if - it is a specific handle. - """ - docEditor = self.mainGui.docEditor - if ( - self.hasProject and docEditor.docHandle - and (tHandle is None or tHandle == docEditor.docHandle) - ): - logger.debug("Saving editor document before action") - docEditor.saveText() - return - def updateSpellCheckLanguage(self, reload: bool = False) -> None: """Update the active spell check language from settings.""" from novelwriter import CONFIG diff --git a/novelwriter/tools/manusbuild.py b/novelwriter/tools/manusbuild.py index d87b9be2..90f6f296 100644 --- a/novelwriter/tools/manusbuild.py +++ b/novelwriter/tools/manusbuild.py @@ -327,7 +327,7 @@ class GuiManuscriptBuild(NDialog): return False # Make sure editor content is saved before we start - SHARED.saveDocument() + SHARED.saveEditor() docBuild = NWBuildDocument(SHARED.project, self._build) docBuild.queueAll() diff --git a/novelwriter/tools/manuscript.py b/novelwriter/tools/manuscript.py index fba899a8..53f3a17b 100644 --- a/novelwriter/tools/manuscript.py +++ b/novelwriter/tools/manuscript.py @@ -325,7 +325,7 @@ class GuiManuscript(NToolDialog): start = time() # Make sure editor content is saved before we start - SHARED.ensureEditorSaved(None) + SHARED.saveEditor() docBuild = NWBuildDocument(SHARED.project, build) docBuild.queueAll()