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()
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:
"""Allow to update the overview window. E.g. add the deck name in the
title."""

View File

@ -49,20 +49,27 @@ class Models(QDialog):
self.model = None
f = self.form
box = f.buttonBox
t = QDialogButtonBox.ActionRole
b = box.addButton(_("Add"), t)
qconnect(b.clicked, self.onAdd)
b = box.addButton(_("Rename"), t)
qconnect(b.clicked, self.onRename)
b = box.addButton(_("Delete"), t)
qconnect(b.clicked, self.onDelete)
default_buttons = [
(_("Add"), self.onAdd),
(_("Rename"), self.onRename),
(_("Delete"), self.onDelete),
]
if self.fromMain:
b = box.addButton(_("Fields..."), t)
qconnect(b.clicked, self.onFields)
b = box.addButton(_("Cards..."), t)
qconnect(b.clicked, self.onCards)
b = box.addButton(_("Options..."), t)
qconnect(b.clicked, self.onAdvanced)
default_buttons.extend(
[
(_("Fields..."), self.onFields),
(_("Cards..."), self.onCards),
]
)
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)
def on_done(fut) -> None:

View File

@ -689,6 +689,15 @@ hooks = [
name="models_advanced_will_show",
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
###################
Hook(