anki/qt/aqt/deckchooser.py
Damien Elmes ce243c2cae Simplify note adding and the deck/notetype choosers
The existing code was really difficult to reason about:

- The default notetype depended on the selected deck, and vice versa,
and this logic was buried in the deck and notetype choosing screens,
and models.py.
- Changes to the notetype were not passed back directly, but were fired
via a hook, which changed any screen in the app that had a notetype
selector.

It also wasn't great for performance, as the most recent deck and tags
were embedded in the notetype, which can be expensive to save and sync
for large notetypes.

To address these points:

- The current deck for a notetype, and notetype for a deck, are now
stored in separate config variables, instead of directly in the deck
or notetype. These are cheap to read and write, and we'll be able to
sync them individually in the future once config syncing is updated in
the future. I seem to recall some users not wanting the tag saving
behaviour, so I've dropped that for now, but if people end up missing
it, it would be simple to add as an extra auxiliary config variable.
- The logic for getting the starting deck and notetype has been moved
into the backend. It should be the same as the older Python code, with
one exception: when "change deck depending on notetype" is enabled in
the preferences, it will start with the current notetype ("curModel"),
instead of first trying to get a deck-specific notetype.
- ModelChooser has been duplicated into notetypechooser.py, and it
has been updated to solely be concerned with keeping track of a selected
notetype - it no longer alters global state.
2021-03-10 11:53:27 +10:00

111 lines
3.3 KiB
Python

# 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 aqt import AnkiQt
from aqt.qt import *
from aqt.utils import TR, HelpPage, shortcut, tr
class DeckChooser(QHBoxLayout):
def __init__(
self,
mw: AnkiQt,
widget: QWidget,
label: bool = True,
starting_deck_id: Optional[int] = None,
) -> None:
QHBoxLayout.__init__(self)
self._widget = widget # type: ignore
self.mw = mw
self._setup_ui(show_label=label)
self._selected_deck_id = 0
# default to current deck if starting id not provided
if starting_deck_id is None:
starting_deck_id = self.mw.col.get_config("curDeck", default=1) or 1
self.selected_deck_id = starting_deck_id
def _setup_ui(self, show_label: bool) -> None:
self.setContentsMargins(0, 0, 0, 0)
self.setSpacing(8)
# text label before button?
if show_label:
self.deckLabel = QLabel(tr(TR.DECKS_DECK))
self.addWidget(self.deckLabel)
# decks box
self.deck = QPushButton()
qconnect(self.deck.clicked, self.choose_deck)
self.deck.setAutoDefault(False)
self.deck.setToolTip(shortcut(tr(TR.QT_MISC_TARGET_DECK_CTRLANDD)))
qconnect(
QShortcut(QKeySequence("Ctrl+D"), self._widget).activated, self.choose_deck
)
sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0))
self.deck.setSizePolicy(sizePolicy)
self.addWidget(self.deck)
self._widget.setLayout(self)
def selected_deck_name(self) -> str:
return (
self.mw.col.decks.name_if_exists(self.selected_deck_id) or "missing default"
)
@property
def selected_deck_id(self) -> int:
self._ensure_selected_deck_valid()
return self._selected_deck_id
@selected_deck_id.setter
def selected_deck_id(self, id: int) -> None:
if id != self._selected_deck_id:
self._selected_deck_id = id
self._ensure_selected_deck_valid()
self._update_button_label()
def _ensure_selected_deck_valid(self) -> None:
if not self.mw.col.decks.get(self._selected_deck_id, default=False):
self.selected_deck_id = 1
def _update_button_label(self) -> None:
self.deck.setText(self.selected_deck_name().replace("&", "&&"))
def show(self) -> None:
self._widget.show() # type: ignore
def hide(self) -> None:
self._widget.hide() # type: ignore
def choose_deck(self) -> None:
from aqt.studydeck import StudyDeck
current = self.selected_deck_name()
ret = StudyDeck(
self.mw,
current=current,
accept=tr(TR.ACTIONS_CHOOSE),
title=tr(TR.QT_MISC_CHOOSE_DECK),
help=HelpPage.EDITING,
cancel=False,
parent=self._widget,
geomKey="selectDeck",
)
if ret.name:
self.selected_deck_id = self.mw.col.decks.byName(ret.name)["id"]
# legacy
onDeckChange = choose_deck
deckName = selected_deck_name
def selectedId(self) -> int:
return self.selected_deck_id
def cleanup(self) -> None:
pass