From 22d761dea321305199c1d1048cd57d377b4de6d7 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 8 Jan 2023 17:57:47 +0100 Subject: [PATCH 1/2] Improve handling of unexpected project items (#1283) --- novelwriter/gui/projtree.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 5faa85b2..96af1ecd 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -1693,19 +1693,23 @@ class GuiProjectTree(QTreeWidget): del self._treeMap[tHandle] return None - else: + elif pHandle in self._treeMap: byIndex = -1 if nHandle is not None and nHandle in self._treeMap: - try: - byIndex = self._treeMap[pHandle].indexOfChild(self._treeMap[nHandle]) - except Exception: - logger.error("Failed to get index of item with handle '%s'", nHandle) + byIndex = self._treeMap[pHandle].indexOfChild(self._treeMap[nHandle]) if byIndex >= 0: self._treeMap[pHandle].insertChild(byIndex + 1, newItem) else: self._treeMap[pHandle].addChild(newItem) 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) newItem.setExpanded(nwItem.isExpanded) From 65bfd20cedffc240ed0fbb002e154bdc40ba7cf8 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 8 Jan 2023 18:11:06 +0100 Subject: [PATCH 2/2] Update tests --- tests/test_gui/test_gui_projtree.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_gui/test_gui_projtree.py b/tests/test_gui/test_gui_projtree.py index cbf607d8..684f4dd3 100644 --- a/tests/test_gui/test_gui_projtree.py +++ b/tests/test_gui/test_gui_projtree.py @@ -885,6 +885,11 @@ def testGuiProjTree_Other(qtbot, monkeypatch, nwGUI, projPath, mockRnd): nwGUI.theProject.tree[nHandle].setParent(None) 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 # ====================