Restructure the record of root items in the tree class

This commit is contained in:
Veronica Berglyd Olsen
2022-04-02 19:13:54 +02:00
parent 8ddda4783d
commit f9ddcd2f8a
7 changed files with 66 additions and 33 deletions
+1 -3
View File
@@ -208,8 +208,6 @@ class NWIndex():
text.
"""
theItem = self.theProject.projTree[tHandle]
theRoot = self.theProject.projTree.getRootItem(tHandle)
if theItem is None:
logger.info("Not indexing unknown item '%s'", tHandle)
return False
@@ -232,7 +230,7 @@ class NWIndex():
if self.theProject.projTree.isTrashRoot(theItem.itemParent):
logger.debug("Not indexing trash item '%s'", tHandle)
return False
if theRoot.itemClass == nwItemClass.ARCHIVE:
if self.theProject.projTree.getItemClass(tHandle) == nwItemClass.ARCHIVE:
logger.debug("Not indexing archived item '%s'", tHandle)
return False
+12
View File
@@ -608,6 +608,7 @@ class NWProject():
self.theParent.setStatus(self.tr("Opened Project: {0}").format(self.projName))
self._scanProjectFolder()
self._checkProjectTree()
self._loadProjectLocalisation()
self.updateWordCounts()
@@ -1340,6 +1341,17 @@ class NWProject():
xEntry.text = aValue
return
def _checkProjectTree(self):
"""Check the project tree and make sure all items have sensible
values.
"""
for tItem in self.projTree:
tHandle = tItem.itemHandle
logger.verbose("Checking item '%s'", tHandle)
if tItem.itemRoot is None:
self.projTree.updateItemRoot(tHandle)
logger.warning("Corrected the root setting of item '%s'", tHandle)
def _scanProjectFolder(self):
"""Scan the project folder and check that the files in it are
also in the project XML file. If they aren't, import them as
+29 -15
View File
@@ -47,7 +47,7 @@ class NWTree():
self._projTree = {} # Holds all the items of the project
self._treeOrder = [] # The order of the tree items on the tree view
self._treeRoots = [] # The root items of the tree
self._treeRoots = {} # The root items of the tree
self._trashRoot = None # The handle of the trash root folder
self._archRoot = None # The handle of the archive root folder
self._theIndex = 0 # The current iterator index
@@ -67,7 +67,7 @@ class NWTree():
"""
self._projTree = {}
self._treeOrder = []
self._treeRoots = []
self._treeRoots = {}
self._trashRoot = None
self._archRoot = None
self._theIndex = 0
@@ -98,7 +98,7 @@ class NWTree():
if nwItem.itemType == nwItemType.ROOT:
logger.verbose("Item '%s' is a root item", str(tHandle))
self._treeRoots.append(tHandle)
self._treeRoots[tHandle] = nwItem
if nwItem.itemClass == nwItemClass.ARCHIVE:
logger.verbose("Item '%s' is the archive folder", str(tHandle))
self._archRoot = tHandle
@@ -253,20 +253,34 @@ class NWTree():
return tItem.itemHandle
return None
def getRootItem(self, tHandle):
"""Iterate upwards in the tree until we find the item with
parent None, the root item. We do this with a for loop with a
maximum depth to make infinite loops impossible.
def isRoot(self, tHandle):
"""Check if a handle is a root item.
"""
return tHandle in self._treeRoots
def updateItemRoot(self, tHandle):
"""Update the root item handle of a given item.
"""
tItem = self.__getitem__(tHandle)
iItem = tItem
if iItem is not None:
for _ in range(nwConst.MAX_DEPTH + 1):
if iItem.itemParent is None:
tItem.setRoot(iItem.itemHandle)
return iItem.itemHandle
else:
tHandle = iItem.itemParent
iItem = self.__getitem__(tHandle)
return None
def getItemClass(self, tHandle):
"""Return the class of a given item.
"""
tItem = self.__getitem__(tHandle)
if tItem is not None:
for i in range(nwConst.MAX_DEPTH + 1):
if tItem.itemParent is None:
return tItem
else:
tHandle = tItem.itemParent
tItem = self.__getitem__(tHandle)
return None
if tItem.itemRoot in self._treeRoots:
return self._treeRoots[tItem.itemRoot].itemClass
return nwItemClass.NO_CLASS
def getItemPath(self, tHandle):
"""Iterate upwards in the tree until we find the item with
@@ -405,7 +419,7 @@ class NWTree():
return
if tHandle in self._treeRoots:
self._treeRoots.remove(tHandle)
del self._treeRoots[tHandle]
if tHandle == self._trashRoot:
self._trashRoot = None
if tHandle == self._archRoot:
+1 -2
View File
@@ -799,8 +799,7 @@ class GuiBuildNovel(QDialog):
if isNovel and not novelFiles:
return False
rootItem = self.theProject.projTree.getRootItem(theItem.itemHandle)
if rootItem.itemClass == nwItemClass.ARCHIVE:
if self.theProject.projTree.getItemClass(theItem.itemHandle) == nwItemClass.ARCHIVE:
return False
return True
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="1.7-alpha0" hexVersion="0x010700a0" fileVersion="1.4" timeStamp="2022-02-17 21:57:32">
<novelWriterXML appVersion="1.7-alpha0" hexVersion="0x010700a0" fileVersion="1.4" timeStamp="2022-04-02 18:36:55">
<project>
<name>New Project</name>
<title></title>
@@ -39,7 +39,7 @@
<entry blue="0" green="200" red="50">Main</entry>
</importance>
</settings>
<content count="12">
<content count="16">
<item handle="73475cb40a568" parent="None" order="0" type="ROOT" class="NOVEL">
<meta expanded="False"/>
<name status="New">Novel</name>
@@ -72,19 +72,35 @@
<meta charCount="0" wordCount="0" paraCount="0" cursorPos="0"/>
<name status="New" exported="True">New Scene</name>
</item>
<item handle="1a6562590ef19" parent="None" order="0" type="ROOT" class="TIMELINE">
<item handle="1a6562590ef19" parent="None" order="0" type="ROOT" class="NOVEL">
<meta expanded="False"/>
<name status="New">Novel</name>
</item>
<item handle="031b4af5197ec" parent="None" order="0" type="ROOT" class="PLOT">
<meta expanded="False"/>
<name status="New">Plot</name>
</item>
<item handle="41cfc0d1f2d12" parent="None" order="0" type="ROOT" class="CHARACTER">
<meta expanded="False"/>
<name status="New">Character</name>
</item>
<item handle="2858dcd1057d3" parent="None" order="0" type="ROOT" class="WORLD">
<meta expanded="False"/>
<name status="New">World</name>
</item>
<item handle="2fca346db6561" parent="None" order="0" type="ROOT" class="TIMELINE">
<meta expanded="False"/>
<name status="New">Timeline</name>
</item>
<item handle="031b4af5197ec" parent="None" order="0" type="ROOT" class="OBJECT">
<item handle="02d20bbd7e394" parent="None" order="0" type="ROOT" class="OBJECT">
<meta expanded="False"/>
<name status="New">Object</name>
</item>
<item handle="41cfc0d1f2d12" parent="None" order="0" type="ROOT" class="CUSTOM">
<item handle="7688b6ef52555" parent="None" order="0" type="ROOT" class="CUSTOM">
<meta expanded="False"/>
<name status="New">Custom1</name>
</item>
<item handle="2858dcd1057d3" parent="None" order="0" type="ROOT" class="CUSTOM">
<item handle="c837649cce43f" parent="None" order="0" type="ROOT" class="CUSTOM">
<meta expanded="False"/>
<name status="New">Custom2</name>
</item>
+1
View File
@@ -284,6 +284,7 @@ def testCoreIndex_ScanText(nwMinimal, mockGUI):
aHandle = theProject.newRoot("Archive", nwItemClass.ARCHIVE)
assert theProject.projTree[aHandle] is not None
xItem.setParent(aHandle)
xItem.setRoot(aHandle)
assert theIndex.scanText(xHandle, "Hello World!") is False
# Make some usable items
-7
View File
@@ -224,17 +224,10 @@ def testCoreTree_Methods(mockGUI, mockItems):
assert theTree.checkType("c000000000001", nwItemType.FILE) is True
# Root item lookup
theTree._treeRoots.append("stuff")
assert theTree.findRoot(nwItemClass.WORLD) is None
assert theTree.findRoot(nwItemClass.NOVEL) == "a000000000001"
assert theTree.findRoot(nwItemClass.CHARACTER) == "a000000000004"
# Find root item of child item
assert theTree.getRootItem("b000000000001").itemHandle == "a000000000001"
assert theTree.getRootItem("c000000000001").itemHandle == "a000000000001"
assert theTree.getRootItem("c000000000002").itemHandle == "a000000000001"
assert theTree.getRootItem("stuff") is None
# Get item path
assert theTree.getItemPath("stuff") == []
assert theTree.getItemPath("c000000000001") == [