Add separate no_arg_trigger decorator

This commit is contained in:
RumovZ 2021-04-26 08:46:08 +02:00
parent f77983fc9f
commit abd02d7891
2 changed files with 43 additions and 36 deletions

View File

@ -54,8 +54,8 @@ from aqt.utils import (
current_top_level_widget, current_top_level_widget,
disable_help_button, disable_help_button,
ensure_editor_saved, ensure_editor_saved,
ensure_editor_saved_on_trigger,
getTag, getTag,
no_arg_trigger,
openHelp, openHelp,
qtMenuShortcutWorkaround, qtMenuShortcutWorkaround,
restore_combo_history, restore_combo_history,
@ -72,7 +72,6 @@ from aqt.utils import (
showInfo, showInfo,
showWarning, showWarning,
skip_if_selection_is_empty, skip_if_selection_is_empty,
skip_if_selection_is_empty_on_trigger,
tooltip, tooltip,
tr, tr,
) )
@ -572,8 +571,9 @@ where id in %s"""
# Misc menu options # Misc menu options
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def onChangeModel(self) -> None: def onChangeModel(self) -> None:
ChangeModel(self, self.oneModelNotes()) ChangeModel(self, self.oneModelNotes())
@ -617,7 +617,8 @@ where id in %s"""
# Card deletion # Card deletion
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@skip_if_selection_is_empty
def delete_selected_notes(self) -> None: def delete_selected_notes(self) -> None:
# ensure deletion is not accidentally triggered when the user is focused # ensure deletion is not accidentally triggered when the user is focused
# in the editing screen or search bar # in the editing screen or search bar
@ -639,8 +640,9 @@ where id in %s"""
# Deck change # Deck change
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def set_deck_of_selected_cards(self) -> None: def set_deck_of_selected_cards(self) -> None:
from aqt.studydeck import StudyDeck from aqt.studydeck import StudyDeck
@ -668,8 +670,9 @@ where id in %s"""
# Tags # Tags
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def add_tags_to_selected_notes( def add_tags_to_selected_notes(
self, self,
tags: Optional[str] = None, tags: Optional[str] = None,
@ -681,8 +684,9 @@ where id in %s"""
parent=self, note_ids=self.selected_notes(), space_separated_tags=tags parent=self, note_ids=self.selected_notes(), space_separated_tags=tags
).run_in_background(initiator=self) ).run_in_background(initiator=self)
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def remove_tags_from_selected_notes(self, tags: Optional[str] = None) -> None: def remove_tags_from_selected_notes(self, tags: Optional[str] = None) -> None:
"Shows prompt if tags not provided." "Shows prompt if tags not provided."
if not ( if not (
@ -701,7 +705,8 @@ where id in %s"""
else: else:
return tags return tags
@ensure_editor_saved_on_trigger @no_arg_trigger
@ensure_editor_saved
def clear_unused_tags(self) -> None: def clear_unused_tags(self) -> None:
clear_unused_tags(parent=self).run_in_background() clear_unused_tags(parent=self).run_in_background()
@ -728,7 +733,8 @@ where id in %s"""
# Exporting # Exporting
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@skip_if_selection_is_empty
def _on_export_notes(self) -> None: def _on_export_notes(self) -> None:
cids = self.selectedNotesAsCards() cids = self.selectedNotesAsCards()
ExportDialog(self.mw, cids=list(cids)) ExportDialog(self.mw, cids=list(cids))
@ -779,8 +785,9 @@ where id in %s"""
# Scheduling # Scheduling
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def reposition(self) -> None: def reposition(self) -> None:
if self.card and self.card.queue != QUEUE_TYPE_NEW: if self.card and self.card.queue != QUEUE_TYPE_NEW:
showInfo(tr.browsing_only_new_cards_can_be_repositioned(), parent=self) showInfo(tr.browsing_only_new_cards_can_be_repositioned(), parent=self)
@ -791,8 +798,9 @@ where id in %s"""
): ):
op.run_in_background() op.run_in_background()
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def set_due_date(self) -> None: def set_due_date(self) -> None:
if op := set_due_date_dialog( if op := set_due_date_dialog(
parent=self, parent=self,
@ -801,8 +809,9 @@ where id in %s"""
): ):
op.run_in_background() op.run_in_background()
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def forget_cards(self) -> None: def forget_cards(self) -> None:
forget_cards( forget_cards(
parent=self, parent=self,
@ -812,8 +821,9 @@ where id in %s"""
# Edit: selection # Edit: selection
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def selectNotes(self) -> None: def selectNotes(self) -> None:
nids = self.selected_notes() nids = self.selected_notes()
# clear the selection so we don't waste energy preserving it # clear the selection so we don't waste energy preserving it
@ -861,15 +871,17 @@ where id in %s"""
# Edit: replacing # Edit: replacing
###################################################################### ######################################################################
@skip_if_selection_is_empty_on_trigger @no_arg_trigger
@ensure_editor_saved_on_trigger @skip_if_selection_is_empty
@ensure_editor_saved
def onFindReplace(self) -> None: def onFindReplace(self) -> None:
FindAndReplaceDialog(self, mw=self.mw, note_ids=self.selected_notes()) FindAndReplaceDialog(self, mw=self.mw, note_ids=self.selected_notes())
# Edit: finding dupes # Edit: finding dupes
###################################################################### ######################################################################
@ensure_editor_saved_on_trigger @no_arg_trigger
@ensure_editor_saved
def onFindDupes(self) -> None: def onFindDupes(self) -> None:
import anki.find import anki.find

View File

@ -1000,15 +1000,6 @@ def ensure_editor_saved(func: Callable) -> Callable:
return decorated return decorated
def ensure_editor_saved_on_trigger(func: Callable) -> Callable:
"""Like ensure_editor_saved(), but tells Qt this function takes no args.
This ensures PyQt doesn't attempt to pass a `toggled` arg
into functions connected to a `triggered` signal.
"""
return pyqtSlot()(ensure_editor_saved(func)) # type: ignore
def skip_if_selection_is_empty(func: Callable) -> Callable: 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.""" """Make the wrapped method a no-op and show a hint if the table selection is empty."""
@ -1022,10 +1013,14 @@ def skip_if_selection_is_empty(func: Callable) -> Callable:
return decorated return decorated
def skip_if_selection_is_empty_on_trigger(func: Callable) -> Callable: def no_arg_trigger(func: Callable) -> Callable:
"""Like skip_if_selection_is_empty(), but tells Qt this function takes no args.""" """Tells Qt this function takes no args.
return pyqtSlot()(skip_if_selection_is_empty(func)) # type: ignore This ensures PyQt doesn't attempt to pass a `toggled` arg
into functions connected to a `triggered` signal.
"""
return pyqtSlot()(func) # type: ignore
class KeyboardModifiersPressed: class KeyboardModifiersPressed: