2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-05-14 07:24:29 +02:00
|
|
|
import copy
|
2019-12-20 10:19:03 +01:00
|
|
|
import json
|
2012-12-21 08:51:59 +01:00
|
|
|
import re
|
2020-05-13 09:24:49 +02:00
|
|
|
from typing import List, Optional
|
2013-10-20 03:02:29 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2020-01-24 02:06:11 +01:00
|
|
|
from anki.cards import Card
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.consts import *
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _, ngettext
|
2020-05-05 03:10:24 +02:00
|
|
|
from anki.notes import Note
|
2020-04-26 04:34:25 +02:00
|
|
|
from anki.rsbackend import TemplateError
|
2020-05-13 09:24:49 +02:00
|
|
|
from anki.template import TemplateRenderContext
|
2020-05-14 07:24:29 +02:00
|
|
|
from anki.utils import isMac, isWin
|
2020-05-05 03:10:24 +02:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2020-01-25 07:01:16 +01:00
|
|
|
from aqt.sound import av_player, play_clicked_audio
|
2020-01-23 06:08:10 +01:00
|
|
|
from aqt.theme import theme_manager
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import (
|
|
|
|
askUser,
|
|
|
|
downArrow,
|
|
|
|
getOnlyText,
|
|
|
|
openHelp,
|
|
|
|
restoreGeom,
|
|
|
|
saveGeom,
|
|
|
|
showInfo,
|
|
|
|
showWarning,
|
2020-05-13 09:24:49 +02:00
|
|
|
tooltip,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.webview import AnkiWebView
|
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
# fixme: previewing with empty fields
|
|
|
|
# fixme: deck name on new cards
|
|
|
|
# fixme: card count when removing
|
|
|
|
# fixme: i18n
|
|
|
|
# fixme: change tracking and tooltip in fields
|
2020-05-14 07:24:29 +02:00
|
|
|
# fixme: replay suppression
|
2013-10-20 03:02:29 +02:00
|
|
|
|
2020-01-24 02:06:11 +01:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
class CardLayout(QDialog):
|
2020-05-05 03:10:24 +02:00
|
|
|
def __init__(
|
2020-05-13 09:24:49 +02:00
|
|
|
self, mw: AnkiQt, note: Note, ord=0, parent: Optional[QWidget] = None,
|
2020-05-05 03:10:24 +02:00
|
|
|
):
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, parent or mw, Qt.Window)
|
2016-07-04 05:22:35 +02:00
|
|
|
mw.setupDialogGC(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = aqt.mw
|
|
|
|
self.note = note
|
|
|
|
self.ord = ord
|
2020-05-13 09:24:49 +02:00
|
|
|
self.col = self.mw.col.weakref()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mm = self.mw.col.models
|
|
|
|
self.model = note.model()
|
2020-05-13 09:24:49 +02:00
|
|
|
self.templates = self.model["tmpls"]
|
|
|
|
self.mm._remove_from_cache(self.model["id"])
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.checkpoint(_("Card Types"))
|
2020-05-13 09:24:49 +02:00
|
|
|
self.changed = False
|
2017-08-08 11:45:31 +02:00
|
|
|
self.setupTopArea()
|
2017-08-08 07:31:36 +02:00
|
|
|
self.setupMainArea()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupButtons()
|
2017-08-12 09:29:47 +02:00
|
|
|
self.setupShortcuts()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.setWindowTitle(_("Card Types for %s") % self.model["name"])
|
2012-12-21 08:51:59 +01:00
|
|
|
v1 = QVBoxLayout()
|
2017-08-08 11:45:31 +02:00
|
|
|
v1.addWidget(self.topArea)
|
2017-08-08 07:31:36 +02:00
|
|
|
v1.addWidget(self.mainArea)
|
2012-12-21 08:51:59 +01:00
|
|
|
v1.addLayout(self.buttons)
|
2019-12-23 01:34:10 +01:00
|
|
|
v1.setContentsMargins(12, 12, 12, 12)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setLayout(v1)
|
2020-02-28 13:34:54 +01:00
|
|
|
gui_hooks.card_layout_will_show(self)
|
2020-05-13 09:24:49 +02:00
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
restoreGeom(self, "CardLayout")
|
2016-07-07 04:32:27 +02:00
|
|
|
self.setWindowModality(Qt.ApplicationModal)
|
|
|
|
self.show()
|
2017-08-08 11:45:31 +02:00
|
|
|
# take the focus away from the first input area when starting up,
|
|
|
|
# as users tend to accidentally type into the template
|
|
|
|
self.setFocus()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def redraw_everything(self):
|
|
|
|
self.ignore_change_signals = True
|
2017-08-08 11:45:31 +02:00
|
|
|
self.updateTopArea()
|
2020-05-13 09:24:49 +02:00
|
|
|
self.ignore_change_signals = False
|
|
|
|
self.update_current_ordinal_and_redraw(self.ord)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def update_current_ordinal_and_redraw(self, idx):
|
|
|
|
if self.ignore_change_signals:
|
|
|
|
return
|
|
|
|
self.ord = idx
|
|
|
|
self.playedAudio = {}
|
|
|
|
self.fill_fields_from_template()
|
|
|
|
self.renderPreview()
|
2017-08-12 09:29:47 +02:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def _isCloze(self):
|
|
|
|
return self.model["type"] == MODEL_CLOZE
|
|
|
|
|
|
|
|
# Top area
|
|
|
|
##########################################################################
|
2017-08-12 09:29:47 +02:00
|
|
|
|
2017-08-08 11:45:31 +02:00
|
|
|
def setupTopArea(self):
|
|
|
|
self.topArea = QWidget()
|
|
|
|
self.topAreaForm = aqt.forms.clayout_top.Ui_Form()
|
|
|
|
self.topAreaForm.setupUi(self.topArea)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.topAreaForm.templateOptions.setText(_("Options") + " " + downArrow())
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.topAreaForm.templateOptions.clicked, self.onMore)
|
2020-05-13 09:24:49 +02:00
|
|
|
qconnect(
|
|
|
|
self.topAreaForm.templatesBox.currentIndexChanged,
|
|
|
|
self.update_current_ordinal_and_redraw,
|
|
|
|
)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
|
|
|
def updateTopArea(self):
|
|
|
|
cnt = self.mw.col.models.useCount(self.model)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.topAreaForm.changesLabel.setText(
|
|
|
|
ngettext(
|
|
|
|
"Changes below will affect the %(cnt)d note that uses this card type.",
|
|
|
|
"Changes below will affect the %(cnt)d notes that use this card type.",
|
|
|
|
cnt,
|
|
|
|
)
|
|
|
|
% dict(cnt=cnt)
|
|
|
|
)
|
2017-08-09 03:11:39 +02:00
|
|
|
self.updateCardNames()
|
|
|
|
|
|
|
|
def updateCardNames(self):
|
2020-05-13 09:24:49 +02:00
|
|
|
self.ignore_change_signals = True
|
2017-08-08 11:45:31 +02:00
|
|
|
combo = self.topAreaForm.templatesBox
|
|
|
|
combo.clear()
|
2020-05-13 09:24:49 +02:00
|
|
|
combo.addItems(
|
|
|
|
self._summarizedName(idx, tmpl) for (idx, tmpl) in enumerate(self.templates)
|
|
|
|
)
|
2017-08-08 11:45:31 +02:00
|
|
|
combo.setCurrentIndex(self.ord)
|
2017-08-11 13:04:17 +02:00
|
|
|
combo.setEnabled(not self._isCloze())
|
2020-05-13 09:24:49 +02:00
|
|
|
self.ignore_change_signals = False
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def _summarizedName(self, idx: int, tmpl: Dict):
|
2020-01-21 23:42:27 +01:00
|
|
|
return "{}: {}: {} -> {}".format(
|
2020-05-13 09:24:49 +02:00
|
|
|
idx + 1,
|
2019-12-23 01:34:10 +01:00
|
|
|
tmpl["name"],
|
|
|
|
self._fieldsOnTemplate(tmpl["qfmt"]),
|
|
|
|
self._fieldsOnTemplate(tmpl["afmt"]),
|
|
|
|
)
|
2017-08-09 03:11:39 +02:00
|
|
|
|
|
|
|
def _fieldsOnTemplate(self, fmt):
|
|
|
|
matches = re.findall("{{[^#/}]+?}}", fmt)
|
2020-05-13 09:24:49 +02:00
|
|
|
chars_allowed = 30
|
|
|
|
field_names: List[str] = []
|
2017-08-09 03:11:39 +02:00
|
|
|
for m in matches:
|
|
|
|
# strip off mustache
|
|
|
|
m = re.sub(r"[{}]", "", m)
|
|
|
|
# strip off modifiers
|
|
|
|
m = m.split(":")[-1]
|
|
|
|
# don't show 'FrontSide'
|
|
|
|
if m == "FrontSide":
|
|
|
|
continue
|
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
field_names.append(m)
|
|
|
|
chars_allowed -= len(m)
|
|
|
|
if chars_allowed <= 0:
|
|
|
|
break
|
2017-08-09 03:11:39 +02:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
s = "+".join(field_names)
|
|
|
|
if chars_allowed <= 0:
|
2020-05-05 03:10:24 +02:00
|
|
|
s += "+..."
|
|
|
|
return s
|
2017-08-09 03:11:39 +02:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def setupShortcuts(self):
|
|
|
|
for i in range(1, 9):
|
|
|
|
QShortcut(
|
|
|
|
QKeySequence("Ctrl+%d" % i),
|
|
|
|
self,
|
|
|
|
activated=lambda i=i: self.selectCard(i),
|
|
|
|
) # type: ignore
|
|
|
|
|
|
|
|
def selectCard(self, n):
|
|
|
|
self.ord = n - 1
|
|
|
|
self.redraw_everything()
|
|
|
|
|
|
|
|
# Main area
|
|
|
|
##########################################################################
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-08 07:31:36 +02:00
|
|
|
def setupMainArea(self):
|
|
|
|
w = self.mainArea = QWidget()
|
2012-12-21 08:51:59 +01:00
|
|
|
l = QHBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
l.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
l.setSpacing(3)
|
|
|
|
left = QWidget()
|
|
|
|
# template area
|
2017-08-08 07:31:36 +02:00
|
|
|
tform = self.tform = aqt.forms.template.Ui_Form()
|
2012-12-21 08:51:59 +01:00
|
|
|
tform.setupUi(left)
|
2020-05-14 07:24:29 +02:00
|
|
|
# tform.groupBox_3.setTitle(_("Styling (shared between cards)"))
|
2020-05-13 09:24:49 +02:00
|
|
|
qconnect(tform.front.textChanged, self.write_edits_to_template_and_redraw)
|
|
|
|
qconnect(tform.css.textChanged, self.write_edits_to_template_and_redraw)
|
|
|
|
qconnect(tform.back.textChanged, self.write_edits_to_template_and_redraw)
|
2020-05-14 07:24:29 +02:00
|
|
|
qconnect(tform.tabWidget.currentChanged, self.on_editor_changed)
|
2012-12-21 08:51:59 +01:00
|
|
|
l.addWidget(left, 5)
|
2020-05-14 07:24:29 +02:00
|
|
|
self.search_box = search = QLineEdit()
|
|
|
|
search.setPlaceholderText("Search")
|
|
|
|
qconnect(search.textChanged, self.on_search_changed)
|
|
|
|
qconnect(search.returnPressed, self.on_search_next)
|
|
|
|
tform.tabWidget.setCornerWidget(search)
|
2012-12-21 08:51:59 +01:00
|
|
|
# preview area
|
|
|
|
right = QWidget()
|
2020-05-04 05:23:08 +02:00
|
|
|
self.pform: Any = aqt.forms.preview.Ui_Form()
|
|
|
|
pform = self.pform
|
2012-12-21 08:51:59 +01:00
|
|
|
pform.setupUi(right)
|
2020-05-14 07:24:29 +02:00
|
|
|
|
|
|
|
if self._isCloze():
|
|
|
|
nums = self.note.cloze_numbers_in_fields()
|
|
|
|
if self.ord + 1 not in nums:
|
|
|
|
# current card is empty
|
|
|
|
nums.append(self.ord + 1)
|
|
|
|
self.cloze_numbers = sorted(nums)
|
|
|
|
self.setup_cloze_number_box()
|
|
|
|
else:
|
|
|
|
self.cloze_numbers = []
|
|
|
|
self.pform.cloze_number_combo.setHidden(True)
|
2017-08-08 08:02:55 +02:00
|
|
|
|
|
|
|
self.setupWebviews()
|
|
|
|
|
|
|
|
l.addWidget(right, 5)
|
|
|
|
w.setLayout(l)
|
|
|
|
|
2020-05-14 07:24:29 +02:00
|
|
|
def setup_cloze_number_box(self):
|
|
|
|
names = (_("Cloze %d") % n for n in self.cloze_numbers)
|
|
|
|
self.pform.cloze_number_combo.addItems(names)
|
|
|
|
try:
|
|
|
|
idx = self.cloze_numbers.index(self.ord + 1)
|
|
|
|
self.pform.cloze_number_combo.setCurrentIndex(idx)
|
|
|
|
except ValueError:
|
|
|
|
# invalid cloze
|
|
|
|
pass
|
|
|
|
qconnect(
|
|
|
|
self.pform.cloze_number_combo.currentIndexChanged, self.on_change_cloze
|
|
|
|
)
|
|
|
|
|
|
|
|
def current_editor(self) -> QTextEdit:
|
|
|
|
idx = self.tform.tabWidget.currentIndex()
|
|
|
|
if idx == 0:
|
|
|
|
return self.tform.front
|
|
|
|
elif idx == 1:
|
|
|
|
return self.tform.back
|
|
|
|
else:
|
|
|
|
return self.tform.css
|
|
|
|
|
|
|
|
def on_change_cloze(self, idx: int) -> None:
|
|
|
|
self.ord = self.cloze_numbers[idx] - 1
|
|
|
|
self._renderPreview()
|
|
|
|
|
|
|
|
def on_editor_changed(self, idx: int) -> None:
|
|
|
|
if idx == 0:
|
|
|
|
self.pform.preview_front.setChecked(True)
|
|
|
|
elif idx == 1:
|
|
|
|
self.pform.preview_back.setChecked(True)
|
|
|
|
|
|
|
|
def on_search_changed(self, text: str):
|
|
|
|
editor = self.current_editor()
|
|
|
|
if not editor.find(text):
|
|
|
|
# try again from top
|
|
|
|
cursor = editor.textCursor()
|
|
|
|
cursor.movePosition(QTextCursor.Start)
|
|
|
|
editor.setTextCursor(cursor)
|
|
|
|
editor.find(text)
|
|
|
|
|
|
|
|
def on_search_next(self):
|
|
|
|
self.on_search_changed(self.search_box.text())
|
|
|
|
|
2017-08-08 08:02:55 +02:00
|
|
|
def setupWebviews(self):
|
2020-05-14 07:24:29 +02:00
|
|
|
if theme_manager.night_mode and not theme_manager.macos_dark_mode():
|
|
|
|
# the grouping box renders incorrectly in the fusion theme. 5.9+
|
|
|
|
# 5.13 behave differently to 5.14, but it looks bad in either case,
|
|
|
|
# and adjusting the top margin makes the 'save PDF' button show in
|
|
|
|
# the wrong place, so for now we just disable the border instead
|
|
|
|
self.setStyleSheet("QGroupBox { border: 0; }")
|
|
|
|
|
2017-08-08 08:02:55 +02:00
|
|
|
pform = self.pform
|
2020-05-14 07:24:29 +02:00
|
|
|
pform.frontWeb = AnkiWebView(title="card layout")
|
|
|
|
pform.verticalLayout.addWidget(pform.frontWeb)
|
|
|
|
pform.verticalLayout.setStretch(1, 99)
|
|
|
|
pform.preview_front.isChecked()
|
|
|
|
qconnect(pform.preview_front.toggled, self.on_preview_toggled)
|
|
|
|
qconnect(pform.preview_back.toggled, self.on_preview_toggled)
|
2019-12-23 01:34:10 +01:00
|
|
|
jsinc = [
|
2020-02-15 15:03:43 +01:00
|
|
|
"jquery.js",
|
|
|
|
"browsersel.js",
|
|
|
|
"mathjax/conf.js",
|
|
|
|
"mathjax/MathJax.js",
|
|
|
|
"reviewer.js",
|
2019-12-23 01:34:10 +01:00
|
|
|
]
|
|
|
|
pform.frontWeb.stdHtml(
|
2020-02-15 15:03:43 +01:00
|
|
|
self.mw.reviewer.revHtml(), css=["reviewer.css"], js=jsinc, context=self,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-02-08 23:59:29 +01:00
|
|
|
pform.frontWeb.set_bridge_command(self._on_bridge_cmd, self)
|
2020-05-14 07:24:29 +02:00
|
|
|
|
|
|
|
def on_preview_toggled(self):
|
|
|
|
self._renderPreview()
|
2020-01-25 07:01:16 +01:00
|
|
|
|
|
|
|
def _on_bridge_cmd(self, cmd: str) -> Any:
|
|
|
|
if cmd.startswith("play:"):
|
2020-05-13 09:24:49 +02:00
|
|
|
play_clicked_audio(cmd, self.rendered_card)
|
2017-08-08 07:31:36 +02:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def ephemeral_card_for_rendering(self) -> Card:
|
|
|
|
card = Card(self.col)
|
|
|
|
card.ord = self.ord
|
2020-05-14 07:24:29 +02:00
|
|
|
template = copy.copy(self.current_template())
|
|
|
|
# may differ in cloze case
|
|
|
|
template["ord"] = card.ord
|
|
|
|
# this fetches notetype, we should pass it in
|
2020-05-13 09:24:49 +02:00
|
|
|
output = TemplateRenderContext.from_card_layout(
|
2020-05-14 07:24:29 +02:00
|
|
|
self.note, card, notetype=self.model, template=template
|
2020-05-13 09:24:49 +02:00
|
|
|
).render()
|
|
|
|
card.set_render_output(output)
|
|
|
|
return card
|
2019-12-30 09:50:00 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Buttons
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupButtons(self):
|
|
|
|
l = self.buttons = QHBoxLayout()
|
|
|
|
help = QPushButton(_("Help"))
|
|
|
|
help.setAutoDefault(False)
|
|
|
|
l.addWidget(help)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(help.clicked, self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
l.addStretch()
|
|
|
|
addField = QPushButton(_("Add Field"))
|
|
|
|
addField.setAutoDefault(False)
|
|
|
|
l.addWidget(addField)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(addField.clicked, self.onAddField)
|
2017-08-08 11:45:31 +02:00
|
|
|
if not self._isCloze():
|
2012-12-21 08:51:59 +01:00
|
|
|
flip = QPushButton(_("Flip"))
|
|
|
|
flip.setAutoDefault(False)
|
|
|
|
l.addWidget(flip)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(flip.clicked, self.onFlip)
|
2012-12-21 08:51:59 +01:00
|
|
|
l.addStretch()
|
2020-04-26 04:34:25 +02:00
|
|
|
save = QPushButton(_("Save"))
|
|
|
|
save.setAutoDefault(False)
|
|
|
|
l.addWidget(save)
|
|
|
|
qconnect(save.clicked, self.accept)
|
|
|
|
|
|
|
|
close = QPushButton(_("Cancel"))
|
2012-12-21 08:51:59 +01:00
|
|
|
close.setAutoDefault(False)
|
|
|
|
l.addWidget(close)
|
2020-04-26 04:34:25 +02:00
|
|
|
qconnect(close.clicked, self.reject)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
# Reading/writing question/answer/css
|
2012-12-21 08:51:59 +01:00
|
|
|
##########################################################################
|
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def current_template(self) -> Dict:
|
2020-05-14 07:24:29 +02:00
|
|
|
if self._isCloze():
|
|
|
|
return self.templates[0]
|
2020-05-13 09:24:49 +02:00
|
|
|
return self.templates[self.ord]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def fill_fields_from_template(self):
|
|
|
|
t = self.current_template()
|
|
|
|
self.ignore_change_signals = True
|
2019-12-23 01:34:10 +01:00
|
|
|
self.tform.front.setPlainText(t["qfmt"])
|
|
|
|
self.tform.css.setPlainText(self.model["css"])
|
|
|
|
self.tform.back.setPlainText(t["afmt"])
|
2017-08-08 07:31:36 +02:00
|
|
|
self.tform.front.setAcceptRichText(False)
|
|
|
|
self.tform.css.setAcceptRichText(False)
|
|
|
|
self.tform.back.setAcceptRichText(False)
|
2020-05-05 03:24:33 +02:00
|
|
|
if qtminor < 10:
|
|
|
|
self.tform.front.setTabStopWidth(30)
|
|
|
|
self.tform.css.setTabStopWidth(30)
|
|
|
|
self.tform.back.setTabStopWidth(30)
|
|
|
|
else:
|
|
|
|
tab_width = self.fontMetrics().width(" " * 4)
|
|
|
|
self.tform.front.setTabStopDistance(tab_width)
|
|
|
|
self.tform.css.setTabStopDistance(tab_width)
|
|
|
|
self.tform.back.setTabStopDistance(tab_width)
|
2020-05-13 09:24:49 +02:00
|
|
|
self.ignore_change_signals = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def write_edits_to_template_and_redraw(self):
|
|
|
|
if self.ignore_change_signals:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
self.changed = True
|
|
|
|
t = self.current_template()
|
|
|
|
t["qfmt"] = self.tform.front.toPlainText()
|
|
|
|
t["afmt"] = self.tform.back.toPlainText()
|
|
|
|
self.model["css"] = self.tform.css.toPlainText()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.renderPreview()
|
|
|
|
|
|
|
|
# Preview
|
|
|
|
##########################################################################
|
|
|
|
|
2017-08-08 08:02:55 +02:00
|
|
|
_previewTimer = None
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def renderPreview(self):
|
2017-08-08 08:02:55 +02:00
|
|
|
# schedule a preview when timing stops
|
|
|
|
self.cancelPreviewTimer()
|
2020-05-13 09:24:49 +02:00
|
|
|
self._previewTimer = self.mw.progress.timer(200, self._renderPreview, False)
|
2017-08-08 08:02:55 +02:00
|
|
|
|
|
|
|
def cancelPreviewTimer(self):
|
|
|
|
if self._previewTimer:
|
|
|
|
self._previewTimer.stop()
|
|
|
|
self._previewTimer = None
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def _renderPreview(self) -> None:
|
2017-08-08 08:02:55 +02:00
|
|
|
self.cancelPreviewTimer()
|
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
c = self.rendered_card = self.ephemeral_card_for_rendering()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
ti = self.maybeTextInput
|
2017-12-04 03:20:56 +01:00
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
|
2017-09-06 05:02:00 +02:00
|
|
|
|
2020-05-14 07:24:29 +02:00
|
|
|
if self.pform.preview_front.isChecked():
|
|
|
|
q = ti(self.mw.prepare_card_text_for_display(c.q()))
|
|
|
|
q = gui_hooks.card_will_show(q, c, "clayoutQuestion")
|
|
|
|
text = q
|
|
|
|
audio = c.question_av_tags()
|
|
|
|
else:
|
|
|
|
a = ti(self.mw.prepare_card_text_for_display(c.a()), type="a")
|
|
|
|
a = gui_hooks.card_will_show(a, c, "clayoutAnswer")
|
|
|
|
text = a
|
|
|
|
audio = c.answer_av_tags()
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2017-08-10 13:39:04 +02:00
|
|
|
# use _showAnswer to avoid the longer delay
|
2020-05-14 07:24:29 +02:00
|
|
|
self.pform.frontWeb.eval(
|
|
|
|
"_showAnswer(%s,'%s');" % (json.dumps(text), bodyclass)
|
|
|
|
)
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
if c.id not in self.playedAudio:
|
2020-05-14 07:24:29 +02:00
|
|
|
av_player.play_tags(audio)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.playedAudio[c.id] = True
|
|
|
|
|
2017-08-09 03:11:39 +02:00
|
|
|
self.updateCardNames()
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
def maybeTextInput(self, txt, type="q"):
|
2013-05-23 04:11:25 +02:00
|
|
|
if "[[type:" not in txt:
|
|
|
|
return txt
|
2013-05-20 11:21:14 +02:00
|
|
|
origLen = len(txt)
|
|
|
|
txt = txt.replace("<hr id=answer>", "")
|
|
|
|
hadHR = origLen != len(txt)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2013-05-20 11:21:14 +02:00
|
|
|
def answerRepl(match):
|
2016-05-12 06:45:35 +02:00
|
|
|
res = self.mw.reviewer.correct("exomple", "an example")
|
2013-05-20 11:21:14 +02:00
|
|
|
if hadHR:
|
|
|
|
res = "<hr id=answer>" + res
|
|
|
|
return res
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-05-05 03:10:24 +02:00
|
|
|
repl: Union[str, Callable]
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
if type == "q":
|
2018-08-28 03:01:07 +02:00
|
|
|
repl = "<input id='typeans' type=text value='exomple' readonly='readonly'>"
|
2013-05-20 11:21:14 +02:00
|
|
|
repl = "<center>%s</center>" % repl
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2013-05-20 11:21:14 +02:00
|
|
|
repl = answerRepl
|
2019-03-04 08:03:43 +01:00
|
|
|
return re.sub(r"\[\[type:.+?\]\]", repl, txt)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Card operations
|
|
|
|
######################################################################
|
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def onRemove(self):
|
|
|
|
if len(self.templates) < 2:
|
|
|
|
return showInfo(_("At least one card type is required."))
|
|
|
|
template = self.current_template()
|
|
|
|
msg = _("Delete the '%(a)s' card type, and its %(b)s?") % dict(
|
|
|
|
a=template["name"], b=_("cards")
|
|
|
|
)
|
|
|
|
if not askUser(msg):
|
|
|
|
return
|
|
|
|
|
|
|
|
self.changed = True
|
|
|
|
self.mm.remTemplate(self.model, template, save=False)
|
|
|
|
|
|
|
|
# ensure current ordinal is within bounds
|
|
|
|
idx = self.ord
|
|
|
|
if idx >= len(self.templates):
|
|
|
|
self.ord = len(self.templates) - 1
|
|
|
|
|
|
|
|
self.redraw_everything()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onRename(self):
|
2020-05-13 09:24:49 +02:00
|
|
|
template = self.current_template()
|
|
|
|
name = getOnlyText(_("New name:"), default=template["name"])
|
|
|
|
if not name.strip():
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
self.changed = True
|
|
|
|
template["name"] = name
|
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onReorder(self):
|
2020-05-13 09:24:49 +02:00
|
|
|
n = len(self.templates)
|
|
|
|
template = self.current_template()
|
|
|
|
current_pos = self.templates.index(template) + 1
|
|
|
|
pos = getOnlyText(
|
|
|
|
_("Enter new card position (1...%s):") % n, default=str(current_pos)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not pos:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
pos = int(pos)
|
|
|
|
except ValueError:
|
|
|
|
return
|
|
|
|
if pos < 1 or pos > n:
|
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
if pos == current_pos:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
new_idx = pos - 1
|
|
|
|
self.changed = True
|
|
|
|
self.mm.moveTemplate(self.model, template, new_idx, save=False)
|
|
|
|
self.ord = new_idx
|
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _newCardName(self):
|
2020-05-13 09:24:49 +02:00
|
|
|
n = len(self.templates) + 1
|
2012-12-21 08:51:59 +01:00
|
|
|
while 1:
|
|
|
|
name = _("Card %d") % n
|
2020-05-13 09:24:49 +02:00
|
|
|
if name not in [t["name"] for t in self.templates]:
|
2012-12-21 08:51:59 +01:00
|
|
|
break
|
|
|
|
n += 1
|
|
|
|
return name
|
|
|
|
|
|
|
|
def onAddCard(self):
|
2017-08-08 11:45:31 +02:00
|
|
|
cnt = self.mw.col.models.useCount(self.model)
|
2019-12-23 01:34:10 +01:00
|
|
|
txt = (
|
|
|
|
ngettext(
|
|
|
|
"This will create %d card. Proceed?",
|
|
|
|
"This will create %d cards. Proceed?",
|
|
|
|
cnt,
|
|
|
|
)
|
|
|
|
% cnt
|
|
|
|
)
|
2017-08-08 11:45:31 +02:00
|
|
|
if not askUser(txt):
|
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
self.changed = True
|
2012-12-21 08:51:59 +01:00
|
|
|
name = self._newCardName()
|
|
|
|
t = self.mm.newTemplate(name)
|
2020-05-13 09:24:49 +02:00
|
|
|
old = self.current_template()
|
2019-12-23 01:34:10 +01:00
|
|
|
t["qfmt"] = old["qfmt"]
|
|
|
|
t["afmt"] = old["afmt"]
|
2020-05-13 09:24:49 +02:00
|
|
|
self.mm.addTemplate(self.model, t, save=False)
|
|
|
|
self.ord = len(self.templates) - 1
|
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onFlip(self):
|
2020-05-13 09:24:49 +02:00
|
|
|
old = self.current_template()
|
2012-12-21 08:51:59 +01:00
|
|
|
self._flipQA(old, old)
|
2020-05-13 09:24:49 +02:00
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _flipQA(self, src, dst):
|
2019-12-23 01:34:10 +01:00
|
|
|
m = re.match("(?s)(.+)<hr id=answer>(.+)", src["afmt"])
|
2012-12-21 08:51:59 +01:00
|
|
|
if not m:
|
2019-12-23 01:34:10 +01:00
|
|
|
showInfo(
|
|
|
|
_(
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
Anki couldn't find the line between the question and answer. Please \
|
2019-12-23 01:34:10 +01:00
|
|
|
adjust the template manually to switch the question and answer."""
|
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
self.changed = True
|
2019-12-23 01:34:10 +01:00
|
|
|
dst["afmt"] = "{{FrontSide}}\n\n<hr id=answer>\n\n%s" % src["qfmt"]
|
|
|
|
dst["qfmt"] = m.group(2).strip()
|
2012-12-21 08:51:59 +01:00
|
|
|
return True
|
|
|
|
|
2017-08-08 11:45:31 +02:00
|
|
|
def onMore(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
m = QMenu(self)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
|
|
|
if not self._isCloze():
|
|
|
|
a = m.addAction(_("Add Card Type..."))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onAddCard)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
|
|
|
a = m.addAction(_("Remove Card Type..."))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onRemove)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
|
|
|
a = m.addAction(_("Rename Card Type..."))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onRename)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
|
|
|
a = m.addAction(_("Reposition Card Type..."))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onReorder)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
|
|
|
m.addSeparator()
|
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
t = self.current_template()
|
2019-12-23 01:34:10 +01:00
|
|
|
if t["did"]:
|
2012-12-21 08:51:59 +01:00
|
|
|
s = _(" (on)")
|
|
|
|
else:
|
|
|
|
s = _(" (off)")
|
2017-08-08 11:45:31 +02:00
|
|
|
a = m.addAction(_("Deck Override...") + s)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onTargetDeck)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
|
|
|
a = m.addAction(_("Browser Appearance..."))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onBrowserDisplay)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
m.exec_(self.topAreaForm.templateOptions.mapToGlobal(QPoint(0, 0)))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onBrowserDisplay(self):
|
|
|
|
d = QDialog()
|
|
|
|
f = aqt.forms.browserdisp.Ui_Dialog()
|
|
|
|
f.setupUi(d)
|
2020-05-13 09:24:49 +02:00
|
|
|
t = self.current_template()
|
2019-12-23 01:34:10 +01:00
|
|
|
f.qfmt.setText(t.get("bqfmt", ""))
|
|
|
|
f.afmt.setText(t.get("bafmt", ""))
|
2017-07-29 08:09:00 +02:00
|
|
|
if t.get("bfont"):
|
|
|
|
f.overrideFont.setChecked(True)
|
2019-12-23 01:34:10 +01:00
|
|
|
f.font.setCurrentFont(QFont(t.get("bfont", "Arial")))
|
|
|
|
f.fontSize.setValue(t.get("bsize", 12))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.buttonBox.accepted, lambda: self.onBrowserDisplayOk(f))
|
2012-12-21 08:51:59 +01:00
|
|
|
d.exec_()
|
|
|
|
|
|
|
|
def onBrowserDisplayOk(self, f):
|
2020-05-13 09:24:49 +02:00
|
|
|
t = self.current_template()
|
|
|
|
self.changed = True
|
2019-12-23 01:34:10 +01:00
|
|
|
t["bqfmt"] = f.qfmt.text().strip()
|
|
|
|
t["bafmt"] = f.afmt.text().strip()
|
2017-07-29 08:09:00 +02:00
|
|
|
if f.overrideFont.isChecked():
|
2019-12-23 01:34:10 +01:00
|
|
|
t["bfont"] = f.font.currentFont().family()
|
|
|
|
t["bsize"] = f.fontSize.value()
|
2017-07-29 08:09:00 +02:00
|
|
|
else:
|
2018-02-26 01:21:12 +01:00
|
|
|
for key in ("bfont", "bsize"):
|
|
|
|
if key in t:
|
|
|
|
del t[key]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onTargetDeck(self):
|
|
|
|
from aqt.tagedit import TagEdit
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
t = self.current_template()
|
2012-12-21 08:51:59 +01:00
|
|
|
d = QDialog(self)
|
|
|
|
d.setWindowTitle("Anki")
|
|
|
|
d.setMinimumWidth(400)
|
|
|
|
l = QVBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
lab = QLabel(
|
|
|
|
_(
|
|
|
|
"""\
|
|
|
|
Enter deck to place new %s cards in, or leave blank:"""
|
|
|
|
)
|
2020-05-13 09:24:49 +02:00
|
|
|
% self.current_template()["name"]
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
lab.setWordWrap(True)
|
|
|
|
l.addWidget(lab)
|
|
|
|
te = TagEdit(d, type=1)
|
|
|
|
te.setCol(self.col)
|
|
|
|
l.addWidget(te)
|
2019-12-23 01:34:10 +01:00
|
|
|
if t["did"]:
|
|
|
|
te.setText(self.col.decks.get(t["did"])["name"])
|
2012-12-21 08:51:59 +01:00
|
|
|
te.selectAll()
|
|
|
|
bb = QDialogButtonBox(QDialogButtonBox.Close)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(bb.rejected, d.close)
|
2012-12-21 08:51:59 +01:00
|
|
|
l.addWidget(bb)
|
|
|
|
d.setLayout(l)
|
|
|
|
d.exec_()
|
2020-05-13 09:24:49 +02:00
|
|
|
self.changed = True
|
2012-12-21 08:51:59 +01:00
|
|
|
if not te.text().strip():
|
2019-12-23 01:34:10 +01:00
|
|
|
t["did"] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
t["did"] = self.col.decks.id(te.text())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onAddField(self):
|
|
|
|
diag = QDialog(self)
|
|
|
|
form = aqt.forms.addfield.Ui_Dialog()
|
|
|
|
form.setupUi(diag)
|
2019-12-23 01:34:10 +01:00
|
|
|
fields = [f["name"] for f in self.model["flds"]]
|
2012-12-21 08:51:59 +01:00
|
|
|
form.fields.addItems(fields)
|
|
|
|
form.font.setCurrentFont(QFont("Arial"))
|
|
|
|
form.size.setValue(20)
|
|
|
|
diag.show()
|
|
|
|
# Work around a Qt bug,
|
|
|
|
# https://bugreports.qt-project.org/browse/QTBUG-1894
|
|
|
|
if isMac or isWin:
|
|
|
|
# No problems on Macs or Windows.
|
|
|
|
form.fields.showPopup()
|
|
|
|
else:
|
|
|
|
# Delay showing the pop-up.
|
|
|
|
self.mw.progress.timer(200, form.fields.showPopup, False)
|
|
|
|
if not diag.exec_():
|
|
|
|
return
|
|
|
|
if form.radioQ.isChecked():
|
2017-08-08 07:31:36 +02:00
|
|
|
obj = self.tform.front
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2017-08-08 07:31:36 +02:00
|
|
|
obj = self.tform.back
|
2019-12-23 01:34:10 +01:00
|
|
|
self._addField(
|
|
|
|
obj,
|
|
|
|
fields[form.fields.currentIndex()],
|
|
|
|
form.font.currentFont().family(),
|
|
|
|
form.size.value(),
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def _addField(self, widg, field, font, size):
|
|
|
|
t = widg.toPlainText()
|
2019-12-23 01:34:10 +01:00
|
|
|
t += "\n<div style='font-family: %s; font-size: %spx;'>{{%s}}</div>\n" % (
|
|
|
|
font,
|
|
|
|
size,
|
|
|
|
field,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
widg.setPlainText(t)
|
2020-05-13 09:24:49 +02:00
|
|
|
self.changed = True
|
|
|
|
self.write_edits_to_template_and_redraw()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Closing & Help
|
|
|
|
######################################################################
|
|
|
|
|
2020-04-26 04:34:25 +02:00
|
|
|
def accept(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
def save():
|
2020-04-26 04:34:25 +02:00
|
|
|
self.mm.save(self.model)
|
|
|
|
|
2020-05-13 09:24:49 +02:00
|
|
|
def on_done(fut):
|
|
|
|
try:
|
|
|
|
fut.result()
|
|
|
|
except TemplateError as e:
|
|
|
|
showWarning("Unable to save changes: " + str(e))
|
|
|
|
return
|
|
|
|
self.mw.reset()
|
|
|
|
tooltip("Changes saved.", parent=self.mw)
|
|
|
|
self.cleanup()
|
|
|
|
return QDialog.accept(self)
|
|
|
|
|
|
|
|
self.mw.taskman.with_progress(save, on_done)
|
2020-04-26 04:34:25 +02:00
|
|
|
|
|
|
|
def reject(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
if self.changed:
|
|
|
|
if not askUser("Discard changes?"):
|
|
|
|
return
|
2020-04-26 04:34:25 +02:00
|
|
|
self.cleanup()
|
|
|
|
return QDialog.reject(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-04-26 04:34:25 +02:00
|
|
|
def cleanup(self) -> None:
|
2017-08-08 08:02:55 +02:00
|
|
|
self.cancelPreviewTimer()
|
2020-01-20 11:10:38 +01:00
|
|
|
av_player.stop_and_clear_queue()
|
2012-12-21 08:51:59 +01:00
|
|
|
saveGeom(self, "CardLayout")
|
2019-03-26 03:31:40 +01:00
|
|
|
self.pform.frontWeb = None
|
2020-05-13 09:24:49 +02:00
|
|
|
self.model = None
|
|
|
|
self.rendered_card = None
|
|
|
|
self.mw = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onHelp(self):
|
|
|
|
openHelp("templates")
|