Folders can now be deleted as well, and depth and haschildren flags removed from item meta data.

This commit is contained in:
Veronica K. B. Olsen
2019-05-04 17:24:17 +02:00
parent c775636a71
commit edcf9665b5
6 changed files with 71 additions and 74 deletions
+40 -13
View File
@@ -81,12 +81,9 @@ class GuiDocTree(QTreeWidget):
## ##
def clearTree(self): def clearTree(self):
self.clear() self.clear()
self.theMap = {} self.theMap = {}
self.orphRoot = None self.orphRoot = None
return return
def newTreeItem(self, itemType, itemClass): def newTreeItem(self, itemType, itemClass):
@@ -185,12 +182,21 @@ class GuiDocTree(QTreeWidget):
] ]
return retVals return retVals
def deleteItem(self): def deleteItem(self, tHandle=None):
tHandle = self.getSelectedHandle() """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) trItemS = self._getTreeItem(tHandle)
nwItemS = self.theProject.getItem(tHandle) nwItemS = self.theProject.getItem(tHandle)
if nwItemS.itemType == nwItemType.FILE: 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() trItemP = trItemS.parent()
trItemT = self._addTrashRoot() trItemT = self._addTrashRoot()
if trItemP is None or trItemT is None: if trItemP is None or trItemT is None:
@@ -200,6 +206,33 @@ class GuiDocTree(QTreeWidget):
trItemC = trItemP.takeChild(tIndex) trItemC = trItemP.takeChild(tIndex)
trItemT.addChild(trItemC) trItemT.addChild(trItemC)
nwItemS.setParent(self.theProject.trashRoot) 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 return
@@ -233,7 +266,7 @@ class GuiDocTree(QTreeWidget):
for i in range(pItem.childCount()): for i in range(pItem.childCount()):
pCount += int(pItem.child(i).text(self.C_COUNT)) pCount += int(pItem.child(i).text(self.C_COUNT))
pHandle = pItem.text(self.C_HANDLE) 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) self.propagateCount(pHandle, pCount, nDepth+1)
return return
@@ -271,11 +304,6 @@ class GuiDocTree(QTreeWidget):
self._scanChildren(theList, theItem.child(i), i) self._scanChildren(theList, theItem.child(i), i)
return theList 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): def _addTreeItem(self, nwItem):
tHandle = nwItem.itemHandle tHandle = nwItem.itemHandle
@@ -371,7 +399,6 @@ class GuiDocTree(QTreeWidget):
return return
pHandle = trItemP.text(self.C_HANDLE) pHandle = trItemP.text(self.C_HANDLE)
nwItemS.setParent(pHandle) nwItemS.setParent(pHandle)
nwItemS.setDepth(self.theProject.countItemDepth(tHandle))
self.setTreeItemValues(tHandle) self.setTreeItemValues(tHandle)
return return
+1 -1
View File
@@ -187,7 +187,7 @@ class GuiMainMenu(QMenuBar):
menuItem = QAction(QIcon.fromTheme("edit-delete"), "&Delete Item", self) menuItem = QAction(QIcon.fromTheme("edit-delete"), "&Delete Item", self)
menuItem.setStatusTip("Delete Selected Item") menuItem.setStatusTip("Delete Selected Item")
menuItem.setShortcut("Ctrl+Del") menuItem.setShortcut("Ctrl+Del")
menuItem.triggered.connect(self.theParent.treeView.deleteItem) menuItem.triggered.connect(lambda : self.theParent.treeView.deleteItem(None))
self.projMenu.addAction(menuItem) self.projMenu.addAction(menuItem)
# Project > Separator # Project > Separator
+6 -16
View File
@@ -35,7 +35,6 @@ class NWItem():
self.itemClass = nwItemClass.NO_CLASS self.itemClass = nwItemClass.NO_CLASS
self.itemLayout = nwItemLayout.NO_LAYOUT self.itemLayout = nwItemLayout.NO_LAYOUT
self.itemStatus = 0 self.itemStatus = 0
self.itemDepth = None
self.isExpanded = False self.isExpanded = False
self.charCount = 0 self.charCount = 0
@@ -54,12 +53,11 @@ class NWItem():
"parent" : str(self.parHandle), "parent" : str(self.parHandle),
"order" : str(self.itemOrder), "order" : str(self.itemOrder),
}) })
xSub = self._subPack(xPack,"name", text=str(self.itemName)) xSub = self._subPack(xPack,"name", text=str(self.itemName))
xSub = self._subPack(xPack,"type", text=str(self.itemType.name)) xSub = self._subPack(xPack,"type", text=str(self.itemType.name))
xSub = self._subPack(xPack,"class", text=str(self.itemClass.name)) xSub = self._subPack(xPack,"class", text=str(self.itemClass.name))
xSub = self._subPack(xPack,"status", text=str(self.itemStatus)) 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,"expanded", text=str(self.isExpanded))
if self.itemType == nwItemType.FILE: if self.itemType == nwItemType.FILE:
xSub = self._subPack(xPack,"layout", text=str(self.itemLayout.name)) xSub = self._subPack(xPack,"layout", text=str(self.itemLayout.name))
xSub = self._subPack(xPack,"charCount", text=str(self.charCount), none=False) 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 == "class": self.setClass(tagValue)
elif tagName == "layout": self.setLayout(tagValue) elif tagName == "layout": self.setLayout(tagValue)
elif tagName == "status": self.setStatus(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 == "expanded": self.setExpanded(tagValue)
elif tagName == "charCount": self.setCharCount(tagValue) elif tagName == "charCount": self.setCharCount(tagValue)
elif tagName == "wordCount": self.setWordCount(tagValue) elif tagName == "wordCount": self.setWordCount(tagValue)
@@ -160,14 +158,6 @@ class NWItem():
self.itemStatus = theStatus self.itemStatus = theStatus
return 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): def setExpanded(self, expState):
if isinstance(expState, str): if isinstance(expState, str):
self.isExpanded = expState == str(True) self.isExpanded = expState == str(True)
+2 -17
View File
@@ -102,6 +102,7 @@ class NWProject():
hNovel = self.newRoot("Novel", nwItemClass.NOVEL) hNovel = self.newRoot("Novel", nwItemClass.NOVEL)
hChars = self.newRoot("Characters", nwItemClass.CHARACTER) hChars = self.newRoot("Characters", nwItemClass.CHARACTER)
hWorld = self.newRoot("Plot", nwItemClass.PLOT)
hWorld = self.newRoot("World", nwItemClass.WORLD) hWorld = self.newRoot("World", nwItemClass.WORLD)
hChapt = self.newFolder("New Chapter", nwItemClass.NOVEL, hNovel) hChapt = self.newFolder("New Chapter", nwItemClass.NOVEL, hNovel)
hScene = self.newFile("New Scene", nwItemClass.NOVEL, hChapt) hScene = self.newFile("New Scene", nwItemClass.NOVEL, hChapt)
@@ -234,7 +235,7 @@ class NWProject():
# Save Tree Content # Save Tree Content
logger.debug("Writing project 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: for tHandle in self.treeOrder:
self.projTree[tHandle].packXML(xContent) self.projTree[tHandle].packXML(xContent)
@@ -373,16 +374,6 @@ class NWProject():
return 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): def _appendItem(self, tHandle, pHandle, nwItem):
tHandle = self._checkString(tHandle,self._makeHandle(),False) tHandle = self._checkString(tHandle,self._makeHandle(),False)
pHandle = self._checkString(pHandle,None,True) pHandle = self._checkString(pHandle,None,True)
@@ -405,12 +396,6 @@ class NWProject():
else: else:
logger.error("Only one trash folder allowed") 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 return
def _makeStatusIcons(self): def _makeStatusIcons(self):
@@ -0,0 +1 @@
# Very Deep File
+20 -26
View File
@@ -1,19 +1,17 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="0.0.1" fileVersion="1.0" timeStamp="2019-05-04 11:53:50"> <novelWriterXML appVersion="0.0.1" fileVersion="1.0" timeStamp="2019-05-04 17:22:40">
<project> <project>
<name>Sample Project</name> <name>Sample Project</name>
<title>Sample Project</title> <title>Sample Project</title>
<author>Jane Smith</author> <author>Jane Smith</author>
<author>Jay Doh</author> <author>Jay Doh</author>
</project> </project>
<content count="12"> <content count="14">
<item handle="7031beac91f75" order="0" parent="None"> <item handle="7031beac91f75" order="0" parent="None">
<name>Novel</name> <name>Novel</name>
<type>ROOT</type> <type>ROOT</type>
<class>NOVEL</class> <class>NOVEL</class>
<status>0</status> <status>0</status>
<depth>0</depth>
<children>True</children>
<expanded>True</expanded> <expanded>True</expanded>
</item> </item>
<item handle="53b69b83cdafc" order="0" parent="7031beac91f75"> <item handle="53b69b83cdafc" order="0" parent="7031beac91f75">
@@ -21,8 +19,6 @@
<type>FILE</type> <type>FILE</type>
<class>NOVEL</class> <class>NOVEL</class>
<status>2</status> <status>2</status>
<depth>1</depth>
<children>False</children>
<expanded>False</expanded> <expanded>False</expanded>
<layout>TITLE</layout> <layout>TITLE</layout>
<charCount>23</charCount> <charCount>23</charCount>
@@ -34,8 +30,6 @@
<type>FOLDER</type> <type>FOLDER</type>
<class>NOVEL</class> <class>NOVEL</class>
<status>1</status> <status>1</status>
<depth>1</depth>
<children>True</children>
<expanded>True</expanded> <expanded>True</expanded>
</item> </item>
<item handle="96b68994dfa3d" order="0" parent="e7ded148d6e4a"> <item handle="96b68994dfa3d" order="0" parent="e7ded148d6e4a">
@@ -43,8 +37,6 @@
<type>FILE</type> <type>FILE</type>
<class>NOVEL</class> <class>NOVEL</class>
<status>2</status> <status>2</status>
<depth>2</depth>
<children>False</children>
<expanded>False</expanded> <expanded>False</expanded>
<layout>SCENE</layout> <layout>SCENE</layout>
<charCount>2571</charCount> <charCount>2571</charCount>
@@ -56,8 +48,6 @@
<type>FILE</type> <type>FILE</type>
<class>NOVEL</class> <class>NOVEL</class>
<status>1</status> <status>1</status>
<depth>2</depth>
<children>False</children>
<expanded>False</expanded> <expanded>False</expanded>
<layout>SCENE</layout> <layout>SCENE</layout>
<charCount>376</charCount> <charCount>376</charCount>
@@ -69,8 +59,6 @@
<type>FILE</type> <type>FILE</type>
<class>NOVEL</class> <class>NOVEL</class>
<status>0</status> <status>0</status>
<depth>2</depth>
<children>False</children>
<expanded>False</expanded> <expanded>False</expanded>
<layout>SCENE</layout> <layout>SCENE</layout>
<charCount>0</charCount> <charCount>0</charCount>
@@ -82,8 +70,6 @@
<type>ROOT</type> <type>ROOT</type>
<class>CHARACTER</class> <class>CHARACTER</class>
<status>0</status> <status>0</status>
<depth>0</depth>
<children>True</children>
<expanded>True</expanded> <expanded>True</expanded>
</item> </item>
<item handle="f7e2d9f330615" order="0" parent="f6622b4617424"> <item handle="f7e2d9f330615" order="0" parent="f6622b4617424">
@@ -91,8 +77,6 @@
<type>FOLDER</type> <type>FOLDER</type>
<class>CHARACTER</class> <class>CHARACTER</class>
<status>0</status> <status>0</status>
<depth>1</depth>
<children>True</children>
<expanded>True</expanded> <expanded>True</expanded>
</item> </item>
<item handle="14298de4d9524" order="0" parent="f7e2d9f330615"> <item handle="14298de4d9524" order="0" parent="f7e2d9f330615">
@@ -100,8 +84,6 @@
<type>FILE</type> <type>FILE</type>
<class>CHARACTER</class> <class>CHARACTER</class>
<status>0</status> <status>0</status>
<depth>2</depth>
<children>False</children>
<expanded>False</expanded> <expanded>False</expanded>
<layout>NOTE</layout> <layout>NOTE</layout>
<charCount>42</charCount> <charCount>42</charCount>
@@ -113,8 +95,6 @@
<type>FILE</type> <type>FILE</type>
<class>CHARACTER</class> <class>CHARACTER</class>
<status>0</status> <status>0</status>
<depth>2</depth>
<children>False</children>
<expanded>False</expanded> <expanded>False</expanded>
<layout>NOTE</layout> <layout>NOTE</layout>
<charCount>51</charCount> <charCount>51</charCount>
@@ -126,8 +106,6 @@
<type>ROOT</type> <type>ROOT</type>
<class>WORLD</class> <class>WORLD</class>
<status>0</status> <status>0</status>
<depth>0</depth>
<children>True</children>
<expanded>True</expanded> <expanded>True</expanded>
</item> </item>
<item handle="b3e74dbc1f584" order="0" parent="73eee34351f85"> <item handle="b3e74dbc1f584" order="0" parent="73eee34351f85">
@@ -135,13 +113,29 @@
<type>FILE</type> <type>FILE</type>
<class>WORLD</class> <class>WORLD</class>
<status>0</status> <status>0</status>
<depth>1</depth>
<children>False</children>
<expanded>False</expanded> <expanded>False</expanded>
<layout>NOTE</layout> <layout>NOTE</layout>
<charCount>66</charCount> <charCount>66</charCount>
<wordCount>13</wordCount> <wordCount>13</wordCount>
<paraCount>1</paraCount> <paraCount>1</paraCount>
</item> </item>
<item handle="f75dac270c2a7" order="3" parent="None">
<name>Trash</name>
<type>TRASH</type>
<class>TRASH</class>
<status>0</status>
<expanded>True</expanded>
</item>
<item handle="4cd0bd12b087d" order="0" parent="f75dac270c2a7">
<name>New File</name>
<type>FILE</type>
<class>NOVEL</class>
<status>0</status>
<expanded>False</expanded>
<layout>SCENE</layout>
<charCount>3</charCount>
<wordCount>1</wordCount>
<paraCount>0</paraCount>
</item>
</content> </content>
</novelWriterXML> </novelWriterXML>