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
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2021-04-30 09:15:59 +02:00
|
|
|
from anki.collection import OpChanges
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.consts import *
|
2020-11-18 16:03:04 +01:00
|
|
|
from anki.lang import without_unicode_isolation
|
2021-03-27 12:46:49 +01:00
|
|
|
from anki.models import NotetypeDict
|
2020-10-11 19:16:30 +02:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2021-04-30 09:15:59 +02:00
|
|
|
from aqt.operations.notetype import 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
|
2021-01-07 05:24:49 +01:00
|
|
|
from aqt.utils import (
|
2021-01-25 14:45:47 +01:00
|
|
|
HelpPage,
|
2021-01-07 05:24:49 +01:00
|
|
|
askUser,
|
|
|
|
disable_help_button,
|
|
|
|
getOnlyText,
|
|
|
|
openHelp,
|
|
|
|
showWarning,
|
|
|
|
tooltip,
|
|
|
|
tr,
|
|
|
|
)
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class FieldDialog(QDialog):
|
2021-02-02 15:00:29 +01:00
|
|
|
def __init__(
|
2021-06-16 15:40:48 +02:00
|
|
|
self,
|
|
|
|
mw: AnkiQt,
|
|
|
|
nt: NotetypeDict,
|
|
|
|
parent: Optional[QWidget] = None,
|
2021-06-16 17:04:11 +02:00
|
|
|
open_at: int = 0,
|
2021-02-02 15:00:29 +01:00
|
|
|
) -> None:
|
2020-05-04 13:52:48 +02:00
|
|
|
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-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-18 16:03:04 +01:00
|
|
|
self.setWindowTitle(
|
2021-03-26 05:21:04 +01:00
|
|
|
without_unicode_isolation(tr.fields_fields_for(val=self.model["name"]))
|
2020-11-18 16:03:04 +01:00
|
|
|
)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2021-10-05 05:53:01 +02:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.StandardButton.Help).setAutoDefault(
|
|
|
|
False
|
|
|
|
)
|
|
|
|
self.form.buttonBox.button(
|
|
|
|
QDialogButtonBox.StandardButton.Cancel
|
|
|
|
).setAutoDefault(False)
|
|
|
|
self.form.buttonBox.button(QDialogButtonBox.StandardButton.Save).setAutoDefault(
|
|
|
|
False
|
|
|
|
)
|
2021-02-01 13:08:56 +01:00
|
|
|
self.currentIdx: Optional[int] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fillFields()
|
|
|
|
self.setupSignals()
|
2021-10-05 05:53:01 +02:00
|
|
|
self.form.fieldList.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)
|
2021-03-26 07:06:02 +01:00
|
|
|
self.form.fieldList.dropEvent = self.onDrop # type: ignore[assignment]
|
2021-06-16 15:40:48 +02:00
|
|
|
self.form.fieldList.setCurrentRow(open_at)
|
2021-10-05 02:01:45 +02:00
|
|
|
self.exec()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def fillFields(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.currentIdx = None
|
|
|
|
self.form.fieldList.clear()
|
2019-12-23 01:34:10 +01:00
|
|
|
for c, f in enumerate(self.model["flds"]):
|
2021-02-11 01:09:06 +01:00
|
|
|
self.form.fieldList.addItem(f"{c + 1}: {f['name']}")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def setupSignals(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
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
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def onDrop(self, ev: QDropEvent) -> None:
|
2020-04-07 09:42:33 +02:00
|
|
|
fieldList = self.form.fieldList
|
|
|
|
indicatorPos = fieldList.dropIndicatorPosition()
|
2021-10-05 05:53:01 +02:00
|
|
|
if qtmajor == 5:
|
|
|
|
pos = ev.pos() # type: ignore
|
|
|
|
else:
|
|
|
|
pos = ev.position().toPoint()
|
|
|
|
dropPos = fieldList.indexAt(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
|
2021-10-05 05:53:01 +02:00
|
|
|
if (
|
|
|
|
indicatorPos == QAbstractItemView.DropIndicatorPosition.OnViewport
|
|
|
|
): # to bottom.
|
2020-05-26 11:27:38 +02:00
|
|
|
movePos = fieldList.count() - 1
|
2021-10-05 05:53:01 +02:00
|
|
|
elif indicatorPos == QAbstractItemView.DropIndicatorPosition.AboveItem:
|
2020-05-26 11:27:38 +02:00
|
|
|
movePos = dropPos
|
2021-10-05 05:53:01 +02:00
|
|
|
elif indicatorPos == QAbstractItemView.DropIndicatorPosition.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
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def onRowChange(self, idx: int) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if idx == -1:
|
|
|
|
return
|
|
|
|
self.saveField()
|
|
|
|
self.loadField(idx)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def _uniqueName(
|
|
|
|
self, prompt: str, ignoreOrd: Optional[int] = None, old: str = ""
|
|
|
|
) -> Optional[str]:
|
2020-12-27 08:29:31 +01:00
|
|
|
txt = getOnlyText(prompt, default=old).replace('"', "").strip()
|
2012-12-21 08:51:59 +01:00
|
|
|
if not txt:
|
2021-02-01 13:08:56 +01:00
|
|
|
return None
|
2020-12-27 08:51:58 +01:00
|
|
|
if txt[0] in "#^/":
|
2021-03-26 04:48:26 +01:00
|
|
|
showWarning(tr.fields_name_first_letter_not_valid())
|
2021-02-01 13:08:56 +01:00
|
|
|
return None
|
2020-12-27 10:07:56 +01:00
|
|
|
for letter in """:{"}""":
|
|
|
|
if letter in txt:
|
2021-03-26 04:48:26 +01:00
|
|
|
showWarning(tr.fields_name_invalid_letter())
|
2021-02-01 13:08:56 +01:00
|
|
|
return None
|
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:
|
2021-03-26 04:48:26 +01:00
|
|
|
showWarning(tr.fields_that_field_name_is_already_used())
|
2021-02-01 13:08:56 +01:00
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
return txt
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onRename(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
idx = self.currentIdx
|
2019-12-23 01:34:10 +01:00
|
|
|
f = self.model["flds"][idx]
|
2021-03-26 04:48:26 +01:00
|
|
|
name = self._uniqueName(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
|
|
|
|
2020-12-01 02:28:10 +01:00
|
|
|
old_name = f["name"]
|
2020-05-15 05:59:44 +02:00
|
|
|
self.change_tracker.mark_basic()
|
|
|
|
self.mm.rename_field(self.model, f, name)
|
2020-12-01 02:28:10 +01:00
|
|
|
gui_hooks.fields_did_rename_field(self, f, old_name)
|
2020-10-11 19:16:30 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.saveField()
|
|
|
|
self.fillFields()
|
|
|
|
self.form.fieldList.setCurrentRow(idx)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def onAdd(self) -> None:
|
2021-03-26 04:48:26 +01:00
|
|
|
name = self._uniqueName(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()
|
2021-06-27 05:49:58 +02:00
|
|
|
f = self.mm.new_field(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
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onDelete(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
if len(self.model["flds"]) < 2:
|
2021-03-26 04:48:26 +01:00
|
|
|
showWarning(tr.fields_notes_require_at_least_one_field())
|
2021-02-01 14:28:21 +01:00
|
|
|
return
|
2021-06-27 05:49:58 +02:00
|
|
|
count = self.mm.use_count(self.model)
|
2021-03-26 05:21:04 +01:00
|
|
|
c = tr.browsing_note_count(count=count)
|
|
|
|
if not askUser(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)
|
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def onPosition(self, delta: int = -1) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
idx = self.currentIdx
|
2019-12-23 01:34:10 +01:00
|
|
|
l = len(self.model["flds"])
|
2021-03-26 05:21:04 +01:00
|
|
|
txt = getOnlyText(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)
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onSortField(self) -> None:
|
2020-05-15 05:59:44 +02:00
|
|
|
if not self.change_tracker.mark_schema():
|
2021-02-01 14:28:21 +01:00
|
|
|
return
|
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
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def moveField(self, pos: int) -> None:
|
2020-05-15 05:59:44 +02:00
|
|
|
if not self.change_tracker.mark_schema():
|
2021-02-01 14:28:21 +01:00
|
|
|
return
|
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
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def loadField(self, idx: int) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
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.sortField.setChecked(self.model["sortf"] == fld["ord"])
|
|
|
|
f.rtl.setChecked(fld["rtl"])
|
2021-11-06 00:42:48 +01:00
|
|
|
f.fieldDescription.setText(fld.get("description", ""))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def saveField(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# 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()
|
|
|
|
rtl = f.rtl.isChecked()
|
|
|
|
if fld["rtl"] != rtl:
|
|
|
|
fld["rtl"] = rtl
|
|
|
|
self.change_tracker.mark_basic()
|
2021-11-06 00:42:48 +01:00
|
|
|
desc = f.fieldDescription.text()
|
|
|
|
if fld.get("description", "") != desc:
|
|
|
|
fld["description"] = desc
|
|
|
|
self.change_tracker.mark_basic()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def reject(self) -> None:
|
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)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def accept(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.saveField()
|
2020-05-04 13:52:48 +02:00
|
|
|
|
2021-04-30 09:15:59 +02:00
|
|
|
def on_done(changes: OpChanges) -> None:
|
2021-04-30 09:30:48 +02:00
|
|
|
tooltip(tr.card_templates_changes_saved(), parent=self.parentWidget())
|
2020-05-04 13:52:48 +02:00
|
|
|
QDialog.accept(self)
|
|
|
|
|
2021-04-30 09:15:59 +02:00
|
|
|
update_notetype_legacy(parent=self.mw, notetype=self.model).success(
|
|
|
|
on_done
|
|
|
|
).run_in_background()
|
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.CUSTOMIZING_FIELDS)
|