mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
Merge pull request #458 from glutanimate/add-deck-conf-hooks
Add deck options hooks
This commit is contained in:
commit
2b1c0ad9e4
3 changed files with 135 additions and 0 deletions
|
@ -6,6 +6,7 @@ from operator import itemgetter
|
|||
import aqt
|
||||
from anki.consts import NEW_CARDS_RANDOM
|
||||
from anki.lang import _, ngettext
|
||||
from aqt import gui_hooks
|
||||
from aqt.qt import *
|
||||
from aqt.utils import (
|
||||
askUser,
|
||||
|
@ -28,6 +29,7 @@ class DeckConf(QDialog):
|
|||
self._origNewOrder = None
|
||||
self.form = aqt.forms.dconf.Ui_Dialog()
|
||||
self.form.setupUi(self)
|
||||
gui_hooks.deck_conf_did_setup_ui_form(self)
|
||||
self.mw.checkpoint(_("Options"))
|
||||
self.setupCombos()
|
||||
self.setupConfs()
|
||||
|
@ -40,6 +42,7 @@ class DeckConf(QDialog):
|
|||
self.setWindowTitle(_("Options for %s") % self.deck["name"])
|
||||
# qt doesn't size properly with altered fonts otherwise
|
||||
restoreGeom(self, "deckconf", adjustSize=True)
|
||||
gui_hooks.deck_conf_will_show(self)
|
||||
self.show()
|
||||
self.exec_()
|
||||
saveGeom(self, "deckconf")
|
||||
|
@ -218,6 +221,7 @@ class DeckConf(QDialog):
|
|||
f.replayQuestion.setChecked(c.get("replayq", True))
|
||||
# description
|
||||
f.desc.setPlainText(self.deck["desc"])
|
||||
gui_hooks.deck_conf_did_load_config(self, self.deck, self.conf)
|
||||
|
||||
def onRestore(self):
|
||||
self.mw.progress.start()
|
||||
|
@ -301,6 +305,7 @@ class DeckConf(QDialog):
|
|||
c["replayq"] = f.replayQuestion.isChecked()
|
||||
# description
|
||||
self.deck["desc"] = f.desc.toPlainText()
|
||||
gui_hooks.deck_conf_will_save_config(self, self.deck, self.conf)
|
||||
self.mw.col.decks.save(self.deck)
|
||||
self.mw.col.decks.save(self.conf)
|
||||
|
||||
|
|
|
@ -518,6 +518,114 @@ class _DeckBrowserWillShowOptionsMenuHook:
|
|||
deck_browser_will_show_options_menu = _DeckBrowserWillShowOptionsMenuHook()
|
||||
|
||||
|
||||
class _DeckConfDidLoadConfigHook:
|
||||
"""Called once widget state has been set from deck config"""
|
||||
|
||||
_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_did_load_config = _DeckConfDidLoadConfigHook()
|
||||
|
||||
|
||||
class _DeckConfDidSetupUiFormHook:
|
||||
"""Allows modifying or adding widgets in the deck options UI form"""
|
||||
|
||||
_hooks: List[Callable[["aqt.deckconf.DeckConf"], None]] = []
|
||||
|
||||
def append(self, cb: Callable[["aqt.deckconf.DeckConf"], None]) -> None:
|
||||
"""(deck_conf: aqt.deckconf.DeckConf)"""
|
||||
self._hooks.append(cb)
|
||||
|
||||
def remove(self, cb: Callable[["aqt.deckconf.DeckConf"], None]) -> None:
|
||||
if cb in self._hooks:
|
||||
self._hooks.remove(cb)
|
||||
|
||||
def __call__(self, deck_conf: aqt.deckconf.DeckConf) -> None:
|
||||
for hook in self._hooks:
|
||||
try:
|
||||
hook(deck_conf)
|
||||
except:
|
||||
# if the hook fails, remove it
|
||||
self._hooks.remove(hook)
|
||||
raise
|
||||
|
||||
|
||||
deck_conf_did_setup_ui_form = _DeckConfDidSetupUiFormHook()
|
||||
|
||||
|
||||
class _DeckConfWillSaveConfigHook:
|
||||
"""Called before widget state is saved to config"""
|
||||
|
||||
_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_save_config = _DeckConfWillSaveConfigHook()
|
||||
|
||||
|
||||
class _DeckConfWillShowHook:
|
||||
"""Allows modifying the deck options dialog before it is shown"""
|
||||
|
||||
_hooks: List[Callable[["aqt.deckconf.DeckConf"], None]] = []
|
||||
|
||||
def append(self, cb: Callable[["aqt.deckconf.DeckConf"], None]) -> None:
|
||||
"""(deck_conf: aqt.deckconf.DeckConf)"""
|
||||
self._hooks.append(cb)
|
||||
|
||||
def remove(self, cb: Callable[["aqt.deckconf.DeckConf"], None]) -> None:
|
||||
if cb in self._hooks:
|
||||
self._hooks.remove(cb)
|
||||
|
||||
def __call__(self, deck_conf: aqt.deckconf.DeckConf) -> None:
|
||||
for hook in self._hooks:
|
||||
try:
|
||||
hook(deck_conf)
|
||||
except:
|
||||
# if the hook fails, remove it
|
||||
self._hooks.remove(hook)
|
||||
raise
|
||||
|
||||
|
||||
deck_conf_will_show = _DeckConfWillShowHook()
|
||||
|
||||
|
||||
class _EditorDidFireTypingTimerHook:
|
||||
_hooks: List[Callable[["anki.notes.Note"], None]] = []
|
||||
|
||||
|
|
|
@ -121,6 +121,28 @@ hooks = [
|
|||
legacy_hook="prepareQA",
|
||||
doc="Can modify card text before review/preview.",
|
||||
),
|
||||
# Deck options
|
||||
###################
|
||||
Hook(
|
||||
name="deck_conf_did_setup_ui_form",
|
||||
args=["deck_conf: aqt.deckconf.DeckConf"],
|
||||
doc="Allows modifying or adding widgets in the deck options UI form",
|
||||
),
|
||||
Hook(
|
||||
name="deck_conf_will_show",
|
||||
args=["deck_conf: aqt.deckconf.DeckConf"],
|
||||
doc="Allows modifying the deck options dialog before it is shown",
|
||||
),
|
||||
Hook(
|
||||
name="deck_conf_did_load_config",
|
||||
args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any"],
|
||||
doc="Called once widget state has been set from deck config",
|
||||
),
|
||||
Hook(
|
||||
name="deck_conf_will_save_config",
|
||||
args=["deck_conf: aqt.deckconf.DeckConf", "deck: Any", "config: Any"],
|
||||
doc="Called before widget state is saved to config",
|
||||
),
|
||||
# Browser
|
||||
###################
|
||||
Hook(
|
||||
|
|
Loading…
Reference in a new issue