Add some clear data functions

This commit is contained in:
Veronica Berglyd Olsen
2024-11-22 00:44:14 +01:00
parent ffe6cd0a65
commit a64ed405f3
8 changed files with 29 additions and 15 deletions
+3 -3
View File
@@ -116,7 +116,7 @@ class NWIndex:
# Public Methods # Public Methods
## ##
def clearIndex(self) -> None: def clear(self) -> None:
"""Clear the index dictionaries and time stamps.""" """Clear the index dictionaries and time stamps."""
self._tagsIndex.clear() self._tagsIndex.clear()
self._itemIndex.clear() self._itemIndex.clear()
@@ -125,9 +125,9 @@ class NWIndex:
SHARED.indexSignalProxy({"event": "clearIndex"}) SHARED.indexSignalProxy({"event": "clearIndex"})
return return
def rebuildIndex(self) -> None: def rebuild(self) -> None:
"""Rebuild the entire index from scratch.""" """Rebuild the entire index from scratch."""
self.clearIndex() self.clear()
for nwItem in self._project.tree: for nwItem in self._project.tree:
if nwItem.isFileType(): if nwItem.isFileType():
text = self._project.storage.getDocumentText(nwItem.itemHandle) text = self._project.storage.getDocumentText(nwItem.itemHandle)
+6
View File
@@ -138,6 +138,7 @@ class ProjectNode:
def updateCount(self, propagate: bool = True) -> None: def updateCount(self, propagate: bool = True) -> None:
"""Update counts, and propagate upwards in the tree.""" """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._count = self._item.wordCount + sum(c._count for c in self._children)
self._cache[C_COUNT_TEXT] = f"{self._count:n}" self._cache[C_COUNT_TEXT] = f"{self._count:n}"
if propagate and (parent := self._parent): if propagate and (parent := self._parent):
@@ -462,6 +463,11 @@ class ProjectModel(QAbstractItemModel):
# Other Methods # Other Methods
## ##
def clear(self) -> None:
"""Clear the project model."""
self._root._children.clear()
return
def allExpanded(self) -> list[QModelIndex]: def allExpanded(self) -> list[QModelIndex]:
"""Return a list of all expanded items.""" """Return a list of all expanded items."""
expanded = [] expanded = []
+8 -2
View File
@@ -89,6 +89,12 @@ class NWProject:
logger.debug("Delete: NWProject") logger.debug("Delete: NWProject")
return return
def clear(self) -> None:
"""Clear the project."""
self._tree.clear()
self._index.clear()
return
## ##
# Properties # Properties
## ##
@@ -352,7 +358,7 @@ class NWProject:
self._index.loadIndex() self._index.loadIndex()
if xmlReader.state == XMLReadState.WAS_LEGACY: if xmlReader.state == XMLReadState.WAS_LEGACY:
# Often, the index needs to be rebuilt when updating format # Often, the index needs to be rebuilt when updating format
self._index.rebuildIndex() self._index.rebuild()
self.updateWordCounts() self.updateWordCounts()
self._session.startSession() self._session.startSession()
@@ -417,7 +423,7 @@ class NWProject:
def closeProject(self, idleTime: float = 0.0) -> None: def closeProject(self, idleTime: float = 0.0) -> None:
"""Close the project.""" """Close the project."""
logger.info("Closing 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._options.saveSettings()
self._tree.writeToCFile() self._tree.writeToCFile()
self._session.appendSession(idleTime) self._session.appendSession(idleTime)
+1
View File
@@ -109,6 +109,7 @@ class NWTree:
def clear(self) -> None: def clear(self) -> None:
"""Clear the item tree entirely.""" """Clear the item tree entirely."""
oldModel = self._model oldModel = self._model
oldModel.clear()
self._model = ProjectModel(self) self._model = ProjectModel(self)
self._items.clear() self._items.clear()
self._nodes.clear() self._nodes.clear()
+1 -1
View File
@@ -738,7 +738,7 @@ class GuiMain(QMainWindow):
QApplication.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor)) QApplication.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor))
tStart = time() tStart = time()
SHARED.project.index.rebuildIndex() SHARED.project.index.rebuild()
SHARED.project.tree.refreshAllItems() SHARED.project.tree.refreshAllItems()
self.novelView.refreshTree() self.novelView.refreshTree()
+1
View File
@@ -398,6 +398,7 @@ class SharedData(QObject):
"""Create a new project and spell checking instance.""" """Create a new project and spell checking instance."""
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
if isinstance(self._project, NWProject): if isinstance(self._project, NWProject):
self._project.clear()
del self._project del self._project
del self._spelling del self._spelling
self._project = NWProject() self._project = NWProject()
+5 -5
View File
@@ -90,7 +90,7 @@ def testCoreIndex_LoadSave(qtbot, monkeypatch, prjLipsum, mockGUI, tstPaths):
assert index._itemIndex["4c4f28287af27"] is None assert index._itemIndex["4c4f28287af27"] is None
# Clear the index # Clear the index
index.clearIndex() index.clear()
assert index._tagsIndex._tags == {} assert index._tagsIndex._tags == {}
assert index._itemIndex._items == {} assert index._itemIndex._items == {}
@@ -113,8 +113,8 @@ def testCoreIndex_LoadSave(qtbot, monkeypatch, prjLipsum, mockGUI, tstPaths):
assert str(index._itemIndex.packData()) == itemsIndex assert str(index._itemIndex.packData()) == itemsIndex
# Rebuild index # Rebuild index
index.clearIndex() index.clear()
index.rebuildIndex() index.rebuild()
assert str(index._tagsIndex.packData()) == tagIndex assert str(index._tagsIndex.packData()) == tagIndex
assert str(index._itemIndex.packData()) == itemsIndex assert str(index._itemIndex.packData()) == itemsIndex
@@ -221,7 +221,7 @@ def testCoreIndex_CheckThese(mockGUI, fncPath, mockRnd):
mockRnd.reset() mockRnd.reset()
buildTestProject(project, fncPath) buildTestProject(project, fncPath)
index = project.index index = project.index
index.clearIndex() index.clear()
nHandle = project.newFile("Hello", C.hNovelRoot) nHandle = project.newFile("Hello", C.hNovelRoot)
cHandle = project.newFile("Jane", C.hCharRoot) cHandle = project.newFile("Jane", C.hCharRoot)
@@ -1155,7 +1155,7 @@ def testCoreIndex_ItemIndex(mockGUI, fncPath, mockRnd):
project = NWProject() project = NWProject()
mockRnd.reset() mockRnd.reset()
buildTestProject(project, fncPath) buildTestProject(project, fncPath)
project.index.clearIndex() project.index.clear()
nHandle = C.hTitlePage nHandle = C.hTitlePage
cHandle = C.hChapterDoc cHandle = C.hChapterDoc
+4 -4
View File
@@ -87,11 +87,11 @@ def testGuiViewerPanel_BackRefs(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
assert item.text(tabBackRefs.C_TITLE) == "Scene One" assert item.text(tabBackRefs.C_TITLE) == "Scene One"
# Clear Index # Clear Index
SHARED.project.index.clearIndex() SHARED.project.index.clear()
assert tabBackRefs.topLevelItemCount() == 0 assert tabBackRefs.topLevelItemCount() == 0
# Rebuild Index # Rebuild Index
SHARED.project.index.rebuildIndex() SHARED.project.index.rebuild()
assert tabBackRefs.topLevelItemCount() == 1 assert tabBackRefs.topLevelItemCount() == 1
# Test Update Theme # Test Update Theme
@@ -182,11 +182,11 @@ def testGuiViewerPanel_Tags(qtbot, monkeypatch, caplog, nwGUI, projPath, mockRnd
assert item.text(charTab.C_TITLE) == "Jane Smith" assert item.text(charTab.C_TITLE) == "Jane Smith"
# Clear Index # Clear Index
SHARED.project.index.clearIndex() SHARED.project.index.clear()
assert charTab.topLevelItemCount() == 0 assert charTab.topLevelItemCount() == 0
# Rebuild Index # Rebuild Index
SHARED.project.index.rebuildIndex() SHARED.project.index.rebuild()
assert charTab.topLevelItemCount() == 2 assert charTab.topLevelItemCount() == 2
# Test Update Theme # Test Update Theme