Merge pull request #891 from BlueGreenMagick/sidebar-delete-deck

Sidebar delete deck
This commit is contained in:
Damien Elmes 2021-01-06 18:56:15 +10:00 committed by GitHub
commit d464d97f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 18 deletions

View File

@ -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
######################################################################

View File

@ -79,6 +79,7 @@ class ResetReason(enum.Enum):
BrowserReschedule = "browserReschedule"
BrowserFindReplace = "browserFindReplace"
BrowserTagDupes = "browserTagDupes"
BrowserDeleteDeck = "browserDeleteDeck"
class ResetRequired:

View File

@ -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)