From 458d891d28293c44993f1fad906cfb64d7a924d6 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Sun, 25 Apr 2021 08:35:21 +0200 Subject: [PATCH] Add dec to make methods no-op if no selection --- ftl/core/browsing.ftl | 1 + qt/aqt/utils.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/ftl/core/browsing.ftl b/ftl/core/browsing.ftl index ca5c72b60..5b44004f0 100644 --- a/ftl/core/browsing.ftl +++ b/ftl/core/browsing.ftl @@ -65,6 +65,7 @@ browsing-nd-names = { $num }: { $name } browsing-new = (new) browsing-new-note-type = New note type: browsing-no-flag = No Flag +browsing-no-selection = No cards or notes selected. browsing-note = Note # Exactly one character representing 'Notes'; should differ from browsing-card-initial. browsing-note-initial = N diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 2b4a0511d..83064712c 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -1009,6 +1009,19 @@ def ensure_editor_saved_on_trigger(func: Callable) -> Callable: return pyqtSlot()(ensure_editor_saved(func)) # type: ignore +def skip_if_selection_is_empty(func: Callable) -> Callable: + """Make the wrapped method a no-op and show a hint if the table selection is empty.""" + + @wraps(func) + def decorated(self: Any, *args: Any, **kwargs: Any) -> None: + if self.table.len_selection() > 0: + func(self, *args, **kwargs) + else: + tooltip(tr.browsing_no_selection()) + + return decorated + + class KeyboardModifiersPressed: "Util for type-safe checks of currently-pressed modifier keys."