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-01-31 19:32:51 +01:00
|
|
|
from typing import Callable, List, Optional
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
import aqt
|
2021-02-11 10:57:19 +01:00
|
|
|
from anki.collection import SearchNode
|
2021-02-26 11:32:40 +01:00
|
|
|
from anki.decks import Deck
|
|
|
|
from anki.errors import DeckIsFilteredError, InvalidInput
|
2020-11-18 16:03:04 +01:00
|
|
|
from anki.lang import without_unicode_isolation
|
2021-02-05 09:50:01 +01:00
|
|
|
from aqt import AnkiQt, colors, gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2021-02-01 18:01:57 +01:00
|
|
|
from aqt.theme import theme_manager
|
2021-01-07 05:24:49 +01:00
|
|
|
from aqt.utils import (
|
|
|
|
TR,
|
2021-01-25 14:45:47 +01:00
|
|
|
HelpPage,
|
2021-01-07 05:24:49 +01:00
|
|
|
askUser,
|
|
|
|
disable_help_button,
|
|
|
|
openHelp,
|
|
|
|
restoreGeom,
|
|
|
|
saveGeom,
|
2021-01-25 23:21:32 +01:00
|
|
|
show_invalid_search_error,
|
2021-01-07 05:24:49 +01:00
|
|
|
showWarning,
|
|
|
|
tr,
|
|
|
|
)
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class DeckConf(QDialog):
|
2021-01-31 18:20:47 +01:00
|
|
|
"""Dialogue to modify and build a filtered deck."""
|
|
|
|
|
2021-02-01 09:56:10 +01:00
|
|
|
def __init__(
|
2021-02-01 19:10:05 +01:00
|
|
|
self,
|
|
|
|
mw: AnkiQt,
|
|
|
|
search: Optional[str] = None,
|
|
|
|
search_2: Optional[str] = None,
|
|
|
|
deck: Optional[Deck] = None,
|
2021-02-01 13:08:56 +01:00
|
|
|
) -> None:
|
2021-01-31 18:20:47 +01:00
|
|
|
"""If 'deck' is an existing filtered deck, load and modify its settings.
|
|
|
|
Otherwise, build a new one and derive settings from the current deck.
|
|
|
|
"""
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, mw)
|
|
|
|
self.mw = mw
|
2021-01-31 18:20:47 +01:00
|
|
|
self.did: Optional[int] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form = aqt.forms.dyndconf.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2021-01-31 18:20:47 +01:00
|
|
|
self.mw.checkpoint(tr(TR.ACTIONS_OPTIONS))
|
|
|
|
self.initialSetup()
|
|
|
|
self.old_deck = self.mw.col.decks.current()
|
|
|
|
|
|
|
|
if deck and deck["dyn"]:
|
|
|
|
# modify existing dyn deck
|
|
|
|
label = tr(TR.ACTIONS_REBUILD)
|
|
|
|
self.deck = deck
|
|
|
|
self.loadConf()
|
|
|
|
elif self.old_deck["dyn"]:
|
|
|
|
# create new dyn deck from other dyn deck
|
2020-11-17 08:42:43 +01:00
|
|
|
label = tr(TR.DECKS_BUILD)
|
2021-01-31 18:20:47 +01:00
|
|
|
self.loadConf(deck=self.old_deck)
|
|
|
|
self.new_dyn_deck()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-01-31 18:20:47 +01:00
|
|
|
# create new dyn deck from regular deck
|
|
|
|
label = tr(TR.DECKS_BUILD)
|
|
|
|
self.new_dyn_deck()
|
|
|
|
self.loadConf()
|
|
|
|
self.set_default_searches(self.old_deck["name"])
|
|
|
|
|
2021-02-01 21:02:22 +01:00
|
|
|
self.form.name.setText(self.deck["name"])
|
|
|
|
self.form.name.setPlaceholderText(self.deck["name"])
|
2021-02-01 19:10:05 +01:00
|
|
|
self.set_custom_searches(search, search_2)
|
2021-02-01 18:01:57 +01:00
|
|
|
qconnect(self.form.search_button.clicked, self.on_search_button)
|
2021-02-01 19:10:05 +01:00
|
|
|
qconnect(self.form.search_button_2.clicked, self.on_search_button_2)
|
2021-02-05 09:50:01 +01:00
|
|
|
color = theme_manager.color(colors.LINK)
|
2021-02-01 18:01:57 +01:00
|
|
|
self.setStyleSheet(
|
|
|
|
f"""QPushButton[flat=true] {{ text-align: left; color: {color}; padding: 0; border: 0 }}
|
|
|
|
QPushButton[flat=true]:hover {{ text-decoration: underline }}"""
|
|
|
|
)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setWindowModality(Qt.WindowModal)
|
2021-01-25 14:45:47 +01:00
|
|
|
qconnect(
|
|
|
|
self.form.buttonBox.helpRequested, lambda: openHelp(HelpPage.FILTERED_DECK)
|
|
|
|
)
|
2020-11-18 16:03:04 +01:00
|
|
|
self.setWindowTitle(
|
|
|
|
without_unicode_isolation(tr(TR.ACTIONS_OPTIONS_FOR, val=self.deck["name"]))
|
|
|
|
)
|
2021-02-01 23:20:57 +01:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Ok).setText(label)
|
2018-01-14 10:20:01 +01:00
|
|
|
if self.mw.col.schedVer() == 1:
|
|
|
|
self.form.secondFilter.setVisible(False)
|
2021-01-31 19:32:51 +01:00
|
|
|
restoreGeom(self, "dyndeckconf")
|
2018-01-14 10:20:01 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
2021-01-31 18:20:47 +01:00
|
|
|
|
2021-02-01 09:56:10 +01:00
|
|
|
def reopen(
|
2021-02-01 19:10:05 +01:00
|
|
|
self,
|
|
|
|
_mw: AnkiQt,
|
|
|
|
search: Optional[str] = None,
|
|
|
|
search_2: Optional[str] = None,
|
|
|
|
_deck: Optional[Deck] = None,
|
2021-02-01 23:46:56 +01:00
|
|
|
) -> None:
|
2021-02-01 19:10:05 +01:00
|
|
|
self.set_custom_searches(search, search_2)
|
2021-01-31 19:32:51 +01:00
|
|
|
|
2021-02-01 23:46:56 +01:00
|
|
|
def new_dyn_deck(self) -> None:
|
2021-01-31 18:20:47 +01:00
|
|
|
suffix: int = 1
|
|
|
|
while self.mw.col.decks.id_for_name(
|
|
|
|
without_unicode_isolation(tr(TR.QT_MISC_FILTERED_DECK, val=suffix))
|
|
|
|
):
|
|
|
|
suffix += 1
|
|
|
|
name: str = without_unicode_isolation(tr(TR.QT_MISC_FILTERED_DECK, val=suffix))
|
|
|
|
self.did = self.mw.col.decks.new_filtered(name)
|
|
|
|
self.deck = self.mw.col.decks.current()
|
|
|
|
|
2021-02-01 23:46:56 +01:00
|
|
|
def set_default_searches(self, deck_name: str) -> None:
|
2021-01-31 18:20:47 +01:00
|
|
|
self.form.search.setText(
|
|
|
|
self.mw.col.build_search_string(
|
2021-02-11 10:57:19 +01:00
|
|
|
SearchNode(deck=deck_name),
|
|
|
|
SearchNode(card_state=SearchNode.CARD_STATE_DUE),
|
2021-01-31 18:20:47 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.form.search_2.setText(
|
|
|
|
self.mw.col.build_search_string(
|
2021-02-11 10:57:19 +01:00
|
|
|
SearchNode(deck=deck_name),
|
|
|
|
SearchNode(card_state=SearchNode.CARD_STATE_NEW),
|
2021-01-31 18:20:47 +01:00
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 19:10:05 +01:00
|
|
|
def set_custom_searches(
|
|
|
|
self, search: Optional[str], search_2: Optional[str]
|
|
|
|
) -> None:
|
|
|
|
if search is not None:
|
|
|
|
self.form.search.setText(search)
|
|
|
|
self.form.search.setFocus()
|
|
|
|
self.form.search.selectAll()
|
|
|
|
if search_2 is not None:
|
|
|
|
self.form.secondFilter.setChecked(True)
|
|
|
|
self.form.filter2group.setVisible(True)
|
|
|
|
self.form.search_2.setText(search_2)
|
|
|
|
self.form.search_2.setFocus()
|
|
|
|
self.form.search_2.selectAll()
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def initialSetup(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
import anki.consts as cs
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-11-17 10:23:06 +01:00
|
|
|
self.form.order.addItems(list(cs.dynOrderLabels(self.mw.col).values()))
|
|
|
|
self.form.order_2.addItems(list(cs.dynOrderLabels(self.mw.col).values()))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.form.resched.stateChanged, self._onReschedToggled)
|
2018-01-20 06:26:11 +01:00
|
|
|
|
2021-02-01 19:10:05 +01:00
|
|
|
def on_search_button(self) -> None:
|
|
|
|
self._on_search_button(self.form.search)
|
|
|
|
|
|
|
|
def on_search_button_2(self) -> None:
|
|
|
|
self._on_search_button(self.form.search_2)
|
|
|
|
|
|
|
|
def _on_search_button(self, line: QLineEdit) -> None:
|
2021-02-01 13:55:03 +01:00
|
|
|
try:
|
2021-02-01 19:10:05 +01:00
|
|
|
search = self.mw.col.build_search_string(line.text())
|
2021-02-01 13:55:03 +01:00
|
|
|
except InvalidInput as err:
|
2021-02-01 19:10:05 +01:00
|
|
|
line.setFocus()
|
|
|
|
line.selectAll()
|
2021-02-01 13:55:03 +01:00
|
|
|
show_invalid_search_error(err)
|
|
|
|
else:
|
|
|
|
aqt.dialogs.open("Browser", self.mw, search=(search,))
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def _onReschedToggled(self, _state: int) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.previewDelayWidget.setVisible(
|
|
|
|
not self.form.resched.isChecked() and self.mw.col.schedVer() > 1
|
|
|
|
)
|
2018-01-20 06:26:11 +01:00
|
|
|
|
2021-02-01 23:33:41 +01:00
|
|
|
def loadConf(self, deck: Optional[Deck] = None) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
f = self.form
|
2021-01-31 18:20:47 +01:00
|
|
|
d = deck or self.deck
|
2018-01-14 04:08:38 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
f.resched.setChecked(d["resched"])
|
2018-01-20 06:26:11 +01:00
|
|
|
self._onReschedToggled(0)
|
2018-01-14 04:08:38 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
search, limit, order = d["terms"][0]
|
2012-12-21 08:51:59 +01:00
|
|
|
f.search.setText(search)
|
2018-04-30 08:26:43 +02:00
|
|
|
|
|
|
|
if self.mw.col.schedVer() == 1:
|
2019-12-23 01:34:10 +01:00
|
|
|
if d["delays"]:
|
|
|
|
f.steps.setText(self.listToUser(d["delays"]))
|
2018-04-30 08:26:43 +02:00
|
|
|
f.stepsOn.setChecked(True)
|
|
|
|
else:
|
|
|
|
f.steps.setVisible(False)
|
|
|
|
f.stepsOn.setVisible(False)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
f.order.setCurrentIndex(order)
|
|
|
|
f.limit.setValue(limit)
|
2018-01-20 06:26:11 +01:00
|
|
|
f.previewDelay.setValue(d.get("previewDelay", 10))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
if len(d["terms"]) > 1:
|
|
|
|
search, limit, order = d["terms"][1]
|
2018-01-14 04:08:38 +01:00
|
|
|
f.search_2.setText(search)
|
|
|
|
f.order_2.setCurrentIndex(order)
|
|
|
|
f.limit_2.setValue(limit)
|
|
|
|
f.secondFilter.setChecked(True)
|
|
|
|
f.filter2group.setVisible(True)
|
|
|
|
else:
|
|
|
|
f.order_2.setCurrentIndex(5)
|
|
|
|
f.limit_2.setValue(20)
|
|
|
|
f.secondFilter.setChecked(False)
|
|
|
|
f.filter2group.setVisible(False)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def saveConf(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
f = self.form
|
|
|
|
d = self.deck
|
2021-02-01 21:02:22 +01:00
|
|
|
|
|
|
|
if f.name.text() and d["name"] != f.name.text():
|
|
|
|
self.mw.col.decks.rename(d, f.name.text())
|
|
|
|
gui_hooks.sidebar_should_refresh_decks()
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
d["resched"] = f.resched.isChecked()
|
|
|
|
d["delays"] = None
|
2018-01-14 04:08:38 +01:00
|
|
|
|
2018-04-30 08:26:43 +02:00
|
|
|
if self.mw.col.schedVer() == 1 and f.stepsOn.isChecked():
|
|
|
|
steps = self.userToList(f.steps)
|
|
|
|
if steps:
|
2019-12-23 01:34:10 +01:00
|
|
|
d["delays"] = steps
|
2018-04-30 08:26:43 +02:00
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
d["delays"] = None
|
2018-04-30 08:26:43 +02:00
|
|
|
|
2021-01-29 18:27:33 +01:00
|
|
|
search = self.mw.col.build_search_string(f.search.text())
|
2021-01-25 23:21:32 +01:00
|
|
|
terms = [[search, f.limit.value(), f.order.currentIndex()]]
|
2018-01-14 04:08:38 +01:00
|
|
|
|
|
|
|
if f.secondFilter.isChecked():
|
2021-01-29 18:27:33 +01:00
|
|
|
search_2 = self.mw.col.build_search_string(f.search_2.text())
|
2021-01-25 23:21:32 +01:00
|
|
|
terms.append([search_2, f.limit_2.value(), f.order_2.currentIndex()])
|
2018-01-14 04:08:38 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
d["terms"] = terms
|
|
|
|
d["previewDelay"] = f.previewDelay.value()
|
2018-01-14 04:08:38 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.col.decks.save(d)
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def reject(self) -> None:
|
2021-01-31 18:20:47 +01:00
|
|
|
if self.did:
|
|
|
|
self.mw.col.decks.rem(self.did)
|
|
|
|
self.mw.col.decks.select(self.old_deck["id"])
|
|
|
|
saveGeom(self, "dyndeckconf")
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.reject(self)
|
2021-01-31 19:32:51 +01:00
|
|
|
aqt.dialogs.markClosed("DynDeckConfDialog")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def accept(self) -> None:
|
2021-01-25 23:21:32 +01:00
|
|
|
try:
|
|
|
|
self.saveConf()
|
|
|
|
except InvalidInput as err:
|
2021-02-01 23:46:56 +01:00
|
|
|
show_invalid_search_error(err)
|
2021-02-26 11:32:40 +01:00
|
|
|
except DeckIsFilteredError as err:
|
|
|
|
showWarning(str(err))
|
2021-02-01 23:46:56 +01:00
|
|
|
else:
|
|
|
|
if not self.mw.col.sched.rebuild_filtered_deck(self.deck["id"]):
|
|
|
|
if askUser(tr(TR.DECKS_THE_PROVIDED_SEARCH_DID_NOT_MATCH)):
|
|
|
|
return
|
|
|
|
saveGeom(self, "dyndeckconf")
|
|
|
|
self.mw.reset()
|
|
|
|
QDialog.accept(self)
|
|
|
|
aqt.dialogs.markClosed("DynDeckConfDialog")
|
|
|
|
|
|
|
|
def closeWithCallback(self, callback: Callable) -> None:
|
2021-01-31 19:32:51 +01:00
|
|
|
self.reject()
|
|
|
|
callback()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Step load/save - fixme: share with std options screen
|
|
|
|
########################################################
|
|
|
|
|
2021-02-01 09:56:10 +01:00
|
|
|
def listToUser(self, values: List[Union[float, int]]) -> str:
|
|
|
|
return " ".join(
|
|
|
|
[str(int(val)) if int(val) == val else str(val) for val in values]
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 09:56:10 +01:00
|
|
|
def userToList(
|
|
|
|
self, line: QLineEdit, minSize: int = 1
|
|
|
|
) -> Optional[List[Union[float, int]]]:
|
|
|
|
items = str(line.text()).split(" ")
|
2012-12-21 08:51:59 +01:00
|
|
|
ret = []
|
2020-07-24 00:04:46 +02:00
|
|
|
for item in items:
|
|
|
|
if not item:
|
2012-12-21 08:51:59 +01:00
|
|
|
continue
|
|
|
|
try:
|
2020-07-24 00:04:46 +02:00
|
|
|
i = float(item)
|
2012-12-21 08:51:59 +01:00
|
|
|
assert i > 0
|
|
|
|
if i == int(i):
|
|
|
|
i = int(i)
|
|
|
|
ret.append(i)
|
|
|
|
except:
|
|
|
|
# invalid, don't update
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.SCHEDULING_STEPS_MUST_BE_NUMBERS))
|
2020-07-24 00:04:46 +02:00
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
if len(ret) < minSize:
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.SCHEDULING_AT_LEAST_ONE_STEP_IS_REQUIRED))
|
2020-07-24 00:04:46 +02:00
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
return ret
|