From 02a06f110ae9b6021ad98497c64ada67b20b6505 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 22 May 2020 20:10:59 +0200 Subject: [PATCH 1/3] Add my name to contributors --- CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 55f663894..2d2d07e86 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -33,6 +33,7 @@ Henry Tang Simone Gaiarin Rai (Michal Pokorny) Zeno Gantner +Henrik Giesel ******************** From 2074ffcf51719c7b71435575258e3ea9d668499e Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 22 May 2020 21:24:05 +0200 Subject: [PATCH 2/3] Add three deck config hooks - gui_hooks.deck_conf_did_add_config - gui_hooks.deck_conf_will_remove_config - gui_hooks.deck_conf_will_rename_config --- qt/aqt/deckconf.py | 5 +++ qt/aqt/gui_hooks.py | 97 ++++++++++++++++++++++++++++++++++++++++ qt/tools/genhooks_gui.py | 15 +++++++ 3 files changed, 117 insertions(+) diff --git a/qt/aqt/deckconf.py b/qt/aqt/deckconf.py index acb6b30d8..cb8c37a4f 100644 --- a/qt/aqt/deckconf.py +++ b/qt/aqt/deckconf.py @@ -119,10 +119,12 @@ class DeckConf(QDialog): name = getOnlyText(_("New options group name:")) if not name: return + # first, save currently entered data to current conf self.saveConf() # then clone the conf id = self.mw.col.decks.add_config_returning_id(name, clone_from=self.conf) + gui_hooks.deck_conf_did_add_config(self, self.deck, self.conf, name, id) # set the deck to the new conf self.deck["conf"] = id # then reload the conf list @@ -132,6 +134,7 @@ class DeckConf(QDialog): if int(self.conf["id"]) == 1: showInfo(_("The default configuration can't be removed."), self) else: + gui_hooks.deck_conf_will_remove_config(self, self.deck, self.conf) self.mw.col.modSchema(check=True) self.mw.col.decks.remove_config(self.conf["id"]) self.conf = None @@ -143,6 +146,8 @@ class DeckConf(QDialog): name = getOnlyText(_("New name:"), default=old) if not name or name == old: return + + gui_hooks.deck_conf_will_rename_config(self, self.deck, self.conf, name) self.conf["name"] = name self.saveConf() self.loadConfs() diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 6fb3b7125..3eca0f770 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -917,6 +917,43 @@ class _DeckBrowserWillShowOptionsMenuHook: deck_browser_will_show_options_menu = _DeckBrowserWillShowOptionsMenuHook() +class _DeckConfDidAddConfigHook: + """Called after a new config group was added as a clone of the current one, but before initializing the widget state""" + + _hooks: List[Callable[["aqt.deckconf.DeckConf", Any, Any, str, int], None]] = [] + + def append( + self, cb: Callable[["aqt.deckconf.DeckConf", Any, Any, str, int], None] + ) -> None: + """(deck_conf: aqt.deckconf.DeckConf, deck: Any, config: Any, new_name: str, new_confg_id: int)""" + self._hooks.append(cb) + + def remove( + self, cb: Callable[["aqt.deckconf.DeckConf", Any, Any, str, int], None] + ) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__( + self, + deck_conf: aqt.deckconf.DeckConf, + deck: Any, + config: Any, + new_name: str, + new_confg_id: int, + ) -> None: + for hook in self._hooks: + try: + hook(deck_conf, deck, config, new_name, new_confg_id) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +deck_conf_did_add_config = _DeckConfDidAddConfigHook() + + class _DeckConfDidLoadConfigHook: """Called once widget state has been set from deck config""" @@ -971,6 +1008,66 @@ class _DeckConfDidSetupUiFormHook: deck_conf_did_setup_ui_form = _DeckConfDidSetupUiFormHook() +class _DeckConfWillRemoveConfigHook: + """Called before current config group is removed""" + + _hooks: List[Callable[["aqt.deckconf.DeckConf", Any, Any], None]] = [] + + def append(self, cb: Callable[["aqt.deckconf.DeckConf", Any, Any], None]) -> None: + """(deck_conf: aqt.deckconf.DeckConf, deck: Any, config: Any)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[["aqt.deckconf.DeckConf", Any, Any], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__( + self, deck_conf: aqt.deckconf.DeckConf, deck: Any, config: Any + ) -> None: + for hook in self._hooks: + try: + hook(deck_conf, deck, config) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +deck_conf_will_remove_config = _DeckConfWillRemoveConfigHook() + + +class _DeckConfWillRenameConfigHook: + """Called before config group is renamed""" + + _hooks: List[Callable[["aqt.deckconf.DeckConf", Any, Any, str], None]] = [] + + def append( + self, cb: Callable[["aqt.deckconf.DeckConf", Any, Any, str], None] + ) -> None: + """(deck_conf: aqt.deckconf.DeckConf, deck: Any, config: Any, new_name: str)""" + self._hooks.append(cb) + + def remove( + self, cb: Callable[["aqt.deckconf.DeckConf", Any, Any, str], None] + ) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__( + self, deck_conf: aqt.deckconf.DeckConf, deck: Any, config: Any, new_name: str + ) -> None: + for hook in self._hooks: + try: + hook(deck_conf, deck, config, new_name) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +deck_conf_will_rename_config = _DeckConfWillRenameConfigHook() + + class _DeckConfWillSaveConfigHook: """Called before widget state is saved to config""" diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 806442eaa..0d9caec22 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -172,6 +172,21 @@ hooks = [ args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any"], doc="Called before widget state is saved to config", ), + Hook( + name="deck_conf_did_add_config", + args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any", "new_name: str", "new_confg_id: int"], + doc="Called after a new config group was added as a clone of the current one, but before initializing the widget state", + ), + Hook( + name="deck_conf_will_remove_config", + args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any"], + doc="Called before current config group is removed", + ), + Hook( + name="deck_conf_will_rename_config", + args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any", "new_name: str"], + doc="Called before config group is renamed", + ), # Browser ################### Hook(name="browser_will_show", args=["browser: aqt.browser.Browser"]), From 788a3eeced0cd01999b5a879fc8a90fb98b3c650 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sat, 23 May 2020 11:14:52 +0200 Subject: [PATCH 3/3] Satisfy black reformatter --- qt/aqt/gui_hooks.py | 17 +++++++++++++---- qt/tools/genhooks_gui.py | 26 +++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 3eca0f770..241bffd2e 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -918,14 +918,23 @@ deck_browser_will_show_options_menu = _DeckBrowserWillShowOptionsMenuHook() class _DeckConfDidAddConfigHook: - """Called after a new config group was added as a clone of the current one, but before initializing the widget state""" + """Allows modification of a newly created config group + + This hook is called after the config group was created, but + before initializing the widget state. + + `deck_conf` will point to the old config group, `new_conf_id` will + point to the newly created config group. + + Config groups are created as clones of the current one. + """ _hooks: List[Callable[["aqt.deckconf.DeckConf", Any, Any, str, int], None]] = [] def append( self, cb: Callable[["aqt.deckconf.DeckConf", Any, Any, str, int], None] ) -> None: - """(deck_conf: aqt.deckconf.DeckConf, deck: Any, config: Any, new_name: str, new_confg_id: int)""" + """(deck_conf: aqt.deckconf.DeckConf, deck: Any, config: Any, new_name: str, new_conf_id: int)""" self._hooks.append(cb) def remove( @@ -940,11 +949,11 @@ class _DeckConfDidAddConfigHook: deck: Any, config: Any, new_name: str, - new_confg_id: int, + new_conf_id: int, ) -> None: for hook in self._hooks: try: - hook(deck_conf, deck, config, new_name, new_confg_id) + hook(deck_conf, deck, config, new_name, new_conf_id) except: # if the hook fails, remove it self._hooks.remove(hook) diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 0d9caec22..8da8a7e69 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -174,8 +174,23 @@ hooks = [ ), Hook( name="deck_conf_did_add_config", - args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any", "new_name: str", "new_confg_id: int"], - doc="Called after a new config group was added as a clone of the current one, but before initializing the widget state", + args=[ + "deck_conf: aqt.deckconf.DeckConf", + "deck: Any", + "config: Any", + "new_name: str", + "new_conf_id: int", + ], + doc="""Allows modification of a newly created config group + + This hook is called after the config group was created, but + before initializing the widget state. + + `deck_conf` will point to the old config group, `new_conf_id` will + point to the newly created config group. + + Config groups are created as clones of the current one. + """, ), Hook( name="deck_conf_will_remove_config", @@ -184,7 +199,12 @@ hooks = [ ), Hook( name="deck_conf_will_rename_config", - args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any", "new_name: str"], + args=[ + "deck_conf: aqt.deckconf.DeckConf", + "deck: Any", + "config: Any", + "new_name: str", + ], doc="Called before config group is renamed", ), # Browser