From cf3350c6b69113c50aac58166f32f9bbdc4b0a87 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 20 Dec 2020 22:07:10 +0100 Subject: [PATCH] Trash and Orphaned folders shouldn't be editable --- nw/constants/constants.py | 7 ++++++- nw/gui/projtree.py | 21 +++++++++++++-------- nw/guimain.py | 12 +++++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/nw/constants/constants.py b/nw/constants/constants.py index a768da9d..8a82f676 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -25,7 +25,9 @@ along with this program. If not, see . """ -from nw.constants.enum import nwItemClass, nwItemLayout, nwOutline +from nw.constants.enum import ( + nwItemClass, nwItemLayout, nwItemType, nwOutline +) class nwConst(): @@ -43,6 +45,9 @@ class nwConst(): SP_INTERNAL = "internal" SP_ENCHANT = "enchant" + # Check Lists + REG_TYPES = {nwItemType.ROOT, nwItemType.FOLDER, nwItemType.FILE} + # END Class nwConst class nwRegEx(): diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index 2c52c4cb..51ef8005 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -687,9 +687,11 @@ class GuiProjectTree(QTreeWidget): selItem = self.itemAt(clickPos) if isinstance(selItem, QTreeWidgetItem): tHandle = selItem.data(self.C_NAME, Qt.UserRole) - tItem = self.theProject.projTree[tHandle] - self.setSelectedHandle(tHandle) # Just to be safe + if tHandle is None: + return + self.setSelectedHandle(tHandle) # Just to be safe + tItem = self.theProject.projTree[tHandle] if self.ctxMenu.filterActions(tItem): # Only open menu if any actions remain after filter self.ctxMenu.exec_(self.viewport().mapToGlobal(clickPos)) @@ -718,6 +720,9 @@ class GuiProjectTree(QTreeWidget): return tHandle = selItem.data(self.C_NAME, Qt.UserRole) + if tHandle is None: + return + tItem = self.theProject.projTree[tHandle] if tItem is None: return @@ -909,16 +914,16 @@ class GuiProjectTree(QTreeWidget): """ if self.orphRoot is None: newItem = QTreeWidgetItem([""]*4) - newItem.setText(self.C_NAME, "Orphaned Files") - newItem.setText(self.C_COUNT, "") + newItem.setText(self.C_NAME, "Orphaned Files") + newItem.setData(self.C_NAME, Qt.UserRole, None) + newItem.setIcon(self.C_NAME, self.theTheme.getIcon("proj_orphan")) + newItem.setText(self.C_COUNT, "") + newItem.setData(self.C_COUNT, Qt.UserRole, 0) newItem.setText(self.C_EXPORT, "") - newItem.setText(self.C_FLAGS, "") + newItem.setText(self.C_FLAGS, "") self.addTopLevelItem(newItem) self.orphRoot = newItem newItem.setExpanded(True) - newItem.setData(self.C_NAME, Qt.UserRole, "") - newItem.setData(self.C_COUNT, Qt.UserRole, 0) - newItem.setIcon(self.C_NAME, self.theTheme.getIcon("proj_orphan")) return diff --git a/nw/guimain.py b/nw/guimain.py index 4361ae8f..7e50cfef 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -47,7 +47,7 @@ from nw.gui import ( GuiTheme, GuiWritingStats ) from nw.core import NWProject, NWDoc, NWIndex -from nw.constants import nwItemType, nwItemClass, nwAlert +from nw.constants import nwItemType, nwItemClass, nwAlert, nwConst from nw.common import getGuiItem logger = logging.getLogger(__name__) @@ -722,6 +722,12 @@ class GuiMain(QMainWindow): logger.warning("No item selected") return + tItem = self.theProject.projTree[tHandle] + if tItem is None: + return + if tItem.itemType not in nwConst.REG_TYPES: + return + logger.verbose("Requesting change to item %s" % tHandle) dlgProj = GuiItemEditor(self, self.theProject, tHandle) dlgProj.exec_() @@ -1312,6 +1318,9 @@ class GuiMain(QMainWindow): we open it. Otherwise, we do nothing. """ tHandle = tItem.data(self.treeView.C_NAME, Qt.UserRole) + if tHandle is None: + return + logger.verbose("User double clicked tree item with handle %s" % tHandle) nwItem = self.theProject.projTree[tHandle] if nwItem is not None: @@ -1320,6 +1329,7 @@ class GuiMain(QMainWindow): self.openDocument(tHandle, changeFocus=False, doScroll=False) else: logger.verbose("Requested item %s is a folder" % tHandle) + return def _treeKeyPressReturn(self):