anki/qt/aqt/deckconf.py

332 lines
11 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2020-04-20 05:15:23 +02:00
from operator import itemgetter
2020-04-20 05:15:23 +02:00
from typing import Dict, Union
2020-04-05 05:08:15 +02:00
import aqt
2019-12-20 10:19:03 +01:00
from anki.consts import NEW_CARDS_RANDOM
2019-03-04 02:58:34 +01:00
from anki.lang import _, ngettext
from aqt import 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 (
askUser,
getOnlyText,
openHelp,
restoreGeom,
saveGeom,
showInfo,
showWarning,
tooltip,
)
2019-12-20 10:19:03 +01:00
class DeckConf(QDialog):
2020-04-20 05:15:23 +02:00
def __init__(self, mw: aqt.AnkiQt, deck: Dict):
QDialog.__init__(self, mw)
self.mw = mw
self.deck = deck
2019-12-23 01:34:10 +01:00
self.childDids = [d[1] for d in self.mw.col.decks.children(self.deck["id"])]
self._origNewOrder = None
self.form = aqt.forms.dconf.Ui_Dialog()
self.form.setupUi(self)
gui_hooks.deck_conf_did_setup_ui_form(self)
self.mw.checkpoint(_("Options"))
self.setupCombos()
self.setupConfs()
self.setWindowModality(Qt.WindowModal)
qconnect(self.form.buttonBox.helpRequested, lambda: openHelp("deckoptions"))
qconnect(self.form.confOpts.clicked, self.confOpts)
qconnect(
self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults).clicked,
self.onRestore,
2019-12-23 01:34:10 +01:00
)
self.setWindowTitle(_("Options for %s") % self.deck["name"])
2013-05-23 04:09:54 +02:00
# qt doesn't size properly with altered fonts otherwise
restoreGeom(self, "deckconf", adjustSize=True)
gui_hooks.deck_conf_will_show(self)
2013-05-23 04:09:54 +02:00
self.show()
self.exec_()
saveGeom(self, "deckconf")
def setupCombos(self):
import anki.consts as cs
2019-12-23 01:34:10 +01:00
f = self.form
f.newOrder.addItems(list(cs.newCardOrderLabels().values()))
qconnect(f.newOrder.currentIndexChanged, self.onNewOrderChanged)
# Conf list
######################################################################
def setupConfs(self):
qconnect(self.form.dconf.currentIndexChanged, self.onConfChange)
self.conf = None
self.loadConfs()
def loadConfs(self):
2019-12-23 01:34:10 +01:00
current = self.deck["conf"]
self.confList = self.mw.col.decks.allConf()
2019-12-23 01:34:10 +01:00
self.confList.sort(key=itemgetter("name"))
startOn = 0
self.ignoreConfChange = True
self.form.dconf.clear()
for idx, conf in enumerate(self.confList):
2019-12-23 01:34:10 +01:00
self.form.dconf.addItem(conf["name"])
if str(conf["id"]) == str(current):
startOn = idx
self.ignoreConfChange = False
self.form.dconf.setCurrentIndex(startOn)
if self._origNewOrder is None:
2019-12-23 01:34:10 +01:00
self._origNewOrder = self.confList[startOn]["new"]["order"]
self.onConfChange(startOn)
def confOpts(self):
m = QMenu(self.mw)
a = m.addAction(_("Add"))
qconnect(a.triggered, self.addGroup)
a = m.addAction(_("Delete"))
qconnect(a.triggered, self.remGroup)
a = m.addAction(_("Rename"))
qconnect(a.triggered, self.renameGroup)
a = m.addAction(_("Set for all subdecks"))
qconnect(a.triggered, self.setChildren)
if not self.childDids:
a.setEnabled(False)
m.exec_(QCursor.pos())
def onConfChange(self, idx):
if self.ignoreConfChange:
return
if self.conf:
self.saveConf()
conf = self.confList[idx]
2019-12-23 01:34:10 +01:00
self.deck["conf"] = conf["id"]
2020-05-17 06:37:59 +02:00
self.mw.col.decks.save(self.deck)
self.loadConf()
2020-05-17 06:37:59 +02:00
cnt = len(self.mw.col.decks.didsForConf(conf))
2013-05-16 09:50:48 +02:00
if cnt > 1:
2019-12-23 01:34:10 +01:00
txt = _(
"Your changes will affect multiple decks. If you wish to "
"change only the current deck, please add a new options group first."
)
2013-05-16 09:50:48 +02:00
else:
txt = ""
self.form.count.setText(txt)
2020-04-20 05:15:23 +02:00
def addGroup(self) -> None:
name = getOnlyText(_("New options group name:"))
if not name:
return
# first, save currently entered data to current conf
self.saveConf()
# then clone the conf
2020-04-20 05:15:23 +02:00
id = self.mw.col.decks.add_config_returning_id(name, clone_from=self.conf)
gui_hooks.deck_conf_did_add_config(self, self.deck, self.conf, name, id)
# set the deck to the new conf
2019-12-23 01:34:10 +01:00
self.deck["conf"] = id
# then reload the conf list
self.loadConfs()
2020-04-20 05:23:05 +02:00
def remGroup(self) -> None:
2019-12-23 01:34:10 +01:00
if int(self.conf["id"]) == 1:
showInfo(_("The default configuration can't be removed."), self)
else:
gui_hooks.deck_conf_will_remove_config(self, self.deck, self.conf)
2020-04-20 05:23:05 +02:00
self.mw.col.modSchema(check=True)
self.mw.col.decks.remove_config(self.conf["id"])
self.conf = None
2019-12-23 01:34:10 +01:00
self.deck["conf"] = 1
self.loadConfs()
2020-04-20 05:23:05 +02:00
def renameGroup(self) -> None:
2019-12-23 01:34:10 +01:00
old = self.conf["name"]
name = getOnlyText(_("New name:"), default=old)
if not name or name == old:
return
gui_hooks.deck_conf_will_rename_config(self, self.deck, self.conf, name)
2019-12-23 01:34:10 +01:00
self.conf["name"] = name
2020-04-20 05:17:55 +02:00
self.saveConf()
self.loadConfs()
def setChildren(self):
if not askUser(
2019-12-23 01:34:10 +01:00
_("Set all decks below %s to this option group?") % self.deck["name"]
):
return
for did in self.childDids:
deck = self.mw.col.decks.get(did)
2019-12-23 01:34:10 +01:00
if deck["dyn"]:
continue
2019-12-23 01:34:10 +01:00
deck["conf"] = self.deck["conf"]
self.mw.col.decks.save(deck)
2019-12-23 01:34:10 +01:00
tooltip(
ngettext("%d deck updated.", "%d decks updated.", len(self.childDids))
% len(self.childDids)
)
# Loading
##################################################
def listToUser(self, l):
2020-04-05 05:08:15 +02:00
def num_to_user(n: Union[int, float]):
if n == round(n):
return str(int(n))
else:
return str(n)
return " ".join(map(num_to_user, l))
def parentLimText(self, type="new"):
# top level?
2019-12-23 01:34:10 +01:00
if "::" not in self.deck["name"]:
return ""
lim = -1
2019-12-23 01:34:10 +01:00
for d in self.mw.col.decks.parents(self.deck["id"]):
c = self.mw.col.decks.confForDid(d["id"])
x = c[type]["perDay"]
if lim == -1:
lim = x
else:
lim = min(x, lim)
return _("(parent limit: %d)") % lim
def loadConf(self):
2019-12-23 01:34:10 +01:00
self.conf = self.mw.col.decks.confForDid(self.deck["id"])
# new
2019-12-23 01:34:10 +01:00
c = self.conf["new"]
f = self.form
2019-12-23 01:34:10 +01:00
f.lrnSteps.setText(self.listToUser(c["delays"]))
f.lrnGradInt.setValue(c["ints"][0])
f.lrnEasyInt.setValue(c["ints"][1])
f.lrnFactor.setValue(c["initialFactor"] / 10.0)
f.newOrder.setCurrentIndex(c["order"])
f.newPerDay.setValue(c["perDay"])
f.bury.setChecked(c.get("bury", True))
2019-12-23 01:34:10 +01:00
f.newplim.setText(self.parentLimText("new"))
# rev
2019-12-23 01:34:10 +01:00
c = self.conf["rev"]
f.revPerDay.setValue(c["perDay"])
f.easyBonus.setValue(c["ease4"] * 100)
f.fi1.setValue(c["ivlFct"] * 100)
f.maxIvl.setValue(c["maxIvl"])
f.revplim.setText(self.parentLimText("rev"))
f.buryRev.setChecked(c.get("bury", True))
2019-12-23 01:34:10 +01:00
f.hardFactor.setValue(int(c.get("hardFactor", 1.2) * 100))
2018-07-11 13:23:22 +02:00
if self.mw.col.schedVer() == 1:
f.hardFactor.setVisible(False)
f.hardFactorLabel.setVisible(False)
# lapse
2019-12-23 01:34:10 +01:00
c = self.conf["lapse"]
f.lapSteps.setText(self.listToUser(c["delays"]))
f.lapMult.setValue(c["mult"] * 100)
f.lapMinInt.setValue(c["minInt"])
f.leechThreshold.setValue(c["leechFails"])
f.leechAction.setCurrentIndex(c["leechAction"])
# general
c = self.conf
2019-12-23 01:34:10 +01:00
f.maxTaken.setValue(c["maxTaken"])
f.showTimer.setChecked(c.get("timer", 0))
f.autoplaySounds.setChecked(c["autoplay"])
f.replayQuestion.setChecked(c.get("replayq", True))
# description
2019-12-23 01:34:10 +01:00
f.desc.setPlainText(self.deck["desc"])
gui_hooks.deck_conf_did_load_config(self, self.deck, self.conf)
def onRestore(self):
self.mw.progress.start()
self.mw.col.decks.restoreToDefault(self.conf)
self.mw.progress.finish()
self.loadConf()
# New order
##################################################
def onNewOrderChanged(self, new):
2019-12-23 01:34:10 +01:00
old = self.conf["new"]["order"]
if old == new:
return
2019-12-23 01:34:10 +01:00
self.conf["new"]["order"] = new
self.mw.progress.start()
self.mw.col.sched.resortConf(self.conf)
self.mw.progress.finish()
# Saving
##################################################
def updateList(self, conf, key, w, minSize=1):
items = str(w.text()).split(" ")
ret = []
for i in items:
if not i:
continue
try:
i = float(i)
assert i > 0
if i == int(i):
i = int(i)
ret.append(i)
except:
# invalid, don't update
showWarning(_("Steps must be numbers."))
return
if len(ret) < minSize:
showWarning(_("At least one step is required."))
return
conf[key] = ret
def saveConf(self):
# new
2019-12-23 01:34:10 +01:00
c = self.conf["new"]
f = self.form
2019-12-23 01:34:10 +01:00
self.updateList(c, "delays", f.lrnSteps)
c["ints"][0] = f.lrnGradInt.value()
c["ints"][1] = f.lrnEasyInt.value()
c["initialFactor"] = f.lrnFactor.value() * 10
c["order"] = f.newOrder.currentIndex()
c["perDay"] = f.newPerDay.value()
c["bury"] = f.bury.isChecked()
if self._origNewOrder != c["order"]:
# order of current deck has changed, so have to resort
2019-12-23 01:34:10 +01:00
if c["order"] == NEW_CARDS_RANDOM:
self.mw.col.sched.randomizeCards(self.deck["id"])
else:
2019-12-23 01:34:10 +01:00
self.mw.col.sched.orderCards(self.deck["id"])
# rev
2019-12-23 01:34:10 +01:00
c = self.conf["rev"]
c["perDay"] = f.revPerDay.value()
c["ease4"] = f.easyBonus.value() / 100.0
c["ivlFct"] = f.fi1.value() / 100.0
c["maxIvl"] = f.maxIvl.value()
c["bury"] = f.buryRev.isChecked()
c["hardFactor"] = f.hardFactor.value() / 100.0
# lapse
2019-12-23 01:34:10 +01:00
c = self.conf["lapse"]
self.updateList(c, "delays", f.lapSteps, minSize=0)
c["mult"] = f.lapMult.value() / 100.0
c["minInt"] = f.lapMinInt.value()
c["leechFails"] = f.leechThreshold.value()
c["leechAction"] = f.leechAction.currentIndex()
# general
c = self.conf
2019-12-23 01:34:10 +01:00
c["maxTaken"] = f.maxTaken.value()
c["timer"] = f.showTimer.isChecked() and 1 or 0
c["autoplay"] = f.autoplaySounds.isChecked()
c["replayq"] = f.replayQuestion.isChecked()
# description
2019-12-23 01:34:10 +01:00
self.deck["desc"] = f.desc.toPlainText()
gui_hooks.deck_conf_will_save_config(self, self.deck, self.conf)
self.mw.col.decks.save(self.deck)
self.mw.col.decks.save(self.conf)
def reject(self):
self.accept()
def accept(self):
self.saveConf()
self.mw.reset()
QDialog.accept(self)