diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index 2640b22f6..8057cf586 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -271,6 +271,7 @@ class AddCards(QDialog): av_player.stop_and_clear_queue() self.editor.cleanup() self.notetype_chooser.cleanup() + self.deck_chooser.cleanup() gui_hooks.operation_did_execute.remove(self.on_operation_did_execute) self.mw.maybeReset() saveGeom(self, "add") diff --git a/qt/aqt/deckchooser.py b/qt/aqt/deckchooser.py index 3939933e1..bc91e0b12 100644 --- a/qt/aqt/deckchooser.py +++ b/qt/aqt/deckchooser.py @@ -1,10 +1,11 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -from typing import Optional +from __future__ import annotations +from anki.collection import OpChanges from anki.decks import DEFAULT_DECK_ID, DeckId -from aqt import AnkiQt +from aqt import AnkiQt, gui_hooks from aqt.qt import * from aqt.utils import HelpPage, shortcut, tr @@ -15,8 +16,8 @@ class DeckChooser(QHBoxLayout): mw: AnkiQt, widget: QWidget, label: bool = True, - starting_deck_id: Optional[DeckId] = None, - on_deck_changed: Optional[Callable[[int], None]] = None, + starting_deck_id: DeckId | None = None, + on_deck_changed: Callable[[int], None] | None = None, ) -> None: QHBoxLayout.__init__(self) self._widget = widget # type: ignore @@ -29,6 +30,7 @@ class DeckChooser(QHBoxLayout): starting_deck_id = DeckId(self.mw.col.get_config("curDeck", default=1) or 1) self.selected_deck_id = starting_deck_id self.on_deck_changed = on_deck_changed + gui_hooks.operation_did_execute.append(self.on_operation_did_execute) def _setup_ui(self, show_label: bool) -> None: self.setContentsMargins(0, 0, 0, 0) @@ -106,6 +108,15 @@ class DeckChooser(QHBoxLayout): if func := self.on_deck_changed: func(new_selected_deck_id) + def on_operation_did_execute( + self, changes: OpChanges, handler: object | None + ) -> None: + if changes.deck: + self._update_button_label() + + def cleanup(self) -> None: + gui_hooks.operation_did_execute.remove(self.on_operation_did_execute) + # legacy onDeckChange = choose_deck @@ -113,6 +124,3 @@ class DeckChooser(QHBoxLayout): def selectedId(self) -> DeckId: return self.selected_deck_id - - def cleanup(self) -> None: - pass diff --git a/qt/aqt/notetypechooser.py b/qt/aqt/notetypechooser.py index fceb1ca0d..ca2a5080d 100644 --- a/qt/aqt/notetypechooser.py +++ b/qt/aqt/notetypechooser.py @@ -1,8 +1,9 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -from typing import Optional +from __future__ import annotations +from anki.collection import OpChanges from anki.models import NotetypeId from aqt import AnkiQt, gui_hooks from aqt.qt import * @@ -32,8 +33,8 @@ class NotetypeChooser(QHBoxLayout): mw: AnkiQt, widget: QWidget, starting_notetype_id: NotetypeId, - on_button_activated: Optional[Callable[[], None]] = None, - on_notetype_changed: Optional[Callable[[NotetypeId], None]] = None, + on_button_activated: Callable[[], None] | None = None, + on_notetype_changed: Callable[[NotetypeId], None] | None = None, show_prefix_label: bool = True, ) -> None: QHBoxLayout.__init__(self) @@ -45,6 +46,7 @@ class NotetypeChooser(QHBoxLayout): self.on_button_activated = self.choose_notetype self._setup_ui(show_label=show_prefix_label) gui_hooks.state_did_reset.append(self.reset_state) + gui_hooks.operation_did_execute.append(self.on_operation_did_execute) self._selected_notetype_id = NotetypeId(0) # triggers UI update; avoid firing changed hook on startup self.on_notetype_changed = None @@ -75,6 +77,7 @@ class NotetypeChooser(QHBoxLayout): def cleanup(self) -> None: gui_hooks.state_did_reset.remove(self.reset_state) + gui_hooks.operation_did_execute.remove(self.on_operation_did_execute) def reset_state(self) -> None: self._ensure_selected_notetype_valid() @@ -149,3 +152,9 @@ class NotetypeChooser(QHBoxLayout): def _update_button_label(self) -> None: self.button.setText(self.selected_notetype_name().replace("&", "&&")) + + def on_operation_did_execute( + self, changes: OpChanges, handler: object | None + ) -> None: + if changes.notetype: + self._update_button_label()