Enable drag for all sidebar items ...

... and set valid drop targets dynamically based on the current
selection.
This commit is contained in:
RumovZ 2021-03-08 11:35:39 +01:00
parent f07890c178
commit 8d9072193c

View File

@ -266,16 +266,10 @@ class SidebarModel(QAbstractItemModel):
def flags(self, index: QModelIndex) -> Qt.ItemFlags: def flags(self, index: QModelIndex) -> Qt.ItemFlags:
if not index.isValid(): if not index.isValid():
return cast(Qt.ItemFlags, Qt.ItemIsEnabled) return cast(Qt.ItemFlags, Qt.ItemIsEnabled)
flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled
item: SidebarItem = index.internalPointer() item: SidebarItem = index.internalPointer()
if item.item_type in ( if item.item_type in self.sidebar.valid_drop_types:
SidebarItemType.DECK, flags |= Qt.ItemIsDropEnabled
SidebarItemType.DECK_ROOT,
SidebarItemType.TAG,
SidebarItemType.TAG_ROOT,
):
flags |= Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled
if item.item_type.is_editable(): if item.item_type.is_editable():
flags |= Qt.ItemIsEditable flags |= Qt.ItemIsEditable
@ -365,6 +359,7 @@ class SidebarTreeView(QTreeView):
self.mw = browser.mw self.mw = browser.mw
self.col = self.mw.col self.col = self.mw.col
self.current_search: Optional[str] = None self.current_search: Optional[str] = None
self.valid_drop_types: Tuple[SidebarItemType, ...] = ()
self.setContextMenuPolicy(Qt.CustomContextMenu) self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.onContextMenu) # type: ignore self.customContextMenuRequested.connect(self.onContextMenu) # type: ignore
@ -436,6 +431,7 @@ class SidebarTreeView(QTreeView):
# tester = QAbstractItemModelTester(model) # tester = QAbstractItemModelTester(model)
self.setModel(model) self.setModel(model)
qconnect(self.selectionModel().selectionChanged, self._on_selection_changed)
if self.current_search: if self.current_search:
self.search_for(self.current_search) self.search_for(self.current_search)
else: else:
@ -568,6 +564,15 @@ class SidebarTreeView(QTreeView):
########### ###########
def _on_selection_changed(self, _new: QItemSelection, _old: QItemSelection) -> None:
selected_types = [item.item_type for item in self._selected_items()]
if all(item_type == SidebarItemType.DECK for item_type in selected_types):
self.valid_drop_types = (SidebarItemType.DECK, SidebarItemType.DECK_ROOT)
elif all(item_type == SidebarItemType.TAG for item_type in selected_types):
self.valid_drop_types = (SidebarItemType.TAG, SidebarItemType.TAG_ROOT)
else:
self.valid_drop_types = ()
def handle_drag_drop(self, sources: List[SidebarItem], target: SidebarItem) -> bool: def handle_drag_drop(self, sources: List[SidebarItem], target: SidebarItem) -> bool:
if target.item_type in (SidebarItemType.DECK, SidebarItemType.DECK_ROOT): if target.item_type in (SidebarItemType.DECK, SidebarItemType.DECK_ROOT):
return self._handle_drag_drop_decks(sources, target) return self._handle_drag_drop_decks(sources, target)