2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-02-01 14:28:21 +01:00
|
|
|
from typing import List, Optional
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 07:25:25 +02:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2021-03-26 04:48:26 +01:00
|
|
|
from aqt.utils import HelpPage, shortcut, tr
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class ModelChooser(QHBoxLayout):
|
2021-03-27 13:03:19 +01:00
|
|
|
"New code should prefer NotetypeChooser."
|
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-08 14:23:24 +01:00
|
|
|
|
2020-10-05 05:33:54 +02:00
|
|
|
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."""
|
2012-12-21 08:51:59 +01:00
|
|
|
QHBoxLayout.__init__(self)
|
2021-02-02 14:30:53 +01:00
|
|
|
self._widget = widget # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.deck = mw.col
|
|
|
|
self.label = label
|
2020-10-05 05:33:54 +02:00
|
|
|
if on_activated:
|
|
|
|
self.on_activated = on_activated
|
|
|
|
else:
|
|
|
|
self.on_activated = self.onModelChange
|
2019-12-23 01:34:10 +01:00
|
|
|
self.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setSpacing(8)
|
|
|
|
self.setupModels()
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2021-02-02 14:30:53 +01:00
|
|
|
self._widget.setLayout(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 07:25:25 +02:00
|
|
|
def setupModels(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if self.label:
|
2021-03-26 04:48:26 +01:00
|
|
|
self.modelLabel = QLabel(tr.notetypes_type())
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addWidget(self.modelLabel)
|
|
|
|
# models box
|
|
|
|
self.models = QPushButton()
|
2021-03-26 04:48:26 +01:00
|
|
|
self.models.setToolTip(shortcut(tr.qt_misc_change_note_type_ctrlandn()))
|
2021-02-02 14:30:53 +01:00
|
|
|
QShortcut(QKeySequence("Ctrl+N"), self._widget, activated=self.on_activated) # type: ignore
|
2013-02-20 06:50:18 +01:00
|
|
|
self.models.setAutoDefault(False)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addWidget(self.models)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.models.clicked, self.onModelChange)
|
2012-12-21 08:51:59 +01:00
|
|
|
# layout
|
2019-12-23 01:34:10 +01:00
|
|
|
sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.models.setSizePolicy(sizePolicy)
|
|
|
|
self.updateModels()
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def cleanup(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 07:25:25 +02:00
|
|
|
def onReset(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.updateModels()
|
|
|
|
|
2020-07-30 07:25:25 +02:00
|
|
|
def show(self) -> None:
|
2021-02-02 14:30:53 +01:00
|
|
|
self._widget.show() # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 07:25:25 +02:00
|
|
|
def hide(self) -> None:
|
2021-02-02 14:30:53 +01:00
|
|
|
self._widget.hide() # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 07:25:25 +02:00
|
|
|
def onEdit(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt.models
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
aqt.models.Models(self.mw, self._widget)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def onModelChange(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.studydeck import StudyDeck
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
|
|
current = self.deck.models.current()["name"]
|
2012-12-21 08:51:59 +01:00
|
|
|
# edit button
|
2021-03-26 04:48:26 +01:00
|
|
|
edit = QPushButton(tr.qt_misc_manage(), clicked=self.onEdit) # type: ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def nameFunc() -> List[str]:
|
2012-12-21 08:51:59 +01:00
|
|
|
return sorted(self.deck.models.allNames())
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
ret = StudyDeck(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw,
|
|
|
|
names=nameFunc,
|
2021-03-26 04:48:26 +01:00
|
|
|
accept=tr.actions_choose(),
|
|
|
|
title=tr.qt_misc_choose_note_type(),
|
2021-01-25 14:45:47 +01:00
|
|
|
help=HelpPage.NOTE_TYPE,
|
2019-12-23 01:34:10 +01:00
|
|
|
current=current,
|
2021-02-02 14:30:53 +01:00
|
|
|
parent=self._widget,
|
2019-12-23 01:34:10 +01:00
|
|
|
buttons=[edit],
|
|
|
|
cancel=True,
|
|
|
|
geomKey="selectModel",
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not ret.name:
|
|
|
|
return
|
|
|
|
m = self.deck.models.byName(ret.name)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.deck.conf["curModel"] = m["id"]
|
2012-12-21 08:51:59 +01:00
|
|
|
cdeck = self.deck.decks.current()
|
2019-12-23 01:34:10 +01:00
|
|
|
cdeck["mid"] = m["id"]
|
2012-12-21 08:51:59 +01:00
|
|
|
self.deck.decks.save(cdeck)
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.current_note_type_did_change(current)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.reset()
|
|
|
|
|
2020-07-30 07:25:25 +02:00
|
|
|
def updateModels(self) -> None:
|
2020-10-07 14:26:32 +02:00
|
|
|
self.models.setText(self.deck.models.current()["name"].replace("&", "&&"))
|