From a64ed405f3c76155178f1225032a576ec5366134 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Fri, 22 Nov 2024 00:44:14 +0100 Subject: [PATCH] Add some clear data functions --- novelwriter/core/index.py | 6 +++--- novelwriter/core/itemmodel.py | 6 ++++++ novelwriter/core/project.py | 10 ++++++++-- novelwriter/core/tree.py | 1 + novelwriter/guimain.py | 2 +- novelwriter/shared.py | 1 + tests/test_core/test_core_index.py | 10 +++++----- tests/test_gui/test_gui_docviewerpanel.py | 8 ++++---- 8 files changed, 29 insertions(+), 15 deletions(-) diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index a78e3b06..81f5e684 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -116,7 +116,7 @@ class NWIndex: # Public Methods ## - def clearIndex(self) -> None: + def clear(self) -> None: """Clear the index dictionaries and time stamps.""" self._tagsIndex.clear() self._itemIndex.clear() @@ -125,9 +125,9 @@ class NWIndex: SHARED.indexSignalProxy({"event": "clearIndex"}) return - def rebuildIndex(self) -> None: + def rebuild(self) -> None: """Rebuild the entire index from scratch.""" - self.clearIndex() + self.clear() for nwItem in self._project.tree: if nwItem.isFileType(): text = self._project.storage.getDocumentText(nwItem.itemHandle) diff --git a/novelwriter/core/itemmodel.py b/novelwriter/core/itemmodel.py index aab17c61..6da4360f 100644 --- a/novelwriter/core/itemmodel.py +++ b/novelwriter/core/itemmodel.py @@ -138,6 +138,7 @@ class ProjectNode: def updateCount(self, propagate: bool = True) -> None: """Update counts, and propagate upwards in the tree.""" + # print("Counting", self._item.itemHandle) self._count = self._item.wordCount + sum(c._count for c in self._children) self._cache[C_COUNT_TEXT] = f"{self._count:n}" if propagate and (parent := self._parent): @@ -462,6 +463,11 @@ class ProjectModel(QAbstractItemModel): # Other Methods ## + def clear(self) -> None: + """Clear the project model.""" + self._root._children.clear() + return + def allExpanded(self) -> list[QModelIndex]: """Return a list of all expanded items.""" expanded = [] diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index bc121b74..28fad307 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -89,6 +89,12 @@ class NWProject: logger.debug("Delete: NWProject") return + def clear(self) -> None: + """Clear the project.""" + self._tree.clear() + self._index.clear() + return + ## # Properties ## @@ -352,7 +358,7 @@ class NWProject: self._index.loadIndex() if xmlReader.state == XMLReadState.WAS_LEGACY: # Often, the index needs to be rebuilt when updating format - self._index.rebuildIndex() + self._index.rebuild() self.updateWordCounts() self._session.startSession() @@ -417,7 +423,7 @@ class NWProject: def closeProject(self, idleTime: float = 0.0) -> None: """Close the project.""" logger.info("Closing project") - self._index.clearIndex() # Triggers clear signal, see #1718 + self._index.clear() # Triggers clear signal, see #1718 self._options.saveSettings() self._tree.writeToCFile() self._session.appendSession(idleTime) diff --git a/novelwriter/core/tree.py b/novelwriter/core/tree.py index 4a7a85d2..4bc6df60 100644 --- a/novelwriter/core/tree.py +++ b/novelwriter/core/tree.py @@ -109,6 +109,7 @@ class NWTree: def clear(self) -> None: """Clear the item tree entirely.""" oldModel = self._model + oldModel.clear() self._model = ProjectModel(self) self._items.clear() self._nodes.clear() diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index 802b1791..ee8c7b32 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -738,7 +738,7 @@ class GuiMain(QMainWindow): QApplication.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor)) tStart = time() - SHARED.project.index.rebuildIndex() + SHARED.project.index.rebuild() SHARED.project.tree.refreshAllItems() self.novelView.refreshTree() diff --git a/novelwriter/shared.py b/novelwriter/shared.py index 148f84d0..f5fa135e 100644 --- a/novelwriter/shared.py +++ b/novelwriter/shared.py @@ -398,6 +398,7 @@ class SharedData(QObject): """Create a new project and spell checking instance.""" from novelwriter.core.project import NWProject if isinstance(self._project, NWProject): + self._project.clear() del self._project del self._spelling self._project = NWProject() diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index 94eea4e4..2b64273b 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -90,7 +90,7 @@ def testCoreIndex_LoadSave(qtbot, monkeypatch, prjLipsum, mockGUI, tstPaths): assert index._itemIndex["4c4f28287af27"] is None # Clear the index - index.clearIndex() + index.clear() assert index._tagsIndex._tags == {} assert index._itemIndex._items == {} @@ -113,8 +113,8 @@ def testCoreIndex_LoadSave(qtbot, monkeypatch, prjLipsum, mockGUI, tstPaths): assert str(index._itemIndex.packData()) == itemsIndex # Rebuild index - index.clearIndex() - index.rebuildIndex() + index.clear() + index.rebuild() assert str(index._tagsIndex.packData()) == tagIndex assert str(index._itemIndex.packData()) == itemsIndex @@ -221,7 +221,7 @@ def testCoreIndex_CheckThese(mockGUI, fncPath, mockRnd): mockRnd.reset() buildTestProject(project, fncPath) index = project.index - index.clearIndex() + index.clear() nHandle = project.newFile("Hello", C.hNovelRoot) cHandle = project.newFile("Jane", C.hCharRoot) @@ -1155,7 +1155,7 @@ def testCoreIndex_ItemIndex(mockGUI, fncPath, mockRnd): project = NWProject() mockRnd.reset() buildTestProject(project, fncPath) - project.index.clearIndex() + project.index.clear() nHandle = C.hTitlePage cHandle = C.hChapterDoc diff --git a/tests/test_gui/test_gui_docviewerpanel.py b/tests/test_gui/test_gui_docviewerpanel.py index 5456a427..729c5690 100644 --- a/tests/test_gui/test_gui_docviewerpanel.py +++ b/tests/test_gui/test_gui_docviewerpanel.py @@ -87,11 +87,11 @@ def testGuiViewerPanel_BackRefs(qtbot, monkeypatch, nwGUI, projPath, mockRnd): assert item.text(tabBackRefs.C_TITLE) == "Scene One" # Clear Index - SHARED.project.index.clearIndex() + SHARED.project.index.clear() assert tabBackRefs.topLevelItemCount() == 0 # Rebuild Index - SHARED.project.index.rebuildIndex() + SHARED.project.index.rebuild() assert tabBackRefs.topLevelItemCount() == 1 # Test Update Theme @@ -182,11 +182,11 @@ def testGuiViewerPanel_Tags(qtbot, monkeypatch, caplog, nwGUI, projPath, mockRnd assert item.text(charTab.C_TITLE) == "Jane Smith" # Clear Index - SHARED.project.index.clearIndex() + SHARED.project.index.clear() assert charTab.topLevelItemCount() == 0 # Rebuild Index - SHARED.project.index.rebuildIndex() + SHARED.project.index.rebuild() assert charTab.topLevelItemCount() == 2 # Test Update Theme