2021-04-22 02:55:32 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import aqt
|
2021-05-27 05:11:20 +02:00
|
|
|
import aqt.deckconf
|
|
|
|
from anki.cards import Card
|
2021-05-31 08:24:51 +02:00
|
|
|
from anki.decks import DeckDict, DeckId
|
2021-05-19 17:21:57 +02:00
|
|
|
from anki.lang import without_unicode_isolation
|
2021-04-24 02:14:54 +02:00
|
|
|
from aqt import gui_hooks
|
2021-04-22 02:55:32 +02:00
|
|
|
from aqt.qt import *
|
2021-05-27 05:11:20 +02:00
|
|
|
from aqt.utils import (
|
|
|
|
KeyboardModifiersPressed,
|
|
|
|
addCloseShortcut,
|
|
|
|
disable_help_button,
|
|
|
|
restoreGeom,
|
|
|
|
saveGeom,
|
|
|
|
tr,
|
|
|
|
)
|
2021-04-22 02:55:32 +02:00
|
|
|
from aqt.webview import AnkiWebView
|
|
|
|
|
|
|
|
|
|
|
|
class DeckOptionsDialog(QDialog):
|
|
|
|
"The new deck configuration screen."
|
|
|
|
|
|
|
|
TITLE = "deckOptions"
|
|
|
|
silentlyClose = True
|
|
|
|
|
2021-05-31 08:24:51 +02:00
|
|
|
def __init__(self, mw: aqt.main.AnkiQt, deck: DeckDict) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
QDialog.__init__(self, mw, Qt.WindowType.Window)
|
2021-04-22 02:55:32 +02:00
|
|
|
self.mw = mw
|
2021-05-27 05:11:20 +02:00
|
|
|
self._deck = deck
|
2021-04-22 02:55:32 +02:00
|
|
|
self._setup_ui()
|
|
|
|
self.show()
|
|
|
|
|
|
|
|
def _setup_ui(self) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
self.setWindowModality(Qt.WindowModality.ApplicationModal)
|
2021-04-22 02:55:32 +02:00
|
|
|
self.mw.garbage_collect_on_dialog_finish(self)
|
|
|
|
self.setMinimumWidth(400)
|
|
|
|
disable_help_button(self)
|
|
|
|
restoreGeom(self, self.TITLE)
|
|
|
|
addCloseShortcut(self)
|
|
|
|
|
|
|
|
self.web = AnkiWebView(title=self.TITLE)
|
|
|
|
self.web.setVisible(False)
|
2021-06-30 19:55:56 +02:00
|
|
|
self.web.load_ts_page("deck-options")
|
2021-04-22 02:55:32 +02:00
|
|
|
layout = QVBoxLayout()
|
|
|
|
layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
layout.addWidget(self.web)
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
2021-04-24 02:14:54 +02:00
|
|
|
self.web.eval(
|
2022-01-16 06:05:35 +01:00
|
|
|
f"""const $deckOptions = anki.setupDeckOptions({self._deck["id"]});"""
|
2021-04-24 02:14:54 +02:00
|
|
|
)
|
2021-05-19 17:21:57 +02:00
|
|
|
self.setWindowTitle(
|
2021-05-31 08:24:51 +02:00
|
|
|
without_unicode_isolation(tr.actions_options_for(val=self._deck["name"]))
|
2021-05-19 17:21:57 +02:00
|
|
|
)
|
2021-04-24 02:14:54 +02:00
|
|
|
gui_hooks.deck_options_did_load(self)
|
2021-04-22 02:55:32 +02:00
|
|
|
|
|
|
|
def reject(self) -> None:
|
2021-11-29 03:31:37 +01:00
|
|
|
self.web.cleanup()
|
2021-04-22 02:55:32 +02:00
|
|
|
self.web = None
|
|
|
|
saveGeom(self, self.TITLE)
|
|
|
|
QDialog.reject(self)
|
2021-05-27 05:11:20 +02:00
|
|
|
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def confirm_deck_then_display_options(active_card: Card | None = None) -> None:
|
2021-05-31 08:24:51 +02:00
|
|
|
decks = [aqt.mw.col.decks.current()]
|
2021-05-27 05:11:20 +02:00
|
|
|
if card := active_card:
|
2021-05-31 08:24:51 +02:00
|
|
|
if card.odid and card.odid != decks[0]["id"]:
|
|
|
|
decks.append(aqt.mw.col.decks.get(card.odid))
|
2021-05-27 05:11:20 +02:00
|
|
|
|
2021-05-31 08:24:51 +02:00
|
|
|
if not any(d["id"] == card.did for d in decks):
|
2021-08-19 04:33:23 +02:00
|
|
|
decks.append(aqt.mw.col.decks.get(card.did))
|
2021-05-27 05:11:20 +02:00
|
|
|
|
|
|
|
if len(decks) == 1:
|
|
|
|
display_options_for_deck(decks[0])
|
|
|
|
else:
|
2021-05-31 08:24:51 +02:00
|
|
|
decks.sort(key=lambda x: x["dyn"])
|
2021-05-27 05:11:20 +02:00
|
|
|
_deck_prompt_dialog(decks)
|
|
|
|
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def _deck_prompt_dialog(decks: list[DeckDict]) -> None:
|
2021-05-27 05:11:20 +02:00
|
|
|
diag = QDialog(aqt.mw.app.activeWindow())
|
|
|
|
diag.setWindowTitle("Anki")
|
|
|
|
box = QVBoxLayout()
|
|
|
|
box.addWidget(QLabel(tr.deck_config_which_deck()))
|
|
|
|
for deck in decks:
|
2021-05-31 08:24:51 +02:00
|
|
|
button = QPushButton(deck["name"])
|
2021-05-27 05:11:20 +02:00
|
|
|
qconnect(button.clicked, diag.close)
|
2021-12-06 08:14:09 +01:00
|
|
|
qconnect(button.clicked, lambda _, deck=deck: display_options_for_deck(deck))
|
2021-05-27 05:11:20 +02:00
|
|
|
box.addWidget(button)
|
|
|
|
button = QPushButton(tr.actions_cancel())
|
|
|
|
qconnect(button.clicked, diag.close)
|
|
|
|
box.addWidget(button)
|
|
|
|
diag.setLayout(box)
|
2021-10-05 02:01:45 +02:00
|
|
|
diag.exec()
|
2021-05-27 05:11:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def display_options_for_deck_id(deck_id: DeckId) -> None:
|
2021-05-31 08:24:51 +02:00
|
|
|
display_options_for_deck(aqt.mw.col.decks.get(deck_id))
|
2021-05-27 05:11:20 +02:00
|
|
|
|
|
|
|
|
2021-05-31 08:24:51 +02:00
|
|
|
def display_options_for_deck(deck: DeckDict) -> None:
|
|
|
|
if not deck["dyn"]:
|
2021-06-27 07:12:22 +02:00
|
|
|
if KeyboardModifiersPressed().shift or aqt.mw.col.sched_ver() == 1:
|
2021-05-31 08:24:51 +02:00
|
|
|
deck_legacy = aqt.mw.col.decks.get(DeckId(deck["id"]))
|
2021-05-27 05:11:20 +02:00
|
|
|
aqt.deckconf.DeckConf(aqt.mw, deck_legacy)
|
|
|
|
else:
|
|
|
|
DeckOptionsDialog(aqt.mw, deck)
|
|
|
|
else:
|
2021-05-31 08:24:51 +02:00
|
|
|
aqt.dialogs.open("FilteredDeckConfigDialog", aqt.mw, deck_id=deck["id"])
|