anki/qt/aqt/deckchooser.py

108 lines
3.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2012-12-22 01:17:10 +01:00
from anki.hooks import addHook, remHook
2019-03-04 02:58:34 +01:00
from anki.lang import _
2019-12-20 10:19:03 +01:00
from aqt.qt import *
from aqt.utils import shortcut
class DeckChooser(QHBoxLayout):
def __init__(self, mw, widget, label=True, start=None):
QHBoxLayout.__init__(self)
self.widget = widget
self.mw = mw
self.deck = mw.col
self.label = label
2019-12-23 01:34:10 +01:00
self.setContentsMargins(0, 0, 0, 0)
self.setSpacing(8)
self.setupDecks()
self.widget.setLayout(self)
2019-12-23 01:34:10 +01:00
addHook("currentModelChanged", self.onModelChange)
def setupDecks(self):
if self.label:
self.deckLabel = QLabel(_("Deck"))
self.addWidget(self.deckLabel)
# decks box
self.deck = QPushButton(clicked=self.onDeckChange)
self.deck.setAutoDefault(False)
self.deck.setToolTip(shortcut(_("Target Deck (Ctrl+D)")))
2019-12-23 01:34:10 +01:00
s = QShortcut(
QKeySequence(_("Ctrl+D")), self.widget, activated=self.onDeckChange
)
self.addWidget(self.deck)
# starting label
if self.mw.col.conf.get("addToCur", True):
col = self.mw.col
2019-12-23 01:34:10 +01:00
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
2019-12-23 01:34:10 +01:00
self.setDeckName(self.mw.col.decks.nameOrNone(did) or _("Default"))
else:
2019-12-23 01:34:10 +01:00
self.setDeckName(
self.mw.col.decks.nameOrNone(self.mw.col.models.current()["did"])
or _("Default")
)
# layout
2019-12-23 01:34:10 +01:00
sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0))
self.deck.setSizePolicy(sizePolicy)
def show(self):
self.widget.show()
def hide(self):
self.widget.hide()
def cleanup(self):
2019-12-23 01:34:10 +01:00
remHook("currentModelChanged", self.onModelChange)
def onModelChange(self):
if not self.mw.col.conf.get("addToCur", True):
2019-12-23 01:34:10 +01:00
self.setDeckName(
self.mw.col.decks.nameOrNone(self.mw.col.models.current()["did"])
or _("Default")
)
def onDeckChange(self):
from aqt.studydeck import StudyDeck
2019-12-23 01:34:10 +01:00
current = self.deckName()
ret = StudyDeck(
2019-12-23 01:34:10 +01:00
self.mw,
current=current,
accept=_("Choose"),
title=_("Choose Deck"),
help="addingnotes",
cancel=False,
parent=self.widget,
geomKey="selectDeck",
)
if ret.name:
self.setDeckName(ret.name)
def setDeckName(self, name):
self.deck.setText(name.replace("&", "&&"))
self._deckName = name
def deckName(self):
return self._deckName
def selectedId(self):
# save deck name
name = self.deckName()
if not name.strip():
did = 1
else:
did = self.mw.col.decks.id(name)
return did