From 394fe86f8f0cde2dfcc829e6ce66a94a5f2ac0c4 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 30 Apr 2021 16:01:47 +1000 Subject: [PATCH] notetype removal undoable --- pylib/anki/models.py | 4 ++-- qt/aqt/models.py | 19 ++++++++----------- qt/aqt/operations/notetype.py | 10 +++++++++- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pylib/anki/models.py b/pylib/anki/models.py index 36488094d..2917b97d7 100644 --- a/pylib/anki/models.py +++ b/pylib/anki/models.py @@ -228,10 +228,10 @@ class ModelManager: self._remove_from_cache(NotetypeId(nt.id)) self.col._backend.remove_notetype(nt.id) - def remove(self, id: NotetypeId) -> None: + def remove(self, id: NotetypeId) -> OpChanges: "Modifies schema." self._remove_from_cache(id) - self.col._backend.remove_notetype(id) + return self.col._backend.remove_notetype(id) def add(self, m: NotetypeDict) -> OpChangesWithId: "Replaced with add_dict()" diff --git a/qt/aqt/models.py b/qt/aqt/models.py index 57b9a396c..9cb1dde3e 100644 --- a/qt/aqt/models.py +++ b/qt/aqt/models.py @@ -11,8 +11,9 @@ from anki.lang import without_unicode_isolation from anki.models import NotetypeDict, NotetypeId, NotetypeNameIdUseCount from anki.notes import Note from aqt import AnkiQt, gui_hooks -from aqt.operations.notetype import add_notetype_legacy +from aqt.operations.notetype import add_notetype_legacy, remove_notetype from aqt.qt import * +from aqt.schema_change_tracker import ChangeTracker from aqt.utils import ( HelpPage, askUser, @@ -171,18 +172,14 @@ class Models(QDialog): if not askUser(msg, parent=self): return - self.col.modSchema(check=True) + tracker = ChangeTracker(self.mw) + if not tracker.mark_schema(): + return nt = self.current_notetype() - - def save() -> Sequence[NotetypeNameIdUseCount]: - self.mm.rem(nt) - return self.col.models.all_use_counts() - - def on_done(fut: Future) -> None: - self.updateModelsList(fut.result()) - - self.mw.taskman.with_progress(save, on_done, self) + remove_notetype(parent=self, notetype_id=nt["id"]).success( + lambda _: self.refresh_list() + ).run_in_background() def onAdvanced(self) -> None: nt = self.current_notetype() diff --git a/qt/aqt/operations/notetype.py b/qt/aqt/operations/notetype.py index 87a805810..dbd16f266 100644 --- a/qt/aqt/operations/notetype.py +++ b/qt/aqt/operations/notetype.py @@ -4,7 +4,7 @@ from __future__ import annotations from anki.collection import OpChanges, OpChangesWithId -from anki.models import NotetypeDict +from anki.models import NotetypeDict, NotetypeId from aqt import QWidget from aqt.operations import CollectionOp @@ -23,3 +23,11 @@ def update_notetype_legacy( notetype: NotetypeDict, ) -> CollectionOp[OpChanges]: return CollectionOp(parent, lambda col: col.models.update_dict(notetype)) + + +def remove_notetype( + *, + parent: QWidget, + notetype_id: NotetypeId, +) -> CollectionOp[OpChanges]: + return CollectionOp(parent, lambda col: col.models.remove(notetype_id))