From 538afd94bc8ab10e3a23d4ea0fa2e74b41bc7a90 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Thu, 11 Mar 2021 22:25:18 +0100 Subject: [PATCH] Add decorator to save editor in sidebar --- qt/aqt/sidebar.py | 11 ++++------- qt/aqt/utils.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/qt/aqt/sidebar.py b/qt/aqt/sidebar.py index da4905b20..5cc38715f 100644 --- a/qt/aqt/sidebar.py +++ b/qt/aqt/sidebar.py @@ -24,6 +24,7 @@ from aqt.utils import ( TR, askUser, getOnlyText, + save_browser_editor, show_invalid_search_error, showInfo, showWarning, @@ -1142,10 +1143,8 @@ class SidebarTreeView(QTreeView): self.mw.deckBrowser.refresh() self.mw.update_undo_actions() - def remove_tags(self, item: SidebarItem) -> None: - self.browser.editor.saveNow(lambda: self._remove_tags(item)) - - def _remove_tags(self, _item: SidebarItem) -> None: + @save_browser_editor + def remove_tags(self, _item: SidebarItem) -> None: tags = self._selected_tags() def do_remove() -> int: @@ -1195,10 +1194,8 @@ class SidebarTreeView(QTreeView): self.browser.model.beginReset() self.mw.taskman.with_progress(do_rename, on_done) + @save_browser_editor def delete_decks(self, _item: SidebarItem) -> None: - self.browser.editor.saveNow(self._delete_decks) - - def _delete_decks(self) -> None: def do_delete() -> int: return self.mw.col.decks.remove(dids) diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 1e3be7639..d12960c1e 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -986,3 +986,13 @@ def save_editor(func: Callable) -> Callable: return decorated + +def save_browser_editor(func: Callable) -> Callable: + """Call editor.saveNow with the decorated method as callback. The method's owner + must hold a reference to the Editor's Browser. + """ + + def decorated(self: Any, *args: Any, **kwargs: Any) -> None: + self.browser.editor.saveNow(lambda: func(self, *args, **kwargs)) + + return decorated