add new hook: add_cards_did_change_deck (#1420)

* add new hook which will be executed after the user selects a new different deck when adding cards.

* Update qt/aqt/deckchooser.py
This commit is contained in:
zhangsn 2021-10-12 15:55:21 +08:00 committed by GitHub
parent a403b957cc
commit 7c74f7805b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 2 deletions

View file

@ -87,6 +87,7 @@ Alexander Giorev <alex.giorev@gmail.com>
Ren Tatsumoto <tatsu@autistici.org> Ren Tatsumoto <tatsu@autistici.org>
lolilolicon <lolilolicon@gmail.com> lolilolicon <lolilolicon@gmail.com>
Gesa Stupperich <gesa.stupperich@gmail.com> Gesa Stupperich <gesa.stupperich@gmail.com>
git9527 <github.com/git9527>
******************** ********************

View file

@ -74,7 +74,10 @@ class AddCards(QDialog):
on_notetype_changed=self.on_notetype_change, on_notetype_changed=self.on_notetype_change,
) )
self.deck_chooser = DeckChooser( self.deck_chooser = DeckChooser(
self.mw, self.form.deckArea, starting_deck_id=DeckId(defaults.deck_id) self.mw,
self.form.deckArea,
starting_deck_id=DeckId(defaults.deck_id),
on_deck_changed=self.on_deck_changed,
) )
def helpRequested(self) -> None: def helpRequested(self) -> None:
@ -117,6 +120,9 @@ class AddCards(QDialog):
def show_notetype_selector(self) -> None: def show_notetype_selector(self) -> None:
self.editor.call_after_note_saved(self.notetype_chooser.choose_notetype) self.editor.call_after_note_saved(self.notetype_chooser.choose_notetype)
def on_deck_changed(self, deck_id: int) -> None:
gui_hooks.add_cards_did_change_deck(deck_id)
def on_notetype_change(self, notetype_id: NotetypeId) -> None: def on_notetype_change(self, notetype_id: NotetypeId) -> None:
# need to adjust current deck? # need to adjust current deck?
if deck_id := self.col.default_deck_for_notetype(notetype_id): if deck_id := self.col.default_deck_for_notetype(notetype_id):

View file

@ -16,6 +16,7 @@ class DeckChooser(QHBoxLayout):
widget: QWidget, widget: QWidget,
label: bool = True, label: bool = True,
starting_deck_id: Optional[DeckId] = None, starting_deck_id: Optional[DeckId] = None,
on_deck_changed: Optional[Callable[[int], None]] = None,
) -> None: ) -> None:
QHBoxLayout.__init__(self) QHBoxLayout.__init__(self)
self._widget = widget # type: ignore self._widget = widget # type: ignore
@ -27,6 +28,7 @@ class DeckChooser(QHBoxLayout):
if starting_deck_id is None: if starting_deck_id is None:
starting_deck_id = DeckId(self.mw.col.get_config("curDeck", default=1) or 1) starting_deck_id = DeckId(self.mw.col.get_config("curDeck", default=1) or 1)
self.selected_deck_id = starting_deck_id self.selected_deck_id = starting_deck_id
self.on_deck_changed = on_deck_changed
def _setup_ui(self, show_label: bool) -> None: def _setup_ui(self, show_label: bool) -> None:
self.setContentsMargins(0, 0, 0, 0) self.setContentsMargins(0, 0, 0, 0)
@ -98,7 +100,11 @@ class DeckChooser(QHBoxLayout):
geomKey="selectDeck", geomKey="selectDeck",
) )
if ret.name: if ret.name:
self.selected_deck_id = self.mw.col.decks.by_name(ret.name)["id"] new_selected_deck_id = self.mw.col.decks.by_name(ret.name)["id"]
if self.selected_deck_id != new_selected_deck_id:
self.selected_deck_id = new_selected_deck_id
if func := self.on_deck_changed:
func(new_selected_deck_id)
# legacy # legacy

View file

@ -787,6 +787,12 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest)
doc="""Executed after the user selects a new note type when adding doc="""Executed after the user selects a new note type when adding
cards.""", cards.""",
), ),
Hook(
name="add_cards_did_change_deck",
args=["new_deck_id: int"],
doc="""Executed after the user selects a new different deck when
adding cards.""",
),
# Editing # Editing
################### ###################
Hook( Hook(