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