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 12:58:45 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
from operator import itemgetter
|
2020-08-23 19:05:40 +02:00
|
|
|
from typing import Any, List, Optional, Sequence
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2018-12-15 03:45:38 +01:00
|
|
|
import aqt.clayout
|
2012-12-21 08:51:59 +01:00
|
|
|
from anki import stdmodels
|
2020-11-18 16:03:04 +01:00
|
|
|
from anki.lang import without_unicode_isolation
|
2021-01-31 06:55:08 +01:00
|
|
|
from anki.models import NoteType, NoteTypeNameIDUseCount
|
2020-05-14 12:58:45 +02:00
|
|
|
from anki.notes import Note
|
2020-03-19 12:03:09 +01:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import (
|
2020-11-17 08:42:43 +01:00
|
|
|
TR,
|
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
|
|
|
getText,
|
|
|
|
maybeHideClose,
|
|
|
|
openHelp,
|
|
|
|
restoreGeom,
|
|
|
|
saveGeom,
|
|
|
|
showInfo,
|
2020-11-17 08:42:43 +01:00
|
|
|
tr,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class Models(QDialog):
|
2021-01-22 03:56:39 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mw: AnkiQt,
|
|
|
|
parent=None,
|
|
|
|
fromMain=False,
|
|
|
|
selected_notetype_id: Optional[int] = None,
|
|
|
|
):
|
2020-05-20 05:53:34 +02:00
|
|
|
self.mw = mw
|
2019-12-16 12:17:32 +01:00
|
|
|
parent = parent or mw
|
2012-12-22 00:21:24 +01:00
|
|
|
self.fromMain = fromMain
|
2021-01-22 03:56:39 +01:00
|
|
|
self.selected_notetype_id = selected_notetype_id
|
2019-12-16 12:17:32 +01:00
|
|
|
QDialog.__init__(self, parent, Qt.Window)
|
2020-05-04 13:52:48 +02:00
|
|
|
self.col = mw.col.weakref()
|
2019-12-23 01:34:10 +01:00
|
|
|
assert self.col
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mm = self.col.models
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.NOTETYPES_NOTE_TYPES))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form = aqt.forms.models.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2020-10-12 04:17:02 +02:00
|
|
|
qconnect(
|
|
|
|
self.form.buttonBox.helpRequested,
|
2021-01-25 14:45:47 +01:00
|
|
|
lambda: openHelp(HelpPage.ADDING_A_NOTE_TYPE),
|
2020-10-12 04:17:02 +02:00
|
|
|
)
|
2021-01-31 06:55:08 +01:00
|
|
|
self.models: List[NoteTypeNameIDUseCount] = []
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupModels()
|
|
|
|
restoreGeom(self, "models")
|
|
|
|
self.exec_()
|
|
|
|
|
|
|
|
# Models
|
|
|
|
##########################################################################
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def maybe_select_provided_notetype(self) -> None:
|
2021-01-22 03:56:39 +01:00
|
|
|
if not self.selected_notetype_id:
|
|
|
|
self.form.modelsList.setCurrentRow(0)
|
|
|
|
return
|
|
|
|
for i, m in enumerate(self.models):
|
|
|
|
if m.id == self.selected_notetype_id:
|
|
|
|
self.form.modelsList.setCurrentRow(i)
|
|
|
|
break
|
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def setupModels(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model = None
|
2019-12-23 01:34:10 +01:00
|
|
|
f = self.form
|
|
|
|
box = f.buttonBox
|
2020-08-23 15:52:02 +02:00
|
|
|
|
|
|
|
default_buttons = [
|
2020-11-17 08:42:43 +01:00
|
|
|
(tr(TR.ACTIONS_ADD), self.onAdd),
|
|
|
|
(tr(TR.ACTIONS_RENAME), self.onRename),
|
|
|
|
(tr(TR.ACTIONS_DELETE), self.onDelete),
|
2020-08-23 15:52:02 +02:00
|
|
|
]
|
|
|
|
|
2012-12-22 00:21:24 +01:00
|
|
|
if self.fromMain:
|
2020-08-31 13:50:38 +02:00
|
|
|
default_buttons.extend(
|
|
|
|
[
|
2020-11-17 08:42:43 +01:00
|
|
|
(tr(TR.NOTETYPES_FIELDS), self.onFields),
|
|
|
|
(tr(TR.NOTETYPES_CARDS), self.onCards),
|
2020-08-31 13:50:38 +02:00
|
|
|
]
|
|
|
|
)
|
2020-08-23 15:52:02 +02:00
|
|
|
|
2020-11-17 08:42:43 +01:00
|
|
|
default_buttons.append((tr(TR.NOTETYPES_OPTIONS), self.onAdvanced))
|
2020-08-23 15:52:02 +02:00
|
|
|
|
2020-08-23 17:10:56 +02:00
|
|
|
for label, func in gui_hooks.models_did_init_buttons(default_buttons, self):
|
2020-08-31 13:40:42 +02:00
|
|
|
button = box.addButton(label, QDialogButtonBox.ActionRole)
|
2020-08-23 15:52:02 +02:00
|
|
|
qconnect(button.clicked, func)
|
|
|
|
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.modelsList.itemDoubleClicked, self.onRename)
|
2020-05-04 13:52:48 +02:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def on_done(fut) -> None:
|
2020-05-04 13:52:48 +02:00
|
|
|
self.updateModelsList(fut.result())
|
2021-01-22 03:56:39 +01:00
|
|
|
self.maybe_select_provided_notetype()
|
2020-05-04 13:52:48 +02:00
|
|
|
|
|
|
|
self.mw.taskman.with_progress(self.col.models.all_use_counts, on_done, self)
|
2012-12-21 08:51:59 +01:00
|
|
|
maybeHideClose(box)
|
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def onRename(self) -> None:
|
2020-05-04 13:52:48 +02:00
|
|
|
nt = self.current_notetype()
|
2020-11-17 08:42:43 +01:00
|
|
|
txt = getText(tr(TR.ACTIONS_NEW_NAME), default=nt["name"])
|
2020-10-22 14:21:30 +02:00
|
|
|
name = txt[0].replace('"', "")
|
2020-10-21 19:10:53 +02:00
|
|
|
if txt[1] and name:
|
|
|
|
nt["name"] = name
|
2020-05-04 13:52:48 +02:00
|
|
|
self.saveAndRefresh(nt)
|
|
|
|
|
|
|
|
def saveAndRefresh(self, nt: NoteType) -> None:
|
2021-01-31 06:55:08 +01:00
|
|
|
def save() -> Sequence[NoteTypeNameIDUseCount]:
|
2020-05-04 13:52:48 +02:00
|
|
|
self.mm.save(nt)
|
|
|
|
return self.col.models.all_use_counts()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def on_done(fut) -> None:
|
2020-05-04 13:52:48 +02:00
|
|
|
self.updateModelsList(fut.result())
|
|
|
|
|
|
|
|
self.mw.taskman.with_progress(save, on_done, self)
|
|
|
|
|
2020-07-24 18:59:45 +02:00
|
|
|
def updateModelsList(self, notetypes: List[NoteTypeNameIDUseCount]) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
row = self.form.modelsList.currentRow()
|
|
|
|
if row == -1:
|
|
|
|
row = 0
|
|
|
|
self.form.modelsList.clear()
|
2020-05-04 13:52:48 +02:00
|
|
|
|
|
|
|
self.models = notetypes
|
2012-12-21 08:51:59 +01:00
|
|
|
for m in self.models:
|
2020-11-18 00:22:27 +01:00
|
|
|
mUse = tr(TR.BROWSING_NOTE_COUNT, count=m.use_count)
|
2020-04-25 12:13:46 +02:00
|
|
|
item = QListWidgetItem("%s [%s]" % (m.name, mUse))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.modelsList.addItem(item)
|
|
|
|
self.form.modelsList.setCurrentRow(row)
|
|
|
|
|
2020-05-04 13:52:48 +02:00
|
|
|
def current_notetype(self) -> NoteType:
|
|
|
|
row = self.form.modelsList.currentRow()
|
|
|
|
return self.mm.get(self.models[row].id)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def onAdd(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
m = AddModel(self.mw, self).get()
|
|
|
|
if m:
|
2020-11-17 08:42:43 +01:00
|
|
|
txt = getText(tr(TR.ACTIONS_NAME), default=m["name"])[0].replace('"', "")
|
2012-12-21 08:51:59 +01:00
|
|
|
if txt:
|
2019-12-23 01:34:10 +01:00
|
|
|
m["name"] = txt
|
2020-05-04 13:52:48 +02:00
|
|
|
self.saveAndRefresh(m)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def onDelete(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if len(self.models) < 2:
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.NOTETYPES_PLEASE_ADD_ANOTHER_NOTE_TYPE_FIRST), parent=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-04 07:46:10 +02:00
|
|
|
idx = self.form.modelsList.currentRow()
|
|
|
|
if self.models[idx].use_count:
|
2020-11-17 08:42:43 +01:00
|
|
|
msg = tr(TR.NOTETYPES_DELETE_THIS_NOTE_TYPE_AND_ALL)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
msg = tr(TR.NOTETYPES_DELETE_THIS_UNUSED_NOTE_TYPE)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not askUser(msg, parent=self):
|
|
|
|
return
|
2020-05-04 13:52:48 +02:00
|
|
|
|
|
|
|
self.col.modSchema(check=True)
|
|
|
|
|
|
|
|
nt = self.current_notetype()
|
|
|
|
|
2021-01-31 06:55:08 +01:00
|
|
|
def save() -> Sequence[NoteTypeNameIDUseCount]:
|
2020-05-04 13:52:48 +02:00
|
|
|
self.mm.rem(nt)
|
|
|
|
return self.col.models.all_use_counts()
|
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def on_done(fut) -> None:
|
2020-05-04 13:52:48 +02:00
|
|
|
self.updateModelsList(fut.result())
|
|
|
|
|
|
|
|
self.mw.taskman.with_progress(save, on_done, self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def onAdvanced(self) -> None:
|
2020-05-04 13:52:48 +02:00
|
|
|
nt = self.current_notetype()
|
2012-12-21 08:51:59 +01:00
|
|
|
d = QDialog(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
frm = aqt.forms.modelopts.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
2020-05-04 13:52:48 +02:00
|
|
|
frm.latexsvg.setChecked(nt.get("latexsvg", False))
|
|
|
|
frm.latexHeader.setText(nt["latexPre"])
|
|
|
|
frm.latexFooter.setText(nt["latexPost"])
|
2020-11-18 16:03:04 +01:00
|
|
|
d.setWindowTitle(
|
|
|
|
without_unicode_isolation(tr(TR.ACTIONS_OPTIONS_FOR, val=nt["name"]))
|
|
|
|
)
|
2021-01-25 14:45:47 +01:00
|
|
|
qconnect(frm.buttonBox.helpRequested, lambda: openHelp(HelpPage.LATEX))
|
2014-06-18 20:47:45 +02:00
|
|
|
restoreGeom(d, "modelopts")
|
2020-03-19 12:03:09 +01:00
|
|
|
gui_hooks.models_advanced_will_show(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
d.exec_()
|
2014-06-18 20:47:45 +02:00
|
|
|
saveGeom(d, "modelopts")
|
2020-05-04 13:52:48 +02:00
|
|
|
nt["latexsvg"] = frm.latexsvg.isChecked()
|
|
|
|
nt["latexPre"] = str(frm.latexHeader.toPlainText())
|
|
|
|
nt["latexPost"] = str(frm.latexFooter.toPlainText())
|
|
|
|
self.saveAndRefresh(nt)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def _tmpNote(self) -> Note:
|
2020-05-04 13:52:48 +02:00
|
|
|
nt = self.current_notetype()
|
2020-05-14 12:58:45 +02:00
|
|
|
return Note(self.col, nt)
|
2012-12-22 00:21:24 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def onFields(self) -> None:
|
2012-12-22 00:21:24 +01:00
|
|
|
from aqt.fields import FieldDialog
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-05-04 13:52:48 +02:00
|
|
|
FieldDialog(self.mw, self.current_notetype(), parent=self)
|
2012-12-22 00:21:24 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def onCards(self) -> None:
|
2012-12-22 00:21:24 +01:00
|
|
|
from aqt.clayout import CardLayout
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-22 00:21:24 +01:00
|
|
|
n = self._tmpNote()
|
2020-05-14 12:58:45 +02:00
|
|
|
CardLayout(self.mw, n, ord=0, parent=self, fill_empty=True)
|
2012-12-22 00:21:24 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Cleanup
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
# need to flush model on change or reject
|
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def reject(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.reset()
|
|
|
|
saveGeom(self, "models")
|
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class AddModel(QDialog):
|
2020-08-11 22:56:58 +02:00
|
|
|
model: Optional[NoteType]
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def __init__(self, mw: AnkiQt, parent: Optional[QWidget] = None) -> None:
|
2020-05-04 05:23:08 +02:00
|
|
|
self.parent_ = parent or mw
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.col = mw.col
|
2020-05-04 05:23:08 +02:00
|
|
|
QDialog.__init__(self, self.parent_, Qt.Window)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model = None
|
|
|
|
self.dialog = aqt.forms.addmodel.Ui_Dialog()
|
|
|
|
self.dialog.setupUi(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
# standard models
|
|
|
|
self.models = []
|
2020-05-15 08:05:58 +02:00
|
|
|
for (name, func) in stdmodels.get_stock_notetypes(self.col):
|
2020-11-17 12:47:47 +01:00
|
|
|
item = QListWidgetItem(tr(TR.NOTETYPES_ADD, val=name))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.dialog.models.addItem(item)
|
|
|
|
self.models.append((True, func))
|
|
|
|
# add copies
|
2013-01-08 02:39:51 +01:00
|
|
|
for m in sorted(self.col.models.all(), key=itemgetter("name")):
|
2020-11-17 12:47:47 +01:00
|
|
|
item = QListWidgetItem(tr(TR.NOTETYPES_CLONE, val=m["name"]))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.dialog.models.addItem(item)
|
2020-05-04 07:21:03 +02:00
|
|
|
self.models.append((False, m)) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
self.dialog.models.setCurrentRow(0)
|
|
|
|
# the list widget will swallow the enter key
|
|
|
|
s = QShortcut(QKeySequence("Return"), self)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(s.activated, self.accept)
|
2012-12-21 08:51:59 +01:00
|
|
|
# help
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.dialog.buttonBox.helpRequested, self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def get(self) -> Any:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.exec_()
|
|
|
|
return self.model
|
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def reject(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.reject(self)
|
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def accept(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
(isStd, model) = self.models[self.dialog.models.currentRow()]
|
|
|
|
if isStd:
|
|
|
|
# create
|
|
|
|
self.model = model(self.col)
|
|
|
|
else:
|
|
|
|
# add copy to deck
|
|
|
|
self.model = self.mw.col.models.copy(model)
|
|
|
|
self.mw.col.models.setCurrent(self.model)
|
|
|
|
QDialog.accept(self)
|
|
|
|
|
2020-07-30 05:28:52 +02:00
|
|
|
def onHelp(self) -> None:
|
2021-01-25 14:45:47 +01:00
|
|
|
openHelp(HelpPage.ADDING_A_NOTE_TYPE)
|