From f6f06986d04cd83d77bf69f1d7ae0e635687395a Mon Sep 17 00:00:00 2001 From: BlueGreenMagick <50060875+BlueGreenMagick@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:28:19 +0900 Subject: [PATCH 1/5] seperate ask_delete_deck and _delete method --- qt/aqt/deckbrowser.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/qt/aqt/deckbrowser.py b/qt/aqt/deckbrowser.py index 037707462..715461d32 100644 --- a/qt/aqt/deckbrowser.py +++ b/qt/aqt/deckbrowser.py @@ -280,8 +280,7 @@ 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"]: @@ -292,9 +291,15 @@ class DeckBrowser: deck["dyn"] or not extra or askUser( - (tr(TR.DECKS_ARE_YOU_SURE_YOU_WISH_TO, val=deck["name"])) + extra + (tr(TR.DECKS_ARE_YOU_SURE_YOU_WISH_TO, val=deck["name"])) + " " + extra ) ): + return True + return False + + def _delete(self, did: int) -> None: + if self.ask_delete_deck(did): + self.mw.checkpoint(tr(TR.DECKS_DELETE_DECK)) self.mw.progress.start() self.mw.col.decks.rem(did, True) self.mw.progress.finish() From 2a1b6f90ae08b088947e83d0532cb0b34daa9371 Mon Sep 17 00:00:00 2001 From: BlueGreenMagick <50060875+BlueGreenMagick@users.noreply.github.com> Date: Mon, 4 Jan 2021 19:30:41 +0900 Subject: [PATCH 2/5] add delete_deck to sidebar context menu --- qt/aqt/sidebar.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/qt/aqt/sidebar.py b/qt/aqt/sidebar.py index 7b8c068b0..dc54c9593 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -11,7 +11,7 @@ import aqt from anki.errors import DeckRenameError from aqt.main import ResetReason from aqt.qt import * -from aqt.utils import TR, getOnlyText, showInfo, showWarning, tr +from aqt.utils import TR, askUser, getOnlyText, showInfo, showWarning, tr class SidebarItemType(Enum): @@ -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,23 @@ 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.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) From ada8c505f63d8bafd7ecfa8386b721c3cf17a73b Mon Sep 17 00:00:00 2001 From: BlueGreenMagick <50060875+BlueGreenMagick@users.noreply.github.com> Date: Mon, 4 Jan 2021 19:36:45 +0900 Subject: [PATCH 3/5] add BrowserDeleteDeck to ResetReason --- qt/aqt/main.py | 1 + qt/aqt/sidebar.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) 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 dc54c9593..69f2a52e9 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -11,7 +11,7 @@ import aqt from anki.errors import DeckRenameError from aqt.main import ResetReason from aqt.qt import * -from aqt.utils import TR, askUser, getOnlyText, showInfo, showWarning, tr +from aqt.utils import TR, getOnlyText, showInfo, showWarning, tr class SidebarItemType(Enum): @@ -149,6 +149,7 @@ class NewSidebarTreeView(SidebarTreeViewBase): 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() From 95a62b20d33cfe8f866a4cd40b9322e9920351d7 Mon Sep 17 00:00:00 2001 From: BlueGreenMagick <50060875+BlueGreenMagick@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:43:37 +0900 Subject: [PATCH 4/5] delete deck in background --- qt/aqt/deckbrowser.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/qt/aqt/deckbrowser.py b/qt/aqt/deckbrowser.py index 715461d32..6830f0375 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 @@ -299,11 +300,16 @@ class DeckBrowser: 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.progress.start() - self.mw.col.decks.rem(did, True) - self.mw.progress.finish() - self.show() + self.mw.taskman.with_progress(do_delete, on_done) # Top buttons ###################################################################### From adb8be92a0302c9ed635b7e40d24ee6caa5c7e32 Mon Sep 17 00:00:00 2001 From: BlueGreenMagick <50060875+BlueGreenMagick@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:50:54 +0900 Subject: [PATCH 5/5] format ask_delete_deck method more readable --- qt/aqt/deckbrowser.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/qt/aqt/deckbrowser.py b/qt/aqt/deckbrowser.py index 6830f0375..87b1cec85 100644 --- a/qt/aqt/deckbrowser.py +++ b/qt/aqt/deckbrowser.py @@ -283,17 +283,16 @@ class DeckBrowser: 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 ): return True return False