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
|
2023-04-18 06:07:51 +02:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-20 10:19:03 +01:00
|
|
|
import json
|
2012-12-21 08:51:59 +01:00
|
|
|
import re
|
2021-02-02 14:30:53 +01:00
|
|
|
from concurrent.futures import Future
|
2023-04-18 06:07:51 +02:00
|
|
|
from typing import Any, Match, Optional, cast
|
2013-10-20 03:02:29 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2022-02-13 04:40:47 +01:00
|
|
|
import aqt.forms
|
|
|
|
import aqt.operations
|
2023-04-18 06:07:51 +02:00
|
|
|
from anki import stdmodels
|
2021-04-30 09:30:48 +02:00
|
|
|
from anki.collection import OpChanges
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.consts import *
|
2023-04-18 09:10:51 +02:00
|
|
|
from anki.lang import with_collapsed_whitespace, without_unicode_isolation
|
2020-05-05 03:10:24 +02:00
|
|
|
from anki.notes import Note
|
2023-04-18 06:07:51 +02:00
|
|
|
from anki.notetypes_pb2 import StockNotetype
|
2020-05-05 03:10:24 +02:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
Move away from Bazel (#2202)
(for upgrading users, please see the notes at the bottom)
Bazel brought a lot of nice things to the table, such as rebuilds based on
content changes instead of modification times, caching of build products,
detection of incorrect build rules via a sandbox, and so on. Rewriting the build
in Bazel was also an opportunity to improve on the Makefile-based build we had
prior, which was pretty poor: most dependencies were external or not pinned, and
the build graph was poorly defined and mostly serialized. It was not uncommon
for fresh checkouts to fail due to floating dependencies, or for things to break
when trying to switch to an older commit.
For day-to-day development, I think Bazel served us reasonably well - we could
generally switch between branches while being confident that builds would be
correct and reasonably fast, and not require full rebuilds (except on Windows,
where the lack of a sandbox and the TS rules would cause build breakages when TS
files were renamed/removed).
Bazel achieves that reliability by defining rules for each programming language
that define how source files should be turned into outputs. For the rules to
work with Bazel's sandboxing approach, they often have to reimplement or
partially bypass the standard tools that each programming language provides. The
Rust rules call Rust's compiler directly for example, instead of using Cargo,
and the Python rules extract each PyPi package into a separate folder that gets
added to sys.path.
These separate language rules allow proper declaration of inputs and outputs,
and offer some advantages such as caching of build products and fine-grained
dependency installation. But they also bring some downsides:
- The rules don't always support use-cases/platforms that the standard language
tools do, meaning they need to be patched to be used. I've had to contribute a
number of patches to the Rust, Python and JS rules to unblock various issues.
- The dependencies we use with each language sometimes make assumptions that do
not hold in Bazel, meaning they either need to be pinned or patched, or the
language rules need to be adjusted to accommodate them.
I was hopeful that after the initial setup work, things would be relatively
smooth-sailing. Unfortunately, that has not proved to be the case. Things
frequently broke when dependencies or the language rules were updated, and I
began to get frustrated at the amount of Anki development time I was instead
spending on build system upkeep. It's now about 2 years since switching to
Bazel, and I think it's time to cut losses, and switch to something else that's
a better fit.
The new build system is based on a small build tool called Ninja, and some
custom Rust code in build/. This means that to build Anki, Bazel is no longer
required, but Ninja and Rust need to be installed on your system. Python and
Node toolchains are automatically downloaded like in Bazel.
This new build system should result in faster builds in some cases:
- Because we're using cargo to build now, Rust builds are able to take advantage
of pipelining and incremental debug builds, which we didn't have with Bazel.
It's also easier to override the default linker on Linux/macOS, which can
further improve speeds.
- External Rust crates are now built with opt=1, which improves performance
of debug builds.
- Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript
compiler. This results in faster builds, by deferring typechecking to test/check
time, and by allowing more work to happen in parallel.
As an example of the differences, when testing with the mold linker on Linux,
adding a new message to tags.proto (which triggers a recompile of the bulk of
the Rust and TypeScript code) results in a compile that goes from about 22s on
Bazel to about 7s in the new system. With the standard linker, it's about 9s.
Some other changes of note:
- Our Rust workspace now uses cargo-hakari to ensure all packages agree on
available features, preventing unnecessary rebuilds.
- pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge
source files and generated files into a single folder for running. By telling
VSCode about the extra search path, code completion now works with generated
files without needing to symlink them into the source folder.
- qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py.
Instead, the generated files are now placed in a separate _aqt package that's
added to the path.
- ts/lib is now exposed as @tslib, so the source code and generated code can be
provided under the same namespace without a merging step.
- MyPy and PyLint are now invoked once for the entire codebase.
- dprint will be used to format TypeScript/json files in the future instead of
the slower prettier (currently turned off to avoid causing conflicts). It can
automatically defer to prettier when formatting Svelte files.
- svelte-check is now used for typechecking our Svelte code, which revealed a
few typing issues that went undetected with the old system.
- The Jest unit tests now work on Windows as well.
If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes:
- please remove node_modules and .bazel
- install rustup (https://rustup.rs/)
- install rsync if not already installed (on windows, use pacman - see docs/windows.md)
- install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and
place on your path, or from your distro/homebrew if it's 1.10+)
- update .vscode/settings.json from .vscode.dist
2022-11-27 06:24:20 +01:00
|
|
|
from aqt.forms import browserdisp
|
2023-04-18 06:07:51 +02:00
|
|
|
from aqt.operations.notetype import restore_notetype_to_stock, update_notetype_legacy
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2020-05-15 05:59:44 +02:00
|
|
|
from aqt.schema_change_tracker import ChangeTracker
|
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 (
|
2021-01-25 14:45:47 +01:00
|
|
|
HelpPage,
|
2019-12-23 01:34:10 +01:00
|
|
|
askUser,
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button,
|
2019-12-23 01:34:10 +01:00
|
|
|
downArrow,
|
|
|
|
getOnlyText,
|
|
|
|
openHelp,
|
|
|
|
restoreGeom,
|
2020-05-31 01:31:34 +02:00
|
|
|
restoreSplitter,
|
2019-12-23 01:34:10 +01:00
|
|
|
saveGeom,
|
2020-05-31 01:31:34 +02:00
|
|
|
saveSplitter,
|
2020-05-14 12:58:45 +02:00
|
|
|
shortcut,
|
2019-12-23 01:34:10 +01:00
|
|
|
showInfo,
|
2020-05-13 09:24:49 +02:00
|
|
|
tooltip,
|
2020-05-14 12:58:45 +02:00
|
|
|
tr,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2023-02-10 05:53:11 +01:00
|
|
|
from aqt.webview import AnkiWebView, AnkiWebViewKind
|
2019-12-20 10:19:03 +01: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-14 12:58:45 +02:00
|
|
|
self,
|
|
|
|
mw: AnkiQt,
|
|
|
|
note: Note,
|
2021-02-02 14:30:53 +01:00
|
|
|
ord: int = 0,
|
2020-05-14 12:58:45 +02:00
|
|
|
parent: Optional[QWidget] = None,
|
|
|
|
fill_empty: bool = False,
|
2021-02-02 14:30:53 +01:00
|
|
|
) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
QDialog.__init__(self, parent or mw, Qt.WindowType.Window)
|
2021-03-05 04:07:52 +01:00
|
|
|
mw.garbage_collect_on_dialog_finish(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
|
2021-06-27 04:12:23 +02:00
|
|
|
self.model = note.note_type()
|
2020-05-13 09:24:49 +02:00
|
|
|
self.templates = self.model["tmpls"]
|
2020-07-30 14:39:02 +02:00
|
|
|
self.fill_empty_action_toggled = fill_empty
|
2021-11-24 22:17:41 +01:00
|
|
|
self.night_mode_is_enabled = theme_manager.night_mode
|
2020-08-03 05:30:34 +02:00
|
|
|
self.mobile_emulation_enabled = False
|
2020-05-15 06:26:00 +02:00
|
|
|
self.have_autoplayed = False
|
2020-05-13 09:24:49 +02:00
|
|
|
self.mm._remove_from_cache(self.model["id"])
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker = ChangeTracker(self.mw)
|
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()
|
2020-11-17 08:42:43 +01:00
|
|
|
self.setWindowTitle(
|
2020-11-18 16:03:04 +01:00
|
|
|
without_unicode_isolation(
|
2021-03-26 05:21:04 +01:00
|
|
|
tr.card_templates_card_types_for(val=self.model["name"])
|
2020-11-18 16:03:04 +01:00
|
|
|
)
|
2020-11-17 08:42:43 +01:00
|
|
|
)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
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")
|
2020-05-31 01:31:34 +02:00
|
|
|
restoreSplitter(self.mainArea, "CardLayoutMainArea")
|
2021-10-05 05:53:01 +02:00
|
|
|
self.setWindowModality(Qt.WindowModality.ApplicationModal)
|
2016-07-07 04:32:27 +02:00
|
|
|
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
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def redraw_everything(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
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-06-25 16:27:52 +02:00
|
|
|
def update_current_ordinal_and_redraw(self, idx: int) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
if self.ignore_change_signals:
|
|
|
|
return
|
|
|
|
self.ord = idx
|
2020-05-15 06:26:00 +02:00
|
|
|
self.have_autoplayed = False
|
2020-05-13 09:24:49 +02:00
|
|
|
self.fill_fields_from_template()
|
|
|
|
self.renderPreview()
|
2017-08-12 09:29:47 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def _isCloze(self) -> bool:
|
2020-05-13 09:24:49 +02:00
|
|
|
return self.model["type"] == MODEL_CLOZE
|
|
|
|
|
|
|
|
# Top area
|
|
|
|
##########################################################################
|
2017-08-12 09:29:47 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setupTopArea(self) -> None:
|
2017-08-08 11:45:31 +02:00
|
|
|
self.topArea = QWidget()
|
2021-10-05 05:53:01 +02:00
|
|
|
self.topArea.setSizePolicy(
|
|
|
|
QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum
|
|
|
|
)
|
2017-08-08 11:45:31 +02:00
|
|
|
self.topAreaForm = aqt.forms.clayout_top.Ui_Form()
|
|
|
|
self.topAreaForm.setupUi(self.topArea)
|
2020-11-17 08:42:43 +01:00
|
|
|
self.topAreaForm.templateOptions.setText(
|
2021-03-26 04:48:26 +01:00
|
|
|
f"{tr.actions_options()} {downArrow()}"
|
2020-11-17 08:42:43 +01:00
|
|
|
)
|
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,
|
|
|
|
)
|
2021-03-26 04:48:26 +01:00
|
|
|
self.topAreaForm.card_type_label.setText(tr.card_templates_card_type())
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def updateTopArea(self) -> None:
|
2017-08-09 03:11:39 +02:00
|
|
|
self.updateCardNames()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def updateCardNames(self) -> None:
|
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
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def _summarizedName(self, idx: int, tmpl: dict) -> str:
|
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
|
|
|
|
2020-06-25 16:27:52 +02:00
|
|
|
def _fieldsOnTemplate(self, fmt: str) -> str:
|
2017-08-09 03:11:39 +02:00
|
|
|
matches = re.findall("{{[^#/}]+?}}", fmt)
|
2020-05-13 09:24:49 +02:00
|
|
|
chars_allowed = 30
|
2021-10-03 10:59:42 +02:00
|
|
|
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
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setupShortcuts(self) -> None:
|
2020-05-14 09:26:40 +02:00
|
|
|
self.tform.front_button.setToolTip(shortcut("Ctrl+1"))
|
|
|
|
self.tform.back_button.setToolTip(shortcut("Ctrl+2"))
|
|
|
|
self.tform.style_button.setToolTip(shortcut("Ctrl+3"))
|
2020-05-18 10:08:57 +02:00
|
|
|
QShortcut( # type: ignore
|
2020-08-31 05:29:28 +02:00
|
|
|
QKeySequence("Ctrl+1"),
|
|
|
|
self,
|
|
|
|
activated=self.tform.front_button.click,
|
2020-05-18 10:08:57 +02:00
|
|
|
)
|
|
|
|
QShortcut( # type: ignore
|
2020-08-31 05:29:28 +02:00
|
|
|
QKeySequence("Ctrl+2"),
|
|
|
|
self,
|
|
|
|
activated=self.tform.back_button.click,
|
2020-05-18 10:08:57 +02:00
|
|
|
)
|
|
|
|
QShortcut( # type: ignore
|
2020-08-31 05:29:28 +02:00
|
|
|
QKeySequence("Ctrl+3"),
|
|
|
|
self,
|
|
|
|
activated=self.tform.style_button.click,
|
2020-05-18 10:08:57 +02:00
|
|
|
)
|
2021-07-12 03:40:43 +02:00
|
|
|
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
|
|
|
|
),
|
|
|
|
)
|
2021-02-19 08:40:30 +01:00
|
|
|
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),
|
|
|
|
)
|
2020-05-13 09:24:49 +02:00
|
|
|
|
2020-05-14 12:58:45 +02:00
|
|
|
# Main area setup
|
2020-05-13 09:24:49 +02:00
|
|
|
##########################################################################
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setupMainArea(self) -> None:
|
2020-05-31 00:54:25 +02:00
|
|
|
split = self.mainArea = QSplitter()
|
2021-10-05 05:53:01 +02:00
|
|
|
split.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
|
|
|
|
split.setOrientation(Qt.Orientation.Horizontal)
|
2012-12-21 08:51:59 +01:00
|
|
|
left = QWidget()
|
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)
|
2022-01-27 00:40:33 +01:00
|
|
|
self.setup_edit_area()
|
2020-05-31 00:54:25 +02:00
|
|
|
split.addWidget(left)
|
|
|
|
split.setCollapsible(0, False)
|
2020-05-14 09:26:40 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
right = QWidget()
|
2020-05-14 12:58:45 +02:00
|
|
|
self.pform = aqt.forms.preview.Ui_Form()
|
2020-05-04 05:23:08 +02:00
|
|
|
pform = self.pform
|
2012-12-21 08:51:59 +01:00
|
|
|
pform.setupUi(right)
|
2021-03-26 04:48:26 +01:00
|
|
|
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())
|
2020-05-14 07:24:29 +02:00
|
|
|
|
2020-05-14 12:58:45 +02:00
|
|
|
self.setup_preview()
|
2020-05-31 00:54:25 +02:00
|
|
|
split.addWidget(right)
|
|
|
|
split.setCollapsible(1, False)
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setup_edit_area(self) -> None:
|
2020-05-14 12:58:45 +02:00
|
|
|
tform = self.tform
|
2022-01-27 00:40:33 +01:00
|
|
|
editor = tform.edit_area
|
2020-05-14 12:58:45 +02:00
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
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())
|
2023-01-19 12:14:52 +01:00
|
|
|
tform.template_box.setTitle(tr.card_templates_template_box())
|
2020-05-14 12:58:45 +02:00
|
|
|
|
2021-06-27 05:49:58 +02:00
|
|
|
cnt = self.mw.col.models.use_count(self.model)
|
2022-01-27 00:40:33 +01:00
|
|
|
tform.changes_affect_label.setText(
|
2021-03-26 05:21:04 +01:00
|
|
|
self.col.tr.card_templates_changes_will_affect_notes(count=cnt)
|
2020-05-14 12:58:45 +02:00
|
|
|
)
|
|
|
|
|
2022-01-27 00:40:33 +01:00
|
|
|
qconnect(editor.textChanged, self.write_edits_to_template_and_redraw)
|
2020-05-19 23:32:47 +02:00
|
|
|
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)
|
2020-05-14 12:58:45 +02:00
|
|
|
|
2020-05-14 09:26:40 +02:00
|
|
|
self.current_editor_index = 0
|
2022-01-27 00:40:33 +01:00
|
|
|
editor.setAcceptRichText(False)
|
|
|
|
editor.setFont(QFont("Courier"))
|
2021-10-05 03:02:18 +02:00
|
|
|
tab_width = self.fontMetrics().horizontalAdvance(" " * 4)
|
2022-01-27 00:40:33 +01:00
|
|
|
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)
|
2020-05-14 09:26:40 +02:00
|
|
|
|
2020-05-14 12:58:45 +02:00
|
|
|
widg = tform.search_edit
|
|
|
|
widg.setPlaceholderText("Search")
|
|
|
|
qconnect(widg.textChanged, self.on_search_changed)
|
|
|
|
qconnect(widg.returnPressed, self.on_search_next)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setup_cloze_number_box(self) -> None:
|
2021-03-26 05:21:04 +01:00
|
|
|
names = (tr.card_templates_cloze(val=n) for n in self.cloze_numbers)
|
2020-05-14 07:24:29 +02:00
|
|
|
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
|
2020-05-15 06:26:00 +02:00
|
|
|
self.have_autoplayed = False
|
2020-05-14 07:24:29 +02:00
|
|
|
self._renderPreview()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def on_editor_toggled(self) -> None:
|
2020-05-14 09:26:40 +02:00
|
|
|
if self.tform.front_button.isChecked():
|
|
|
|
self.current_editor_index = 0
|
2020-05-14 07:24:29 +02:00
|
|
|
self.pform.preview_front.setChecked(True)
|
2020-05-19 23:32:47 +02:00
|
|
|
self.on_preview_toggled()
|
2020-05-14 10:01:15 +02:00
|
|
|
self.add_field_button.setHidden(False)
|
2020-05-14 09:26:40 +02:00
|
|
|
elif self.tform.back_button.isChecked():
|
|
|
|
self.current_editor_index = 1
|
2020-05-14 07:24:29 +02:00
|
|
|
self.pform.preview_back.setChecked(True)
|
2020-05-19 23:32:47 +02:00
|
|
|
self.on_preview_toggled()
|
2020-05-14 10:01:15 +02:00
|
|
|
self.add_field_button.setHidden(False)
|
2020-05-14 09:26:40 +02:00
|
|
|
else:
|
|
|
|
self.current_editor_index = 2
|
2020-05-14 10:01:15 +02:00
|
|
|
self.add_field_button.setHidden(True)
|
2020-05-14 09:26:40 +02:00
|
|
|
|
|
|
|
self.fill_fields_from_template()
|
2020-05-14 07:24:29 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def on_search_changed(self, text: str) -> None:
|
2020-05-14 09:26:40 +02:00
|
|
|
editor = self.tform.edit_area
|
2020-05-14 07:24:29 +02:00
|
|
|
if not editor.find(text):
|
|
|
|
# try again from top
|
|
|
|
cursor = editor.textCursor()
|
2021-10-05 05:53:01 +02:00
|
|
|
cursor.movePosition(QTextCursor.MoveOperation.Start)
|
2020-05-14 07:24:29 +02:00
|
|
|
editor.setTextCursor(cursor)
|
2020-05-22 02:43:28 +02:00
|
|
|
if not editor.find(text):
|
|
|
|
tooltip("No matches found.")
|
2020-05-14 07:24:29 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def on_search_next(self) -> None:
|
2020-05-14 09:26:40 +02:00
|
|
|
text = self.tform.search_edit.text()
|
|
|
|
self.on_search_changed(text)
|
2020-05-14 07:24:29 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setup_preview(self) -> None:
|
2017-08-08 08:02:55 +02:00
|
|
|
pform = self.pform
|
2023-02-10 05:53:11 +01:00
|
|
|
self.preview_web = AnkiWebView(kind=AnkiWebViewKind.CARD_LAYOUT)
|
2020-05-14 12:58:45 +02:00
|
|
|
pform.verticalLayout.addWidget(self.preview_web)
|
2020-05-14 07:24:29 +02:00
|
|
|
pform.verticalLayout.setStretch(1, 99)
|
|
|
|
pform.preview_front.isChecked()
|
2020-05-19 23:32:47 +02:00
|
|
|
qconnect(pform.preview_front.clicked, self.on_preview_toggled)
|
|
|
|
qconnect(pform.preview_back.clicked, self.on_preview_toggled)
|
2020-07-30 17:04:50 +02:00
|
|
|
pform.preview_settings.setText(
|
2021-03-26 04:48:26 +01:00
|
|
|
f"{tr.card_templates_preview_settings()} {downArrow()}"
|
2020-07-30 17:04:50 +02:00
|
|
|
)
|
|
|
|
qconnect(pform.preview_settings.clicked, self.on_preview_settings)
|
2020-07-30 14:39:02 +02:00
|
|
|
|
2020-05-14 12:58:45 +02:00
|
|
|
self.preview_web.stdHtml(
|
2020-08-31 05:29:28 +02:00
|
|
|
self.mw.reviewer.revHtml(),
|
2020-11-01 05:26:58 +01:00
|
|
|
css=["css/reviewer.css"],
|
2021-07-16 16:33:12 +02:00
|
|
|
js=[
|
|
|
|
"js/mathjax.js",
|
2023-11-20 07:50:55 +01:00
|
|
|
"js/vendor/mathjax/tex-chtml-full.js",
|
2021-07-16 16:33:12 +02:00
|
|
|
"js/reviewer.js",
|
|
|
|
],
|
2020-08-31 05:29:28 +02:00
|
|
|
context=self,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2022-06-10 15:33:53 +02:00
|
|
|
self.preview_web.allow_drops = True
|
|
|
|
self.preview_web.eval("_blockDefaultDragDropBehavior();")
|
2020-05-14 12:58:45 +02:00
|
|
|
self.preview_web.set_bridge_command(self._on_bridge_cmd, self)
|
|
|
|
|
2023-02-13 05:50:26 +01:00
|
|
|
gui_hooks.card_review_webview_did_init(
|
|
|
|
self.preview_web, AnkiWebViewKind.CARD_LAYOUT
|
|
|
|
)
|
|
|
|
|
2020-05-14 12:58:45 +02:00
|
|
|
if self._isCloze():
|
2020-05-24 01:05:48 +02:00
|
|
|
nums = list(self.note.cloze_numbers_in_fields())
|
2020-05-14 12:58:45 +02:00
|
|
|
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)
|
2020-05-14 07:24:29 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def on_fill_empty_action_toggled(self) -> None:
|
2020-07-30 14:39:02 +02:00
|
|
|
self.fill_empty_action_toggled = not self.fill_empty_action_toggled
|
|
|
|
self.on_preview_toggled()
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def on_night_mode_action_toggled(self) -> None:
|
2020-07-31 06:47:17 +02:00
|
|
|
self.night_mode_is_enabled = not self.night_mode_is_enabled
|
2021-06-19 20:31:41 +02:00
|
|
|
force = json.dumps(self.night_mode_is_enabled)
|
|
|
|
self.preview_web.eval(
|
|
|
|
f"document.documentElement.classList.toggle('night-mode', {force});"
|
|
|
|
)
|
2020-07-30 14:39:02 +02:00
|
|
|
self.on_preview_toggled()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def on_mobile_class_action_toggled(self) -> None:
|
2020-08-03 05:30:34 +02:00
|
|
|
self.mobile_emulation_enabled = not self.mobile_emulation_enabled
|
2020-07-30 14:39:02 +02:00
|
|
|
self.on_preview_toggled()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def on_preview_settings(self) -> None:
|
2020-07-30 14:39:02 +02:00
|
|
|
m = QMenu(self)
|
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_fill_empty())
|
2020-07-30 14:39:02 +02:00
|
|
|
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)
|
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_night_mode())
|
2020-07-30 14:39:02 +02:00
|
|
|
a.setCheckable(True)
|
2020-07-31 06:47:17 +02:00
|
|
|
a.setChecked(self.night_mode_is_enabled)
|
2020-07-30 14:39:02 +02:00
|
|
|
qconnect(a.triggered, self.on_night_mode_action_toggled)
|
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_add_mobile_class())
|
2020-07-30 14:39:02 +02:00
|
|
|
a.setCheckable(True)
|
2020-08-03 05:30:34 +02:00
|
|
|
a.setChecked(self.mobile_emulation_enabled)
|
2020-07-30 14:39:02 +02:00
|
|
|
qconnect(a.toggled, self.on_mobile_class_action_toggled)
|
|
|
|
|
2022-02-18 08:18:29 +01:00
|
|
|
m.popup(self.pform.preview_settings.mapToGlobal(QPoint(0, 0)))
|
2020-07-30 14:39:02 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def on_preview_toggled(self) -> None:
|
2020-05-15 06:26:00 +02:00
|
|
|
self.have_autoplayed = False
|
2020-05-14 07:24:29 +02:00
|
|
|
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-14 12:58:45 +02:00
|
|
|
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
|
2019-12-30 09:50:00 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Buttons
|
|
|
|
##########################################################################
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setupButtons(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
l = self.buttons = QHBoxLayout()
|
2021-03-26 04:48:26 +01:00
|
|
|
help = QPushButton(tr.actions_help())
|
2012-12-21 08:51:59 +01:00
|
|
|
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()
|
2021-03-26 04:48:26 +01:00
|
|
|
self.add_field_button = QPushButton(tr.fields_add_field())
|
2020-05-14 10:01:15 +02:00
|
|
|
self.add_field_button.setAutoDefault(False)
|
|
|
|
l.addWidget(self.add_field_button)
|
|
|
|
qconnect(self.add_field_button.clicked, self.onAddField)
|
2017-08-08 11:45:31 +02:00
|
|
|
if not self._isCloze():
|
2021-03-26 04:48:26 +01:00
|
|
|
flip = QPushButton(tr.card_templates_flip())
|
2012-12-21 08:51:59 +01:00
|
|
|
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()
|
2021-03-26 04:48:26 +01:00
|
|
|
save = QPushButton(tr.actions_save())
|
2020-04-26 04:34:25 +02:00
|
|
|
save.setAutoDefault(False)
|
2020-08-06 12:22:54 +02:00
|
|
|
save.setShortcut(QKeySequence("Ctrl+Return"))
|
2020-04-26 04:34:25 +02:00
|
|
|
l.addWidget(save)
|
|
|
|
qconnect(save.clicked, self.accept)
|
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
close = QPushButton(tr.actions_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
|
|
|
##########################################################################
|
|
|
|
|
2021-10-03 10:59:42 +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
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def fill_fields_from_template(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
t = self.current_template()
|
|
|
|
self.ignore_change_signals = True
|
2020-05-14 09:26:40 +02:00
|
|
|
|
|
|
|
if self.current_editor_index == 0:
|
|
|
|
text = t["qfmt"]
|
|
|
|
elif self.current_editor_index == 1:
|
|
|
|
text = t["afmt"]
|
2020-05-05 03:24:33 +02:00
|
|
|
else:
|
2020-05-14 09:26:40 +02:00
|
|
|
text = self.model["css"]
|
|
|
|
|
|
|
|
self.tform.edit_area.setPlainText(text)
|
2020-05-13 09:24:49 +02:00
|
|
|
self.ignore_change_signals = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def write_edits_to_template_and_redraw(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
if self.ignore_change_signals:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-14 09:26:40 +02:00
|
|
|
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker.mark_basic()
|
2020-05-14 09:26:40 +02:00
|
|
|
|
|
|
|
text = self.tform.edit_area.toPlainText()
|
|
|
|
|
|
|
|
if self.current_editor_index == 0:
|
2020-05-14 12:58:45 +02:00
|
|
|
self.current_template()["qfmt"] = text
|
2020-05-14 09:26:40 +02:00
|
|
|
elif self.current_editor_index == 1:
|
2020-05-14 12:58:45 +02:00
|
|
|
self.current_template()["afmt"] = text
|
2020-05-14 09:26:40 +02:00
|
|
|
else:
|
|
|
|
self.model["css"] = text
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.renderPreview()
|
|
|
|
|
|
|
|
# Preview
|
|
|
|
##########################################################################
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
_previewTimer: Optional[QTimer] = None
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def renderPreview(self) -> None:
|
2017-08-08 08:02:55 +02:00
|
|
|
# schedule a preview when timing stops
|
|
|
|
self.cancelPreviewTimer()
|
2022-02-18 10:00:12 +01:00
|
|
|
self._previewTimer = self.mw.progress.timer(
|
|
|
|
200, self._renderPreview, False, parent=self
|
|
|
|
)
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def cancelPreviewTimer(self) -> None:
|
2017-08-08 08:02:55 +02:00
|
|
|
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()
|
|
|
|
|
2021-02-12 01:53:03 +01:00
|
|
|
c = self.rendered_card = self.note.ephemeral_card(
|
|
|
|
self.ord,
|
2021-02-12 01:59:35 +01:00
|
|
|
custom_note_type=self.model,
|
|
|
|
custom_template=self.current_template(),
|
2021-02-12 01:53:03 +01:00
|
|
|
fill_empty=self.fill_empty_action_toggled,
|
|
|
|
)
|
2021-02-12 01:04:48 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
ti = self.maybeTextInput
|
2017-12-04 03:20:56 +01:00
|
|
|
|
2020-07-31 06:47:17 +02:00
|
|
|
bodyclass = theme_manager.body_classes_for_card_ord(
|
|
|
|
c.ord, self.night_mode_is_enabled
|
|
|
|
)
|
2017-09-06 05:02:00 +02:00
|
|
|
|
2020-05-14 07:24:29 +02:00
|
|
|
if self.pform.preview_front.isChecked():
|
2021-06-27 04:12:23 +02:00
|
|
|
q = ti(self.mw.prepare_card_text_for_display(c.question()))
|
2020-05-14 07:24:29 +02:00
|
|
|
q = gui_hooks.card_will_show(q, c, "clayoutQuestion")
|
|
|
|
text = q
|
|
|
|
else:
|
2021-06-27 04:12:23 +02:00
|
|
|
a = ti(self.mw.prepare_card_text_for_display(c.answer()), type="a")
|
2020-05-14 07:24:29 +02:00
|
|
|
a = gui_hooks.card_will_show(a, c, "clayoutAnswer")
|
|
|
|
text = a
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2017-08-10 13:39:04 +02:00
|
|
|
# use _showAnswer to avoid the longer delay
|
2021-02-11 01:09:06 +01:00
|
|
|
self.preview_web.eval(f"_showAnswer({json.dumps(text)},'{bodyclass}');")
|
2020-08-03 05:30:34 +02:00
|
|
|
self.preview_web.eval(
|
|
|
|
f"_emulateMobile({json.dumps(self.mobile_emulation_enabled)});"
|
|
|
|
)
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2020-05-15 06:26:00 +02:00
|
|
|
if not self.have_autoplayed:
|
|
|
|
self.have_autoplayed = True
|
|
|
|
|
|
|
|
if c.autoplay():
|
2021-12-07 23:08:56 +01:00
|
|
|
self.preview_web.setPlaybackRequiresGesture(False)
|
2020-05-15 06:26:00 +02:00
|
|
|
if self.pform.preview_front.isChecked():
|
|
|
|
audio = c.question_av_tags()
|
|
|
|
else:
|
|
|
|
audio = c.answer_av_tags()
|
|
|
|
else:
|
2022-05-09 03:08:34 +02:00
|
|
|
audio = []
|
2021-12-07 23:08:56 +01:00
|
|
|
self.preview_web.setPlaybackRequiresGesture(True)
|
2022-05-09 03:08:34 +02:00
|
|
|
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)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-09 03:11:39 +02:00
|
|
|
self.updateCardNames()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def maybeTextInput(self, txt: str, type: str = "q") -> str:
|
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
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def answerRepl(match: Match) -> str:
|
2022-07-22 11:20:04 +02:00
|
|
|
res = self.mw.col.compare_answer("example", "sample")
|
2013-05-20 11:21:14 +02:00
|
|
|
if hadHR:
|
2021-02-11 01:09:06 +01:00
|
|
|
res = f"<hr id=answer>{res}"
|
2013-05-20 11:21:14 +02:00
|
|
|
return res
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2022-09-05 08:52:25 +02:00
|
|
|
type_filter = r"\[\[type:.+?\]\]"
|
2020-05-05 03:10:24 +02:00
|
|
|
repl: Union[str, Callable]
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
if type == "q":
|
2022-07-09 04:24:43 +02:00
|
|
|
repl = "<input id='typeans' type=text value='example' readonly='readonly'>"
|
2021-02-11 01:09:06 +01:00
|
|
|
repl = f"<center>{repl}</center>"
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2013-05-20 11:21:14 +02:00
|
|
|
repl = answerRepl
|
2022-09-05 08:52:25 +02:00
|
|
|
out = re.sub(type_filter, repl, txt, count=1)
|
|
|
|
|
2022-09-30 06:08:03 +02:00
|
|
|
warning = f"<center><b>{tr.card_templates_type_boxes_warning()}</b></center>"
|
2022-09-05 08:52:25 +02:00
|
|
|
return re.sub(type_filter, warning, out)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Card operations
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onRemove(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
if len(self.templates) < 2:
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(tr.card_templates_at_least_one_card_type_is())
|
2021-02-01 14:28:21 +01:00
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def get_count() -> int:
|
2021-06-17 07:51:21 +02:00
|
|
|
ord = self.current_template()["ord"]
|
|
|
|
return self.mm.template_use_count(self.model["id"], ord)
|
2020-05-15 07:28:07 +02:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def on_done(fut: Future) -> None:
|
2020-05-15 07:28:07 +02:00
|
|
|
card_cnt = fut.result()
|
|
|
|
|
|
|
|
template = self.current_template()
|
2021-03-26 05:21:04 +01:00
|
|
|
cards = tr.card_templates_card_count(count=card_cnt)
|
2021-03-26 05:38:15 +01:00
|
|
|
msg = tr.card_templates_delete_the_as_card_type_and(
|
2020-11-22 05:57:53 +01:00
|
|
|
template=template["name"],
|
2021-03-26 07:33:53 +01:00
|
|
|
# unlike most cases, 'cards' is a string in this message
|
|
|
|
cards=cards, # type: ignore[arg-type]
|
2020-05-15 07:28:07 +02:00
|
|
|
)
|
|
|
|
if not askUser(msg):
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.change_tracker.mark_schema():
|
|
|
|
return
|
|
|
|
|
|
|
|
self.onRemoveInner(template)
|
|
|
|
|
|
|
|
self.mw.taskman.with_progress(get_count, on_done)
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def onRemoveInner(self, template: dict) -> None:
|
2020-05-15 05:59:44 +02:00
|
|
|
self.mm.remove_template(self.model, template)
|
2020-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
# ensure current ordinal is within bounds
|
|
|
|
idx = self.ord
|
|
|
|
if idx >= len(self.templates):
|
|
|
|
self.ord = len(self.templates) - 1
|
|
|
|
|
|
|
|
self.redraw_everything()
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onRename(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
template = self.current_template()
|
2021-03-26 04:48:26 +01:00
|
|
|
name = getOnlyText(tr.actions_new_name(), default=template["name"]).replace(
|
2020-11-17 08:42:43 +01:00
|
|
|
'"', ""
|
|
|
|
)
|
2020-05-13 09:24:49 +02:00
|
|
|
if not name.strip():
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
template["name"] = name
|
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onReorder(self) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
n = len(self.templates)
|
|
|
|
template = self.current_template()
|
|
|
|
current_pos = self.templates.index(template) + 1
|
2021-02-01 11:23:48 +01:00
|
|
|
pos_txt = getOnlyText(
|
2021-03-26 05:21:04 +01:00
|
|
|
tr.card_templates_enter_new_card_position_1(val=n),
|
2020-11-17 08:42:43 +01:00
|
|
|
default=str(current_pos),
|
2020-05-13 09:24:49 +02:00
|
|
|
)
|
2021-02-01 11:23:48 +01:00
|
|
|
if not pos_txt:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
try:
|
2021-02-01 11:23:48 +01:00
|
|
|
pos = int(pos_txt)
|
2012-12-21 08:51:59 +01:00
|
|
|
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
|
2020-05-15 05:59:44 +02:00
|
|
|
if not self.change_tracker.mark_schema():
|
|
|
|
return
|
|
|
|
self.mm.reposition_template(self.model, template, new_idx)
|
2020-05-13 09:24:49 +02:00
|
|
|
self.ord = new_idx
|
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def _newCardName(self) -> str:
|
2020-05-13 09:24:49 +02:00
|
|
|
n = len(self.templates) + 1
|
2012-12-21 08:51:59 +01:00
|
|
|
while 1:
|
2021-03-26 05:21:04 +01:00
|
|
|
name = without_unicode_isolation(tr.card_templates_card(val=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
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onAddCard(self) -> None:
|
2021-06-27 05:49:58 +02:00
|
|
|
cnt = self.mw.col.models.use_count(self.model)
|
2021-03-26 05:21:04 +01:00
|
|
|
txt = tr.card_templates_this_will_create_card_proceed(count=cnt)
|
2023-09-14 02:42:59 +02:00
|
|
|
if cnt and not askUser(txt):
|
2017-08-08 11:45:31 +02:00
|
|
|
return
|
2020-05-15 05:59:44 +02:00
|
|
|
if not self.change_tracker.mark_schema():
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
name = self._newCardName()
|
2021-06-27 05:49:58 +02:00
|
|
|
t = self.mm.new_template(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-15 05:59:44 +02:00
|
|
|
self.mm.add_template(self.model, t)
|
2020-05-13 09:24:49 +02:00
|
|
|
self.ord = len(self.templates) - 1
|
|
|
|
self.redraw_everything()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2023-04-18 06:07:51 +02:00
|
|
|
def on_restore_to_default(
|
|
|
|
self, force_kind: StockNotetype.Kind.V | None = None
|
|
|
|
) -> None:
|
|
|
|
if force_kind is None and not self.model.get("originalStockKind", 0):
|
|
|
|
SelectStockNotetype(
|
|
|
|
mw=self.mw,
|
|
|
|
on_success=lambda kind: self.on_restore_to_default(force_kind=kind),
|
|
|
|
parent=self,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not askUser(
|
2023-04-18 09:10:51 +02:00
|
|
|
with_collapsed_whitespace(
|
|
|
|
tr.card_templates_restore_to_default_confirmation()
|
|
|
|
),
|
|
|
|
defaultno=True,
|
2023-04-18 06:07:51 +02:00
|
|
|
):
|
|
|
|
return
|
|
|
|
|
|
|
|
def on_success(changes: OpChanges) -> None:
|
|
|
|
self.change_tracker.set_unchanged()
|
|
|
|
self.close()
|
|
|
|
showInfo(tr.card_templates_restored_to_default(), parent=self.mw)
|
|
|
|
|
|
|
|
restore_notetype_to_stock(
|
|
|
|
parent=self, notetype_id=self.model["id"], force_kind=force_kind
|
|
|
|
).success(on_success).run_in_background()
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onFlip(self) -> None:
|
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
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def _flipQA(self, src: dict, dst: dict) -> None:
|
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:
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(tr.card_templates_anki_couldnt_find_the_line_between())
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker.mark_basic()
|
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
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def onMore(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
m = QMenu(self)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2023-04-18 09:10:51 +02:00
|
|
|
a = m.addAction(
|
|
|
|
tr.actions_with_ellipsis(action=tr.card_templates_restore_to_default())
|
|
|
|
)
|
|
|
|
qconnect(
|
|
|
|
a.triggered,
|
|
|
|
lambda: self.on_restore_to_default(), # pylint: disable=unnecessary-lambda
|
|
|
|
)
|
|
|
|
|
2017-08-08 11:45:31 +02:00
|
|
|
if not self._isCloze():
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_add_card_type())
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onAddCard)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_remove_card_type())
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onRemove)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_rename_card_type())
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onRename)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_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"]:
|
2021-03-26 04:48:26 +01:00
|
|
|
s = tr.card_templates_on()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-03-26 04:48:26 +01:00
|
|
|
s = tr.card_templates_off()
|
|
|
|
a = m.addAction(tr.card_templates_deck_override() + s)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onTargetDeck)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.card_templates_browser_appearance())
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, self.onBrowserDisplay)
|
2017-08-08 11:45:31 +02:00
|
|
|
|
2022-02-18 08:18:29 +01:00
|
|
|
m.popup(self.topAreaForm.templateOptions.mapToGlobal(QPoint(0, 0)))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def onBrowserDisplay(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
d = QDialog()
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
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)
|
2021-02-26 09:46:09 +01:00
|
|
|
f.font.setCurrentFont(QFont(t.get("bfont") or "Arial"))
|
|
|
|
f.fontSize.setValue(t.get("bsize") or 12)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.buttonBox.accepted, lambda: self.onBrowserDisplayOk(f))
|
2021-10-05 02:01:45 +02:00
|
|
|
d.exec()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
Move away from Bazel (#2202)
(for upgrading users, please see the notes at the bottom)
Bazel brought a lot of nice things to the table, such as rebuilds based on
content changes instead of modification times, caching of build products,
detection of incorrect build rules via a sandbox, and so on. Rewriting the build
in Bazel was also an opportunity to improve on the Makefile-based build we had
prior, which was pretty poor: most dependencies were external or not pinned, and
the build graph was poorly defined and mostly serialized. It was not uncommon
for fresh checkouts to fail due to floating dependencies, or for things to break
when trying to switch to an older commit.
For day-to-day development, I think Bazel served us reasonably well - we could
generally switch between branches while being confident that builds would be
correct and reasonably fast, and not require full rebuilds (except on Windows,
where the lack of a sandbox and the TS rules would cause build breakages when TS
files were renamed/removed).
Bazel achieves that reliability by defining rules for each programming language
that define how source files should be turned into outputs. For the rules to
work with Bazel's sandboxing approach, they often have to reimplement or
partially bypass the standard tools that each programming language provides. The
Rust rules call Rust's compiler directly for example, instead of using Cargo,
and the Python rules extract each PyPi package into a separate folder that gets
added to sys.path.
These separate language rules allow proper declaration of inputs and outputs,
and offer some advantages such as caching of build products and fine-grained
dependency installation. But they also bring some downsides:
- The rules don't always support use-cases/platforms that the standard language
tools do, meaning they need to be patched to be used. I've had to contribute a
number of patches to the Rust, Python and JS rules to unblock various issues.
- The dependencies we use with each language sometimes make assumptions that do
not hold in Bazel, meaning they either need to be pinned or patched, or the
language rules need to be adjusted to accommodate them.
I was hopeful that after the initial setup work, things would be relatively
smooth-sailing. Unfortunately, that has not proved to be the case. Things
frequently broke when dependencies or the language rules were updated, and I
began to get frustrated at the amount of Anki development time I was instead
spending on build system upkeep. It's now about 2 years since switching to
Bazel, and I think it's time to cut losses, and switch to something else that's
a better fit.
The new build system is based on a small build tool called Ninja, and some
custom Rust code in build/. This means that to build Anki, Bazel is no longer
required, but Ninja and Rust need to be installed on your system. Python and
Node toolchains are automatically downloaded like in Bazel.
This new build system should result in faster builds in some cases:
- Because we're using cargo to build now, Rust builds are able to take advantage
of pipelining and incremental debug builds, which we didn't have with Bazel.
It's also easier to override the default linker on Linux/macOS, which can
further improve speeds.
- External Rust crates are now built with opt=1, which improves performance
of debug builds.
- Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript
compiler. This results in faster builds, by deferring typechecking to test/check
time, and by allowing more work to happen in parallel.
As an example of the differences, when testing with the mold linker on Linux,
adding a new message to tags.proto (which triggers a recompile of the bulk of
the Rust and TypeScript code) results in a compile that goes from about 22s on
Bazel to about 7s in the new system. With the standard linker, it's about 9s.
Some other changes of note:
- Our Rust workspace now uses cargo-hakari to ensure all packages agree on
available features, preventing unnecessary rebuilds.
- pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge
source files and generated files into a single folder for running. By telling
VSCode about the extra search path, code completion now works with generated
files without needing to symlink them into the source folder.
- qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py.
Instead, the generated files are now placed in a separate _aqt package that's
added to the path.
- ts/lib is now exposed as @tslib, so the source code and generated code can be
provided under the same namespace without a merging step.
- MyPy and PyLint are now invoked once for the entire codebase.
- dprint will be used to format TypeScript/json files in the future instead of
the slower prettier (currently turned off to avoid causing conflicts). It can
automatically defer to prettier when formatting Svelte files.
- svelte-check is now used for typechecking our Svelte code, which revealed a
few typing issues that went undetected with the old system.
- The Jest unit tests now work on Windows as well.
If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes:
- please remove node_modules and .bazel
- install rustup (https://rustup.rs/)
- install rsync if not already installed (on windows, use pacman - see docs/windows.md)
- install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and
place on your path, or from your distro/homebrew if it's 1.10+)
- update .vscode/settings.json from .vscode.dist
2022-11-27 06:24:20 +01:00
|
|
|
def onBrowserDisplayOk(self, f: browserdisp.Ui_Dialog) -> None:
|
2020-05-13 09:24:49 +02:00
|
|
|
t = self.current_template()
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker.mark_basic()
|
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
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onTargetDeck(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
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")
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
d.setMinimumWidth(400)
|
|
|
|
l = QVBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
lab = QLabel(
|
2021-03-26 05:21:04 +01:00
|
|
|
tr.card_templates_enter_deck_to_place_new(val="%s")
|
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()
|
2021-10-05 05:53:01 +02:00
|
|
|
bb = QDialogButtonBox(QDialogButtonBox.StandardButton.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)
|
2021-10-05 02:01:45 +02:00
|
|
|
d.exec()
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker.mark_basic()
|
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
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onAddField(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
diag = QDialog(self)
|
|
|
|
form = aqt.forms.addfield.Ui_Dialog()
|
|
|
|
form.setupUi(diag)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(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)
|
2020-05-14 10:01:15 +02:00
|
|
|
form.fields.setCurrentRow(0)
|
2012-12-21 08:51:59 +01:00
|
|
|
form.font.setCurrentFont(QFont("Arial"))
|
|
|
|
form.size.setValue(20)
|
2021-10-05 02:01:45 +02:00
|
|
|
if not diag.exec():
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-14 10:01:15 +02:00
|
|
|
row = form.fields.currentIndex().row()
|
|
|
|
if row >= 0:
|
|
|
|
self._addField(
|
2020-08-31 05:29:28 +02:00
|
|
|
fields[row],
|
|
|
|
form.font.currentFont().family(),
|
|
|
|
form.size.value(),
|
2020-05-14 10:01:15 +02:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def _addField(self, field: str, font: str, size: int) -> None:
|
2020-05-14 10:01:15 +02:00
|
|
|
text = self.tform.edit_area.toPlainText()
|
2021-09-02 13:05:16 +02:00
|
|
|
text += (
|
|
|
|
"\n<div style='font-family: \"%s\"; font-size: %spx;'>{{%s}}</div>\n"
|
|
|
|
% (
|
|
|
|
font,
|
|
|
|
size,
|
|
|
|
field,
|
|
|
|
)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-05-14 10:01:15 +02:00
|
|
|
self.tform.edit_area.setPlainText(text)
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker.mark_basic()
|
2020-05-13 09:24:49 +02:00
|
|
|
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:
|
2021-04-30 09:30:48 +02:00
|
|
|
def on_done(changes: OpChanges) -> None:
|
2021-03-26 04:48:26 +01:00
|
|
|
tooltip(tr.card_templates_changes_saved(), parent=self.parentWidget())
|
2020-05-13 09:24:49 +02:00
|
|
|
self.cleanup()
|
2020-05-22 02:47:14 +02:00
|
|
|
gui_hooks.sidebar_should_refresh_notetypes()
|
2021-04-30 09:30:48 +02:00
|
|
|
QDialog.accept(self)
|
2020-05-13 09:24:49 +02:00
|
|
|
|
2021-05-27 16:51:03 +02:00
|
|
|
update_notetype_legacy(parent=self, notetype=self.model).success(
|
2021-04-30 09:30:48 +02:00
|
|
|
on_done
|
2021-05-27 16:51:03 +02:00
|
|
|
).run_in_background()
|
2020-04-26 04:34:25 +02:00
|
|
|
|
|
|
|
def reject(self) -> None:
|
2020-05-15 05:59:44 +02:00
|
|
|
if self.change_tracker.changed():
|
2021-03-26 04:48:26 +01:00
|
|
|
if not askUser(tr.card_templates_discard_changes()):
|
2020-05-13 09:24:49 +02:00
|
|
|
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")
|
2020-05-31 01:31:34 +02:00
|
|
|
saveSplitter(self.mainArea, "CardLayoutMainArea")
|
2021-11-29 03:31:37 +01:00
|
|
|
self.preview_web.cleanup()
|
2020-05-14 12:58:45 +02:00
|
|
|
self.preview_web = 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
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onHelp(self) -> None:
|
2021-01-25 14:45:47 +01:00
|
|
|
openHelp(HelpPage.TEMPLATES)
|
2023-04-18 06:07:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SelectStockNotetype(QDialog):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mw: AnkiQt,
|
|
|
|
on_success: Callable[[StockNotetype.Kind.V], None],
|
|
|
|
parent: QWidget,
|
|
|
|
) -> None:
|
|
|
|
self.mw = mw
|
|
|
|
QDialog.__init__(self, parent, Qt.WindowType.Window)
|
|
|
|
self.dialog = aqt.forms.addmodel.Ui_Dialog()
|
|
|
|
self.dialog.setupUi(self)
|
|
|
|
self.setWindowTitle("Anki")
|
|
|
|
self.setWindowModality(Qt.WindowModality.ApplicationModal)
|
|
|
|
disable_help_button(self)
|
|
|
|
stock_types = stdmodels.get_stock_notetypes(mw.col)
|
|
|
|
|
|
|
|
for name, func in stock_types:
|
|
|
|
item = QListWidgetItem(name)
|
|
|
|
self.dialog.models.addItem(item)
|
|
|
|
self.dialog.models.setCurrentRow(0)
|
|
|
|
# the list widget will swallow the enter key
|
|
|
|
s = QShortcut(QKeySequence("Return"), self)
|
|
|
|
qconnect(s.activated, self.accept)
|
|
|
|
# help
|
|
|
|
# self.dialog.buttonBox.standardButton(QDialogButtonBox.StandardButton.Help).
|
|
|
|
self.on_success = on_success
|
|
|
|
self.show()
|
|
|
|
|
|
|
|
def reject(self) -> None:
|
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
def accept(self) -> None:
|
2023-04-19 08:43:23 +02:00
|
|
|
kind = cast(StockNotetype.Kind.ValueType, self.dialog.models.currentRow())
|
2023-04-18 06:07:51 +02:00
|
|
|
QDialog.accept(self)
|
|
|
|
# On Mac, we need to allow time for the existing modal to close or
|
|
|
|
# Qt gets confused.
|
|
|
|
self.mw.progress.single_shot(100, lambda: self.on_success(kind), True)
|