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
|
|
|
|
|
|
|
|
from aqt.qt import *
|
|
|
|
from operator import itemgetter
|
|
|
|
from aqt.utils import showInfo, askUser, getText, maybeHideClose, openHelp
|
2018-12-15 03:45:38 +01:00
|
|
|
import aqt.clayout
|
2012-12-21 08:51:59 +01:00
|
|
|
from anki import stdmodels
|
|
|
|
from aqt.utils import saveGeom, restoreGeom
|
2016-05-12 06:45:35 +02:00
|
|
|
import collections
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _, ngettext
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class Models(QDialog):
|
2012-12-22 00:21:24 +01:00
|
|
|
def __init__(self, mw, parent=None, fromMain=False):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.parent = parent or mw
|
2012-12-22 00:21:24 +01:00
|
|
|
self.fromMain = fromMain
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, self.parent, Qt.Window)
|
|
|
|
self.col = mw.col
|
|
|
|
self.mm = self.col.models
|
|
|
|
self.mw.checkpoint(_("Note Types"))
|
|
|
|
self.form = aqt.forms.models.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.form.buttonBox.helpRequested.connect(lambda: openHelp("notetypes"))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupModels()
|
|
|
|
restoreGeom(self, "models")
|
|
|
|
self.exec_()
|
|
|
|
|
|
|
|
# Models
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def setupModels(self):
|
|
|
|
self.model = None
|
2016-05-31 10:51:40 +02:00
|
|
|
f = self.form; box = f.buttonBox
|
2012-12-21 08:51:59 +01:00
|
|
|
t = QDialogButtonBox.ActionRole
|
|
|
|
b = box.addButton(_("Add"), t)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(self.onAdd)
|
2012-12-21 08:51:59 +01:00
|
|
|
b = box.addButton(_("Rename"), t)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(self.onRename)
|
2012-12-21 08:51:59 +01:00
|
|
|
b = box.addButton(_("Delete"), t)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(self.onDelete)
|
2012-12-22 00:21:24 +01:00
|
|
|
if self.fromMain:
|
|
|
|
b = box.addButton(_("Fields..."), t)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(self.onFields)
|
2012-12-22 00:21:24 +01:00
|
|
|
b = box.addButton(_("Cards..."), t)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(self.onCards)
|
2012-12-21 08:51:59 +01:00
|
|
|
b = box.addButton(_("Options..."), t)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(self.onAdvanced)
|
|
|
|
f.modelsList.currentRowChanged.connect(self.modelChanged)
|
|
|
|
f.modelsList.itemDoubleClicked.connect(self.onRename)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.updateModelsList()
|
|
|
|
f.modelsList.setCurrentRow(0)
|
|
|
|
maybeHideClose(box)
|
|
|
|
|
|
|
|
def onRename(self):
|
|
|
|
txt = getText(_("New name:"), default=self.model['name'])
|
2013-01-08 02:38:23 +01:00
|
|
|
if txt[1] and txt[0]:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model['name'] = txt[0]
|
|
|
|
self.mm.save(self.model)
|
|
|
|
self.updateModelsList()
|
|
|
|
|
|
|
|
def updateModelsList(self):
|
|
|
|
row = self.form.modelsList.currentRow()
|
|
|
|
if row == -1:
|
|
|
|
row = 0
|
|
|
|
self.models = self.col.models.all()
|
|
|
|
self.models.sort(key=itemgetter("name"))
|
|
|
|
self.form.modelsList.clear()
|
|
|
|
for m in self.models:
|
|
|
|
mUse = self.mm.useCount(m)
|
|
|
|
mUse = ngettext("%d note", "%d notes", mUse) % mUse
|
|
|
|
item = QListWidgetItem("%s [%s]" % (m['name'], mUse))
|
|
|
|
self.form.modelsList.addItem(item)
|
|
|
|
self.form.modelsList.setCurrentRow(row)
|
|
|
|
|
|
|
|
def modelChanged(self):
|
|
|
|
if self.model:
|
|
|
|
self.saveModel()
|
|
|
|
idx = self.form.modelsList.currentRow()
|
|
|
|
self.model = self.models[idx]
|
|
|
|
|
|
|
|
def onAdd(self):
|
|
|
|
m = AddModel(self.mw, self).get()
|
|
|
|
if m:
|
|
|
|
txt = getText(_("Name:"), default=m['name'])[0]
|
|
|
|
if txt:
|
|
|
|
m['name'] = txt
|
2013-10-04 02:00:08 +02:00
|
|
|
self.mm.ensureNameUnique(m)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mm.save(m)
|
|
|
|
self.updateModelsList()
|
|
|
|
|
|
|
|
def onDelete(self):
|
|
|
|
if len(self.models) < 2:
|
|
|
|
showInfo(_("Please add another note type first."),
|
|
|
|
parent=self)
|
|
|
|
return
|
|
|
|
if self.mm.useCount(self.model):
|
|
|
|
msg = _("Delete this note type and all its cards?")
|
|
|
|
else:
|
|
|
|
msg = _("Delete this unused note type?")
|
|
|
|
if not askUser(msg, parent=self):
|
|
|
|
return
|
|
|
|
self.mm.rem(self.model)
|
|
|
|
self.model = None
|
|
|
|
self.updateModelsList()
|
|
|
|
|
|
|
|
def onAdvanced(self):
|
|
|
|
d = QDialog(self)
|
|
|
|
frm = aqt.forms.modelopts.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
2017-07-28 10:38:31 +02:00
|
|
|
frm.latexsvg.setChecked(self.model.get("latexsvg", False))
|
2012-12-21 08:51:59 +01:00
|
|
|
frm.latexHeader.setText(self.model['latexPre'])
|
|
|
|
frm.latexFooter.setText(self.model['latexPost'])
|
|
|
|
d.setWindowTitle(_("Options for %s") % self.model['name'])
|
2016-05-31 10:51:40 +02:00
|
|
|
frm.buttonBox.helpRequested.connect(lambda: openHelp("latex"))
|
2014-06-18 20:47:45 +02:00
|
|
|
restoreGeom(d, "modelopts")
|
2012-12-21 08:51:59 +01:00
|
|
|
d.exec_()
|
2014-06-18 20:47:45 +02:00
|
|
|
saveGeom(d, "modelopts")
|
2017-07-28 10:38:31 +02:00
|
|
|
self.model['latexsvg'] = frm.latexsvg.isChecked()
|
2016-05-12 06:45:35 +02:00
|
|
|
self.model['latexPre'] = str(frm.latexHeader.toPlainText())
|
|
|
|
self.model['latexPost'] = str(frm.latexFooter.toPlainText())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def saveModel(self):
|
|
|
|
self.mm.save(self.model)
|
|
|
|
|
2012-12-22 00:21:24 +01:00
|
|
|
def _tmpNote(self):
|
|
|
|
self.mm.setCurrent(self.model)
|
2013-01-08 13:39:37 +01:00
|
|
|
n = self.col.newNote(forDeck=False)
|
2016-05-12 06:45:35 +02:00
|
|
|
for name in list(n.keys()):
|
2012-12-22 00:21:24 +01:00
|
|
|
n[name] = "("+name+")"
|
2013-07-02 21:27:46 +02:00
|
|
|
try:
|
|
|
|
if "{{cloze:Text}}" in self.model['tmpls'][0]['qfmt']:
|
|
|
|
n['Text'] = _("This is a {{c1::sample}} cloze deletion.")
|
|
|
|
except:
|
|
|
|
# invalid cloze
|
|
|
|
pass
|
2012-12-22 00:21:24 +01:00
|
|
|
return n
|
|
|
|
|
|
|
|
def onFields(self):
|
|
|
|
from aqt.fields import FieldDialog
|
|
|
|
n = self._tmpNote()
|
|
|
|
FieldDialog(self.mw, n, parent=self)
|
|
|
|
|
|
|
|
def onCards(self):
|
|
|
|
from aqt.clayout import CardLayout
|
|
|
|
n = self._tmpNote()
|
|
|
|
CardLayout(self.mw, n, ord=0, parent=self, addMode=True)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Cleanup
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
# need to flush model on change or reject
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
self.saveModel()
|
|
|
|
self.mw.reset()
|
|
|
|
saveGeom(self, "models")
|
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
class AddModel(QDialog):
|
|
|
|
|
|
|
|
def __init__(self, mw, parent=None):
|
|
|
|
self.parent = parent or mw
|
|
|
|
self.mw = mw
|
|
|
|
self.col = mw.col
|
|
|
|
QDialog.__init__(self, self.parent, Qt.Window)
|
|
|
|
self.model = None
|
|
|
|
self.dialog = aqt.forms.addmodel.Ui_Dialog()
|
|
|
|
self.dialog.setupUi(self)
|
|
|
|
# standard models
|
|
|
|
self.models = []
|
|
|
|
for (name, func) in stdmodels.models:
|
2016-05-12 06:45:35 +02:00
|
|
|
if isinstance(name, collections.Callable):
|
2012-12-21 08:51:59 +01:00
|
|
|
name = name()
|
|
|
|
item = QListWidgetItem(_("Add: %s") % name)
|
|
|
|
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")):
|
2012-12-21 08:51:59 +01:00
|
|
|
item = QListWidgetItem(_("Clone: %s") % m['name'])
|
|
|
|
self.dialog.models.addItem(item)
|
|
|
|
self.models.append((False, m))
|
|
|
|
self.dialog.models.setCurrentRow(0)
|
|
|
|
# the list widget will swallow the enter key
|
|
|
|
s = QShortcut(QKeySequence("Return"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
s.activated.connect(self.accept)
|
2012-12-21 08:51:59 +01:00
|
|
|
# help
|
2016-05-31 10:51:40 +02:00
|
|
|
self.dialog.buttonBox.helpRequested.connect(self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def get(self):
|
|
|
|
self.exec_()
|
|
|
|
return self.model
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
(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)
|
|
|
|
|
|
|
|
def onHelp(self):
|
|
|
|
openHelp("notetypes")
|