diff --git a/qt/aqt/deckbrowser.py b/qt/aqt/deckbrowser.py index 037707462..87b1cec85 100644 --- a/qt/aqt/deckbrowser.py +++ b/qt/aqt/deckbrowser.py @@ -3,6 +3,7 @@ # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html from __future__ import annotations +from concurrent.futures import Future from copy import deepcopy from dataclasses import dataclass @@ -280,25 +281,34 @@ class DeckBrowser: self.show() - def _delete(self, did: int): - self.mw.checkpoint(tr(TR.DECKS_DELETE_DECK)) + def ask_delete_deck(self, did: int) -> bool: deck = self.mw.col.decks.get(did) - extra = None - if not deck["dyn"]: - count = self.mw.col.decks.card_count(did, include_subdecks=True) - if count: - extra = tr(TR.DECKS_IT_HAS_CARD, count=count) - if ( - deck["dyn"] - or not extra - or askUser( - (tr(TR.DECKS_ARE_YOU_SURE_YOU_WISH_TO, val=deck["name"])) + extra - ) + if deck["dyn"]: + return True + + count = self.mw.col.decks.card_count(did, include_subdecks=True) + if not count: + return True + + extra = tr(TR.DECKS_IT_HAS_CARD, count=count) + if askUser( + tr(TR.DECKS_ARE_YOU_SURE_YOU_WISH_TO, val=deck["name"]) + " " + extra ): - self.mw.progress.start() - self.mw.col.decks.rem(did, True) - self.mw.progress.finish() - self.show() + return True + return False + + def _delete(self, did: int) -> None: + if self.ask_delete_deck(did): + + def do_delete(): + return self.mw.col.decks.rem(did, True) + + def on_done(fut: Future): + self.show() + res = fut.result() # Required to check for errors + + self.mw.checkpoint(tr(TR.DECKS_DELETE_DECK)) + self.mw.taskman.with_progress(do_delete, on_done) # Top buttons ###################################################################### diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 50bb61144..0a865c550 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -79,6 +79,7 @@ class ResetReason(enum.Enum): BrowserReschedule = "browserReschedule" BrowserFindReplace = "browserFindReplace" BrowserTagDupes = "browserTagDupes" + BrowserDeleteDeck = "browserDeleteDeck" class ResetRequired: diff --git a/qt/aqt/sidebar.py b/qt/aqt/sidebar.py index 7b8c068b0..69f2a52e9 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -72,7 +72,10 @@ class NewSidebarTreeView(SidebarTreeViewBase): self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.onContextMenu) # type: ignore self.context_menus = { - SidebarItemType.DECK: ((tr(TR.ACTIONS_RENAME), self.rename_deck),), + SidebarItemType.DECK: ( + (tr(TR.ACTIONS_RENAME), self.rename_deck), + (tr(TR.ACTIONS_DELETE), self.delete_deck), + ), SidebarItemType.TAG: ((tr(TR.ACTIONS_RENAME), self.rename_tag),), } @@ -134,3 +137,24 @@ class NewSidebarTreeView(SidebarTreeViewBase): self.mw.checkpoint(tr(TR.ACTIONS_RENAME_TAG)) self.browser.model.beginReset() self.mw.taskman.run_in_background(do_rename, on_done) + + def delete_deck(self, item: "aqt.browser.SidebarItem") -> None: + self.browser.editor.saveNow(lambda: self._delete_deck(item)) + + def _delete_deck(self, item: "aqt.browser.SidebarItem") -> None: + did = item.id + if self.mw.deckBrowser.ask_delete_deck(did): + + def do_delete(): + return self.mw.col.decks.rem(did, True) + + def on_done(fut: Future): + self.mw.requireReset(reason=ResetReason.BrowserDeleteDeck, context=self) + self.browser.search() + self.browser.model.endReset() + self.browser.maybeRefreshSidebar() + res = fut.result() # Required to check for errors + + self.mw.checkpoint(tr(TR.DECKS_DELETE_DECK)) + self.browser.model.beginReset() + self.mw.taskman.run_in_background(do_delete, on_done)