Merge pull request #746 from hgiesel/modelsbuttons

Create a gui_hook for initializing buttons in Models
This commit is contained in:
Damien Elmes 2020-09-01 08:36:03 +10:00 committed by GitHub
commit 992626af0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 13 deletions

View file

@ -1869,6 +1869,55 @@ class _ModelsAdvancedWillShowHook:
models_advanced_will_show = _ModelsAdvancedWillShowHook() models_advanced_will_show = _ModelsAdvancedWillShowHook()
class _ModelsDidInitButtonsFilter:
"""Allows adding buttons to the Model dialog"""
_hooks: List[
Callable[
[List[Tuple[str, Callable[[], None]]], "aqt.models.Models"],
List[Tuple[str, Callable[[], None]]],
]
] = []
def append(
self,
cb: Callable[
[List[Tuple[str, Callable[[], None]]], "aqt.models.Models"],
List[Tuple[str, Callable[[], None]]],
],
) -> None:
"""(buttons: List[Tuple[str, Callable[[], None]]], models: aqt.models.Models)"""
self._hooks.append(cb)
def remove(
self,
cb: Callable[
[List[Tuple[str, Callable[[], None]]], "aqt.models.Models"],
List[Tuple[str, Callable[[], None]]],
],
) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def count(self) -> int:
return len(self._hooks)
def __call__(
self, buttons: List[Tuple[str, Callable[[], None]]], models: aqt.models.Models
) -> List[Tuple[str, Callable[[], None]]]:
for filter in self._hooks:
try:
buttons = filter(buttons, models)
except:
# if the hook fails, remove it
self._hooks.remove(filter)
raise
return buttons
models_did_init_buttons = _ModelsDidInitButtonsFilter()
class _OverviewDidRefreshHook: class _OverviewDidRefreshHook:
"""Allow to update the overview window. E.g. add the deck name in the """Allow to update the overview window. E.g. add the deck name in the
title.""" title."""

View file

@ -49,20 +49,27 @@ class Models(QDialog):
self.model = None self.model = None
f = self.form f = self.form
box = f.buttonBox box = f.buttonBox
t = QDialogButtonBox.ActionRole
b = box.addButton(_("Add"), t) default_buttons = [
qconnect(b.clicked, self.onAdd) (_("Add"), self.onAdd),
b = box.addButton(_("Rename"), t) (_("Rename"), self.onRename),
qconnect(b.clicked, self.onRename) (_("Delete"), self.onDelete),
b = box.addButton(_("Delete"), t) ]
qconnect(b.clicked, self.onDelete)
if self.fromMain: if self.fromMain:
b = box.addButton(_("Fields..."), t) default_buttons.extend(
qconnect(b.clicked, self.onFields) [
b = box.addButton(_("Cards..."), t) (_("Fields..."), self.onFields),
qconnect(b.clicked, self.onCards) (_("Cards..."), self.onCards),
b = box.addButton(_("Options..."), t) ]
qconnect(b.clicked, self.onAdvanced) )
default_buttons.append((_("Options..."), self.onAdvanced))
for label, func in gui_hooks.models_did_init_buttons(default_buttons, self):
button = box.addButton(label, QDialogButtonBox.ActionRole)
qconnect(button.clicked, func)
qconnect(f.modelsList.itemDoubleClicked, self.onRename) qconnect(f.modelsList.itemDoubleClicked, self.onRename)
def on_done(fut) -> None: def on_done(fut) -> None:

View file

@ -689,6 +689,15 @@ hooks = [
name="models_advanced_will_show", name="models_advanced_will_show",
args=["advanced: QDialog"], args=["advanced: QDialog"],
), ),
Hook(
name="models_did_init_buttons",
args=[
"buttons: List[Tuple[str, Callable[[], None]]]",
"models: aqt.models.Models",
],
return_type="List[Tuple[str, Callable[[], None]]]",
doc="""Allows adding buttons to the Model dialog""",
),
# Stats # Stats
################### ###################
Hook( Hook(