Some minor cleanup and fixes

This commit is contained in:
Veronica K. B. Olsen
2020-12-21 11:03:47 +01:00
parent dc16077205
commit 4509545214
3 changed files with 53 additions and 42 deletions
+16 -1
View File
@@ -1317,10 +1317,12 @@ class NWProject():
# Handle orphans # Handle orphans
aDoc = NWDoc(self, self.theParent) aDoc = NWDoc(self, self.theParent)
nOrph = 0 nOrph = 0
noWhere = False
for oHandle in orphanFiles: for oHandle in orphanFiles:
# Look for meta data # Look for meta data
oName = "" oName = ""
oParent = None
oClass = None oClass = None
oLayout = None oLayout = None
if aDoc.openDocument(oHandle, showStatus=False, isOrphan=True) is not None: if aDoc.openDocument(oHandle, showStatus=False, isOrphan=True) is not None:
@@ -1332,16 +1334,23 @@ class NWProject():
nOrph += 1 nOrph += 1
oName = "Recovered File %d" % nOrph oName = "Recovered File %d" % nOrph
# Recover file meta data
if oClass is None: if oClass is None:
oClass = nwItemClass.NOVEL oClass = nwItemClass.NOVEL
if oLayout is None: if oLayout is None:
oLayout = nwItemLayout.NOTE oLayout = nwItemLayout.NOTE
if oParent is None or not self.projTree.isValid(oParent): if oParent is None or not self.projTree.handleExists(oParent):
oParent = self.projTree.findRoot(oClass) oParent = self.projTree.findRoot(oClass)
if oParent is None: if oParent is None:
oParent = self.projTree.findRoot(nwItemClass.NOVEL) oParent = self.projTree.findRoot(nwItemClass.NOVEL)
# If the file still has no parent item, skip it
if oParent is None:
noWhere = True
continue
orphItem = NWItem(self) orphItem = NWItem(self)
orphItem.setName(oName) orphItem.setName(oName)
orphItem.setType(nwItemType.FILE) orphItem.setType(nwItemType.FILE)
@@ -1349,6 +1358,12 @@ class NWProject():
orphItem.setLayout(oLayout) orphItem.setLayout(oLayout)
self.projTree.append(oHandle, oParent, orphItem) self.projTree.append(oHandle, oParent, orphItem)
if noWhere:
self.makeAlert((
"One or more orphaned files could not be added back into the "
"project. Make sure at least a Novel root folder exists."
), nwAlert.WARN)
return True return True
def _appendSessionStats(self): def _appendSessionStats(self):
+1 -1
View File
@@ -294,7 +294,7 @@ class NWTree():
tTree.append(tHandle) tTree.append(tHandle)
return tTree return tTree
def isValid(self, tHandle): def handleExists(self, tHandle):
"""Check if a handle exists in the project. """Check if a handle exists in the project.
""" """
return tHandle in self._treeOrder return tHandle in self._treeOrder
+36 -40
View File
@@ -61,7 +61,7 @@ class GuiProjectTree(QTreeWidget):
self.theIndex = theParent.theIndex self.theIndex = theParent.theIndex
# Tree Settings # Tree Settings
self.theMap = None self.theMap = {}
self.treeChanged = False self.treeChanged = False
self.ctxMenu = GuiProjectTreeMenu(self) self.ctxMenu = GuiProjectTreeMenu(self)
@@ -144,10 +144,11 @@ class GuiProjectTree(QTreeWidget):
## ##
def clearTree(self): def clearTree(self):
"""Clear the GUI content and the related maps. """Clear the GUI content and the related map.
""" """
self.clear() self.clear()
self.theMap = {} self.theMap = {}
self.treeChanged = False
return return
def newTreeItem(self, itemType, itemClass): def newTreeItem(self, itemType, itemClass):
@@ -390,14 +391,12 @@ class GuiProjectTree(QTreeWidget):
return False return False
msgYes = self.theParent.askQuestion( msgYes = self.theParent.askQuestion(
"Empty Trash", "Permanently delete %d file%s from Trash?" % ( "Empty Trash", "Permanently delete %d file(s) from Trash?" % nTrash
nTrash, "s" if nTrash > 1 else ""
)
) )
if not msgYes: if not msgYes:
return False return False
logger.verbose("Deleting %d files from Trash" % nTrash) logger.verbose("Deleting %d file(s) from Trash" % nTrash)
for tHandle in self.getTreeFromHandle(trashHandle): for tHandle in self.getTreeFromHandle(trashHandle):
if tHandle == trashHandle: if tHandle == trashHandle:
continue continue
@@ -637,11 +636,7 @@ class GuiProjectTree(QTreeWidget):
selected, return the first. selected, return the first.
""" """
selItem = self.selectedItems() selItem = self.selectedItems()
if selItem:
if len(selItem) == 0:
return None
if isinstance(selItem[0], QTreeWidgetItem):
return selItem[0].data(self.C_NAME, Qt.UserRole) return selItem[0].data(self.C_NAME, Qt.UserRole)
return None return None
@@ -660,18 +655,21 @@ class GuiProjectTree(QTreeWidget):
def setSelectedHandle(self, tHandle, doScroll=False): def setSelectedHandle(self, tHandle, doScroll=False):
"""Set a specific handle as the selected item. """Set a specific handle as the selected item.
""" """
if tHandle in self.theMap: if tHandle not in self.theMap:
self.clearSelection() return False
self.theMap[tHandle].setSelected(True)
selItems = self.selectedIndexes() tItem = self._getTreeItem(tHandle)
if selItems and doScroll: if tItem is None:
self.scrollTo( return False
selItems[0], QAbstractItemView.PositionAtCenter
)
return True
return False self.clearSelection()
self.theMap[tHandle].setSelected(True)
selItems = self.selectedIndexes()
if selItems and doScroll:
self.scrollTo(selItems[0], QAbstractItemView.PositionAtCenter)
return True
## ##
# Slots # Slots
@@ -684,14 +682,12 @@ class GuiProjectTree(QTreeWidget):
selItem = self.itemAt(clickPos) selItem = self.itemAt(clickPos)
if isinstance(selItem, QTreeWidgetItem): if isinstance(selItem, QTreeWidgetItem):
tHandle = selItem.data(self.C_NAME, Qt.UserRole) tHandle = selItem.data(self.C_NAME, Qt.UserRole)
if tHandle is None:
return
self.setSelectedHandle(tHandle) # Just to be safe self.setSelectedHandle(tHandle) # Just to be safe
tItem = self.theProject.projTree[tHandle] tItem = self.theProject.projTree[tHandle]
if self.ctxMenu.filterActions(tItem): if tItem is not None:
# Only open menu if any actions remain after filter if self.ctxMenu.filterActions(tItem):
self.ctxMenu.exec_(self.viewport().mapToGlobal(clickPos)) # Only open menu if any actions remain after filter
self.ctxMenu.exec_(self.viewport().mapToGlobal(clickPos))
return return
@@ -770,7 +766,7 @@ class GuiProjectTree(QTreeWidget):
# If the item does not have the same class as the target, # If the item does not have the same class as the target,
# and the target is not a free root folder, update its class # and the target is not a free root folder, update its class
if not isSame and not onFree: if not (isSame or onFree):
logger.debug("Item %s class has been changed from %s to %s" % ( logger.debug("Item %s class has been changed from %s to %s" % (
sHandle, sHandle,
snItem.itemClass.name, snItem.itemClass.name,
@@ -803,9 +799,7 @@ class GuiProjectTree(QTreeWidget):
def _getTreeItem(self, tHandle): def _getTreeItem(self, tHandle):
"""Returns the QTreeWidgetItem of a given item handle. """Returns the QTreeWidgetItem of a given item handle.
""" """
if tHandle in self.theMap.keys(): return self.theMap.get(tHandle, None)
return self.theMap[tHandle]
return None
def _scanChildren(self, theList, theItem, theIndex): def _scanChildren(self, theList, theItem, theIndex):
"""This is a recursive function returning all items in a tree """This is a recursive function returning all items in a tree
@@ -829,19 +823,20 @@ class GuiProjectTree(QTreeWidget):
tClass = nwItem.itemClass tClass = nwItem.itemClass
newItem = QTreeWidgetItem([""]*4) newItem = QTreeWidgetItem([""]*4)
newItem.setText(self.C_NAME, "") newItem.setText(self.C_NAME, "")
newItem.setText(self.C_COUNT, "0") newItem.setText(self.C_COUNT, "0")
newItem.setText(self.C_EXPORT, "") newItem.setText(self.C_EXPORT, "")
newItem.setText(self.C_FLAGS, "") newItem.setText(self.C_FLAGS, "")
newItem.setTextAlignment(self.C_NAME, Qt.AlignLeft | Qt.AlignVCenter) newItem.setTextAlignment(self.C_NAME, Qt.AlignLeft)
newItem.setTextAlignment(self.C_COUNT, Qt.AlignRight | Qt.AlignVCenter) newItem.setTextAlignment(self.C_COUNT, Qt.AlignRight)
newItem.setTextAlignment(self.C_EXPORT, Qt.AlignLeft | Qt.AlignVCenter) newItem.setTextAlignment(self.C_EXPORT, Qt.AlignLeft)
newItem.setTextAlignment(self.C_FLAGS, Qt.AlignLeft | Qt.AlignVCenter) newItem.setTextAlignment(self.C_FLAGS, Qt.AlignLeft)
newItem.setData(self.C_NAME, Qt.UserRole, tHandle) newItem.setData(self.C_NAME, Qt.UserRole, tHandle)
newItem.setData(self.C_COUNT, Qt.UserRole, 0) newItem.setData(self.C_COUNT, Qt.UserRole, 0)
self.theMap[tHandle] = newItem
if pHandle is None: if pHandle is None:
if nwItem.itemType == nwItemType.ROOT: if nwItem.itemType == nwItemType.ROOT:
self.addTopLevelItem(newItem) self.addTopLevelItem(newItem)
@@ -850,9 +845,11 @@ class GuiProjectTree(QTreeWidget):
self.addTopLevelItem(newItem) self.addTopLevelItem(newItem)
else: else:
self.makeAlert( self.makeAlert(
"There is nowhere to add file with name '%s'" % nwItem.itemName, nwAlert.ERROR "There is nowhere to add item with name '%s'" % nwItem.itemName, nwAlert.ERROR
) )
del self.theMap[tHandle]
return None return None
else: else:
byIndex = -1 byIndex = -1
if nHandle is not None and nHandle in self.theMap: if nHandle is not None and nHandle in self.theMap:
@@ -866,7 +863,6 @@ class GuiProjectTree(QTreeWidget):
self.theMap[pHandle].addChild(newItem) self.theMap[pHandle].addChild(newItem)
self.propagateCount(tHandle, nwItem.wordCount) self.propagateCount(tHandle, nwItem.wordCount)
self.theMap[tHandle] = newItem
self.setTreeItemValues(tHandle) self.setTreeItemValues(tHandle)
newItem.setExpanded(nwItem.isExpanded) newItem.setExpanded(nwItem.isExpanded)