anki/qt/aqt/reviewer.py

833 lines
28 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-02-05 04:59:03 +01: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
2013-10-22 07:28:45 +02:00
import difflib
2017-12-11 07:20:00 +01:00
import html
2019-03-04 08:25:19 +01:00
import json
2019-12-20 10:19:03 +01:00
import re
import unicodedata as ucd
2020-02-27 04:11:21 +01:00
from typing import Callable, List, Optional, Sequence, Tuple, Union
from PyQt5.QtCore import Qt
from anki import hooks
from anki.cards import Card
from anki.lang import _, ngettext
from anki.utils import stripHTML
from aqt import AnkiQt, gui_hooks
from aqt.qt import *
from aqt.sound import av_player, getAudio, play_clicked_audio
from aqt.theme import theme_manager
from aqt.toolbar import BottomBar
from aqt.utils import askUserDialog, downArrow, qtMenuShortcutWorkaround, tooltip
2013-10-22 07:28:45 +02:00
class ReviewerBottomBar:
2020-02-27 04:11:21 +01:00
def __init__(self, reviewer: Reviewer) -> None:
self.reviewer = reviewer
class Reviewer:
"Manage reviews. Maintains a separate state."
def __init__(self, mw: AnkiQt) -> None:
self.mw = mw
self.web = mw.web
self.card: Optional[Card] = None
self.cardQueue: List[Card] = []
self.hadCardQueue = False
self._answeredIds: List[int] = []
2020-02-27 04:11:21 +01:00
self._recordedAudio: Optional[str] = None
self.typeCorrect: str = None # web init happens before this is set
self.state: Optional[str] = None
self.bottom = BottomBar(mw, mw.bottomWeb)
2020-01-15 07:53:24 +01:00
hooks.card_did_leech.append(self.onLeech)
2020-02-27 04:11:21 +01:00
def show(self) -> None:
self.mw.col.reset()
2020-02-27 04:11:21 +01:00
self.mw.setStateShortcuts(self._shortcutKeys()) # type: ignore
self.web.set_bridge_command(self._linkHandler, self)
self.bottom.web.set_bridge_command(self._linkHandler, ReviewerBottomBar(self))
2020-02-27 04:11:21 +01:00
self._reps: int = None
self.nextCard()
2020-02-27 04:11:21 +01:00
def lastCard(self) -> Optional[Card]:
if self._answeredIds:
if not self.card or self._answeredIds[-1] != self.card.id:
try:
return self.mw.col.getCard(self._answeredIds[-1])
except TypeError:
# id was deleted
2020-02-27 04:11:21 +01:00
return None
return None
def cleanup(self) -> None:
2020-01-15 07:53:24 +01:00
gui_hooks.reviewer_will_end()
# Fetching a card
##########################################################################
2020-02-27 04:11:21 +01:00
def nextCard(self) -> None:
elapsed = self.mw.col.timeboxReached()
if elapsed:
2020-02-27 04:11:21 +01:00
assert not isinstance(elapsed, bool)
2019-12-23 01:34:10 +01:00
part1 = (
ngettext("%d card studied in", "%d cards studied in", elapsed[1])
% elapsed[1]
)
mins = int(round(elapsed[0] / 60))
part2 = ngettext("%s minute.", "%s minutes.", mins) % mins
fin = _("Finish")
2019-12-23 01:34:10 +01:00
diag = askUserDialog("%s %s" % (part1, part2), [_("Continue"), fin])
diag.setIcon(QMessageBox.Information)
if diag.run() == fin:
return self.mw.moveToState("deckBrowser")
self.mw.col.startTimebox()
if self.cardQueue:
# undone/edited cards to show
c = self.cardQueue.pop()
c.startTimer()
self.hadCardQueue = True
else:
if self.hadCardQueue:
# the undone/edited cards may be sitting in the regular queue;
# need to reset
self.mw.col.reset()
self.hadCardQueue = False
c = self.mw.col.sched.getCard()
self.card = c
if not c:
self.mw.moveToState("overview")
return
if self._reps is None or self._reps % 100 == 0:
# we recycle the webview periodically so webkit can free memory
self._initWeb()
self._showQuestion()
# Audio
##########################################################################
2014-06-21 22:35:45 +02:00
def replayAudio(self, previewer=None):
if previewer:
state = previewer._previewState
c = previewer.card
else:
state = self.state
c = self.card
if state == "question":
av_player.play_tags(c.question_av_tags())
2014-06-21 22:35:45 +02:00
elif state == "answer":
tags = c.answer_av_tags()
2014-06-21 22:35:45 +02:00
if self._replayq(c, previewer):
tags = c.question_av_tags() + tags
av_player.play_tags(tags)
2020-01-21 05:47:03 +01:00
# Initializing the webview
##########################################################################
2020-02-27 04:11:21 +01:00
def revHtml(self) -> str:
extra = self.mw.col.conf.get("reviewExtra", "")
2019-12-23 01:34:10 +01:00
fade = ""
if self.mw.pm.glMode() == "software":
2019-12-23 01:34:10 +01:00
fade = "<script>qFade=0;</script>"
return """
<div id=_mark>&#x2605;</div>
<div id=_flag>&#x2691;</div>
{}
<div id=qa></div>
{}
2019-12-23 01:34:10 +01:00
""".format(
fade, extra
)
2020-02-27 04:11:21 +01:00
def _initWeb(self) -> None:
self._reps = 0
# main window
2019-12-23 01:34:10 +01:00
self.web.stdHtml(
self.revHtml(),
css=["reviewer.css"],
2019-12-23 01:34:10 +01:00
js=[
"jquery.js",
"browsersel.js",
"mathjax/conf.js",
"mathjax/MathJax.js",
"reviewer.js",
2019-12-23 01:34:10 +01:00
],
context=self,
2019-12-23 01:34:10 +01:00
)
# show answer / ease buttons
self.bottom.web.show()
self.bottom.web.stdHtml(
self._bottomHTML(),
css=["toolbar-bottom.css", "reviewer-bottom.css"],
js=["jquery.js", "reviewer-bottom.js"],
context=ReviewerBottomBar(self),
)
# Showing the question
##########################################################################
2020-02-27 04:11:21 +01:00
def _mungeQA(self, buf: str) -> str:
return self.typeAnsFilter(self.mw.prepare_card_text_for_display(buf))
def _showQuestion(self) -> None:
self._reps += 1
self.state = "question"
2020-02-27 04:11:21 +01:00
self.typedAnswer: str = None
c = self.card
# grab the question and play audio
if c.isEmpty():
2019-12-23 01:34:10 +01:00
q = _(
"""\
The front of this card is empty. Please run Tools>Empty Cards."""
)
else:
q = c.q()
2020-01-21 05:47:03 +01:00
# play audio?
2013-05-03 10:52:46 +02:00
if self.autoplay(c):
av_player.play_tags(c.question_av_tags())
else:
av_player.clear_queue_and_maybe_interrupt()
2020-01-21 05:47:03 +01:00
# render & update bottom
q = self._mungeQA(q)
q = gui_hooks.card_will_show(q, c, "reviewQuestion")
bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
self.web.eval("_showQuestion(%s,'%s');" % (json.dumps(q), bodyclass))
self._drawFlag()
self._drawMark()
self._showAnswerButton()
# if we have a type answer field, focus main web
if self.typeCorrect:
self.mw.web.setFocus()
# user hook
gui_hooks.reviewer_did_show_question(c)
2020-02-27 04:11:21 +01:00
def autoplay(self, card: Card) -> bool:
2019-12-23 01:34:10 +01:00
return self.mw.col.decks.confForDid(card.odid or card.did)["autoplay"]
2014-06-21 22:35:45 +02:00
def _replayq(self, card, previewer=None):
s = previewer if previewer else self
2019-12-23 01:34:10 +01:00
return s.mw.col.decks.confForDid(s.card.odid or s.card.did).get("replayq", True)
2020-02-27 04:11:21 +01:00
def _drawFlag(self) -> None:
self.web.eval("_drawFlag(%s);" % self.card.userFlag())
2020-02-27 04:11:21 +01:00
def _drawMark(self) -> None:
2019-12-23 01:34:10 +01:00
self.web.eval("_drawMark(%s);" % json.dumps(self.card.note().hasTag("marked")))
# Showing the answer
##########################################################################
def _showAnswer(self) -> None:
if self.mw.state != "review":
# showing resetRequired screen; ignore space
return
self.state = "answer"
c = self.card
a = c.a()
# play audio?
2013-05-03 10:52:46 +02:00
if self.autoplay(c):
av_player.play_tags(c.answer_av_tags())
else:
av_player.clear_queue_and_maybe_interrupt()
2017-08-07 08:15:15 +02:00
a = self._mungeQA(a)
a = gui_hooks.card_will_show(a, c, "reviewAnswer")
# render and update bottom
self.web.eval("_showAnswer(%s);" % json.dumps(a))
self._showEaseButtons()
# user hook
gui_hooks.reviewer_did_show_answer(c)
# Answering a card
############################################################
2020-02-27 04:11:21 +01:00
def _answerCard(self, ease: int) -> None:
"Reschedule card and show next."
if self.mw.state != "review":
# showing resetRequired screen; ignore key
return
if self.state != "answer":
return
if self.mw.col.sched.answerButtons(self.card) < ease:
return
proceed, ease = gui_hooks.reviewer_will_answer_card(
(True, ease), self, self.card
)
if not proceed:
return
self.mw.col.sched.answerCard(self.card, ease)
gui_hooks.reviewer_did_answer_card(self, self.card, ease)
self._answeredIds.append(self.card.id)
self.mw.autosave()
self.nextCard()
# Handlers
############################################################
2020-02-27 04:11:21 +01:00
def _shortcutKeys(
self,
) -> List[Union[Tuple[str, Callable], Tuple[Qt.Key, Callable]]]:
return [
("e", self.mw.onEditCurrent),
(" ", self.onEnterKey),
(Qt.Key_Return, self.onEnterKey),
(Qt.Key_Enter, self.onEnterKey),
("r", self.replayAudio),
(Qt.Key_F5, self.replayAudio),
("Ctrl+1", lambda: self.setFlag(1)),
("Ctrl+2", lambda: self.setFlag(2)),
("Ctrl+3", lambda: self.setFlag(3)),
("Ctrl+4", lambda: self.setFlag(4)),
("*", self.onMark),
("=", self.onBuryNote),
("-", self.onBuryCard),
("!", self.onSuspend),
("@", self.onSuspendCard),
("Ctrl+Delete", self.onDelete),
("v", self.onReplayRecorded),
("Shift+v", self.onRecordVoice),
("o", self.onOptions),
("1", lambda: self._answerCard(1)),
("2", lambda: self._answerCard(2)),
("3", lambda: self._answerCard(3)),
("4", lambda: self._answerCard(4)),
("5", self.on_pause_audio),
("6", self.on_seek_backward),
("7", self.on_seek_forward),
]
2020-02-27 04:11:21 +01:00
def on_pause_audio(self) -> None:
av_player.toggle_pause()
seek_secs = 5
2020-02-27 04:11:21 +01:00
def on_seek_backward(self) -> None:
av_player.seek_relative(-self.seek_secs)
2020-02-27 04:11:21 +01:00
def on_seek_forward(self) -> None:
av_player.seek_relative(self.seek_secs)
2020-02-27 04:11:21 +01:00
def onEnterKey(self) -> None:
if self.state == "question":
self._getTypedAnswer()
elif self.state == "answer":
2019-12-23 01:34:10 +01:00
self.bottom.web.evalWithCallback(
"selectedAnswerButton()", self._onAnswerButton
)
2020-02-27 04:11:21 +01:00
def _onAnswerButton(self, val: str) -> None:
# button selected?
if val and val in "1234":
self._answerCard(int(val))
else:
self._answerCard(self._defaultEase())
2020-02-27 04:11:21 +01:00
def _linkHandler(self, url: str) -> None:
if url == "ans":
2016-07-05 05:14:45 +02:00
self._getTypedAnswer()
elif url.startswith("ease"):
self._answerCard(int(url[4:]))
elif url == "edit":
self.mw.onEditCurrent()
elif url == "more":
self.showContextMenu()
2020-01-21 05:47:03 +01:00
elif url.startswith("play:"):
play_clicked_audio(url, self.card)
else:
print("unrecognized anki link:", url)
# Type in the answer
##########################################################################
2019-03-04 08:03:43 +01:00
typeAnsPat = r"\[\[type:(.+?)\]\]"
2020-02-27 04:11:21 +01:00
def typeAnsFilter(self, buf: str) -> str:
if self.state == "question":
return self.typeAnsQuestionFilter(buf)
else:
return self.typeAnsAnswerFilter(buf)
2020-02-27 04:11:21 +01:00
def typeAnsQuestionFilter(self, buf: str) -> str:
self.typeCorrect = None
clozeIdx = None
m = re.search(self.typeAnsPat, buf)
if not m:
return buf
fld = m.group(1)
# if it's a cloze, extract data
if fld.startswith("cloze:"):
# get field and cloze position
clozeIdx = self.card.ord + 1
fld = fld.split(":")[1]
# loop through fields for a match
2019-12-23 01:34:10 +01:00
for f in self.card.model()["flds"]:
if f["name"] == fld:
self.typeCorrect = self.card.note()[f["name"]]
if clozeIdx:
# narrow to cloze
2019-12-23 01:34:10 +01:00
self.typeCorrect = self._contentForCloze(self.typeCorrect, clozeIdx)
self.typeFont = f["font"]
self.typeSize = f["size"]
break
if not self.typeCorrect:
if self.typeCorrect is None:
if clozeIdx:
2019-12-23 01:34:10 +01:00
warn = _(
"""\
Please run Tools>Empty Cards"""
)
else:
warn = _("Type answer: unknown field %s") % fld
return re.sub(self.typeAnsPat, warn, buf)
else:
# empty field, remove type answer pattern
return re.sub(self.typeAnsPat, "", buf)
2019-12-23 01:34:10 +01:00
return re.sub(
self.typeAnsPat,
"""
<center>
<input type=text id=typeans onkeypress="_typeAnsPress();"
style="font-family: '%s'; font-size: %spx;">
</center>
2019-12-23 01:34:10 +01:00
"""
% (self.typeFont, self.typeSize),
buf,
)
2020-02-27 04:11:21 +01:00
def typeAnsAnswerFilter(self, buf: str) -> str:
if not self.typeCorrect:
return re.sub(self.typeAnsPat, "", buf)
origSize = len(buf)
buf = buf.replace("<hr id=answer>", "")
hadHR = len(buf) != origSize
# munge correct value
cor = self.mw.col.media.strip(self.typeCorrect)
cor = re.sub("(\n|<br ?/?>|</?div>)+", " ", cor)
cor = stripHTML(cor)
2013-05-24 04:01:24 +02:00
# ensure we don't chomp multiple whitespace
cor = cor.replace(" ", "&nbsp;")
2020-02-27 04:11:21 +01:00
cor = html.unescape(cor)
cor = cor.replace("\xa0", " ")
cor = cor.strip()
given = self.typedAnswer
# compare with typed answer
res = self.correct(given, cor, showBad=False)
# and update the type answer area
def repl(match):
# can't pass a string in directly, and can't use re.escape as it
# escapes too much
s = """
<span style="font-family: '%s'; font-size: %spx">%s</span>""" % (
2019-12-23 01:34:10 +01:00
self.typeFont,
self.typeSize,
res,
)
if hadHR:
# a hack to ensure the q/a separator falls before the answer
# comparison when user is using {{FrontSide}}
s = "<hr id=answer>" + s
return s
2019-12-23 01:34:10 +01:00
return re.sub(self.typeAnsPat, repl, buf)
2020-02-27 04:11:21 +01:00
def _contentForCloze(self, txt: str, idx) -> str:
2019-12-23 01:34:10 +01:00
matches = re.findall(r"\{\{c%s::(.+?)\}\}" % idx, txt, re.DOTALL)
if not matches:
return None
2019-12-23 01:34:10 +01:00
def noHint(txt):
if "::" in txt:
return txt.split("::")[0]
return txt
2019-12-23 01:34:10 +01:00
matches = [noHint(txt) for txt in matches]
uniqMatches = set(matches)
if len(uniqMatches) == 1:
txt = matches[0]
else:
txt = ", ".join(matches)
return txt
2020-02-27 04:11:21 +01:00
def tokenizeComparison(
self, given: str, correct: str
) -> Tuple[List[Tuple[bool, str]], List[Tuple[bool, str]]]:
2013-06-05 11:39:42 +02:00
# compare in NFC form so accents appear correct
given = ucd.normalize("NFC", given)
correct = ucd.normalize("NFC", correct)
s = difflib.SequenceMatcher(None, given, correct, autojunk=False)
2020-02-27 04:11:21 +01:00
givenElems: List[Tuple[bool, str]] = []
correctElems: List[Tuple[bool, str]] = []
givenPoint = 0
correctPoint = 0
offby = 0
2019-12-23 01:34:10 +01:00
2020-02-27 04:11:21 +01:00
def logBad(old: int, new: int, s: str, array: List[Tuple[bool, str]]) -> None:
if old != new:
2020-02-27 04:11:21 +01:00
array.append((False, s[old:new]))
2019-12-23 01:34:10 +01:00
2020-02-27 04:11:21 +01:00
def logGood(
start: int, cnt: int, s: str, array: List[Tuple[bool, str]]
) -> None:
if cnt:
2020-02-27 04:11:21 +01:00
array.append((True, s[start : start + cnt]))
2019-12-23 01:34:10 +01:00
for x, y, cnt in s.get_matching_blocks():
# if anything was missed in correct, pad given
2019-12-23 01:34:10 +01:00
if cnt and y - offby > x:
givenElems.append((False, "-" * (y - x - offby)))
offby = y - x
# log any proceeding bad elems
logBad(givenPoint, x, given, givenElems)
logBad(correctPoint, y, correct, correctElems)
2019-12-23 01:34:10 +01:00
givenPoint = x + cnt
correctPoint = y + cnt
# log the match
logGood(x, cnt, given, givenElems)
logGood(y, cnt, correct, correctElems)
return givenElems, correctElems
2020-02-27 04:11:21 +01:00
def correct(self, given: str, correct: str, showBad: bool = True) -> str:
"Diff-corrects the typed-in answer."
givenElems, correctElems = self.tokenizeComparison(given, correct)
2019-12-23 01:34:10 +01:00
2020-02-27 04:11:21 +01:00
def good(s: str) -> str:
2019-12-23 01:34:10 +01:00
return "<span class=typeGood>" + html.escape(s) + "</span>"
2020-02-27 04:11:21 +01:00
def bad(s: str) -> str:
2019-12-23 01:34:10 +01:00
return "<span class=typeBad>" + html.escape(s) + "</span>"
2020-02-27 04:11:21 +01:00
def missed(s: str) -> str:
2019-12-23 01:34:10 +01:00
return "<span class=typeMissed>" + html.escape(s) + "</span>"
if given == correct:
res = good(given)
else:
res = ""
for ok, txt in givenElems:
txt = self._noLoneMarks(txt)
if ok:
res += good(txt)
else:
res += bad(txt)
res += "<br><span id=typearrow>&darr;</span><br>"
for ok, txt in correctElems:
txt = self._noLoneMarks(txt)
if ok:
res += good(txt)
else:
res += missed(txt)
res = "<div><code id=typeans>" + res + "</code></div>"
return res
2020-02-27 04:11:21 +01:00
def _noLoneMarks(self, s: str) -> str:
# ensure a combining character at the start does not join to
# previous text
if s and ucd.category(s[0]).startswith("M"):
return "\xa0" + s
return s
2020-02-27 04:11:21 +01:00
def _getTypedAnswer(self) -> None:
2016-07-05 05:14:45 +02:00
self.web.evalWithCallback("typeans ? typeans.value : null", self._onTypedAnswer)
2020-02-27 04:11:21 +01:00
def _onTypedAnswer(self, val: None) -> None:
self.typedAnswer = val or ""
2016-07-05 05:14:45 +02:00
self._showAnswer()
# Bottom bar
##########################################################################
2020-02-27 04:11:21 +01:00
def _bottomHTML(self) -> str:
return """
2016-06-07 06:27:33 +02:00
<center id=outer>
<table id=innertable width=100%% cellspacing=0 cellpadding=0>
<tr>
<td align=left width=50 valign=top class=stat>
<br>
<button title="%(editkey)s" onclick="pycmd('edit');">%(edit)s</button></td>
<td align=center valign=top id=middle>
</td>
<td width=50 align=right valign=top class=stat><span id=time class=stattxt>
</span><br>
<button onclick="pycmd('more');">%(more)s %(downArrow)s</button>
</td>
</tr>
</table>
2016-06-07 06:27:33 +02:00
</center>
<script>
time = %(time)d;
</script>
2019-12-23 01:34:10 +01:00
""" % dict(
rem=self._remaining(),
edit=_("Edit"),
editkey=_("Shortcut key: %s") % "E",
more=_("More"),
downArrow=downArrow(),
time=self.card.timeTaken() // 1000,
)
2020-02-27 04:11:21 +01:00
def _showAnswerButton(self) -> None:
if not self.typeCorrect:
self.bottom.web.setFocus()
2019-12-23 01:34:10 +01:00
middle = """
<span class=stattxt>%s</span><br>
2019-12-23 01:34:10 +01:00
<button title="%s" id=ansbut onclick='pycmd("ans");'>%s</button>""" % (
self._remaining(),
_("Shortcut key: %s") % _("Space"),
_("Show Answer"),
)
# wrap it in a table so it has the same top margin as the ease buttons
2019-12-23 01:34:10 +01:00
middle = (
"<table cellpadding=0><tr><td class=stat2 align=center>%s</td></tr></table>"
% middle
)
if self.card.shouldShowTimer():
maxTime = self.card.timeLimit() / 1000
else:
maxTime = 0
2019-12-23 01:34:10 +01:00
self.bottom.web.eval("showQuestion(%s,%d);" % (json.dumps(middle), maxTime))
self.bottom.web.adjustHeightToFit()
2020-02-27 04:11:21 +01:00
def _showEaseButtons(self) -> None:
self.bottom.web.setFocus()
middle = self._answerButtons()
self.bottom.web.eval("showAnswer(%s);" % json.dumps(middle))
2020-02-27 04:11:21 +01:00
def _remaining(self) -> str:
2019-12-23 01:34:10 +01:00
if not self.mw.col.conf["dueCounts"]:
return ""
if self.hadCardQueue:
# if it's come from the undo queue, don't count it separately
2020-02-27 04:11:21 +01:00
counts: List[Union[int, str]] = list(self.mw.col.sched.counts())
else:
counts = list(self.mw.col.sched.counts(self.card))
idx = self.mw.col.sched.countIdx(self.card)
counts[idx] = "<u>%s</u>" % (counts[idx])
space = " + "
ctxt = "<span class=new-count>%s</span>" % counts[0]
ctxt += space + "<span class=learn-count>%s</span>" % counts[1]
ctxt += space + "<span class=review-count>%s</span>" % counts[2]
return ctxt
2020-02-27 04:11:21 +01:00
def _defaultEase(self) -> int:
if self.mw.col.sched.answerButtons(self.card) == 4:
return 3
else:
return 2
2020-02-27 04:11:21 +01:00
def _answerButtonList(self) -> Sequence[Tuple[int, str]]:
l = ((1, _("Again")),)
cnt = self.mw.col.sched.answerButtons(self.card)
if cnt == 2:
return l + ((2, _("Good")),)
elif cnt == 3:
return l + ((2, _("Good")), (3, _("Easy")))
else:
return l + ((2, _("Hard")), (3, _("Good")), (4, _("Easy")))
2020-02-27 04:11:21 +01:00
def _answerButtons(self) -> str:
default = self._defaultEase()
2019-12-23 01:34:10 +01:00
def but(i, label):
if i == default:
extra = "id=defease"
else:
extra = ""
due = self._buttonTime(i)
2019-12-23 01:34:10 +01:00
return """
<td align=center>%s<button %s title="%s" data-ease="%s" onclick='pycmd("ease%d");'>\
2019-12-23 01:34:10 +01:00
%s</button></td>""" % (
due,
extra,
_("Shortcut key: %s") % i,
i,
i,
label,
)
buf = "<center><table cellpading=0 cellspacing=0><tr>"
for ease, label in self._answerButtonList():
buf += but(ease, label)
buf += "</tr></table>"
script = """
<script>$(function () { $("#defease").focus(); });</script>"""
return buf + script
2020-02-27 04:11:21 +01:00
def _buttonTime(self, i: int) -> str:
2019-12-23 01:34:10 +01:00
if not self.mw.col.conf["estTimes"]:
return "<div class=spacer></div>"
txt = self.mw.col.sched.nextIvlStr(self.card, i, True) or "&nbsp;"
2019-12-23 01:34:10 +01:00
return "<span class=nobold>%s</span><br>" % txt
# Leeches
##########################################################################
2020-02-27 04:11:21 +01:00
def onLeech(self, card: Card) -> None:
# for now
s = _("Card was a leech.")
if card.queue < 0:
s += " " + _("It has been suspended.")
tooltip(s)
# Context menu
##########################################################################
# note the shortcuts listed here also need to be defined above
def _contextMenu(self):
currentFlag = self.card and self.card.userFlag()
opts = [
2019-12-23 01:34:10 +01:00
[
_("Flag Card"),
[
[
_("Red Flag"),
"Ctrl+1",
lambda: self.setFlag(1),
dict(checked=currentFlag == 1),
],
[
_("Orange Flag"),
"Ctrl+2",
lambda: self.setFlag(2),
dict(checked=currentFlag == 2),
],
[
_("Green Flag"),
"Ctrl+3",
lambda: self.setFlag(3),
dict(checked=currentFlag == 3),
],
[
_("Blue Flag"),
"Ctrl+4",
lambda: self.setFlag(4),
dict(checked=currentFlag == 4),
],
],
],
[_("Mark Note"), "*", self.onMark],
2013-10-22 07:28:45 +02:00
[_("Bury Card"), "-", self.onBuryCard],
[_("Bury Note"), "=", self.onBuryNote],
2013-01-29 01:46:17 +01:00
[_("Suspend Card"), "@", self.onSuspendCard],
[_("Suspend Note"), "!", self.onSuspend],
[_("Delete Note"), "Ctrl+Delete", self.onDelete],
[_("Options"), "O", self.onOptions],
None,
[_("Replay Audio"), "R", self.replayAudio],
[_("Pause Audio"), "5", self.on_pause_audio],
[_("Audio -5s"), "6", self.on_seek_backward],
[_("Audio +5s"), "7", self.on_seek_forward],
[_("Record Own Voice"), "Shift+V", self.onRecordVoice],
[_("Replay Own Voice"), "V", self.onReplayRecorded],
]
return opts
2019-12-23 01:34:10 +01:00
def showContextMenu(self) -> None:
opts = self._contextMenu()
m = QMenu(self.mw)
self._addMenuItems(m, opts)
gui_hooks.reviewer_will_show_context_menu(self, m)
qtMenuShortcutWorkaround(m)
m.exec_(QCursor.pos())
2020-02-27 04:11:21 +01:00
def _addMenuItems(self, m, rows) -> None:
for row in rows:
if not row:
m.addSeparator()
continue
if len(row) == 2:
subm = m.addMenu(row[0])
self._addMenuItems(subm, row[1])
qtMenuShortcutWorkaround(subm)
continue
if len(row) == 4:
label, scut, func, opts = row
else:
label, scut, func = row
opts = {}
a = m.addAction(label)
2017-01-10 09:39:31 +01:00
if scut:
a.setShortcut(QKeySequence(scut))
if opts.get("checked"):
a.setCheckable(True)
a.setChecked(True)
a.triggered.connect(func)
2020-02-27 04:11:21 +01:00
def onOptions(self) -> None:
2019-12-23 01:34:10 +01:00
self.mw.onDeckConf(self.mw.col.decks.get(self.card.odid or self.card.did))
2020-02-27 04:11:21 +01:00
def setFlag(self, flag: int) -> None:
2018-11-12 02:58:36 +01:00
# need to toggle off?
if self.card.userFlag() == flag:
flag = 0
self.card.setUserFlag(flag)
self.card.flush()
self._drawFlag()
2020-02-27 04:11:21 +01:00
def onMark(self) -> None:
f = self.card.note()
if f.hasTag("marked"):
f.delTag("marked")
else:
f.addTag("marked")
f.flush()
self._drawMark()
2020-02-27 04:11:21 +01:00
def onSuspend(self) -> None:
self.mw.checkpoint(_("Suspend"))
2019-12-23 01:34:10 +01:00
self.mw.col.sched.suspendCards([c.id for c in self.card.note().cards()])
tooltip(_("Note suspended."))
2013-01-29 01:46:17 +01:00
self.mw.reset()
2020-02-27 04:11:21 +01:00
def onSuspendCard(self) -> None:
2013-01-29 01:46:17 +01:00
self.mw.checkpoint(_("Suspend"))
self.mw.col.sched.suspendCards([self.card.id])
tooltip(_("Card suspended."))
self.mw.reset()
2020-02-27 04:11:21 +01:00
def onDelete(self) -> None:
# need to check state because the shortcut is global to the main
# window
if self.mw.state != "review" or not self.card:
return
self.mw.checkpoint(_("Delete"))
cnt = len(self.card.note().cards())
self.mw.col.remNotes([self.card.note().id])
self.mw.reset()
2019-12-23 01:34:10 +01:00
tooltip(
ngettext(
"Note and its %d card deleted.", "Note and its %d cards deleted.", cnt
)
% cnt
)
2020-02-27 04:11:21 +01:00
def onBuryCard(self) -> None:
2013-10-22 07:28:45 +02:00
self.mw.checkpoint(_("Bury"))
self.mw.col.sched.buryCards([self.card.id])
self.mw.reset()
tooltip(_("Card buried."))
2020-02-27 04:11:21 +01:00
def onBuryNote(self) -> None:
self.mw.checkpoint(_("Bury"))
self.mw.col.sched.buryNote(self.card.nid)
self.mw.reset()
tooltip(_("Note buried."))
2020-02-27 04:11:21 +01:00
def onRecordVoice(self) -> None:
self._recordedAudio = getAudio(self.mw, encode=False)
self.onReplayRecorded()
2020-02-27 04:11:21 +01:00
def onReplayRecorded(self) -> None:
if not self._recordedAudio:
2020-02-27 04:11:21 +01:00
tooltip(_("You haven't recorded your voice yet."))
return
2020-01-20 11:10:38 +01:00
av_player.play_file(self._recordedAudio)