anki/qt/aqt/deckchooser.py
Arthur Milchior e0a2d90a68 NF: HelpPage in an enum
Hopefully, this can help with updating on next manual update and maybe even linking to manual translation
2021-01-26 02:16:37 +01:00

112 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from typing import Any
from aqt import AnkiQt, gui_hooks
from aqt.qt import *
from aqt.utils import TR, HelpPage, shortcut, tr
class DeckChooser(QHBoxLayout):
def __init__(
self, mw: AnkiQt, widget: QWidget, label: bool = True, start: Any = None
) -> None:
QHBoxLayout.__init__(self)
self.widget = widget # type: ignore
self.mw = mw
self.label = label
self.setContentsMargins(0, 0, 0, 0)
self.setSpacing(8)
self.setupDecks()
self.widget.setLayout(self)
gui_hooks.current_note_type_did_change.append(self.onModelChangeNew)
def setupDecks(self) -> None:
if self.label:
self.deckLabel = QLabel(tr(TR.DECKS_DECK))
self.addWidget(self.deckLabel)
# decks box
self.deck = QPushButton(clicked=self.onDeckChange) # type: ignore
self.deck.setAutoDefault(False)
self.deck.setToolTip(shortcut(tr(TR.QT_MISC_TARGET_DECK_CTRLANDD)))
QShortcut(QKeySequence("Ctrl+D"), self.widget, activated=self.onDeckChange) # type: ignore
self.addWidget(self.deck)
# starting label
if self.mw.col.conf.get("addToCur", True):
col = self.mw.col
did = col.conf["curDeck"]
if col.decks.isDyn(did):
# if they're reviewing, try default to current card
c = self.mw.reviewer.card
if self.mw.state == "review" and c:
if not c.odid:
did = c.did
else:
did = c.odid
else:
did = 1
self.setDeckName(
self.mw.col.decks.nameOrNone(did) or tr(TR.QT_MISC_DEFAULT)
)
else:
self.setDeckName(
self.mw.col.decks.nameOrNone(self.mw.col.models.current()["did"])
or tr(TR.QT_MISC_DEFAULT)
)
# layout
sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0))
self.deck.setSizePolicy(sizePolicy)
def show(self) -> None:
self.widget.show() # type: ignore
def hide(self) -> None:
self.widget.hide() # type: ignore
def cleanup(self) -> None:
gui_hooks.current_note_type_did_change.remove(self.onModelChangeNew)
def onModelChangeNew(self, unused: Any = None) -> None:
self.onModelChange()
def onModelChange(self) -> None:
if not self.mw.col.conf.get("addToCur", True):
self.setDeckName(
self.mw.col.decks.nameOrNone(self.mw.col.models.current()["did"])
or tr(TR.QT_MISC_DEFAULT)
)
def onDeckChange(self) -> None:
from aqt.studydeck import StudyDeck
current = self.deckName()
ret = StudyDeck(
self.mw,
current=current,
accept=tr(TR.ACTIONS_CHOOSE),
title=tr(TR.QT_MISC_CHOOSE_DECK),
help=HelpPage.EDITING,
cancel=False,
parent=self.widget,
geomKey="selectDeck",
)
if ret.name:
self.setDeckName(ret.name)
def setDeckName(self, name: str) -> None:
self.deck.setText(name.replace("&", "&&"))
self._deckName = name
def deckName(self) -> str:
return self._deckName
def selectedId(self) -> int:
# save deck name
name = self.deckName()
if not name.strip():
did = 1
else:
did = self.mw.col.decks.id(name)
return did