From edcf9665b5dce3d37ec690fd2c012ad4a3924b0d Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sat, 4 May 2019 17:24:17 +0200 Subject: [PATCH] Folders can now be deleted as well, and depth and haschildren flags removed from item meta data. --- nw/gui/doctree.py | 55 ++++++++++++++----- nw/gui/mainmenu.py | 2 +- nw/project/item.py | 22 ++------ nw/project/project.py | 19 +------ .../sampleNovel/data_4/cd0bd12b087d_main.nwd | 1 + sample/sampleNovel/nwProject.nwx | 46 +++++++--------- 6 files changed, 71 insertions(+), 74 deletions(-) create mode 100644 sample/sampleNovel/data_4/cd0bd12b087d_main.nwd diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index a86057a1..e1f2c8f9 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -81,12 +81,9 @@ class GuiDocTree(QTreeWidget): ## def clearTree(self): - self.clear() - self.theMap = {} self.orphRoot = None - return def newTreeItem(self, itemType, itemClass): @@ -185,12 +182,21 @@ class GuiDocTree(QTreeWidget): ] return retVals - def deleteItem(self): - tHandle = self.getSelectedHandle() + def deleteItem(self, tHandle=None): + """Delete items from the tree. Note that this does not delete the item from the item tree in + the project object. However, since this is only meta data, there isn't really a need to do + that to save memory. As items not in the tree are not saved to the project file, a loaded + project will be clean anyway. + """ + + if tHandle is None: + tHandle = self.getSelectedHandle() + trItemS = self._getTreeItem(tHandle) nwItemS = self.theProject.getItem(tHandle) + if nwItemS.itemType == nwItemType.FILE: - logger.debug("User requested item %s moved to trash" % tHandle) + logger.debug("User requested file %s moved to trash" % tHandle) trItemP = trItemS.parent() trItemT = self._addTrashRoot() if trItemP is None or trItemT is None: @@ -200,6 +206,33 @@ class GuiDocTree(QTreeWidget): trItemC = trItemP.takeChild(tIndex) trItemT.addChild(trItemC) nwItemS.setParent(self.theProject.trashRoot) + self.clearSelection() + trItemP.setSelected(True) + + elif nwItemS.itemType == nwItemType.FOLDER: + logger.debug("User requested folder %s deleted" % tHandle) + trItemP = trItemS.parent() + if trItemP is None: + logger.error("Could not delete folder") + return + tIndex = trItemP.indexOfChild(trItemS) + if trItemS.childCount() == 0: + trItemP.takeChild(tIndex) + self.clearSelection() + trItemP.setSelected(True) + else: + self.theParent.makeAlert(["Cannot delete folder.","It is not empty."],2) + return + + elif nwItemS.itemType == nwItemType.ROOT: + logger.debug("User requested root folder %s deleted" % tHandle) + tIndex = self.indexOfTopLevelItem(trItemS) + if trItemS.childCount() == 0: + self.takeTopLevelItem(tIndex) + self.theParent.mainMenu.setAvailableRoot() + else: + self.theParent.makeAlert(["Cannot delete root folder.","It is not empty."],2) + return return @@ -233,7 +266,7 @@ class GuiDocTree(QTreeWidget): for i in range(pItem.childCount()): pCount += int(pItem.child(i).text(self.C_COUNT)) pHandle = pItem.text(self.C_HANDLE) - if not nDepth > NWItem.MAX_DEPTH and pHandle != "": + if not nDepth > 200 and pHandle != "": self.propagateCount(pHandle, pCount, nDepth+1) return @@ -271,11 +304,6 @@ class GuiDocTree(QTreeWidget): self._scanChildren(theList, theItem.child(i), i) return theList - def _scanParents(self, theItem, theCount=0): - if theItem.parent() is not None: - theItem, theCount = self._scanParents(theItem.parent(), theCount) - return theItem, theCount+1 - def _addTreeItem(self, nwItem): tHandle = nwItem.itemHandle @@ -341,7 +369,7 @@ class GuiDocTree(QTreeWidget): self.orphRoot = newItem newItem.setExpanded(True) return - + def _cleanOrphanedRoot(self): if self.orphRoot.childCount() == 0: self.takeTopLevelItem(self.indexOfTopLevelItem(self.orphRoot)) @@ -371,7 +399,6 @@ class GuiDocTree(QTreeWidget): return pHandle = trItemP.text(self.C_HANDLE) nwItemS.setParent(pHandle) - nwItemS.setDepth(self.theProject.countItemDepth(tHandle)) self.setTreeItemValues(tHandle) return diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index a2669b22..d6dace0d 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -187,7 +187,7 @@ class GuiMainMenu(QMenuBar): menuItem = QAction(QIcon.fromTheme("edit-delete"), "&Delete Item", self) menuItem.setStatusTip("Delete Selected Item") menuItem.setShortcut("Ctrl+Del") - menuItem.triggered.connect(self.theParent.treeView.deleteItem) + menuItem.triggered.connect(lambda : self.theParent.treeView.deleteItem(None)) self.projMenu.addAction(menuItem) # Project > Separator diff --git a/nw/project/item.py b/nw/project/item.py index 8e0cbbaf..b9f4d971 100644 --- a/nw/project/item.py +++ b/nw/project/item.py @@ -35,7 +35,6 @@ class NWItem(): self.itemClass = nwItemClass.NO_CLASS self.itemLayout = nwItemLayout.NO_LAYOUT self.itemStatus = 0 - self.itemDepth = None self.isExpanded = False self.charCount = 0 @@ -54,12 +53,11 @@ class NWItem(): "parent" : str(self.parHandle), "order" : str(self.itemOrder), }) - xSub = self._subPack(xPack,"name", text=str(self.itemName)) - xSub = self._subPack(xPack,"type", text=str(self.itemType.name)) - xSub = self._subPack(xPack,"class", text=str(self.itemClass.name)) - xSub = self._subPack(xPack,"status", text=str(self.itemStatus)) - xSub = self._subPack(xPack,"depth", text=str(self.itemDepth)) - xSub = self._subPack(xPack,"expanded", text=str(self.isExpanded)) + xSub = self._subPack(xPack,"name", text=str(self.itemName)) + xSub = self._subPack(xPack,"type", text=str(self.itemType.name)) + xSub = self._subPack(xPack,"class", text=str(self.itemClass.name)) + xSub = self._subPack(xPack,"status", text=str(self.itemStatus)) + xSub = self._subPack(xPack,"expanded", text=str(self.isExpanded)) if self.itemType == nwItemType.FILE: xSub = self._subPack(xPack,"layout", text=str(self.itemLayout.name)) xSub = self._subPack(xPack,"charCount", text=str(self.charCount), none=False) @@ -87,7 +85,7 @@ class NWItem(): elif tagName == "class": self.setClass(tagValue) elif tagName == "layout": self.setLayout(tagValue) elif tagName == "status": self.setStatus(tagValue) - elif tagName == "depth": self.setDepth(tagValue) + elif tagName == "children": self.setChildren(tagValue) elif tagName == "expanded": self.setExpanded(tagValue) elif tagName == "charCount": self.setCharCount(tagValue) elif tagName == "wordCount": self.setWordCount(tagValue) @@ -160,14 +158,6 @@ class NWItem(): self.itemStatus = theStatus return - def setDepth(self, theDepth): - theDepth = self._checkInt(theDepth,-1) - if theDepth >= 0 and theDepth <= self.MAX_DEPTH: - self.itemDepth = theDepth - else: - logger.error("Invalid item depth %d" % theDepth) - return - def setExpanded(self, expState): if isinstance(expState, str): self.isExpanded = expState == str(True) diff --git a/nw/project/project.py b/nw/project/project.py index 89dc230b..83b3cc08 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -102,6 +102,7 @@ class NWProject(): hNovel = self.newRoot("Novel", nwItemClass.NOVEL) hChars = self.newRoot("Characters", nwItemClass.CHARACTER) + hWorld = self.newRoot("Plot", nwItemClass.PLOT) hWorld = self.newRoot("World", nwItemClass.WORLD) hChapt = self.newFolder("New Chapter", nwItemClass.NOVEL, hNovel) hScene = self.newFile("New Scene", nwItemClass.NOVEL, hChapt) @@ -234,7 +235,7 @@ class NWProject(): # Save Tree Content logger.debug("Writing project content") - xContent = etree.SubElement(nwXML,"content",attrib={"count":str(len(self.projTree))}) + xContent = etree.SubElement(nwXML,"content",attrib={"count":str(len(self.treeOrder))}) for tHandle in self.treeOrder: self.projTree[tHandle].packXML(xContent) @@ -373,16 +374,6 @@ class NWProject(): return - def countItemDepth(self, tHandle): - theDepth = 0 - nwItem = self.getItem(tHandle) - while nwItem.parHandle is not None: - theDepth += 1 - nwItem = self.getItem(nwItem.parHandle) - if theDepth > NWItem.MAX_DEPTH: - return None - return theDepth - def _appendItem(self, tHandle, pHandle, nwItem): tHandle = self._checkString(tHandle,self._makeHandle(),False) pHandle = self._checkString(pHandle,None,True) @@ -405,12 +396,6 @@ class NWProject(): else: logger.error("Only one trash folder allowed") - itemDepth = self.countItemDepth(tHandle) - if itemDepth is None: - logger.error("The depth of entry %s could not be determined" % tHandle) - else: - nwItem.setDepth(itemDepth) - return def _makeStatusIcons(self): diff --git a/sample/sampleNovel/data_4/cd0bd12b087d_main.nwd b/sample/sampleNovel/data_4/cd0bd12b087d_main.nwd new file mode 100644 index 00000000..21e28fe8 --- /dev/null +++ b/sample/sampleNovel/data_4/cd0bd12b087d_main.nwd @@ -0,0 +1 @@ +# Very Deep File \ No newline at end of file diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index 6c914cfc..9634be58 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,19 +1,17 @@ - + Sample Project Sample Project Jane Smith Jay Doh - + Novel ROOT NOVEL 0 - 0 - True True @@ -21,8 +19,6 @@ FILE NOVEL 2 - 1 - False False TITLE 23 @@ -34,8 +30,6 @@ FOLDER NOVEL 1 - 1 - True True @@ -43,8 +37,6 @@ FILE NOVEL 2 - 2 - False False SCENE 2571 @@ -56,8 +48,6 @@ FILE NOVEL 1 - 2 - False False SCENE 376 @@ -69,8 +59,6 @@ FILE NOVEL 0 - 2 - False False SCENE 0 @@ -82,8 +70,6 @@ ROOT CHARACTER 0 - 0 - True True @@ -91,8 +77,6 @@ FOLDER CHARACTER 0 - 1 - True True @@ -100,8 +84,6 @@ FILE CHARACTER 0 - 2 - False False NOTE 42 @@ -113,8 +95,6 @@ FILE CHARACTER 0 - 2 - False False NOTE 51 @@ -126,8 +106,6 @@ ROOT WORLD 0 - 0 - True True @@ -135,13 +113,29 @@ FILE WORLD 0 - 1 - False False NOTE 66 13 1 + + Trash + TRASH + TRASH + 0 + True + + + New File + FILE + NOVEL + 0 + False + SCENE + 3 + 1 + 0 +