From 99257b82cd8004c6e2c8c55435bd751ccb7df721 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Mon, 15 Apr 2019 23:30:24 +0200 Subject: [PATCH] Drag'n'drop now checks that the drop is allowed. --- nw/gui/doctree.py | 28 ++++++++++++++++++++++++++++ nw/gui/doctreectx.py | 12 ++++++++++++ nw/gui/winmain.py | 7 ++++--- sample/sampleNovel/nwProject.nwx | 22 +++++++++++++++++++--- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index 489dc89c..778bded8 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -181,4 +181,32 @@ class GuiDocTree(QTreeWidget): self.clearSelection() return + def dropEvent(self, theEvent): + """Overload the drop of dragged item event to check whether the drop is allowed + or not. Disallowed drops are cancelled. + """ + sHandle = self._getSelectedHandle() + if sHandle is None: + return + + dIndex = self.indexAt(theEvent.pos()) + if not dIndex.isValid(): + return + + dItem = self.itemFromIndex(dIndex) + dHandle = dItem.text(3) + snItem = self.theProject.getItem(sHandle) + dnItem = self.theProject.getItem(dHandle) + isSame = snItem.itemClass == dnItem.itemClass and dnItem.itemType + isFile = dnItem.itemType == nwItemType.FILE + isRoot = snItem.itemType == nwItemType.ROOT + isOnTop = self.dropIndicatorPosition() == QAbstractItemView.OnItem + if isSame and not (isFile and isOnTop) and not isRoot: + logger.verbose("Drag'n'drop on item %s allowed" % sHandle) + QTreeWidget.dropEvent(self, theEvent) + else: + logger.verbose("Drag'n'drop on item %s not allowed" % sHandle) + + return + # END Class GuiDocTree diff --git a/nw/gui/doctreectx.py b/nw/gui/doctreectx.py index d7bece76..eb020bc8 100644 --- a/nw/gui/doctreectx.py +++ b/nw/gui/doctreectx.py @@ -76,6 +76,7 @@ class GuiDocTreeCtx(QMenu): self._buildMenuDeleteRoot(vActs) if nwItemAction.EMPTY_TRASH in vActs.keys(): self._buildMenuEmptyTrash(vActs) + return ## # Build Sub Menus @@ -87,6 +88,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.DELETE, None, None )) self.addAction(mnuItem) + return def _buildMenuDeleteRoot(self, vActs): mnuItem = QAction("Remove Root", self) @@ -94,6 +96,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.DELETE_ROOT, None, None )) self.addAction(mnuItem) + return def _buildMenuEmptyTrash(self, vActs): mnuItem = QAction("Empty Trash", self) @@ -101,6 +104,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.EMPTY_TRASH, None, None )) self.addAction(mnuItem) + return def _buildMenuSplit(self, vActs): mnuItem = QAction("Split File", self) @@ -108,6 +112,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.SPLIT, None, None )) self.addAction(mnuItem) + return def _buildMenuMerge(self, vActs): mnuItem = QAction("Merge Folder", self) @@ -115,6 +120,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.MERGE, None, None )) self.addAction(mnuItem) + return def _buildMenuMoveUp(self, vActs): mnuItem = QAction("Move Up", self) @@ -122,6 +128,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.MOVE_UP, None, None )) self.addAction(mnuItem) + return def _buildMenuMoveDown(self, vActs): mnuItem = QAction("Move Down", self) @@ -129,6 +136,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.MOVE_DOWN, None, None )) self.addAction(mnuItem) + return def _buildMenuMoveTrash(self, vActs): mnuItem = QAction("Move to Trash", self) @@ -136,6 +144,7 @@ class GuiDocTreeCtx(QMenu): nwItemAction.MOVE_TRASH, None, None )) self.addAction(mnuItem) + return def _buildMenuMoveTo(self, vActs): mnuItem = QAction("Move to", self) @@ -144,6 +153,7 @@ class GuiDocTreeCtx(QMenu): None )) self.addAction(mnuItem) + return def _buildMenuAddFile(self, vActs): mnuItem = QAction("Add File", self) @@ -153,6 +163,7 @@ class GuiDocTreeCtx(QMenu): nwItemType.FILE )) self.addAction(mnuItem) + return def _buildMenuAddFolder(self, vActs): mnuItem = QAction("Add Folder", self) @@ -162,6 +173,7 @@ class GuiDocTreeCtx(QMenu): nwItemType.FOLDER )) self.addAction(mnuItem) + return def _buildMenuAddRoot(self, vActs): diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index faff023f..528fce80 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -206,13 +206,14 @@ class GuiMain(QMainWindow): def _openDocTreeContextMenu(self, thePosition): - ctxMenu = GuiDocTreeCtx(self.treeView, self.theProject, thePosition) - + ctxMenu = GuiDocTreeCtx(self.treeView, self.theProject, thePosition) + selHandle = ctxMenu.selHandle selAction = ctxMenu.selAction selClass = ctxMenu.selClass selType = ctxMenu.selType + selTarget = ctxMenu.selTarget - print(selAction, selClass, selType) + print(selHandle, selAction, selClass, selType, selTarget) return diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index d66541fa..7a58ca19 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,12 +1,12 @@ - + Sample Project Sample Project Jane Smith Jay Doh - + Novel ROOT @@ -44,13 +44,29 @@ ROOT CHARACTER 0 + True + True + + + Jon Smith + FILE + CHARACTER + 1 + False + False + + + Jane Smith + FILE + CHARACTER + 1 False False World ROOT - WORLD + NONE 0 False False