anki/qt/aqt/modelchooser.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

105 lines
3.4 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 List, Optional
from aqt import AnkiQt, gui_hooks
from aqt.qt import *
from aqt.utils import TR, HelpPage, shortcut, tr
class ModelChooser(QHBoxLayout):
"New code should prefer NoteTypeChooser."
def __init__(
self,
mw: AnkiQt,
widget: QWidget,
label: bool = True,
on_activated: Optional[Callable[[], None]] = None,
) -> None:
"""If provided, on_activated() will be called when the button is clicked,
and the caller can call .onModelChange() to pull up the dialog when they
are ready."""
QHBoxLayout.__init__(self)
self._widget = widget # type: ignore
self.mw = mw
self.deck = mw.col
self.label = label
if on_activated:
self.on_activated = on_activated
else:
self.on_activated = self.onModelChange
self.setContentsMargins(0, 0, 0, 0)
self.setSpacing(8)
self.setupModels()
gui_hooks.state_did_reset.append(self.onReset)
self._widget.setLayout(self)
def setupModels(self) -> None:
if self.label:
self.modelLabel = QLabel(tr(TR.NOTETYPES_TYPE))
self.addWidget(self.modelLabel)
# models box
self.models = QPushButton()
self.models.setToolTip(shortcut(tr(TR.QT_MISC_CHANGE_NOTE_TYPE_CTRLANDN)))
QShortcut(QKeySequence("Ctrl+N"), self._widget, activated=self.on_activated) # type: ignore
self.models.setAutoDefault(False)
self.addWidget(self.models)
qconnect(self.models.clicked, self.onModelChange)
# layout
sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0))
self.models.setSizePolicy(sizePolicy)
self.updateModels()
def cleanup(self) -> None:
gui_hooks.state_did_reset.remove(self.onReset)
def onReset(self) -> None:
self.updateModels()
def show(self) -> None:
self._widget.show() # type: ignore
def hide(self) -> None:
self._widget.hide() # type: ignore
def onEdit(self) -> None:
import aqt.models
aqt.models.Models(self.mw, self._widget)
def onModelChange(self) -> None:
from aqt.studydeck import StudyDeck
current = self.deck.models.current()["name"]
# edit button
edit = QPushButton(tr(TR.QT_MISC_MANAGE), clicked=self.onEdit) # type: ignore
def nameFunc() -> List[str]:
return sorted(self.deck.models.allNames())
ret = StudyDeck(
self.mw,
names=nameFunc,
accept=tr(TR.ACTIONS_CHOOSE),
title=tr(TR.QT_MISC_CHOOSE_NOTE_TYPE),
help=HelpPage.NOTE_TYPE,
current=current,
parent=self._widget,
buttons=[edit],
cancel=True,
geomKey="selectModel",
)
if not ret.name:
return
m = self.deck.models.byName(ret.name)
self.deck.conf["curModel"] = m["id"]
cdeck = self.deck.decks.current()
cdeck["mid"] = m["id"]
self.deck.decks.save(cdeck)
gui_hooks.current_note_type_did_change(current)
self.mw.reset()
def updateModels(self) -> None:
self.models.setText(self.deck.models.current()["name"].replace("&", "&&"))