From c2059a870148c7520977f95d9988c8702a8af984 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 18 May 2025 16:58:39 +0200 Subject: [PATCH 1/2] Notify the index when an item's class changes (#2330) --- novelwriter/core/index.py | 15 +++++++++++++++ novelwriter/core/item.py | 6 +++++- tests/test_core/test_core_index.py | 6 ++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index 0a74cf7e..6720d171 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -131,6 +131,12 @@ class Index: self._novelExtra = extra return + def setItemClass(self, tHandle: str, itemClass: nwItemClass) -> None: + """Update the class for all tags of a handle.""" + logger.info("Updating class for '%s'", tHandle) + self._tagsIndex.updateClass(tHandle, itemClass.name) + return + ## # Public Methods ## @@ -854,6 +860,15 @@ class TagsIndex: x.get("name", "") for x in self._tags.values() if x.get("class", "") == className ] + def updateClass(self, tHandle: str, className: str) -> None: + """Update the class name of an item. This must be called when a + document moves to another class. + """ + for entry in self._tags.values(): + if entry.get("handle") == tHandle: + entry["class"] = className + return + ## # Pack/Unpack ## diff --git a/novelwriter/core/item.py b/novelwriter/core/item.py index e8f067a9..a8f2fbbd 100644 --- a/novelwriter/core/item.py +++ b/novelwriter/core/item.py @@ -435,7 +435,11 @@ class NWItem: """ if self._parent is not None: # Only update for child items - self.setClass(itemClass) + if itemClass != self._class: + self.setClass(itemClass) + if self._type == nwItemType.FILE: + # Notify the index of the class change + self._project.index.setItemClass(self._handle, itemClass) if self._layout == nwItemLayout.NO_LAYOUT: # If no layout is set, pick one diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index 723f8f9f..990a2206 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -1049,6 +1049,12 @@ def testCoreIndex_TagsIndex(): assert tagsIndex.tagClass("Tag3") == nwItemClass.PLOT.name assert tagsIndex.tagClass("Tag4") is None + # Change class of item + tagsIndex.updateClass("0000000000003", nwItemClass.WORLD.name) + assert tagsIndex.tagClass("Tag3") == nwItemClass.WORLD.name + tagsIndex.updateClass("0000000000003", nwItemClass.PLOT.name) + assert tagsIndex.tagClass("Tag3") == nwItemClass.PLOT.name + # Pack Data assert tagsIndex.packData() == content From bbcd11531a5220f28da7e8637602c6fc507b9659 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 18 May 2025 17:10:32 +0200 Subject: [PATCH 2/2] Fix order of story structure columns in CSV export (#2313) --- novelwriter/gui/outline.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/novelwriter/gui/outline.py b/novelwriter/gui/outline.py index 3937ed53..efe20aea 100644 --- a/novelwriter/gui/outline.py +++ b/novelwriter/gui/outline.py @@ -784,7 +784,8 @@ class GuiOutlineTree(QTreeWidget): ): if novIdx.level != "H0" and (nwItem := SHARED.project.tree[tHandle]): refs = SHARED.project.index.getReferences(tHandle, sTitle) - story = {k: v for k, v in novIdx.comments.items() if k in sMatch}.values() + comments = dict(novIdx.comments.items()) + story = [comments.get(k, "") for k in sMatch] data.append([ novIdx.level, novIdx.title,