2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
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
|
2020-06-11 07:19:36 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2020-06-11 07:19:36 +02:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2020-11-17 08:42:43 +01:00
|
|
|
from aqt.utils import TR, shortcut, tr
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class DeckChooser(QHBoxLayout):
|
2020-06-11 07:19:36 +02:00
|
|
|
def __init__(
|
|
|
|
self, mw: AnkiQt, widget: QWidget, label: bool = True, start: Any = None
|
|
|
|
) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QHBoxLayout.__init__(self)
|
2020-01-15 22:53:12 +01:00
|
|
|
self.widget = widget # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.label = label
|
2019-12-23 01:34:10 +01:00
|
|
|
self.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setSpacing(8)
|
|
|
|
self.setupDecks()
|
|
|
|
self.widget.setLayout(self)
|
2020-01-15 22:53:12 +01:00
|
|
|
gui_hooks.current_note_type_did_change.append(self.onModelChangeNew)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def setupDecks(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if self.label:
|
2020-11-17 08:42:43 +01:00
|
|
|
self.deckLabel = QLabel(tr(TR.DECKS_DECK))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addWidget(self.deckLabel)
|
|
|
|
# decks box
|
2020-06-11 07:19:36 +02:00
|
|
|
self.deck = QPushButton(clicked=self.onDeckChange) # type: ignore
|
2019-05-15 06:50:55 +02:00
|
|
|
self.deck.setAutoDefault(False)
|
2020-11-17 08:42:43 +01:00
|
|
|
self.deck.setToolTip(shortcut(tr(TR.QT_MISC_TARGET_DECK_CTRLANDD)))
|
2020-05-11 17:09:22 +02:00
|
|
|
QShortcut(QKeySequence("Ctrl+D"), self.widget, activated=self.onDeckChange) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
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"]
|
2012-12-21 08:51:59 +01:00
|
|
|
if col.decks.isDyn(did):
|
2013-05-22 04:51:42 +02:00
|
|
|
# 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
|
2020-11-17 08:42:43 +01:00
|
|
|
self.setDeckName(
|
|
|
|
self.mw.col.decks.nameOrNone(did) or tr(TR.QT_MISC_DEFAULT)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.setDeckName(
|
|
|
|
self.mw.col.decks.nameOrNone(self.mw.col.models.current()["did"])
|
2020-11-17 08:42:43 +01:00
|
|
|
or tr(TR.QT_MISC_DEFAULT)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
# layout
|
2019-12-23 01:34:10 +01:00
|
|
|
sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.deck.setSizePolicy(sizePolicy)
|
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def show(self) -> None:
|
|
|
|
self.widget.show() # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def hide(self) -> None:
|
|
|
|
self.widget.hide() # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def cleanup(self) -> None:
|
2020-01-15 22:53:12 +01:00
|
|
|
gui_hooks.current_note_type_did_change.remove(self.onModelChangeNew)
|
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def onModelChangeNew(self, unused: Any = None) -> None:
|
2020-01-15 22:53:12 +01:00
|
|
|
self.onModelChange()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def onModelChange(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
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"])
|
2020-11-17 08:42:43 +01:00
|
|
|
or tr(TR.QT_MISC_DEFAULT)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def onDeckChange(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.studydeck import StudyDeck
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-07-28 09:40:26 +02:00
|
|
|
current = self.deckName()
|
2012-12-21 08:51:59 +01:00
|
|
|
ret = StudyDeck(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw,
|
|
|
|
current=current,
|
2020-11-17 08:42:43 +01:00
|
|
|
accept=tr(TR.ACTIONS_CHOOSE),
|
|
|
|
title=tr(TR.QT_MISC_CHOOSE_DECK),
|
2019-12-23 01:34:10 +01:00
|
|
|
help="addingnotes",
|
|
|
|
cancel=False,
|
|
|
|
parent=self.widget,
|
|
|
|
geomKey="selectDeck",
|
|
|
|
)
|
2018-11-26 18:06:29 +01:00
|
|
|
if ret.name:
|
|
|
|
self.setDeckName(ret.name)
|
2017-07-28 09:40:26 +02:00
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def setDeckName(self, name: str) -> None:
|
2017-07-28 09:40:26 +02:00
|
|
|
self.deck.setText(name.replace("&", "&&"))
|
|
|
|
self._deckName = name
|
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def deckName(self) -> str:
|
2017-07-28 09:40:26 +02:00
|
|
|
return self._deckName
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-11 07:19:36 +02:00
|
|
|
def selectedId(self) -> int:
|
2012-12-21 08:51:59 +01:00
|
|
|
# save deck name
|
2017-07-28 09:40:26 +02:00
|
|
|
name = self.deckName()
|
2012-12-21 08:51:59 +01:00
|
|
|
if not name.strip():
|
|
|
|
did = 1
|
|
|
|
else:
|
|
|
|
did = self.mw.col.decks.id(name)
|
|
|
|
return did
|