From 18f0b78716e5ac579738681f05914cb0ed120236 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 30 Jan 2021 21:32:25 +1000 Subject: [PATCH] disable dragging of unsupported items --- qt/aqt/sidebar.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/qt/aqt/sidebar.py b/qt/aqt/sidebar.py index a9d900bf9..5a528ff06 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -175,15 +175,19 @@ class SidebarModel(QAbstractItemModel): def supportedDropActions(self): return Qt.MoveAction - def flags(self, index): + def flags(self, index: QModelIndex): if not index.isValid(): return Qt.ItemIsEnabled - return ( - Qt.ItemIsEnabled - | Qt.ItemIsSelectable - | Qt.ItemIsDragEnabled - | Qt.ItemIsDropEnabled - ) + flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable + + item: SidebarItem = index.internalPointer() + if item.item_type in ( + SidebarItemType.DECK, + SidebarItemType.DECK_ROOT, + ): + flags |= Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled + + return flags # Helpers ###################################################################### @@ -353,10 +357,12 @@ class SidebarTreeView(QTreeView): return self._handle_drag_drop_decks(sources, target) return False - def _handle_drag_drop_decks(self, sources: List[SidebarItem], target: SidebarItem) -> bool: - source_ids = [source.id - for source in sources - if source.item_type == SidebarItemType.DECK] + def _handle_drag_drop_decks( + self, sources: List[SidebarItem], target: SidebarItem + ) -> bool: + source_ids = [ + source.id for source in sources if source.item_type == SidebarItemType.DECK + ] if not source_ids: return False @@ -364,8 +370,9 @@ class SidebarTreeView(QTreeView): fut.result() self.refresh() - self.mw.taskman.with_progress(lambda: self.col.decks.drag_drop_decks(source_ids, target.id), - on_done) + self.mw.taskman.with_progress( + lambda: self.col.decks.drag_drop_decks(source_ids, target.id), on_done + ) return True def onClickCurrent(self) -> None: