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
|
|
|
|
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2020-01-14 23:53:57 +01:00
|
|
|
from aqt import gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
|
|
|
from aqt.utils import shortcut
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class ModelChooser(QHBoxLayout):
|
2020-01-15 22:41:23 +01:00
|
|
|
def __init__(self, mw, widget, label=True) -> 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.deck = mw.col
|
|
|
|
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.setupModels()
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2020-01-15 22:41:23 +01:00
|
|
|
self.widget.setLayout(self) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def setupModels(self):
|
|
|
|
if self.label:
|
|
|
|
self.modelLabel = QLabel(_("Type"))
|
|
|
|
self.addWidget(self.modelLabel)
|
|
|
|
# models box
|
|
|
|
self.models = QPushButton()
|
2019-12-23 01:34:10 +01:00
|
|
|
# self.models.setStyleSheet("* { text-align: left; }")
|
2012-12-21 08:51:59 +01:00
|
|
|
self.models.setToolTip(shortcut(_("Change Note Type (Ctrl+N)")))
|
2020-02-05 14:46:11 +01:00
|
|
|
s = QShortcut(QKeySequence("Ctrl+N"), self.widget, activated=self.onModelChange)
|
2013-02-20 06:50:18 +01:00
|
|
|
self.models.setAutoDefault(False)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addWidget(self.models)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.models.clicked, self.onModelChange)
|
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.models.setSizePolicy(sizePolicy)
|
|
|
|
self.updateModels()
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def cleanup(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onReset(self):
|
|
|
|
self.updateModels()
|
|
|
|
|
|
|
|
def show(self):
|
|
|
|
self.widget.show()
|
|
|
|
|
|
|
|
def hide(self):
|
|
|
|
self.widget.hide()
|
|
|
|
|
|
|
|
def onEdit(self):
|
|
|
|
import aqt.models
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
aqt.models.Models(self.mw, self.widget)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def onModelChange(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.studydeck import StudyDeck
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
|
|
current = self.deck.models.current()["name"]
|
2012-12-21 08:51:59 +01:00
|
|
|
# edit button
|
2020-01-15 22:41:23 +01:00
|
|
|
edit = QPushButton(_("Manage"), clicked=self.onEdit) # type: ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def nameFunc():
|
|
|
|
return sorted(self.deck.models.allNames())
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
ret = StudyDeck(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw,
|
|
|
|
names=nameFunc,
|
|
|
|
accept=_("Choose"),
|
|
|
|
title=_("Choose Note Type"),
|
|
|
|
help="_notes",
|
|
|
|
current=current,
|
|
|
|
parent=self.widget,
|
|
|
|
buttons=[edit],
|
|
|
|
cancel=True,
|
|
|
|
geomKey="selectModel",
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not ret.name:
|
|
|
|
return
|
|
|
|
m = self.deck.models.byName(ret.name)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.deck.conf["curModel"] = m["id"]
|
2012-12-21 08:51:59 +01:00
|
|
|
cdeck = self.deck.decks.current()
|
2019-12-23 01:34:10 +01:00
|
|
|
cdeck["mid"] = m["id"]
|
2012-12-21 08:51:59 +01:00
|
|
|
self.deck.decks.save(cdeck)
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.current_note_type_did_change(current)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.reset()
|
|
|
|
|
|
|
|
def updateModels(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
self.models.setText(self.deck.models.current()["name"])
|