From 4fd8a4cf2d460a09f8bcfe6ce7fa34a3c12435a3 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 20 May 2020 13:39:21 +1000 Subject: [PATCH] update empty cards hook Closes #515 --- qt/aqt/emptycards.py | 3 +++ qt/aqt/gui_hooks.py | 41 +++++++++++----------------------------- qt/tools/genhooks_gui.py | 25 +++--------------------- 3 files changed, 17 insertions(+), 52 deletions(-) diff --git a/qt/aqt/emptycards.py b/qt/aqt/emptycards.py index f3174130e..909090411 100644 --- a/qt/aqt/emptycards.py +++ b/qt/aqt/emptycards.py @@ -7,6 +7,7 @@ import re import aqt from anki.backend_pb2 import EmptyCardsReport, NoteWithEmptyCards +from aqt import gui_hooks from aqt.qt import QDialog, QDialogButtonBox, qconnect from aqt.utils import TR, restoreGeom, saveGeom, tooltip, tr @@ -41,6 +42,8 @@ class EmptyCardsDialog(QDialog): self.form.webview.title = "empty cards" self.form.webview.set_bridge_command(self._on_note_link_clicked, self) + gui_hooks.empty_cards_will_show(self) + # make the note ids clickable html = re.sub( r"\[anki:nid:(\d+)\]", diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 88b6872ef..ebdb36dce 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -1343,49 +1343,30 @@ class _EditorWillUseFontForFieldFilter: editor_will_use_font_for_field = _EditorWillUseFontForFieldFilter() -class _EmptyCardsWillBeDeletedFilter: - """Allow to change the list of cards to delete. +class _EmptyCardsWillShowHook: + """Allows changing the list of cards to delete.""" - For example, an add-on creating a method to delete only empty - new cards would be done as follow: -``` -from anki.consts import CARD_TYPE_NEW -from anki.utils import ids2str -from aqt import mw -from aqt import gui_hooks + _hooks: List[Callable[["aqt.emptycards.EmptyCardsDialog"], None]] = [] -def filter(cids, col): - return col.db.list( - f"select id from cards where (type={CARD_TYPE_NEW} and (id in {ids2str(cids)))") - -def emptyNewCard(): - gui_hooks.append(filter) - mw.onEmptyCards() - gui_hooks.remove(filter) -```""" - - _hooks: List[Callable[[List[int]], List[int]]] = [] - - def append(self, cb: Callable[[List[int]], List[int]]) -> None: - """(cids: List[int])""" + def append(self, cb: Callable[["aqt.emptycards.EmptyCardsDialog"], None]) -> None: + """(diag: aqt.emptycards.EmptyCardsDialog)""" self._hooks.append(cb) - def remove(self, cb: Callable[[List[int]], List[int]]) -> None: + def remove(self, cb: Callable[["aqt.emptycards.EmptyCardsDialog"], None]) -> None: if cb in self._hooks: self._hooks.remove(cb) - def __call__(self, cids: List[int]) -> List[int]: - for filter in self._hooks: + def __call__(self, diag: aqt.emptycards.EmptyCardsDialog) -> None: + for hook in self._hooks: try: - cids = filter(cids) + hook(diag) except: # if the hook fails, remove it - self._hooks.remove(filter) + self._hooks.remove(hook) raise - return cids -empty_cards_will_be_deleted = _EmptyCardsWillBeDeletedFilter() +empty_cards_will_show = _EmptyCardsWillShowHook() class _MediaSyncDidProgressHook: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 39cb22e28..5d212abc8 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -402,28 +402,9 @@ hooks = [ ), Hook(name="media_sync_did_start_or_stop", args=["running: bool"]), Hook( - name="empty_cards_will_be_deleted", - args=["cids: List[int]"], - return_type="List[int]", - doc="""Allow to change the list of cards to delete. - - For example, an add-on creating a method to delete only empty - new cards would be done as follow: -``` -from anki.consts import CARD_TYPE_NEW -from anki.utils import ids2str -from aqt import mw -from aqt import gui_hooks - -def filter(cids, col): - return col.db.list( - f"select id from cards where (type={CARD_TYPE_NEW} and (id in {ids2str(cids)))") - -def emptyNewCard(): - gui_hooks.append(filter) - mw.onEmptyCards() - gui_hooks.remove(filter) -```""", + name="empty_cards_will_show", + args=["diag: aqt.emptycards.EmptyCardsDialog"], + doc="""Allows changing the list of cards to delete.""", ), # Adding cards ###################