Merge pull request #633 from hgiesel/dconf_hooks

Add three deck config hooks
This commit is contained in:
Damien Elmes 2020-05-24 09:53:22 +10:00 committed by GitHub
commit 6b05272ed4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 147 additions and 0 deletions

View File

@ -33,6 +33,7 @@ Henry Tang <hktang@ualberta.ca>
Simone Gaiarin <simgunz@gmail.com>
Rai (Michal Pokorny) <agentydragon@gmail.com>
Zeno Gantner <zeno.gantner@gmail.com>
Henrik Giesel <hengiesel@gmail.com>
********************

View File

@ -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()

View File

@ -917,6 +917,52 @@ class _DeckBrowserWillShowOptionsMenuHook:
deck_browser_will_show_options_menu = _DeckBrowserWillShowOptionsMenuHook()
class _DeckConfDidAddConfigHook:
"""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_conf_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_conf_id: int,
) -> None:
for hook in self._hooks:
try:
hook(deck_conf, deck, config, new_name, new_conf_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 +1017,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"""

View File

@ -172,6 +172,41 @@ 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_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",
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"]),