2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import aqt
|
2020-04-06 22:23:31 +02:00
|
|
|
from anki.decks import DeckManager
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2020-01-15 04:49:26 +01:00
|
|
|
from aqt import gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import getOnlyText, openHelp, restoreGeom, saveGeom, shortcut, showInfo
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class StudyDeck(QDialog):
|
2019-12-23 01:34:10 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mw,
|
|
|
|
names=None,
|
|
|
|
accept=None,
|
|
|
|
title=None,
|
|
|
|
help="studydeck",
|
|
|
|
current=None,
|
|
|
|
cancel=True,
|
|
|
|
parent=None,
|
|
|
|
dyn=False,
|
|
|
|
buttons=None,
|
|
|
|
geomKey="default",
|
2020-01-15 22:41:23 +01:00
|
|
|
) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, parent or mw)
|
2019-03-04 07:01:10 +01:00
|
|
|
if buttons is None:
|
|
|
|
buttons = []
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.form = aqt.forms.studydeck.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
self.form.filter.installEventFilter(self)
|
|
|
|
self.cancel = cancel
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.geomKey = "studyDeck-" + geomKey
|
2013-09-20 07:51:12 +02:00
|
|
|
restoreGeom(self, self.geomKey)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not cancel:
|
|
|
|
self.form.buttonBox.removeButton(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Cancel)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if buttons:
|
|
|
|
for b in buttons:
|
|
|
|
self.form.buttonBox.addButton(b, QDialogButtonBox.ActionRole)
|
|
|
|
else:
|
|
|
|
b = QPushButton(_("Add"))
|
|
|
|
b.setShortcut(QKeySequence("Ctrl+N"))
|
|
|
|
b.setToolTip(shortcut(_("Add New Deck (Ctrl+N)")))
|
|
|
|
self.form.buttonBox.addButton(b, QDialogButtonBox.ActionRole)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(b.clicked, self.onAddDeck)
|
2012-12-21 08:51:59 +01:00
|
|
|
if title:
|
|
|
|
self.setWindowTitle(title)
|
|
|
|
if not names:
|
2020-04-06 22:23:31 +02:00
|
|
|
names = sorted(
|
|
|
|
self.mw.col.decks.allNames(dyn=dyn, force_default=False),
|
|
|
|
key=DeckManager._path,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.nameFunc = None
|
|
|
|
self.origNames = names
|
|
|
|
else:
|
|
|
|
self.nameFunc = names
|
|
|
|
self.origNames = names()
|
|
|
|
self.name = None
|
|
|
|
self.ok = self.form.buttonBox.addButton(
|
2019-12-23 01:34:10 +01:00
|
|
|
accept or _("Study"), QDialogButtonBox.AcceptRole
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setWindowModality(Qt.WindowModal)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.form.buttonBox.helpRequested, lambda: openHelp(help))
|
|
|
|
qconnect(self.form.filter.textEdited, self.redraw)
|
|
|
|
qconnect(self.form.list.itemDoubleClicked, self.accept)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
|
|
|
# redraw after show so position at center correct
|
|
|
|
self.redraw("", current)
|
|
|
|
self.exec_()
|
|
|
|
|
|
|
|
def eventFilter(self, obj, evt):
|
|
|
|
if evt.type() == QEvent.KeyPress:
|
|
|
|
if evt.key() == Qt.Key_Up:
|
|
|
|
c = self.form.list.count()
|
|
|
|
row = self.form.list.currentRow() - 1
|
|
|
|
if row < 0:
|
|
|
|
row = c - 1
|
|
|
|
self.form.list.setCurrentRow(row)
|
|
|
|
return True
|
|
|
|
elif evt.key() == Qt.Key_Down:
|
|
|
|
c = self.form.list.count()
|
|
|
|
row = self.form.list.currentRow() + 1
|
|
|
|
if row == c:
|
|
|
|
row = 0
|
|
|
|
self.form.list.setCurrentRow(row)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def redraw(self, filt, focus=None):
|
|
|
|
self.filt = filt
|
|
|
|
self.focus = focus
|
|
|
|
self.names = [n for n in self.origNames if self._matches(n, filt)]
|
|
|
|
l = self.form.list
|
|
|
|
l.clear()
|
|
|
|
l.addItems(self.names)
|
|
|
|
if focus in self.names:
|
|
|
|
idx = self.names.index(focus)
|
|
|
|
else:
|
|
|
|
idx = 0
|
|
|
|
l.setCurrentRow(idx)
|
|
|
|
l.scrollToItem(l.item(idx), QAbstractItemView.PositionAtCenter)
|
|
|
|
|
|
|
|
def _matches(self, name, filt):
|
|
|
|
name = name.lower()
|
|
|
|
filt = filt.lower()
|
|
|
|
if not filt:
|
|
|
|
return True
|
2013-05-23 07:38:47 +02:00
|
|
|
for word in filt.split(" "):
|
|
|
|
if word not in name:
|
2012-12-21 08:51:59 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def onReset(self):
|
|
|
|
# model updated?
|
|
|
|
if self.nameFunc:
|
|
|
|
self.origNames = self.nameFunc()
|
|
|
|
self.redraw(self.filt, self.focus)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def accept(self) -> None:
|
2013-09-20 07:51:12 +02:00
|
|
|
saveGeom(self, self.geomKey)
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2012-12-21 08:51:59 +01:00
|
|
|
row = self.form.list.currentRow()
|
|
|
|
if row < 0:
|
|
|
|
showInfo(_("Please select something."))
|
|
|
|
return
|
|
|
|
self.name = self.names[self.form.list.currentRow()]
|
|
|
|
QDialog.accept(self)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def reject(self) -> None:
|
2014-06-18 20:47:45 +02:00
|
|
|
saveGeom(self, self.geomKey)
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.reject(self)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def onAddDeck(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
row = self.form.list.currentRow()
|
|
|
|
if row < 0:
|
|
|
|
default = self.form.filter.text()
|
|
|
|
else:
|
|
|
|
default = self.names[self.form.list.currentRow()]
|
|
|
|
n = getOnlyText(_("New deck name:"), default=default)
|
|
|
|
if n:
|
|
|
|
self.mw.col.decks.id(n)
|
|
|
|
self.name = n
|
|
|
|
# make sure we clean up reset hook when manually exiting
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.accept(self)
|