Fix chooser label not being updated when current notetype/deck renamed (#1452)

* Switch to PEP 604 syntax

* Fix chooser label not being updated when current notetype/deck renamed

- fixes #1450
- fixes https://forums.ankiweb.net/t/deck-name-not-updated/14330
This commit is contained in:
Hikaru Y 2021-10-25 12:23:06 +09:00 committed by GitHub
parent ae59f68729
commit b660e9d95e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View file

@ -271,6 +271,7 @@ class AddCards(QDialog):
av_player.stop_and_clear_queue() av_player.stop_and_clear_queue()
self.editor.cleanup() self.editor.cleanup()
self.notetype_chooser.cleanup() self.notetype_chooser.cleanup()
self.deck_chooser.cleanup()
gui_hooks.operation_did_execute.remove(self.on_operation_did_execute) gui_hooks.operation_did_execute.remove(self.on_operation_did_execute)
self.mw.maybeReset() self.mw.maybeReset()
saveGeom(self, "add") saveGeom(self, "add")

View file

@ -1,10 +1,11 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # 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 anki.decks import DEFAULT_DECK_ID, DeckId
from aqt import AnkiQt from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import HelpPage, shortcut, tr from aqt.utils import HelpPage, shortcut, tr
@ -15,8 +16,8 @@ class DeckChooser(QHBoxLayout):
mw: AnkiQt, mw: AnkiQt,
widget: QWidget, widget: QWidget,
label: bool = True, label: bool = True,
starting_deck_id: Optional[DeckId] = None, starting_deck_id: DeckId | None = None,
on_deck_changed: Optional[Callable[[int], None]] = None, on_deck_changed: Callable[[int], None] | None = None,
) -> None: ) -> None:
QHBoxLayout.__init__(self) QHBoxLayout.__init__(self)
self._widget = widget # type: ignore 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) 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 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: def _setup_ui(self, show_label: bool) -> None:
self.setContentsMargins(0, 0, 0, 0) self.setContentsMargins(0, 0, 0, 0)
@ -106,6 +108,15 @@ class DeckChooser(QHBoxLayout):
if func := self.on_deck_changed: if func := self.on_deck_changed:
func(new_selected_deck_id) 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 # legacy
onDeckChange = choose_deck onDeckChange = choose_deck
@ -113,6 +124,3 @@ class DeckChooser(QHBoxLayout):
def selectedId(self) -> DeckId: def selectedId(self) -> DeckId:
return self.selected_deck_id return self.selected_deck_id
def cleanup(self) -> None:
pass

View file

@ -1,8 +1,9 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # 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 anki.models import NotetypeId
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
@ -32,8 +33,8 @@ class NotetypeChooser(QHBoxLayout):
mw: AnkiQt, mw: AnkiQt,
widget: QWidget, widget: QWidget,
starting_notetype_id: NotetypeId, starting_notetype_id: NotetypeId,
on_button_activated: Optional[Callable[[], None]] = None, on_button_activated: Callable[[], None] | None = None,
on_notetype_changed: Optional[Callable[[NotetypeId], None]] = None, on_notetype_changed: Callable[[NotetypeId], None] | None = None,
show_prefix_label: bool = True, show_prefix_label: bool = True,
) -> None: ) -> None:
QHBoxLayout.__init__(self) QHBoxLayout.__init__(self)
@ -45,6 +46,7 @@ class NotetypeChooser(QHBoxLayout):
self.on_button_activated = self.choose_notetype self.on_button_activated = self.choose_notetype
self._setup_ui(show_label=show_prefix_label) self._setup_ui(show_label=show_prefix_label)
gui_hooks.state_did_reset.append(self.reset_state) 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) self._selected_notetype_id = NotetypeId(0)
# triggers UI update; avoid firing changed hook on startup # triggers UI update; avoid firing changed hook on startup
self.on_notetype_changed = None self.on_notetype_changed = None
@ -75,6 +77,7 @@ class NotetypeChooser(QHBoxLayout):
def cleanup(self) -> None: def cleanup(self) -> None:
gui_hooks.state_did_reset.remove(self.reset_state) 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: def reset_state(self) -> None:
self._ensure_selected_notetype_valid() self._ensure_selected_notetype_valid()
@ -149,3 +152,9 @@ class NotetypeChooser(QHBoxLayout):
def _update_button_label(self) -> None: def _update_button_label(self) -> None:
self.button.setText(self.selected_notetype_name().replace("&", "&&")) 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()