2020-03-29 21:10:30 +02:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import time
|
2020-03-30 10:37:38 +02:00
|
|
|
from typing import Any, List, Optional, Union
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-03-30 15:40:19 +02:00
|
|
|
from anki.cards import Card
|
2020-03-29 21:10:30 +02:00
|
|
|
from anki.lang import _
|
|
|
|
from aqt import AnkiQt, gui_hooks
|
|
|
|
from aqt.qt import (
|
|
|
|
QAbstractItemView,
|
|
|
|
QCheckBox,
|
|
|
|
QDialog,
|
|
|
|
QDialogButtonBox,
|
|
|
|
QKeySequence,
|
|
|
|
Qt,
|
|
|
|
QVBoxLayout,
|
|
|
|
QWidget,
|
|
|
|
)
|
|
|
|
from aqt.sound import av_player, play_clicked_audio
|
|
|
|
from aqt.theme import theme_manager
|
|
|
|
from aqt.utils import restoreGeom, saveGeom
|
|
|
|
from aqt.webview import AnkiWebView
|
|
|
|
|
|
|
|
|
|
|
|
class Previewer:
|
2020-04-02 09:48:25 +02:00
|
|
|
_lastState = None
|
|
|
|
_cardChanged = False
|
|
|
|
_lastRender: Union[int, float] = 0
|
|
|
|
_timer = None
|
2020-03-29 21:10:30 +02:00
|
|
|
|
|
|
|
def __init__(self, parent: QWidget, mw: AnkiQt):
|
2020-04-02 10:31:29 +02:00
|
|
|
self._parent = parent
|
2020-03-29 21:10:30 +02:00
|
|
|
self.mw = mw
|
|
|
|
|
2020-03-30 15:40:19 +02:00
|
|
|
def card(self) -> Optional[Card]:
|
2020-03-29 22:32:24 +02:00
|
|
|
raise NotImplementedError
|
2020-03-30 15:40:19 +02:00
|
|
|
|
2020-04-02 09:35:51 +02:00
|
|
|
def open(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
self._state = "question"
|
|
|
|
self._lastState = None
|
2020-03-29 22:27:55 +02:00
|
|
|
self._create_gui()
|
2020-04-02 09:48:25 +02:00
|
|
|
self._setupWebview()
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render(True)
|
2020-04-02 09:48:25 +02:00
|
|
|
self._window.show()
|
2020-03-29 22:27:55 +02:00
|
|
|
|
|
|
|
def _create_gui(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
self._window = QDialog(None, Qt.Window)
|
|
|
|
self._window.setWindowTitle(_("Preview"))
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
self._window.finished.connect(self._onFinished)
|
|
|
|
self._window.silentlyClose = True
|
2020-03-29 22:28:47 +02:00
|
|
|
self.vbox = QVBoxLayout()
|
|
|
|
self.vbox.setContentsMargins(0, 0, 0, 0)
|
2020-04-02 09:48:25 +02:00
|
|
|
self._web = AnkiWebView(title="previewer")
|
|
|
|
self.vbox.addWidget(self._web)
|
2020-03-29 22:28:47 +02:00
|
|
|
self.bbox = QDialogButtonBox()
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
self._replay = self.bbox.addButton(
|
2020-03-29 21:10:30 +02:00
|
|
|
_("Replay Audio"), QDialogButtonBox.ActionRole
|
|
|
|
)
|
2020-04-02 09:48:25 +02:00
|
|
|
self._replay.setAutoDefault(False)
|
|
|
|
self._replay.setShortcut(QKeySequence("R"))
|
|
|
|
self._replay.setToolTip(_("Shortcut key: %s" % "R"))
|
|
|
|
self._replay.clicked.connect(self._onReplayAudio)
|
|
|
|
|
|
|
|
self.showBothSides = QCheckBox(_("Show Both Sides"))
|
|
|
|
self.showBothSides.setShortcut(QKeySequence("B"))
|
|
|
|
self.showBothSides.setToolTip(_("Shortcut key: %s" % "B"))
|
|
|
|
self.bbox.addButton(self.showBothSides, QDialogButtonBox.ActionRole)
|
|
|
|
self._bothSides = self.mw.col.conf.get("previewBothSides", False)
|
|
|
|
self.showBothSides.setChecked(self._bothSides)
|
|
|
|
self.showBothSides.toggled.connect(self._onShowBothSides)
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-03-29 22:28:47 +02:00
|
|
|
self.vbox.addWidget(self.bbox)
|
2020-04-02 09:48:25 +02:00
|
|
|
self._window.setLayout(self.vbox)
|
|
|
|
restoreGeom(self._window, "preview")
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onFinished(self, ok):
|
|
|
|
saveGeom(self._window, "preview")
|
|
|
|
self.mw.progress.timer(100, self._onClose, False)
|
2020-03-29 21:10:30 +02:00
|
|
|
|
|
|
|
def _onReplayAudio(self):
|
|
|
|
self.mw.reviewer.replayAudio(self)
|
|
|
|
|
2020-04-02 09:35:51 +02:00
|
|
|
def close(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._window:
|
|
|
|
self._window.close()
|
|
|
|
self._onClose()
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onClose(self):
|
|
|
|
self._window = None
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _setupWebview(self):
|
2020-03-29 21:10:30 +02:00
|
|
|
jsinc = [
|
|
|
|
"jquery.js",
|
|
|
|
"browsersel.js",
|
|
|
|
"mathjax/conf.js",
|
|
|
|
"mathjax/MathJax.js",
|
|
|
|
"reviewer.js",
|
|
|
|
]
|
|
|
|
self._previewWeb.stdHtml(
|
2020-04-02 09:31:49 +02:00
|
|
|
self.mw.reviewer.revHtml(), css=["reviewer.css"], js=jsinc, context=self,
|
2020-03-29 21:10:30 +02:00
|
|
|
)
|
2020-04-02 09:48:25 +02:00
|
|
|
self._web.set_bridge_command(self._on_bridge_cmd, self)
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _on_bridge_cmd(self, cmd: str) -> Any:
|
2020-03-29 21:10:30 +02:00
|
|
|
if cmd.startswith("play:"):
|
2020-03-30 15:40:19 +02:00
|
|
|
play_clicked_audio(cmd, self.card())
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:35:51 +02:00
|
|
|
def render(self, cardChanged=False):
|
|
|
|
self.cancelTimer()
|
|
|
|
# Keep track of whether render() has ever been called
|
2020-03-29 21:10:30 +02:00
|
|
|
# with cardChanged=True since the last successful render
|
2020-04-02 09:48:25 +02:00
|
|
|
self._cardChanged |= cardChanged
|
2020-03-29 21:10:30 +02:00
|
|
|
# avoid rendering in quick succession
|
2020-04-02 09:48:25 +02:00
|
|
|
elapMS = int((time.time() - self._lastRender) * 1000)
|
2020-03-29 21:10:30 +02:00
|
|
|
delay = 300
|
|
|
|
if elapMS < delay:
|
2020-04-02 09:48:25 +02:00
|
|
|
self._timer = self.mw.progress.timer(
|
|
|
|
delay - elapMS, self._renderScheduled, False
|
2020-03-29 21:10:30 +02:00
|
|
|
)
|
|
|
|
else:
|
2020-04-02 09:48:25 +02:00
|
|
|
self._renderScheduled()
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:35:51 +02:00
|
|
|
def cancelTimer(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._timer:
|
|
|
|
self._timer.stop()
|
|
|
|
self._timer = None
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _renderScheduled(self) -> None:
|
2020-04-02 09:35:51 +02:00
|
|
|
self.cancelTimer()
|
2020-04-02 09:48:25 +02:00
|
|
|
self._lastRender = time.time()
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
if not self._window:
|
2020-03-29 21:10:30 +02:00
|
|
|
return
|
2020-03-30 15:40:19 +02:00
|
|
|
c = self.card()
|
2020-03-29 21:10:30 +02:00
|
|
|
func = "_showQuestion"
|
2020-03-29 22:32:24 +02:00
|
|
|
if not c:
|
2020-03-29 21:10:30 +02:00
|
|
|
txt = _("(please select 1 card)")
|
|
|
|
bodyclass = ""
|
2020-04-02 09:48:25 +02:00
|
|
|
self._lastState = None
|
2020-03-29 21:10:30 +02:00
|
|
|
else:
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._bothSides:
|
|
|
|
self._state = "answer"
|
|
|
|
elif self._cardChanged:
|
|
|
|
self._state = "question"
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
currentState = self._stateAndMod()
|
|
|
|
if currentState == self._lastState:
|
2020-03-29 21:10:30 +02:00
|
|
|
# nothing has changed, avoid refreshing
|
|
|
|
return
|
|
|
|
|
|
|
|
# need to force reload even if answer
|
|
|
|
txt = c.q(reload=True)
|
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._state == "answer":
|
2020-03-29 21:10:30 +02:00
|
|
|
func = "_showAnswer"
|
|
|
|
txt = c.a()
|
|
|
|
txt = re.sub(r"\[\[type:[^]]+\]\]", "", txt)
|
|
|
|
|
|
|
|
bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
|
|
|
|
|
|
|
|
if self.mw.reviewer.autoplay(c):
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._bothSides:
|
2020-03-29 21:10:30 +02:00
|
|
|
# if we're showing both sides at once, remove any audio
|
|
|
|
# from the answer that's appeared on the question already
|
|
|
|
question_audio = c.question_av_tags()
|
|
|
|
only_on_answer_audio = [
|
|
|
|
x for x in c.answer_av_tags() if x not in question_audio
|
|
|
|
]
|
|
|
|
audio = question_audio + only_on_answer_audio
|
2020-04-02 09:48:25 +02:00
|
|
|
elif self._state == "question":
|
2020-03-29 21:10:30 +02:00
|
|
|
audio = c.question_av_tags()
|
|
|
|
else:
|
|
|
|
audio = c.answer_av_tags()
|
|
|
|
av_player.play_tags(audio)
|
|
|
|
else:
|
|
|
|
av_player.clear_queue_and_maybe_interrupt()
|
|
|
|
|
|
|
|
txt = self.mw.prepare_card_text_for_display(txt)
|
2020-04-02 09:48:25 +02:00
|
|
|
txt = gui_hooks.card_will_show(txt, c, "preview" + self._state.capitalize())
|
|
|
|
self._lastState = self._stateAndMod()
|
|
|
|
self._web.eval("{}({},'{}');".format(func, json.dumps(txt), bodyclass))
|
|
|
|
self._cardChanged = False
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onShowBothSides(self, toggle):
|
|
|
|
self._bothSides = toggle
|
2020-03-29 21:10:30 +02:00
|
|
|
self.mw.col.conf["previewBothSides"] = toggle
|
|
|
|
self.mw.col.setMod()
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._state == "answer" and not toggle:
|
|
|
|
self._state = "question"
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render()
|
2020-03-29 21:10:30 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _stateAndMod(self):
|
2020-03-30 15:40:19 +02:00
|
|
|
c = self.card()
|
2020-03-29 21:10:30 +02:00
|
|
|
n = c.note()
|
|
|
|
n.load()
|
2020-04-02 09:48:25 +02:00
|
|
|
return (self._state, c.id, n.mod)
|
2020-03-29 22:32:24 +02:00
|
|
|
|
|
|
|
|
2020-03-30 10:13:38 +02:00
|
|
|
class MultipleCardsPreviewer(Previewer):
|
2020-03-29 22:32:24 +02:00
|
|
|
def card(self) -> Optional[Card]:
|
2020-03-30 10:13:38 +02:00
|
|
|
# need to state explicitly it's not implement to avoid W0223
|
|
|
|
raise NotImplementedError
|
2020-03-29 22:32:24 +02:00
|
|
|
|
|
|
|
def _create_gui(self):
|
|
|
|
super()._create_gui()
|
2020-04-02 09:48:25 +02:00
|
|
|
self._prev = self.bbox.addButton("<", QDialogButtonBox.ActionRole)
|
|
|
|
self._prev.setAutoDefault(False)
|
|
|
|
self._prev.setShortcut(QKeySequence("Left"))
|
|
|
|
self._prev.setToolTip(_("Shortcut key: Left arrow"))
|
|
|
|
|
|
|
|
self._next = self.bbox.addButton(">", QDialogButtonBox.ActionRole)
|
|
|
|
self._next.setAutoDefault(True)
|
|
|
|
self._next.setShortcut(QKeySequence("Right"))
|
|
|
|
self._next.setToolTip(_("Shortcut key: Right arrow or Enter"))
|
|
|
|
|
|
|
|
self._prev.clicked.connect(self._onPrev)
|
|
|
|
self._next.clicked.connect(self._onNext)
|
|
|
|
|
|
|
|
def _onPrev(self):
|
|
|
|
if self._state == "answer" and not self._bothSides:
|
|
|
|
self._state = "question"
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render()
|
2020-03-29 22:32:24 +02:00
|
|
|
else:
|
2020-04-02 09:48:25 +02:00
|
|
|
self._onPrevCard()
|
2020-03-30 09:58:51 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onPrevCard(self):
|
2020-03-30 10:13:38 +02:00
|
|
|
...
|
2020-03-29 22:32:24 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onNext(self):
|
|
|
|
if self._state == "question":
|
|
|
|
self._state = "answer"
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render()
|
2020-03-29 22:32:24 +02:00
|
|
|
else:
|
2020-04-02 09:48:25 +02:00
|
|
|
self._onNextCard()
|
2020-03-30 09:58:51 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onNextCard(self):
|
2020-03-30 10:13:38 +02:00
|
|
|
...
|
2020-03-29 22:32:24 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _updateButtons(self):
|
|
|
|
if not self._window:
|
2020-03-29 22:32:24 +02:00
|
|
|
return
|
2020-04-02 09:48:25 +02:00
|
|
|
self._prev.setEnabled(self._should_enable_prev())
|
|
|
|
self._next.setEnabled(self._should_enable_next())
|
2020-03-30 09:56:46 +02:00
|
|
|
|
|
|
|
def _should_enable_prev(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
return self._state == "answer" and not self._bothSides
|
2020-03-30 10:13:38 +02:00
|
|
|
|
|
|
|
def _should_enable_next(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
return self._state == "question"
|
2020-03-30 10:13:38 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onClose(self):
|
|
|
|
super()._onClose()
|
|
|
|
self._prev = None
|
|
|
|
self._next = None
|
2020-03-30 10:13:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BrowserPreviewer(MultipleCardsPreviewer):
|
|
|
|
def card(self) -> Optional[Card]:
|
2020-04-02 10:31:29 +02:00
|
|
|
if self._parent.singleCard:
|
|
|
|
return self._parent.card
|
2020-03-30 10:13:38 +02:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onFinished(self, ok):
|
|
|
|
super()._onFinished(ok)
|
2020-04-02 10:31:29 +02:00
|
|
|
self._parent.form.previewButton.setChecked(False)
|
2020-03-30 10:13:38 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onPrevCard(self):
|
2020-04-02 10:31:29 +02:00
|
|
|
self._parent.editor.saveNow(
|
|
|
|
lambda: self._parent._moveCur(QAbstractItemView.MoveUp)
|
2020-03-29 22:32:24 +02:00
|
|
|
)
|
2020-03-30 10:13:38 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onNextCard(self):
|
2020-04-02 10:31:29 +02:00
|
|
|
self._parent.editor.saveNow(
|
|
|
|
lambda: self._parent._moveCur(QAbstractItemView.MoveDown)
|
2020-03-30 10:13:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def _should_enable_prev(self):
|
2020-04-02 10:31:29 +02:00
|
|
|
return super()._should_enable_prev() or self._parent.currentRow() > 0
|
2020-03-30 09:56:46 +02:00
|
|
|
|
|
|
|
def _should_enable_next(self):
|
2020-03-30 10:13:38 +02:00
|
|
|
return (
|
|
|
|
super()._should_enable_next()
|
2020-04-02 10:31:29 +02:00
|
|
|
or self._parent.currentRow() < self._parent.model.rowCount(None) - 1
|
2020-03-29 22:32:24 +02:00
|
|
|
)
|
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onClose(self):
|
|
|
|
super()._onClose()
|
2020-04-02 10:31:29 +02:00
|
|
|
self._parent.previewer = None
|
2020-03-29 22:32:24 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _renderScheduled(self) -> None:
|
|
|
|
super()._renderScheduled()
|
|
|
|
self._updateButtons()
|
2020-03-29 23:41:00 +02:00
|
|
|
|
|
|
|
|
2020-03-30 10:37:38 +02:00
|
|
|
class ListCardsPreviewer(MultipleCardsPreviewer):
|
2020-03-30 15:10:47 +02:00
|
|
|
def __init__(self, cards: List[Union[Card, int]], *args, **kwargs):
|
|
|
|
"""A previewer displaying a list of card.
|
|
|
|
|
|
|
|
List can be changed by setting self.cards to a new value.
|
|
|
|
|
|
|
|
self.cards contains both cid and card. So that card is loaded
|
|
|
|
only when required and is not loaded twice.
|
|
|
|
|
|
|
|
"""
|
2020-03-30 10:37:38 +02:00
|
|
|
self.index = 0
|
|
|
|
self.cards = cards
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def card(self):
|
|
|
|
if not self.cards:
|
|
|
|
return None
|
2020-03-30 15:10:47 +02:00
|
|
|
if isinstance(self.cards[self.index], int):
|
|
|
|
self.cards[self.index] = self.mw.col.getCard(self.cards[self.index])
|
|
|
|
return self.cards[self.index]
|
2020-03-30 10:37:38 +02:00
|
|
|
|
2020-04-02 09:35:51 +02:00
|
|
|
def open(self):
|
2020-03-30 10:37:38 +02:00
|
|
|
if not self.cards:
|
|
|
|
return
|
2020-04-02 09:35:51 +02:00
|
|
|
super().open()
|
2020-03-30 10:37:38 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onPrevCard(self):
|
2020-03-30 10:37:38 +02:00
|
|
|
self.index -= 1
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render()
|
2020-03-30 10:37:38 +02:00
|
|
|
|
2020-04-02 09:48:25 +02:00
|
|
|
def _onNextCard(self):
|
2020-03-30 10:37:38 +02:00
|
|
|
self.index += 1
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render()
|
2020-03-30 10:37:38 +02:00
|
|
|
|
|
|
|
def _should_enable_prev(self):
|
|
|
|
return super()._should_enable_prev() or self.index > 0
|
|
|
|
|
|
|
|
def _should_enable_next(self):
|
|
|
|
return super()._should_enable_next() or self.index < len(self.cards) - 1
|
|
|
|
|
|
|
|
def _on_other_side(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._state == "question":
|
|
|
|
self._state = "answer"
|
2020-03-30 10:37:38 +02:00
|
|
|
else:
|
2020-04-02 09:48:25 +02:00
|
|
|
self._state = "question"
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render()
|
2020-03-30 10:37:38 +02:00
|
|
|
|
|
|
|
|
2020-03-29 23:41:00 +02:00
|
|
|
class SingleCardPreviewer(Previewer):
|
|
|
|
def __init__(self, card: Card, *args, **kwargs):
|
|
|
|
self._card = card
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def card(self) -> Card:
|
|
|
|
return self._card
|
|
|
|
|
|
|
|
def _create_gui(self):
|
|
|
|
super()._create_gui()
|
|
|
|
self._other_side = self.bbox.addButton(
|
|
|
|
"Other side", QDialogButtonBox.ActionRole
|
|
|
|
)
|
|
|
|
self._other_side.setAutoDefault(False)
|
|
|
|
self._other_side.setShortcut(QKeySequence("Right"))
|
|
|
|
self._other_side.setShortcut(QKeySequence("Left"))
|
|
|
|
self._other_side.setToolTip(_("Shortcut key: Left or Right arrow"))
|
|
|
|
self._other_side.clicked.connect(self._on_other_side)
|
|
|
|
|
|
|
|
def _on_other_side(self):
|
2020-04-02 09:48:25 +02:00
|
|
|
if self._state == "question":
|
|
|
|
self._state = "answer"
|
2020-03-29 23:41:00 +02:00
|
|
|
else:
|
2020-04-02 09:48:25 +02:00
|
|
|
self._state = "question"
|
2020-04-02 09:35:51 +02:00
|
|
|
self.render()
|