From 37970bd4461054a08216c8d9ca2151881972f23c Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 2 Apr 2022 19:43:26 +0200 Subject: [PATCH] Add root information to project file --- novelwriter/core/item.py | 18 +++++++ novelwriter/core/tree.py | 1 + tests/lipsum/nwProject.nwx | 48 +++++++++---------- tests/minimal/nwProject.nwx | 22 ++++----- .../coreProject_NewCustomA_nwProject.nwx | 48 +++++++++---------- .../coreProject_NewCustomB_nwProject.nwx | 30 ++++++------ .../coreProject_NewFile_nwProject.nwx | 22 ++++----- .../coreProject_NewMinimal_nwProject.nwx | 18 +++---- .../coreProject_NewRoot_nwProject.nwx | 34 ++++++------- .../guiEditor_Main_Final_nwProject.nwx | 26 +++++----- .../guiEditor_Main_Initial_nwProject.nwx | 18 +++---- .../guiProjSettings_Dialog_nwProject.nwx | 18 +++---- tests/test_core/test_core_item.py | 29 +++++++++-- tests/test_core/test_core_tree.py | 41 ++++++++-------- 14 files changed, 207 insertions(+), 166 deletions(-) diff --git a/novelwriter/core/item.py b/novelwriter/core/item.py index 67dd836c..73dfdd6b 100644 --- a/novelwriter/core/item.py +++ b/novelwriter/core/item.py @@ -45,6 +45,7 @@ class NWItem(): self._name = "" self._handle = None self._parent = None + self._root = None self._order = 0 self._type = nwItemType.NO_TYPE self._class = nwItemClass.NO_CLASS @@ -84,6 +85,10 @@ class NWItem(): def itemParent(self): return self._parent + @property + def itemRoot(self): + return self._root + @property def itemOrder(self): return self._order @@ -142,6 +147,7 @@ class NWItem(): itemAttrib = {} itemAttrib["handle"] = str(self._handle) itemAttrib["parent"] = str(self._parent) + itemAttrib["root"] = str(self._root) itemAttrib["order"] = str(self._order) itemAttrib["type"] = str(self._type.name) itemAttrib["class"] = str(self._class.name) @@ -182,6 +188,7 @@ class NWItem(): return False self.setParent(xItem.attrib.get("parent", None)) + self.setRoot(xItem.attrib.get("root", None)) self.setOrder(xItem.attrib.get("order", 0)) self.setType(xItem.attrib.get("type", nwItemType.NO_TYPE)) self.setClass(xItem.attrib.get("class", nwItemClass.NO_CLASS)) @@ -301,6 +308,17 @@ class NWItem(): self._parent = None return + def setRoot(self, theRoot): + """Set the root handle, and ensure it is valid. + """ + if theRoot is None: + self._root = None + elif isHandle(theRoot): + self._root = theRoot + else: + self._root = None + return + def setOrder(self, theOrder): """Set the item order, and ensure that it is valid. This value is purely a meta value, and not actually used by novelWriter at diff --git a/novelwriter/core/tree.py b/novelwriter/core/tree.py index 3316f993..b146c5a1 100644 --- a/novelwriter/core/tree.py +++ b/novelwriter/core/tree.py @@ -113,6 +113,7 @@ class NWTree(): self._projTree[tHandle] = nwItem self._treeOrder.append(tHandle) + self.updateItemRoot(tHandle) self._setTreeChanged(True) return True diff --git a/tests/lipsum/nwProject.nwx b/tests/lipsum/nwProject.nwx index c6eab805..457d8672 100644 --- a/tests/lipsum/nwProject.nwx +++ b/tests/lipsum/nwProject.nwx @@ -1,12 +1,12 @@ - + Lorem Ipsum Lorem Ipsum lipsum.com - 21 + 23 24 - 1847 + 1857 False @@ -44,87 +44,87 @@ - + Novel - + Lorem Ipsum - + Front Matter - + Prologue - + Act One - + Chapter One - + Chapter One - + Scene One - + Scene Two - + Interlude - + Chapter Two - + Chapter Two - + Scene Three - + Scene Four - + Scene Five - + Characters - + Mr. Nobody - + Plot - + Main - + World - + Ancient Europe diff --git a/tests/minimal/nwProject.nwx b/tests/minimal/nwProject.nwx index 991d8b68..708d449f 100644 --- a/tests/minimal/nwProject.nwx +++ b/tests/minimal/nwProject.nwx @@ -1,13 +1,13 @@ - + Test Minimal Minimal Jane Doe John Doh - 12 + 13 2 - 129 + 134 True @@ -42,35 +42,35 @@ - + Novel - + Title Page - + New Chapter - + New Chapter - + New Scene - + Plot - + Characters - + World diff --git a/tests/reference/coreProject_NewCustomA_nwProject.nwx b/tests/reference/coreProject_NewCustomA_nwProject.nwx index 4b8a67bf..92517d59 100644 --- a/tests/reference/coreProject_NewCustomA_nwProject.nwx +++ b/tests/reference/coreProject_NewCustomA_nwProject.nwx @@ -1,5 +1,5 @@ - + Test Custom Test Novel @@ -42,95 +42,95 @@ - + Novel - + Plot - + Characters - + Locations - + Timeline - + Objects - + Entities - + Title Page - + Chapter 1 - + Chapter 1 - + Scene 1.1 - + Scene 1.2 - + Scene 1.3 - + Chapter 2 - + Chapter 2 - + Scene 2.1 - + Scene 2.2 - + Scene 2.3 - + Chapter 3 - + Chapter 3 - + Scene 3.1 - + Scene 3.2 - + Scene 3.3 diff --git a/tests/reference/coreProject_NewCustomB_nwProject.nwx b/tests/reference/coreProject_NewCustomB_nwProject.nwx index 3f55663e..2614990c 100644 --- a/tests/reference/coreProject_NewCustomB_nwProject.nwx +++ b/tests/reference/coreProject_NewCustomB_nwProject.nwx @@ -1,5 +1,5 @@ - + Test Custom Test Novel @@ -42,59 +42,59 @@ - + Novel - + Plot - + Characters - + Locations - + Timeline - + Objects - + Entities - + Title Page - + Scene 1 - + Scene 2 - + Scene 3 - + Scene 4 - + Scene 5 - + Scene 6 diff --git a/tests/reference/coreProject_NewFile_nwProject.nwx b/tests/reference/coreProject_NewFile_nwProject.nwx index d623ee2d..a0c16221 100644 --- a/tests/reference/coreProject_NewFile_nwProject.nwx +++ b/tests/reference/coreProject_NewFile_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project @@ -40,43 +40,43 @@ - + Novel - + Plot - + Characters - + World - + Title Page - + New Chapter - + New Chapter - + New Scene - + Hello - + Jane diff --git a/tests/reference/coreProject_NewMinimal_nwProject.nwx b/tests/reference/coreProject_NewMinimal_nwProject.nwx index 6becb112..4581e521 100644 --- a/tests/reference/coreProject_NewMinimal_nwProject.nwx +++ b/tests/reference/coreProject_NewMinimal_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project @@ -40,35 +40,35 @@ - + Novel - + Plot - + Characters - + World - + Title Page - + New Chapter - + New Chapter - + New Scene diff --git a/tests/reference/coreProject_NewRoot_nwProject.nwx b/tests/reference/coreProject_NewRoot_nwProject.nwx index b69de2b3..25b47036 100644 --- a/tests/reference/coreProject_NewRoot_nwProject.nwx +++ b/tests/reference/coreProject_NewRoot_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project @@ -40,67 +40,67 @@ - + Novel - + Plot - + Characters - + World - + Title Page - + New Chapter - + New Chapter - + New Scene - + Novel - + Plot - + Character - + World - + Timeline - + Object - + Custom1 - + Custom2 diff --git a/tests/reference/guiEditor_Main_Final_nwProject.nwx b/tests/reference/guiEditor_Main_Final_nwProject.nwx index 4dd43b9a..a33a6e6a 100644 --- a/tests/reference/guiEditor_Main_Final_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Final_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project @@ -40,51 +40,51 @@ - + Novel - + Title Page - + New Chapter - + New Chapter - + New Scene - + Plot - + New File - + Characters - + New File - + World - + New File - + Trash diff --git a/tests/reference/guiEditor_Main_Initial_nwProject.nwx b/tests/reference/guiEditor_Main_Initial_nwProject.nwx index f1b17740..5fbd17bb 100644 --- a/tests/reference/guiEditor_Main_Initial_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Initial_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project @@ -40,35 +40,35 @@ - + Novel - + Title Page - + New Chapter - + New Chapter - + New Scene - + Plot - + Characters - + World diff --git a/tests/reference/guiProjSettings_Dialog_nwProject.nwx b/tests/reference/guiProjSettings_Dialog_nwProject.nwx index b88e7f17..e34a844b 100644 --- a/tests/reference/guiProjSettings_Dialog_nwProject.nwx +++ b/tests/reference/guiProjSettings_Dialog_nwProject.nwx @@ -1,5 +1,5 @@ - + Project Name Project Title @@ -46,35 +46,35 @@ - + Novel - + Title Page - + New Chapter - + New Chapter - + New Scene - + Plot - + Characters - + World diff --git a/tests/test_core/test_core_item.py b/tests/test_core/test_core_item.py index 368592f1..05c1d077 100644 --- a/tests/test_core/test_core_item.py +++ b/tests/test_core/test_core_item.py @@ -65,6 +65,18 @@ def testCoreItem_Setters(mockGUI): theItem.setParent("0123456789abc") assert theItem.itemParent == "0123456789abc" + # Root + theItem.setRoot(None) + assert theItem.itemRoot is None + theItem.setRoot(123) + assert theItem.itemRoot is None + theItem.setRoot("0123456789abcdef") + assert theItem.itemRoot is None + theItem.setRoot("0123456789abg") + assert theItem.itemRoot is None + theItem.setRoot("0123456789abc") + assert theItem.itemRoot == "0123456789abc" + # Order theItem.setOrder(None) assert theItem.itemOrder == 0 @@ -326,6 +338,7 @@ def testCoreItem_XMLPackUnpack(mockGUI, caplog): theItem = NWItem(theProject) theItem.setHandle("0123456789abc") theItem.setParent("0123456789abc") + theItem.setRoot("0123456789abc") theItem.setOrder(1) theItem.setName("A Name") theItem.setClass("NOVEL") @@ -342,9 +355,11 @@ def testCoreItem_XMLPackUnpack(mockGUI, caplog): xContent = etree.SubElement(nwXML, "content") theItem.packXML(xContent) assert etree.tostring(xContent, pretty_print=False, encoding="utf-8") == ( - b'A Name' + b'' + b'A Name' + b'' ) # Unpack @@ -368,6 +383,7 @@ def testCoreItem_XMLPackUnpack(mockGUI, caplog): theItem = NWItem(theProject) theItem.setHandle("0123456789abc") theItem.setParent("0123456789abc") + theItem.setRoot("0123456789abc") theItem.setOrder(1) theItem.setName("A Name") theItem.setClass("NOVEL") @@ -385,8 +401,11 @@ def testCoreItem_XMLPackUnpack(mockGUI, caplog): xContent = etree.SubElement(nwXML, "content") theItem.packXML(xContent) assert etree.tostring(xContent, pretty_print=False, encoding="utf-8") == ( - b'A Name' + b'' + b'A Name' + b'' + b'' ) # Unpack diff --git a/tests/test_core/test_core_tree.py b/tests/test_core/test_core_tree.py index a86993a3..6216a1b1 100644 --- a/tests/test_core/test_core_tree.py +++ b/tests/test_core/test_core_tree.py @@ -374,25 +374,28 @@ def testCoreTree_XMLPackUnpack(mockGUI, mockItems): assert etree.tostring(nwXML, pretty_print=False, encoding="utf-8") == ( b'' b'' - b'' - b'Novel' - b'Act One' - b'' - b'Chapter One' - b'' - b'Scene One' - b'' - b'Outtakes' - b'' - b'Trash' - b'' - b'Characters' - b'Jane Doe' + b'Novel' + b'' + b'Act One' + b'Chapter One' + b'' + b'Scene One' + b'' + b'Outtakes' + b'Trash' + b'Characters' + b'Jane Doe' b'' b'' )