diff --git a/nw/core/project.py b/nw/core/project.py index 8bfbaa74..f1af6d5c 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -1324,23 +1324,30 @@ class NWProject(): oClass = None oLayout = None if aDoc.openDocument(oHandle, showStatus=False, isOrphan=True) is not None: - oName, _, oClass, oLayout = aDoc.getMeta() + oName, oParent, oClass, oLayout = aDoc.getMeta() - if oName == "": + if oName: + oName = "Recovered: %s" % oName.lstrip("Recovered: ") + else: nOrph += 1 - oName = "Orphaned File %d" % nOrph + oName = "Recovered File %d" % nOrph if oClass is None: - oClass = nwItemClass.NO_CLASS + oClass = nwItemClass.NOVEL if oLayout is None: - oLayout = nwItemLayout.NO_LAYOUT + oLayout = nwItemLayout.NOTE + + if oParent is None or not self.projTree.isValid(oParent): + oParent = self.projTree.findRoot(oClass) + if oParent is None: + oParent = self.projTree.findRoot(nwItemClass.NOVEL) orphItem = NWItem(self) orphItem.setName(oName) orphItem.setType(nwItemType.FILE) orphItem.setClass(oClass) orphItem.setLayout(oLayout) - self.projTree.append(oHandle, None, orphItem) + self.projTree.append(oHandle, oParent, orphItem) return True diff --git a/nw/core/tree.py b/nw/core/tree.py index 43678b73..6e103ec1 100644 --- a/nw/core/tree.py +++ b/nw/core/tree.py @@ -294,6 +294,11 @@ class NWTree(): tTree.append(tHandle) return tTree + def isValid(self, tHandle): + """Check if a handle exists in the project. + """ + return tHandle in self._treeOrder + ## # Setters ## diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index 51ef8005..748d9ed8 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -851,7 +851,6 @@ class GuiProjectTree(QTreeWidget): newItem.setData(self.C_NAME, Qt.UserRole, tHandle) newItem.setData(self.C_COUNT, Qt.UserRole, 0) - self.theMap[tHandle] = newItem if pHandle is None: if nwItem.itemType == nwItemType.ROOT: self.addTopLevelItem(newItem) @@ -859,8 +858,11 @@ class GuiProjectTree(QTreeWidget): elif nwItem.itemType == nwItemType.TRASH: self.addTopLevelItem(newItem) else: - self._addOrphanedRoot() - self.orphRoot.addChild(newItem) + self.makeAlert( + "There is nowhere to add file with name '%s'" % nwItem.itemName, nwAlert.ERROR + ) + # self._addOrphanedRoot() + # self.orphRoot.addChild(newItem) else: byIndex = -1 if nHandle is not None and nHandle in self.theMap: @@ -874,6 +876,7 @@ class GuiProjectTree(QTreeWidget): self.theMap[pHandle].addChild(newItem) self.propagateCount(tHandle, nwItem.wordCount) + self.theMap[tHandle] = newItem self.setTreeItemValues(tHandle) newItem.setExpanded(nwItem.isExpanded)