diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index fd4b93f4b..65b16e5f7 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -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.""" diff --git a/qt/aqt/models.py b/qt/aqt/models.py index 13426a306..538442e25 100644 --- a/qt/aqt/models.py +++ b/qt/aqt/models.py @@ -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: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 7a0abb44f..1d7b3336b 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -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(