2021-05-08 07:08:45 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-16 23:33:58 +02:00
|
|
|
from typing import Callable
|
|
|
|
|
2021-05-08 07:08:45 +02:00
|
|
|
import aqt
|
2021-10-14 11:22:47 +02:00
|
|
|
from anki.cards import Card, CardId
|
2021-10-17 17:58:21 +02:00
|
|
|
from anki.lang import without_unicode_isolation
|
2021-05-08 07:08:45 +02:00
|
|
|
from aqt.qt import *
|
2021-10-14 11:22:47 +02:00
|
|
|
from aqt.utils import (
|
|
|
|
addCloseShortcut,
|
|
|
|
disable_help_button,
|
|
|
|
qconnect,
|
|
|
|
restoreGeom,
|
|
|
|
saveGeom,
|
2021-10-17 17:56:40 +02:00
|
|
|
setWindowIcon,
|
2021-10-16 23:38:11 +02:00
|
|
|
tr,
|
2021-10-14 11:22:47 +02:00
|
|
|
)
|
2021-05-08 07:08:45 +02:00
|
|
|
from aqt.webview import AnkiWebView
|
|
|
|
|
|
|
|
|
|
|
|
class CardInfoDialog(QDialog):
|
2021-10-14 11:22:47 +02:00
|
|
|
TITLE = "browser card info"
|
|
|
|
GEOMETRY_KEY = "revlog"
|
2021-05-08 07:08:45 +02:00
|
|
|
silentlyClose = True
|
|
|
|
|
2021-10-16 23:33:58 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
parent: QWidget | None,
|
|
|
|
mw: aqt.AnkiQt,
|
|
|
|
card: Card,
|
|
|
|
on_close: Callable | None = None,
|
|
|
|
geometry_key: str | None = None,
|
|
|
|
window_title: str | None = None,
|
|
|
|
) -> None:
|
2021-05-08 07:08:45 +02:00
|
|
|
super().__init__(parent)
|
2021-10-14 11:22:47 +02:00
|
|
|
self.mw = mw
|
2021-10-16 23:33:58 +02:00
|
|
|
self._on_close = on_close
|
|
|
|
self.GEOMETRY_KEY = geometry_key or self.GEOMETRY_KEY
|
|
|
|
if window_title:
|
|
|
|
self.setWindowTitle(window_title)
|
2021-10-14 11:22:47 +02:00
|
|
|
self._setup_ui(card.id)
|
2021-05-08 07:08:45 +02:00
|
|
|
self.show()
|
|
|
|
|
2021-10-14 11:22:47 +02:00
|
|
|
def _setup_ui(self, card_id: CardId) -> None:
|
|
|
|
self.mw.garbage_collect_on_dialog_finish(self)
|
|
|
|
disable_help_button(self)
|
|
|
|
restoreGeom(self, self.GEOMETRY_KEY)
|
|
|
|
addCloseShortcut(self)
|
2021-10-17 17:56:40 +02:00
|
|
|
setWindowIcon(self)
|
2021-10-14 11:22:47 +02:00
|
|
|
|
|
|
|
self.web = AnkiWebView(title=self.TITLE)
|
|
|
|
self.web.setVisible(False)
|
|
|
|
self.web.load_ts_page("card-info")
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
layout.addWidget(self.web)
|
2021-10-05 05:53:01 +02:00
|
|
|
buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
|
2021-10-14 11:22:47 +02:00
|
|
|
buttons.setContentsMargins(10, 0, 10, 10)
|
|
|
|
layout.addWidget(buttons)
|
|
|
|
qconnect(buttons.rejected, self.reject)
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
|
self.web.eval(
|
2021-10-17 20:29:32 +02:00
|
|
|
"let cardInfo = anki.cardInfo(document.getElementById('main'));\n" \
|
|
|
|
"cardInfo.then((c) => c.$set({ includeRevlog: true }));"
|
2021-10-14 11:22:47 +02:00
|
|
|
)
|
2021-10-17 20:29:32 +02:00
|
|
|
self.update_card(card_id)
|
2021-10-14 11:22:47 +02:00
|
|
|
|
2021-10-16 23:33:58 +02:00
|
|
|
def update_card(self, card_id: CardId) -> None:
|
2021-10-17 20:29:32 +02:00
|
|
|
self.web.eval(f"cardInfo.then((c) => c.$set({{ cardId: {card_id} }}));")
|
2021-10-16 23:33:58 +02:00
|
|
|
|
2021-05-08 07:08:45 +02:00
|
|
|
def reject(self) -> None:
|
2021-10-16 23:33:58 +02:00
|
|
|
if self._on_close:
|
|
|
|
self._on_close()
|
2021-10-14 11:22:47 +02:00
|
|
|
self.web = None
|
|
|
|
saveGeom(self, self.GEOMETRY_KEY)
|
2021-05-08 07:08:45 +02:00
|
|
|
return QDialog.reject(self)
|
2021-10-16 23:36:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CardInfoManager:
|
|
|
|
"""Wrapper class to conveniently toggle, update and close a card info dialog."""
|
|
|
|
|
|
|
|
def __init__(self, mw: aqt.AnkiQt, geometry_key: str, window_title: str):
|
|
|
|
self.mw = mw
|
|
|
|
self.geometry_key = geometry_key
|
|
|
|
self.window_title = window_title
|
|
|
|
self._card: Card | None = None
|
|
|
|
self._dialog: CardInfoDialog | None = None
|
|
|
|
|
|
|
|
def toggle(self) -> None:
|
|
|
|
"""Opening requires a card to be set."""
|
|
|
|
if not self._dialog and self._card:
|
|
|
|
self._dialog = CardInfoDialog(
|
|
|
|
None,
|
|
|
|
self.mw,
|
|
|
|
self._card,
|
|
|
|
self._on_close,
|
|
|
|
self.geometry_key,
|
|
|
|
self.window_title,
|
|
|
|
)
|
|
|
|
elif self._dialog:
|
|
|
|
self._dialog.reject()
|
|
|
|
|
|
|
|
def set_card(self, card: Card | None) -> None:
|
|
|
|
"""Closes the dialog if card is None."""
|
|
|
|
self._card = card
|
|
|
|
if self._dialog and self._card:
|
|
|
|
self._dialog.update_card(card.id)
|
|
|
|
elif self._dialog:
|
|
|
|
self._dialog.reject()
|
|
|
|
|
|
|
|
def close(self) -> None:
|
|
|
|
if self._dialog:
|
|
|
|
self.toggle()
|
|
|
|
|
|
|
|
def _on_close(self) -> None:
|
|
|
|
self._dialog = None
|
|
|
|
|
2021-10-16 23:38:11 +02:00
|
|
|
|
|
|
|
class BrowserCardInfo(CardInfoManager):
|
|
|
|
def __init__(self, mw: aqt.AnkiQt):
|
2021-10-17 17:58:21 +02:00
|
|
|
super().__init__(
|
|
|
|
mw,
|
|
|
|
"revlog",
|
|
|
|
without_unicode_isolation(
|
|
|
|
tr.card_stats_current_card(context=tr.qt_misc_browse())
|
|
|
|
),
|
|
|
|
)
|
2021-10-16 23:38:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ReviewerCardInfo(CardInfoManager):
|
|
|
|
def __init__(self, mw: aqt.AnkiQt):
|
2021-10-17 17:58:21 +02:00
|
|
|
super().__init__(
|
|
|
|
mw,
|
|
|
|
"reviewerCardInfo",
|
|
|
|
without_unicode_isolation(
|
|
|
|
tr.card_stats_current_card(context=tr.decks_study())
|
|
|
|
),
|
|
|
|
)
|
2021-10-16 23:38:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PreviousReviewerCardInfo(CardInfoManager):
|
|
|
|
def __init__(self, mw: aqt.AnkiQt):
|
|
|
|
super().__init__(
|
2021-10-17 17:58:21 +02:00
|
|
|
mw,
|
|
|
|
"previousReviewerCardInfo",
|
|
|
|
without_unicode_isolation(
|
|
|
|
tr.card_stats_previous_card(context=tr.decks_study())
|
|
|
|
),
|
2021-10-16 23:38:11 +02:00
|
|
|
)
|