# Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html from __future__ import annotations import json import re from concurrent.futures import Future from typing import Any, Match, Optional, cast import aqt import aqt.forms import aqt.operations from anki import stdmodels from anki.collection import OpChanges from anki.consts import * from anki.lang import with_collapsed_whitespace, without_unicode_isolation from anki.notes import Note from anki.notetypes_pb2 import StockNotetype from aqt import AnkiQt, gui_hooks from aqt.forms import browserdisp from aqt.operations.notetype import restore_notetype_to_stock, update_notetype_legacy from aqt.qt import * from aqt.schema_change_tracker import ChangeTracker from aqt.sound import av_player, play_clicked_audio from aqt.theme import theme_manager from aqt.utils import ( HelpPage, askUser, disable_help_button, downArrow, getOnlyText, openHelp, restoreGeom, restoreSplitter, saveGeom, saveSplitter, shortcut, showInfo, tooltip, tr, ) from aqt.webview import AnkiWebView, AnkiWebViewKind class CardLayout(QDialog): def __init__( self, mw: AnkiQt, note: Note, ord: int = 0, parent: Optional[QWidget] = None, fill_empty: bool = False, ) -> None: QDialog.__init__(self, parent or mw, Qt.WindowType.Window) mw.garbage_collect_on_dialog_finish(self) self.mw = aqt.mw self.note = note self.ord = ord self.col = self.mw.col.weakref() self.mm = self.mw.col.models self.model = note.note_type() self.templates = self.model["tmpls"] self.fill_empty_action_toggled = fill_empty self.night_mode_is_enabled = theme_manager.night_mode self.mobile_emulation_enabled = False self.have_autoplayed = False self.mm._remove_from_cache(self.model["id"]) self.change_tracker = ChangeTracker(self.mw) self.setupTopArea() self.setupMainArea() self.setupButtons() self.setupShortcuts() self.setWindowTitle( without_unicode_isolation( tr.card_templates_card_types_for(val=self.model["name"]) ) ) disable_help_button(self) v1 = QVBoxLayout() v1.addWidget(self.topArea) v1.addWidget(self.mainArea) v1.addLayout(self.buttons) v1.setContentsMargins(12, 12, 12, 12) self.setLayout(v1) gui_hooks.card_layout_will_show(self) self.redraw_everything() restoreGeom(self, "CardLayout") restoreSplitter(self.mainArea, "CardLayoutMainArea") self.setWindowModality(Qt.WindowModality.ApplicationModal) self.show() # take the focus away from the first input area when starting up, # as users tend to accidentally type into the template self.setFocus() def redraw_everything(self) -> None: self.ignore_change_signals = True self.updateTopArea() self.ignore_change_signals = False self.update_current_ordinal_and_redraw(self.ord) def update_current_ordinal_and_redraw(self, idx: int) -> None: if self.ignore_change_signals: return self.ord = idx self.have_autoplayed = False self.fill_fields_from_template() self.renderPreview() def _isCloze(self) -> bool: return self.model["type"] == MODEL_CLOZE # Top area ########################################################################## def setupTopArea(self) -> None: self.topArea = QWidget() self.topArea.setSizePolicy( QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum ) self.topAreaForm = aqt.forms.clayout_top.Ui_Form() self.topAreaForm.setupUi(self.topArea) self.topAreaForm.templateOptions.setText( f"{tr.actions_options()} {downArrow()}" ) qconnect(self.topAreaForm.templateOptions.clicked, self.onMore) qconnect( self.topAreaForm.templatesBox.currentIndexChanged, self.update_current_ordinal_and_redraw, ) self.topAreaForm.card_type_label.setText(tr.card_templates_card_type()) def updateTopArea(self) -> None: self.updateCardNames() def updateCardNames(self) -> None: self.ignore_change_signals = True combo = self.topAreaForm.templatesBox combo.clear() combo.addItems( self._summarizedName(idx, tmpl) for (idx, tmpl) in enumerate(self.templates) ) combo.setCurrentIndex(self.ord) combo.setEnabled(not self._isCloze()) self.ignore_change_signals = False def _summarizedName(self, idx: int, tmpl: dict) -> str: return "{}: {}: {} -> {}".format( idx + 1, tmpl["name"], self._fieldsOnTemplate(tmpl["qfmt"]), self._fieldsOnTemplate(tmpl["afmt"]), ) def _fieldsOnTemplate(self, fmt: str) -> str: matches = re.findall("{{[^#/}]+?}}", fmt) chars_allowed = 30 field_names: list[str] = [] 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 field_names.append(m) chars_allowed -= len(m) if chars_allowed <= 0: break s = "+".join(field_names) if chars_allowed <= 0: s += "+..." return s def setupShortcuts(self) -> None: self.tform.front_button.setToolTip(shortcut("Ctrl+1")) self.tform.back_button.setToolTip(shortcut("Ctrl+2")) self.tform.style_button.setToolTip(shortcut("Ctrl+3")) QShortcut( # type: ignore QKeySequence("Ctrl+1"), self, activated=self.tform.front_button.click, ) QShortcut( # type: ignore QKeySequence("Ctrl+2"), self, activated=self.tform.back_button.click, ) QShortcut( # type: ignore QKeySequence("Ctrl+3"), self, activated=self.tform.style_button.click, ) QShortcut( # type: ignore QKeySequence("F3"), self, activated=lambda: ( self.update_current_ordinal_and_redraw(self.ord - 1) if self.ord - 1 > -1 else None ), ) QShortcut( # type: ignore QKeySequence("F4"), self, activated=lambda: ( self.update_current_ordinal_and_redraw(self.ord + 1) if self.ord + 1 < len(self.templates) else None ), ) for i in range(min(len(self.cloze_numbers), 9)): QShortcut( # type: ignore QKeySequence(f"Alt+{i+1}"), self, activated=lambda n=i: self.pform.cloze_number_combo.setCurrentIndex(n), ) # Main area setup ########################################################################## def setupMainArea(self) -> None: split = self.mainArea = QSplitter() split.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) split.setOrientation(Qt.Orientation.Horizontal) left = QWidget() tform = self.tform = aqt.forms.template.Ui_Form() tform.setupUi(left) self.setup_edit_area() split.addWidget(left) split.setCollapsible(0, False) right = QWidget() self.pform = aqt.forms.preview.Ui_Form() pform = self.pform pform.setupUi(right) pform.preview_front.setText(tr.card_templates_front_preview()) pform.preview_back.setText(tr.card_templates_back_preview()) pform.preview_box.setTitle(tr.card_templates_preview_box()) self.setup_preview() split.addWidget(right) split.setCollapsible(1, False) def setup_edit_area(self) -> None: tform = self.tform editor = tform.edit_area tform.front_button.setText(tr.card_templates_front_template()) tform.back_button.setText(tr.card_templates_back_template()) tform.style_button.setText(tr.card_templates_template_styling()) tform.template_box.setTitle(tr.card_templates_template_box()) cnt = self.mw.col.models.use_count(self.model) tform.changes_affect_label.setText( self.col.tr.card_templates_changes_will_affect_notes(count=cnt) ) qconnect(editor.textChanged, self.write_edits_to_template_and_redraw) qconnect(tform.front_button.clicked, self.on_editor_toggled) qconnect(tform.back_button.clicked, self.on_editor_toggled) qconnect(tform.style_button.clicked, self.on_editor_toggled) self.current_editor_index = 0 editor.setAcceptRichText(False) editor.setFont(QFont("Courier")) tab_width = self.fontMetrics().horizontalAdvance(" " * 4) editor.setTabStopDistance(tab_width) palette = editor.palette() palette.setColor( QPalette.ColorGroup.Inactive, QPalette.ColorRole.Highlight, QColor("#4169e1" if theme_manager.night_mode else "#FFFF80"), ) palette.setColor( QPalette.ColorGroup.Inactive, QPalette.ColorRole.HighlightedText, QColor("#ffffff" if theme_manager.night_mode else "#000000"), ) editor.setPalette(palette) widg = tform.search_edit widg.setPlaceholderText("Search") qconnect(widg.textChanged, self.on_search_changed) qconnect(widg.returnPressed, self.on_search_next) def setup_cloze_number_box(self) -> None: names = (tr.card_templates_cloze(val=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 on_change_cloze(self, idx: int) -> None: self.ord = self.cloze_numbers[idx] - 1 self.have_autoplayed = False self._renderPreview() def on_editor_toggled(self) -> None: if self.tform.front_button.isChecked(): self.current_editor_index = 0 self.pform.preview_front.setChecked(True) self.on_preview_toggled() self.add_field_button.setHidden(False) elif self.tform.back_button.isChecked(): self.current_editor_index = 1 self.pform.preview_back.setChecked(True) self.on_preview_toggled() self.add_field_button.setHidden(False) else: self.current_editor_index = 2 self.add_field_button.setHidden(True) self.fill_fields_from_template() def on_search_changed(self, text: str) -> None: editor = self.tform.edit_area if not editor.find(text): # try again from top cursor = editor.textCursor() cursor.movePosition(QTextCursor.MoveOperation.Start) editor.setTextCursor(cursor) if not editor.find(text): tooltip("No matches found.") def on_search_next(self) -> None: text = self.tform.search_edit.text() self.on_search_changed(text) def setup_preview(self) -> None: pform = self.pform self.preview_web = AnkiWebView(kind=AnkiWebViewKind.CARD_LAYOUT) pform.verticalLayout.addWidget(self.preview_web) pform.verticalLayout.setStretch(1, 99) pform.preview_front.isChecked() qconnect(pform.preview_front.clicked, self.on_preview_toggled) qconnect(pform.preview_back.clicked, self.on_preview_toggled) pform.preview_settings.setText( f"{tr.card_templates_preview_settings()} {downArrow()}" ) qconnect(pform.preview_settings.clicked, self.on_preview_settings) self.preview_web.stdHtml( self.mw.reviewer.revHtml(), css=["css/reviewer.css"], js=[ "js/mathjax.js", "js/vendor/mathjax/tex-chtml-full.js", "js/reviewer.js", ], context=self, ) self.preview_web.allow_drops = True self.preview_web.eval("_blockDefaultDragDropBehavior();") self.preview_web.set_bridge_command(self._on_bridge_cmd, self) gui_hooks.card_review_webview_did_init( self.preview_web, AnkiWebViewKind.CARD_LAYOUT ) if self._isCloze(): nums = list(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) def on_fill_empty_action_toggled(self) -> None: self.fill_empty_action_toggled = not self.fill_empty_action_toggled self.on_preview_toggled() def on_night_mode_action_toggled(self) -> None: self.night_mode_is_enabled = not self.night_mode_is_enabled force = json.dumps(self.night_mode_is_enabled) self.preview_web.eval( f"document.documentElement.classList.toggle('night-mode', {force});" ) self.on_preview_toggled() def on_mobile_class_action_toggled(self) -> None: self.mobile_emulation_enabled = not self.mobile_emulation_enabled self.on_preview_toggled() def on_preview_settings(self) -> None: m = QMenu(self) a = m.addAction(tr.card_templates_fill_empty()) a.setCheckable(True) a.setChecked(self.fill_empty_action_toggled) qconnect(a.triggered, self.on_fill_empty_action_toggled) if not self.note_has_empty_field(): a.setVisible(False) a = m.addAction(tr.card_templates_night_mode()) a.setCheckable(True) a.setChecked(self.night_mode_is_enabled) qconnect(a.triggered, self.on_night_mode_action_toggled) a = m.addAction(tr.card_templates_add_mobile_class()) a.setCheckable(True) a.setChecked(self.mobile_emulation_enabled) qconnect(a.toggled, self.on_mobile_class_action_toggled) m.popup(self.pform.preview_settings.mapToGlobal(QPoint(0, 0))) def on_preview_toggled(self) -> None: self.have_autoplayed = False self._renderPreview() def _on_bridge_cmd(self, cmd: str) -> Any: if cmd.startswith("play:"): play_clicked_audio(cmd, self.rendered_card) def note_has_empty_field(self) -> bool: for field in self.note.fields: if not field.strip(): # ignores HTML, but this should suffice return True return False # Buttons ########################################################################## def setupButtons(self) -> None: l = self.buttons = QHBoxLayout() help = QPushButton(tr.actions_help()) help.setAutoDefault(False) l.addWidget(help) qconnect(help.clicked, self.onHelp) l.addStretch() self.add_field_button = QPushButton(tr.fields_add_field()) self.add_field_button.setAutoDefault(False) l.addWidget(self.add_field_button) qconnect(self.add_field_button.clicked, self.onAddField) if not self._isCloze(): flip = QPushButton(tr.card_templates_flip()) flip.setAutoDefault(False) l.addWidget(flip) qconnect(flip.clicked, self.onFlip) l.addStretch() save = QPushButton(tr.actions_save()) save.setAutoDefault(False) save.setShortcut(QKeySequence("Ctrl+Return")) l.addWidget(save) qconnect(save.clicked, self.accept) close = QPushButton(tr.actions_cancel()) close.setAutoDefault(False) l.addWidget(close) qconnect(close.clicked, self.reject) # Reading/writing question/answer/css ########################################################################## def current_template(self) -> dict: if self._isCloze(): return self.templates[0] return self.templates[self.ord] def fill_fields_from_template(self) -> None: t = self.current_template() self.ignore_change_signals = True if self.current_editor_index == 0: text = t["qfmt"] elif self.current_editor_index == 1: text = t["afmt"] else: text = self.model["css"] self.tform.edit_area.setPlainText(text) self.ignore_change_signals = False def write_edits_to_template_and_redraw(self) -> None: if self.ignore_change_signals: return self.change_tracker.mark_basic() text = self.tform.edit_area.toPlainText() if self.current_editor_index == 0: self.current_template()["qfmt"] = text elif self.current_editor_index == 1: self.current_template()["afmt"] = text else: self.model["css"] = text self.renderPreview() # Preview ########################################################################## _previewTimer: Optional[QTimer] = None def renderPreview(self) -> None: # schedule a preview when timing stops self.cancelPreviewTimer() self._previewTimer = self.mw.progress.timer( 200, self._renderPreview, False, parent=self ) def cancelPreviewTimer(self) -> None: if self._previewTimer: self._previewTimer.stop() self._previewTimer = None def _renderPreview(self) -> None: self.cancelPreviewTimer() c = self.rendered_card = self.note.ephemeral_card( self.ord, custom_note_type=self.model, custom_template=self.current_template(), fill_empty=self.fill_empty_action_toggled, ) ti = self.maybeTextInput bodyclass = theme_manager.body_classes_for_card_ord( c.ord, self.night_mode_is_enabled ) if self.pform.preview_front.isChecked(): q = ti(self.mw.prepare_card_text_for_display(c.question())) q = gui_hooks.card_will_show(q, c, "clayoutQuestion") text = q else: a = ti(self.mw.prepare_card_text_for_display(c.answer()), type="a") a = gui_hooks.card_will_show(a, c, "clayoutAnswer") text = a # use _showAnswer to avoid the longer delay self.preview_web.eval(f"_showAnswer({json.dumps(text)},'{bodyclass}');") self.preview_web.eval( f"_emulateMobile({json.dumps(self.mobile_emulation_enabled)});" ) if not self.have_autoplayed: self.have_autoplayed = True if c.autoplay(): self.preview_web.setPlaybackRequiresGesture(False) if self.pform.preview_front.isChecked(): audio = c.question_av_tags() else: audio = c.answer_av_tags() else: audio = [] self.preview_web.setPlaybackRequiresGesture(True) side = "question" if self.pform.preview_front.isChecked() else "answer" gui_hooks.av_player_will_play_tags( audio, side, self, ) av_player.play_tags(audio) self.updateCardNames() def maybeTextInput(self, txt: str, type: str = "q") -> str: if "[[type:" not in txt: return txt origLen = len(txt) txt = txt.replace("