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
|
|
|
|
|
|
|
|
import aqt
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.consts import *
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _, ngettext
|
2020-05-04 13:52:48 +02:00
|
|
|
from anki.models import NoteType
|
2020-04-26 04:34:25 +02:00
|
|
|
from anki.rsbackend import TemplateError
|
2020-10-11 19:16:30 +02:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
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-11-17 08:42:43 +01:00
|
|
|
from aqt.utils import TR, askUser, getOnlyText, openHelp, showWarning, tooltip, tr
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class FieldDialog(QDialog):
|
2020-05-04 13:52:48 +02:00
|
|
|
def __init__(self, mw: AnkiQt, nt: NoteType, parent=None):
|
|
|
|
QDialog.__init__(self, parent or mw)
|
2020-05-15 05:59:44 +02:00
|
|
|
self.mw = mw
|
2012-12-21 08:51:59 +01:00
|
|
|
self.col = self.mw.col
|
|
|
|
self.mm = self.mw.col.models
|
2020-05-04 13:52:48 +02:00
|
|
|
self.model = nt
|
2020-05-13 09:24:49 +02:00
|
|
|
self.mm._remove_from_cache(self.model["id"])
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.EDITING_FIELDS))
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker = ChangeTracker(self.mw)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form = aqt.forms.fields.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2020-11-17 12:47:47 +01:00
|
|
|
self.setWindowTitle(tr(TR.FIELDS_FIELDS_FOR, val=self.model["name"]))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Help).setAutoDefault(False)
|
2020-04-26 04:34:25 +02:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Cancel).setAutoDefault(False)
|
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Save).setAutoDefault(False)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.currentIdx = None
|
2019-12-23 01:34:10 +01:00
|
|
|
self.oldSortField = self.model["sortf"]
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fillFields()
|
|
|
|
self.setupSignals()
|
2020-04-07 09:42:33 +02:00
|
|
|
self.form.fieldList.setDragDropMode(QAbstractItemView.InternalMove)
|
|
|
|
self.form.fieldList.dropEvent = self.onDrop
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.fieldList.setCurrentRow(0)
|
|
|
|
self.exec_()
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def fillFields(self):
|
|
|
|
self.currentIdx = None
|
|
|
|
self.form.fieldList.clear()
|
2019-12-23 01:34:10 +01:00
|
|
|
for c, f in enumerate(self.model["flds"]):
|
|
|
|
self.form.fieldList.addItem("{}: {}".format(c + 1, f["name"]))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def setupSignals(self):
|
|
|
|
f = self.form
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.fieldList.currentRowChanged, self.onRowChange)
|
|
|
|
qconnect(f.fieldAdd.clicked, self.onAdd)
|
|
|
|
qconnect(f.fieldDelete.clicked, self.onDelete)
|
|
|
|
qconnect(f.fieldRename.clicked, self.onRename)
|
|
|
|
qconnect(f.fieldPosition.clicked, self.onPosition)
|
|
|
|
qconnect(f.sortField.clicked, self.onSortField)
|
|
|
|
qconnect(f.buttonBox.helpRequested, self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-04-07 09:42:33 +02:00
|
|
|
def onDrop(self, ev):
|
|
|
|
fieldList = self.form.fieldList
|
|
|
|
indicatorPos = fieldList.dropIndicatorPosition()
|
|
|
|
dropPos = fieldList.indexAt(ev.pos()).row()
|
2020-05-26 11:27:38 +02:00
|
|
|
idx = self.currentIdx
|
2020-05-26 11:59:53 +02:00
|
|
|
if dropPos == idx:
|
|
|
|
return
|
2020-05-26 11:27:38 +02:00
|
|
|
if indicatorPos == QAbstractItemView.OnViewport: # to bottom.
|
|
|
|
movePos = fieldList.count() - 1
|
2020-04-07 09:42:33 +02:00
|
|
|
elif indicatorPos == QAbstractItemView.AboveItem:
|
2020-05-26 11:27:38 +02:00
|
|
|
movePos = dropPos
|
2020-04-07 09:42:33 +02:00
|
|
|
elif indicatorPos == QAbstractItemView.BelowItem:
|
2020-05-26 11:27:38 +02:00
|
|
|
movePos = dropPos + 1
|
|
|
|
# the item in idx is removed thus subtract 1.
|
|
|
|
if idx < dropPos:
|
|
|
|
movePos -= 1
|
|
|
|
self.moveField(movePos + 1) # convert to 1 based.
|
2020-04-07 09:42:33 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onRowChange(self, idx):
|
|
|
|
if idx == -1:
|
|
|
|
return
|
|
|
|
self.saveField()
|
|
|
|
self.loadField(idx)
|
|
|
|
|
|
|
|
def _uniqueName(self, prompt, ignoreOrd=None, old=""):
|
2020-10-22 14:21:30 +02:00
|
|
|
txt = getOnlyText(prompt, default=old).replace('"', "")
|
2012-12-21 08:51:59 +01:00
|
|
|
if not txt:
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
for f in self.model["flds"]:
|
|
|
|
if ignoreOrd is not None and f["ord"] == ignoreOrd:
|
2012-12-21 08:51:59 +01:00
|
|
|
continue
|
2019-12-23 01:34:10 +01:00
|
|
|
if f["name"] == txt:
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.FIELDS_THAT_FIELD_NAME_IS_ALREADY_USED))
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
return txt
|
|
|
|
|
|
|
|
def onRename(self):
|
|
|
|
idx = self.currentIdx
|
2019-12-23 01:34:10 +01:00
|
|
|
f = self.model["flds"][idx]
|
2020-11-17 08:42:43 +01:00
|
|
|
name = self._uniqueName(tr(TR.ACTIONS_NEW_NAME), self.currentIdx, f["name"])
|
2012-12-21 08:51:59 +01:00
|
|
|
if not name:
|
|
|
|
return
|
2020-05-15 05:59:44 +02:00
|
|
|
|
|
|
|
self.change_tracker.mark_basic()
|
|
|
|
self.mm.rename_field(self.model, f, name)
|
2020-10-11 19:16:30 +02:00
|
|
|
gui_hooks.fields_did_rename_field(self, f, f["name"])
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.saveField()
|
|
|
|
self.fillFields()
|
|
|
|
self.form.fieldList.setCurrentRow(idx)
|
|
|
|
|
|
|
|
def onAdd(self):
|
2020-11-17 08:42:43 +01:00
|
|
|
name = self._uniqueName(tr(TR.FIELDS_FIELD_NAME))
|
2012-12-21 08:51:59 +01:00
|
|
|
if not name:
|
|
|
|
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
|
|
|
self.saveField()
|
|
|
|
f = self.mm.newField(name)
|
2020-05-15 05:59:44 +02:00
|
|
|
self.mm.add_field(self.model, f)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fillFields()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.fieldList.setCurrentRow(len(self.model["flds"]) - 1)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onDelete(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
if len(self.model["flds"]) < 2:
|
2020-11-17 08:42:43 +01:00
|
|
|
return showWarning(tr(TR.FIELDS_NOTES_REQUIRE_AT_LEAST_ONE_FIELD))
|
2020-08-11 22:56:58 +02:00
|
|
|
count = self.mm.useCount(self.model)
|
|
|
|
c = ngettext("%d note", "%d notes", count) % count
|
2020-11-17 12:47:47 +01:00
|
|
|
if not askUser(tr(TR.FIELDS_DELETE_FIELD_FROM, val=c)):
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-15 05:59:44 +02:00
|
|
|
if not self.change_tracker.mark_schema():
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
f = self.model["flds"][self.form.fieldList.currentRow()]
|
2020-05-15 05:59:44 +02:00
|
|
|
self.mm.remove_field(self.model, f)
|
2020-10-11 19:16:30 +02:00
|
|
|
gui_hooks.fields_did_delete_field(self, f)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fillFields()
|
|
|
|
self.form.fieldList.setCurrentRow(0)
|
|
|
|
|
|
|
|
def onPosition(self, delta=-1):
|
|
|
|
idx = self.currentIdx
|
2019-12-23 01:34:10 +01:00
|
|
|
l = len(self.model["flds"])
|
2020-11-17 12:47:47 +01:00
|
|
|
txt = getOnlyText(tr(TR.FIELDS_NEW_POSITION_1, val=l), default=str(idx + 1))
|
2012-12-21 08:51:59 +01:00
|
|
|
if not txt:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
pos = int(txt)
|
|
|
|
except ValueError:
|
|
|
|
return
|
|
|
|
if not 0 < pos <= l:
|
|
|
|
return
|
2020-04-07 09:42:33 +02:00
|
|
|
self.moveField(pos)
|
|
|
|
|
|
|
|
def onSortField(self):
|
2020-05-15 05:59:44 +02:00
|
|
|
if not self.change_tracker.mark_schema():
|
|
|
|
return False
|
2020-04-07 09:42:33 +02:00
|
|
|
# don't allow user to disable; it makes no sense
|
|
|
|
self.form.sortField.setChecked(True)
|
2020-05-15 05:59:44 +02:00
|
|
|
self.mm.set_sort_index(self.model, self.form.fieldList.currentRow())
|
2020-04-07 09:42:33 +02:00
|
|
|
|
|
|
|
def moveField(self, pos):
|
2020-05-15 05:59:44 +02:00
|
|
|
if not self.change_tracker.mark_schema():
|
|
|
|
return False
|
2012-12-21 08:51:59 +01:00
|
|
|
self.saveField()
|
2019-12-23 01:34:10 +01:00
|
|
|
f = self.model["flds"][self.currentIdx]
|
2020-05-15 05:59:44 +02:00
|
|
|
self.mm.reposition_field(self.model, f, pos - 1)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fillFields()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.fieldList.setCurrentRow(pos - 1)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def loadField(self, idx):
|
|
|
|
self.currentIdx = idx
|
2019-12-23 01:34:10 +01:00
|
|
|
fld = self.model["flds"][idx]
|
2012-12-21 08:51:59 +01:00
|
|
|
f = self.form
|
2019-12-23 01:34:10 +01:00
|
|
|
f.fontFamily.setCurrentFont(QFont(fld["font"]))
|
|
|
|
f.fontSize.setValue(fld["size"])
|
|
|
|
f.sticky.setChecked(fld["sticky"])
|
|
|
|
f.sortField.setChecked(self.model["sortf"] == fld["ord"])
|
|
|
|
f.rtl.setChecked(fld["rtl"])
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def saveField(self):
|
|
|
|
# not initialized yet?
|
|
|
|
if self.currentIdx is None:
|
|
|
|
return
|
|
|
|
idx = self.currentIdx
|
2019-12-23 01:34:10 +01:00
|
|
|
fld = self.model["flds"][idx]
|
2012-12-21 08:51:59 +01:00
|
|
|
f = self.form
|
2020-05-15 05:59:44 +02:00
|
|
|
font = f.fontFamily.currentFont().family()
|
|
|
|
if fld["font"] != font:
|
|
|
|
fld["font"] = font
|
|
|
|
self.change_tracker.mark_basic()
|
|
|
|
size = f.fontSize.value()
|
|
|
|
if fld["size"] != size:
|
|
|
|
fld["size"] = size
|
|
|
|
self.change_tracker.mark_basic()
|
|
|
|
sticky = f.sticky.isChecked()
|
|
|
|
if fld["sticky"] != sticky:
|
|
|
|
fld["sticky"] = sticky
|
|
|
|
self.change_tracker.mark_basic()
|
|
|
|
rtl = f.rtl.isChecked()
|
|
|
|
if fld["rtl"] != rtl:
|
|
|
|
fld["rtl"] = rtl
|
|
|
|
self.change_tracker.mark_basic()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def reject(self):
|
2020-05-15 05:59:44 +02:00
|
|
|
if self.change_tracker.changed():
|
|
|
|
if not askUser("Discard changes?"):
|
|
|
|
return
|
|
|
|
|
2020-04-26 04:34:25 +02:00
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
def accept(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.saveField()
|
2020-05-04 13:52:48 +02:00
|
|
|
|
|
|
|
def save():
|
2020-04-26 04:34:25 +02:00
|
|
|
self.mm.save(self.model)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-04 13:52:48 +02:00
|
|
|
def on_done(fut):
|
|
|
|
try:
|
|
|
|
fut.result()
|
|
|
|
except TemplateError as e:
|
|
|
|
# fixme: i18n
|
|
|
|
showWarning("Unable to save changes: " + str(e))
|
|
|
|
return
|
|
|
|
self.mw.reset()
|
2020-05-15 05:59:44 +02:00
|
|
|
tooltip("Changes saved.", parent=self.mw)
|
2020-05-04 13:52:48 +02:00
|
|
|
QDialog.accept(self)
|
|
|
|
|
|
|
|
self.mw.taskman.with_progress(save, on_done, self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onHelp(self):
|
2020-10-09 18:38:07 +02:00
|
|
|
openHelp("editing?id=customizing-fields")
|