Skip to content

Commit

Permalink
Add blockers for illegal drop events on invisible root and for root i…
Browse files Browse the repository at this point in the history
…tems (#1569)
  • Loading branch information
vkbo committed Nov 4, 2023
1 parent 0413684 commit a7e2b51
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion novelwriter/gui/projtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a7e2b51

Please sign in to comment.