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
##
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)
+6
View File
@@ -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 = []
+8 -2
View File
@@ -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)
+1
View File
@@ -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()
+1 -1
View File
@@ -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()
+1
View File
@@ -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()
+5 -5
View File
@@ -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
+4 -4
View File
@@ -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