From 63e91f0ee541a9dbce6b83ae40732612e5e0252b Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 9 Mar 2021 10:52:27 +0100 Subject: [PATCH 1/2] Fix broken emptu trash feature (#701) --- nw/gui/projtree.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index eff1ae7c..21740171 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -451,14 +451,14 @@ class GuiProjectTree(QTreeWidget): for tHandle in self.getTreeFromHandle(trashHandle): if tHandle == trashHandle: continue - self.deleteItem(tHandle, alreadyAsked=True) + self.deleteItem(tHandle, alreadyAsked=True, bulkAction=True) if nTrash > 0: self._setTreeChanged(True) return True - def deleteItem(self, tHandle=None, alreadyAsked=False, askForTrash=False): + def deleteItem(self, tHandle=None, alreadyAsked=False, askForTrash=False, bulkAction=False): """Delete an item from the project tree. As a first step, files are moved to the Trash folder. Permanent deletion is a second step. This second step also deletes the item from the project object as well as @@ -469,24 +469,27 @@ class GuiProjectTree(QTreeWidget): logger.error("No project open") return False - if not self.hasFocus(): + if not self.hasFocus() and not bulkAction: + logger.info("Delete action blocked due to no widget focus") return False if tHandle is None: tHandle = self.getSelectedHandle() if tHandle is None: + logger.error("There is no item to delete") return False trItemS = self._getTreeItem(tHandle) nwItemS = self.theProject.projTree[tHandle] if trItemS is None or nwItemS is None: + logger.error("Could not find tree item for deletion") return False wCount = int(trItemS.data(self.C_COUNT, Qt.UserRole)) if nwItemS.itemType == nwItemType.FILE: - logger.debug("User requested file %s moved to trash" % tHandle) + logger.debug("User requested file %s deleted" % tHandle) trItemP = trItemS.parent() trItemT = self._addTrashRoot() if trItemP is None or trItemT is None: @@ -540,6 +543,8 @@ class GuiProjectTree(QTreeWidget): if pHandle is None: logger.warning("File has no parent item") + logger.debug("Moving file %s to trash" % tHandle) + self.propagateCount(tHandle, 0) tIndex = trItemP.indexOfChild(trItemS) trItemC = trItemP.takeChild(tIndex) From 065e3828b18d3de0f6d6b834b9d11c6ac73ab83f Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 9 Mar 2021 11:02:44 +0100 Subject: [PATCH 2/2] Update test and always ask before moving to trash --- nw/gui/projtree.py | 19 ++++++------------- tests/test_gui/test_gui_projtree.py | 1 - 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index 21740171..e1cd00eb 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -458,7 +458,7 @@ class GuiProjectTree(QTreeWidget): return True - def deleteItem(self, tHandle=None, alreadyAsked=False, askForTrash=False, bulkAction=False): + def deleteItem(self, tHandle=None, alreadyAsked=False, bulkAction=False): """Delete an item from the project tree. As a first step, files are moved to the Trash folder. Permanent deletion is a second step. This second step also deletes the item from the project object as well as @@ -529,17 +529,10 @@ class GuiProjectTree(QTreeWidget): else: # The file is not already in the trash folder, so we # move it there. - doTrash = False - if askForTrash: - msgYes = self.askQuestion( - "Delete File", "Move file '%s' to Trash?" % nwItemS.itemName - ) - if msgYes: - doTrash = True - else: - doTrash = True - - if doTrash: + msgYes = self.askQuestion( + "Delete File", "Move file '%s' to Trash?" % nwItemS.itemName + ) + if msgYes: if pHandle is None: logger.warning("File has no parent item") @@ -1210,7 +1203,7 @@ class GuiProjectTreeMenu(QMenu): """Forward the delete item call to the project tree. """ if self.theItem is not None: - self.theTree.deleteItem(askForTrash=True) + self.theTree.deleteItem() return def _doEmptyTrash(self): diff --git a/tests/test_gui/test_gui_projtree.py b/tests/test_gui/test_gui_projtree.py index b0fdaa89..07fbc312 100644 --- a/tests/test_gui/test_gui_projtree.py +++ b/tests/test_gui/test_gui_projtree.py @@ -42,7 +42,6 @@ def testGuiProjTree_TreeItems(qtbot, caplog, monkeypatch, nwGUI, nwMinimal): monkeypatch.setattr(QMessageBox, "warning", lambda *args: QMessageBox.Yes) monkeypatch.setattr(QMessageBox, "information", lambda *args: QMessageBox.Yes) monkeypatch.setattr(GuiMain, "editItem", lambda *args: None) - monkeypatch.setattr(GuiProjectTree, "hasFocus", lambda *args: True) nwGUI.theProject.projTree.setSeed(42) nwTree = nwGUI.treeView