From a7e2b512252b349693dac226ee1fea243fd59a43 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 4 Nov 2023 10:05:37 +0100 Subject: [PATCH] Add blockers for illegal drop events on invisible root and for root items (#1569) --- novelwriter/gui/projtree.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index e753a8288..aad2fd4d5 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -507,8 +507,11 @@ def __init__(self, projView: GuiProjectView) -> None: # Allow Move by Drag & Drop self.setDragEnabled(True) self.setDragDropMode(QAbstractItemView.InternalMove) + self.setDropIndicatorShown(True) # But don't allow drop on root level + # Due to a bug, this stops working somewhere between Qt 5.15.3 + # and 5.15.8, so this is also blocked in dropEvent trRoot = self.invisibleRootItem() trRoot.setFlags(trRoot.flags() ^ Qt.ItemIsDropEnabled) @@ -1380,8 +1383,16 @@ def dropEvent(self, event: QDropEvent) -> None: """ sHandle = self.getSelectedHandle() sItem = self._getTreeItem(sHandle) if sHandle else None - if sHandle is None or sItem is None: + if sHandle is None or sItem is None or sItem.parent() is None: logger.error("Invalid drag and drop event") + event.ignore() + return + + if not self.indexAt(event.pos()).isValid(): + # Needed due to a bug somewhere around Qt 5.15.8 that + # ignores the invisible root item flags + logger.error("Invalid drop location") + event.ignore() return logger.debug("Drag'n'drop of item '%s' accepted", sHandle) @@ -1728,7 +1739,9 @@ def _addTreeItem(self, nwItem: NWItem | None, newItem.setData(self.C_DATA, self.D_WORDS, 0) if pHandle is None and nwItem.isRootType(): + # newItem.setFlags(newItem.flags() ^ Qt.ItemFlag.ItemIsDragEnabled) pItem = self.invisibleRootItem() + # pItem.setFlags(pItem.flags() ^ Qt.ItemFlag.ItemIsDropEnabled) elif pHandle and pHandle in self._treeMap: pItem = self._treeMap[pHandle] else: