From 3199dcb54daeac8025cc4ec6b60fb9cce1308154 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 23 Mar 2021 19:13:52 +1000 Subject: [PATCH] add hook to modify browser row content --- qt/aqt/browser.py | 7 +++++-- qt/tools/genhooks_gui.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index 5bac052ef..4a1f96df8 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -166,7 +166,7 @@ class DataModel(QAbstractTableModel): self.browser = browser self.col = browser.col self.sortKey = None - self.activeCols = self.col.get_config( + self.activeCols: List[str] = self.col.get_config( "activeCols", ["noteFld", "template", "cardDue", "deck"] ) self.cards: Sequence[int] = [] @@ -199,12 +199,15 @@ class DataModel(QAbstractTableModel): def _fetch_row_from_backend(self, cid: int) -> CellRow: try: - return CellRow(*self.col.browser_row_for_card(cid)) + row = CellRow(*self.col.browser_row_for_card(cid)) except NotFoundError: return CellRow.deleted(len(self.activeCols)) except Exception as e: return CellRow.generic(len(self.activeCols), str(e)) + gui_hooks.browser_did_fetch_row(cid, row, self.activeCols) + return row + def getCard(self, index: QModelIndex) -> Optional[Card]: """Try to return the indicated, possibly deleted card.""" try: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 5e950eb93..8487e8a2e 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -365,6 +365,19 @@ hooks = [ args=["context: aqt.browser.SearchContext"], doc="""Allows you to modify the list of returned card ids from a search.""", ), + Hook( + name="browser_did_fetch_row", + args=["card_id: int", "row: aqt.browser.CellRow", "columns: Sequence[str]"], + doc="""Allows you to add or modify content to a row in the browser. + + You can mutate the row object to change what is displayed. Any columns the + backend did not recognize will be returned as an empty string, and can be + replaced with custom content. + + Columns is a list of string values identifying what each column in the row + represents. + """, + ), # Main window states ################### # these refer to things like deckbrowser, overview and reviewer state,