Add a couple of hooks to trigger add-on actions before showing stats

This commit is contained in:
Glutanimate 2020-07-16 21:48:46 +02:00
parent 61017a2e21
commit b89811bfba
3 changed files with 73 additions and 0 deletions

View file

@ -2342,6 +2342,64 @@ class _StateWillChangeHook:
state_will_change = _StateWillChangeHook() state_will_change = _StateWillChangeHook()
class _StatsDialogOldWillShowHook:
"""Allows changing the old stats dialog before it is shown."""
_hooks: List[Callable[["aqt.stats.NewDeckStats"], None]] = []
def append(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
"""(dialog: aqt.stats.NewDeckStats)"""
self._hooks.append(cb)
def remove(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def count(self) -> int:
return len(self._hooks)
def __call__(self, dialog: aqt.stats.NewDeckStats) -> None:
for hook in self._hooks:
try:
hook(dialog)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
stats_dialog_old_will_show = _StatsDialogOldWillShowHook()
class _StatsDialogWillShowHook:
"""Allows changing the stats dialog before it is shown."""
_hooks: List[Callable[["aqt.stats.NewDeckStats"], None]] = []
def append(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
"""(dialog: aqt.stats.NewDeckStats)"""
self._hooks.append(cb)
def remove(self, cb: Callable[["aqt.stats.NewDeckStats"], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def count(self) -> int:
return len(self._hooks)
def __call__(self, dialog: aqt.stats.NewDeckStats) -> None:
for hook in self._hooks:
try:
hook(dialog)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
stats_dialog_will_show = _StatsDialogWillShowHook()
class _StyleDidInitFilter: class _StyleDidInitFilter:
_hooks: List[Callable[[str], str]] = [] _hooks: List[Callable[[str], str]] = []

View file

@ -8,6 +8,7 @@ import time
import aqt import aqt
from anki.lang import _ from anki.lang import _
from aqt import gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
@ -43,6 +44,7 @@ class NewDeckStats(QDialog):
b.setAutoDefault(False) b.setAutoDefault(False)
maybeHideClose(self.form.buttonBox) maybeHideClose(self.form.buttonBox)
addCloseShortcut(self) addCloseShortcut(self)
gui_hooks.stats_dialog_will_show(self)
self.show() self.show()
self.refresh() self.refresh()
self.activateWindow() self.activateWindow()
@ -125,6 +127,7 @@ class DeckStats(QDialog):
qconnect(f.life.clicked, lambda: self.changePeriod(2)) qconnect(f.life.clicked, lambda: self.changePeriod(2))
maybeHideClose(self.form.buttonBox) maybeHideClose(self.form.buttonBox)
addCloseShortcut(self) addCloseShortcut(self)
gui_hooks.stats_dialog_old_will_show(self)
self.show() self.show()
self.refresh() self.refresh()
self.activateWindow() self.activateWindow()

View file

@ -601,6 +601,18 @@ hooks = [
# Model # Model
################### ###################
Hook(name="models_advanced_will_show", args=["advanced: QDialog"],), Hook(name="models_advanced_will_show", args=["advanced: QDialog"],),
# Stats
###################
Hook(
name="stats_dialog_will_show",
args=["dialog: aqt.stats.NewDeckStats"],
doc="""Allows changing the stats dialog before it is shown.""",
),
Hook(
name="stats_dialog_old_will_show",
args=["dialog: aqt.stats.NewDeckStats"],
doc="""Allows changing the old stats dialog before it is shown.""",
),
# Other # Other
################### ###################
Hook( Hook(