Fix handling of item with missing parent item (#1309)

This commit is contained in:
Veronica Berglyd Olsen
2023-01-08 18:23:45 +01:00
committed by GitHub
2 changed files with 14 additions and 5 deletions
+9 -5
View File
@@ -1693,19 +1693,23 @@ class GuiProjectTree(QTreeWidget):
del self._treeMap[tHandle] del self._treeMap[tHandle]
return None return None
else: elif pHandle in self._treeMap:
byIndex = -1 byIndex = -1
if nHandle is not None and nHandle in self._treeMap: if nHandle is not None and nHandle in self._treeMap:
try: byIndex = self._treeMap[pHandle].indexOfChild(self._treeMap[nHandle])
byIndex = self._treeMap[pHandle].indexOfChild(self._treeMap[nHandle])
except Exception:
logger.error("Failed to get index of item with handle '%s'", nHandle)
if byIndex >= 0: if byIndex >= 0:
self._treeMap[pHandle].insertChild(byIndex + 1, newItem) self._treeMap[pHandle].insertChild(byIndex + 1, newItem)
else: else:
self._treeMap[pHandle].addChild(newItem) self._treeMap[pHandle].addChild(newItem)
self.propagateCount(tHandle, nwItem.wordCount, countChildren=True) self.propagateCount(tHandle, nwItem.wordCount, countChildren=True)
else:
self.mainGui.makeAlert(self.tr(
"There is nowhere to add item with name '{0}'."
).format(nwItem.itemName), nwAlert.ERROR)
del self._treeMap[tHandle]
return None
self.setTreeItemValues(tHandle) self.setTreeItemValues(tHandle)
newItem.setExpanded(nwItem.isExpanded) newItem.setExpanded(nwItem.isExpanded)
+5
View File
@@ -885,6 +885,11 @@ def testGuiProjTree_Other(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
nwGUI.theProject.tree[nHandle].setParent(None) nwGUI.theProject.tree[nHandle].setParent(None)
assert projTree.revealNewTreeItem(nHandle) is False assert projTree.revealNewTreeItem(nHandle) is False
# Try to add an item with unknown parent to the tree
nHandle = nwGUI.theProject.newFile("Test", C.hNovelRoot)
nwGUI.theProject.tree[nHandle].setParent(C.hInvalid)
assert projTree.revealNewTreeItem(nHandle) is False
# Method: undoLastMove # Method: undoLastMove
# ==================== # ====================