add delete_deck to sidebar context menu

This commit is contained in:
BlueGreenMagick 2021-01-04 19:30:41 +09:00
parent f6f06986d0
commit 2a1b6f90ae

View File

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