mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
Add fields_did_rename_field and fields_did_remove_field hooks
This commit is contained in:
parent
99af63d81c
commit
591d3738d1
3 changed files with 83 additions and 1 deletions
|
@ -6,7 +6,7 @@ from anki.consts import *
|
||||||
from anki.lang import _, ngettext
|
from anki.lang import _, ngettext
|
||||||
from anki.models import NoteType
|
from anki.models import NoteType
|
||||||
from anki.rsbackend import TemplateError
|
from anki.rsbackend import TemplateError
|
||||||
from aqt import AnkiQt
|
from aqt import AnkiQt, gui_hooks
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from aqt.schema_change_tracker import ChangeTracker
|
from aqt.schema_change_tracker import ChangeTracker
|
||||||
from aqt.utils import askUser, getOnlyText, openHelp, showWarning, tooltip
|
from aqt.utils import askUser, getOnlyText, openHelp, showWarning, tooltip
|
||||||
|
@ -100,6 +100,8 @@ class FieldDialog(QDialog):
|
||||||
|
|
||||||
self.change_tracker.mark_basic()
|
self.change_tracker.mark_basic()
|
||||||
self.mm.rename_field(self.model, f, name)
|
self.mm.rename_field(self.model, f, name)
|
||||||
|
gui_hooks.fields_did_rename_field(self, f, f["name"])
|
||||||
|
|
||||||
self.saveField()
|
self.saveField()
|
||||||
self.fillFields()
|
self.fillFields()
|
||||||
self.form.fieldList.setCurrentRow(idx)
|
self.form.fieldList.setCurrentRow(idx)
|
||||||
|
@ -127,6 +129,8 @@ class FieldDialog(QDialog):
|
||||||
return
|
return
|
||||||
f = self.model["flds"][self.form.fieldList.currentRow()]
|
f = self.model["flds"][self.form.fieldList.currentRow()]
|
||||||
self.mm.remove_field(self.model, f)
|
self.mm.remove_field(self.model, f)
|
||||||
|
gui_hooks.fields_did_delete_field(self, f)
|
||||||
|
|
||||||
self.fillFields()
|
self.fillFields()
|
||||||
self.form.fieldList.setCurrentRow(0)
|
self.form.fieldList.setCurrentRow(0)
|
||||||
|
|
||||||
|
|
|
@ -1729,6 +1729,74 @@ class _EmptyCardsWillShowHook:
|
||||||
empty_cards_will_show = _EmptyCardsWillShowHook()
|
empty_cards_will_show = _EmptyCardsWillShowHook()
|
||||||
|
|
||||||
|
|
||||||
|
class _FieldsDidDeleteFieldHook:
|
||||||
|
_hooks: List[Callable[["aqt.fields.FieldDialog", "anki.models.Field"], None]] = []
|
||||||
|
|
||||||
|
def append(
|
||||||
|
self, cb: Callable[["aqt.fields.FieldDialog", "anki.models.Field"], None]
|
||||||
|
) -> None:
|
||||||
|
"""(dialog: aqt.fields.FieldDialog, field: anki.models.Field)"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(
|
||||||
|
self, cb: Callable[["aqt.fields.FieldDialog", "anki.models.Field"], None]
|
||||||
|
) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def count(self) -> int:
|
||||||
|
return len(self._hooks)
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self, dialog: aqt.fields.FieldDialog, field: anki.models.Field
|
||||||
|
) -> None:
|
||||||
|
for hook in self._hooks:
|
||||||
|
try:
|
||||||
|
hook(dialog, field)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(hook)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
fields_did_delete_field = _FieldsDidDeleteFieldHook()
|
||||||
|
|
||||||
|
|
||||||
|
class _FieldsDidRenameFieldHook:
|
||||||
|
_hooks: List[
|
||||||
|
Callable[["aqt.fields.FieldDialog", "anki.models.Field", str], None]
|
||||||
|
] = []
|
||||||
|
|
||||||
|
def append(
|
||||||
|
self, cb: Callable[["aqt.fields.FieldDialog", "anki.models.Field", str], None]
|
||||||
|
) -> None:
|
||||||
|
"""(dialog: aqt.fields.FieldDialog, field: anki.models.Field, old_name: str)"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(
|
||||||
|
self, cb: Callable[["aqt.fields.FieldDialog", "anki.models.Field", str], None]
|
||||||
|
) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def count(self) -> int:
|
||||||
|
return len(self._hooks)
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self, dialog: aqt.fields.FieldDialog, field: anki.models.Field, old_name: str
|
||||||
|
) -> None:
|
||||||
|
for hook in self._hooks:
|
||||||
|
try:
|
||||||
|
hook(dialog, field, old_name)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(hook)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
fields_did_rename_field = _FieldsDidRenameFieldHook()
|
||||||
|
|
||||||
|
|
||||||
class _MainWindowDidInitHook:
|
class _MainWindowDidInitHook:
|
||||||
"""Executed after the main window is fully initialized
|
"""Executed after the main window is fully initialized
|
||||||
|
|
||||||
|
|
|
@ -734,6 +734,16 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest)
|
||||||
return_type="List[Tuple[str, Callable[[], None]]]",
|
return_type="List[Tuple[str, Callable[[], None]]]",
|
||||||
doc="""Allows adding buttons to the Model dialog""",
|
doc="""Allows adding buttons to the Model dialog""",
|
||||||
),
|
),
|
||||||
|
# Fields
|
||||||
|
###################
|
||||||
|
Hook(
|
||||||
|
name="fields_did_rename_field",
|
||||||
|
args=["dialog: aqt.fields.FieldDialog", "field: anki.models.Field", "old_name: str"],
|
||||||
|
),
|
||||||
|
Hook(
|
||||||
|
name="fields_did_delete_field",
|
||||||
|
args=["dialog: aqt.fields.FieldDialog", "field: anki.models.Field"],
|
||||||
|
),
|
||||||
# Stats
|
# Stats
|
||||||
###################
|
###################
|
||||||
Hook(
|
Hook(
|
||||||
|
|
Loading…
Reference in a new issue