This commit is contained in:
Arthur Milchior 2026-01-01 10:21:31 +00:00 committed by GitHub
commit ca94950954
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,11 +6,10 @@ from __future__ import annotations
from collections.abc import Callable, Sequence from collections.abc import Callable, Sequence
from concurrent.futures import Future from concurrent.futures import Future
from operator import itemgetter from operator import itemgetter
from typing import Any
import aqt.clayout import aqt.clayout
from anki import stdmodels from anki import stdmodels
from anki.collection import Collection from anki.collection import Collection, OpChangesWithId
from anki.lang import without_unicode_isolation from anki.lang import without_unicode_isolation
from anki.models import NotetypeDict, NotetypeId, NotetypeNameIdUseCount from anki.models import NotetypeDict, NotetypeId, NotetypeNameIdUseCount
from anki.notes import Note from anki.notes import Note
@ -74,12 +73,18 @@ class Models(QDialog):
# Models # Models
########################################################################## ##########################################################################
def maybe_select_provided_notetype(self) -> None: def maybe_select_provided_notetype(
if not self.selected_notetype_id: self, selected_notetype_id: NotetypeId | None = None, row: int = 0
self.form.modelsList.setCurrentRow(0) ) -> None:
"""Select the provided notetype ID, if any.
Otherwise the one at `self.selected_notetype_id`,
otherwise the `row`-th element."""
selected_notetype_id = selected_notetype_id or self.selected_notetype_id
if not selected_notetype_id:
self.form.modelsList.setCurrentRow(row)
return return
for i, m in enumerate(self.models): for i, m in enumerate(self.models):
if m.id == self.selected_notetype_id: if m.id == selected_notetype_id:
self.form.modelsList.setCurrentRow(i) self.form.modelsList.setCurrentRow(i)
break break
@ -117,24 +122,31 @@ class Models(QDialog):
self.mw.taskman.with_progress(self.col.models.all_use_counts, on_done, self) self.mw.taskman.with_progress(self.col.models.all_use_counts, on_done, self)
maybeHideClose(box) maybeHideClose(box)
def refresh_list(self, *ignored_args: Any) -> None: def refresh_list(self, selected_notetype_id: NotetypeId | None = None) -> None:
QueryOp( QueryOp(
parent=self, parent=self,
op=lambda col: col.models.all_use_counts(), op=lambda col: col.models.all_use_counts(),
success=self.updateModelsList, success=lambda notetypes: self.updateModelsList(
notetypes, selected_notetype_id
),
).run_in_background() ).run_in_background()
def onRename(self) -> None: def onRename(self) -> None:
nt = self.current_notetype() nt = self.current_notetype()
text, ok = getText(tr.actions_new_name(), default=nt["name"]) text, ok = getText(tr.actions_new_name(), default=nt["name"])
if ok and text.strip(): if ok and text.strip():
selected_notetype_id = nt["id"]
nt["name"] = text nt["name"] = text
update_notetype_legacy(parent=self, notetype=nt).success( update_notetype_legacy(parent=self, notetype=nt).success(
self.refresh_list lambda _: self.refresh_list(selected_notetype_id)
).run_in_background() ).run_in_background()
def updateModelsList(self, notetypes: Sequence[NotetypeNameIdUseCount]) -> None: def updateModelsList(
self,
notetypes: Sequence[NotetypeNameIdUseCount],
selected_notetype_id: NotetypeId | None = None,
) -> None:
row = self.form.modelsList.currentRow() row = self.form.modelsList.currentRow()
if row == -1: if row == -1:
row = 0 row = 0
@ -145,7 +157,7 @@ class Models(QDialog):
mUse = tr.browsing_note_count(count=m.use_count) mUse = tr.browsing_note_count(count=m.use_count)
item = QListWidgetItem(f"{m.name} [{mUse}]") item = QListWidgetItem(f"{m.name} [{mUse}]")
self.form.modelsList.addItem(item) self.form.modelsList.addItem(item)
self.form.modelsList.setCurrentRow(row) self.maybe_select_provided_notetype(selected_notetype_id, row)
def current_notetype(self) -> NotetypeDict: def current_notetype(self) -> NotetypeDict:
row = self.form.modelsList.currentRow() row = self.form.modelsList.currentRow()
@ -154,8 +166,9 @@ class Models(QDialog):
def onAdd(self) -> None: def onAdd(self) -> None:
def on_success(notetype: NotetypeDict) -> None: def on_success(notetype: NotetypeDict) -> None:
# if legacy add-ons already added the notetype, skip adding # if legacy add-ons already added the notetype, skip adding
if notetype["id"]: nid = notetype["id"]
self.refresh_list() if nid:
self.refresh_list(nid)
return return
# prompt for name # prompt for name
@ -164,8 +177,11 @@ class Models(QDialog):
return return
notetype["name"] = text notetype["name"] = text
def refresh_list(op: OpChangesWithId) -> None:
self.refresh_list(NotetypeId(op.id))
add_notetype_legacy(parent=self, notetype=notetype).success( add_notetype_legacy(parent=self, notetype=notetype).success(
self.refresh_list refresh_list
).run_in_background() ).run_in_background()
AddModel(self.mw, on_success, self) AddModel(self.mw, on_success, self)
@ -188,7 +204,7 @@ class Models(QDialog):
nt = self.current_notetype() nt = self.current_notetype()
remove_notetype(parent=self, notetype_id=nt["id"]).success( remove_notetype(parent=self, notetype_id=nt["id"]).success(
lambda _: self.refresh_list() lambda _: self.refresh_list(None)
).run_in_background() ).run_in_background()
def onAdvanced(self) -> None: def onAdvanced(self) -> None:
@ -212,7 +228,7 @@ class Models(QDialog):
nt["latexPre"] = str(frm.latexHeader.toPlainText()) nt["latexPre"] = str(frm.latexHeader.toPlainText())
nt["latexPost"] = str(frm.latexFooter.toPlainText()) nt["latexPost"] = str(frm.latexFooter.toPlainText())
update_notetype_legacy(parent=self, notetype=nt).success( update_notetype_legacy(parent=self, notetype=nt).success(
self.refresh_list lambda _: self.refresh_list(nt["id"])
).run_in_background() ).run_in_background()
def _tmpNote(self) -> Note: def _tmpNote(self) -> Note: