rework to allow refresh of list when models added/removed

This commit is contained in:
Damien Elmes 2012-05-26 20:25:09 +09:00
parent 75b9e21661
commit a6c9c8203c
2 changed files with 24 additions and 6 deletions

View file

@ -23,7 +23,6 @@ class ModelChooser(QHBoxLayout):
self.widget.setLayout(self) self.widget.setLayout(self)
def setupModels(self): def setupModels(self):
if self.label: if self.label:
self.modelLabel = QLabel(_("Type")) self.modelLabel = QLabel(_("Type"))
self.addWidget(self.modelLabel) self.addWidget(self.modelLabel)
@ -64,10 +63,13 @@ class ModelChooser(QHBoxLayout):
# edit button # edit button
edit = QPushButton(_("Manage")) edit = QPushButton(_("Manage"))
self.connect(edit, SIGNAL("clicked()"), self.onEdit) self.connect(edit, SIGNAL("clicked()"), self.onEdit)
ret = StudyDeck(self.mw, names=sorted(self.deck.models.allNames()), def nameFunc():
accept=_("Choose"), title=_("Choose Note Type"), return sorted(self.deck.models.allNames())
help="_notes", current=current, parent=self.widget, ret = StudyDeck(
buttons=[edit], cancel=False) self.mw, names=nameFunc,
accept=_("Choose"), title=_("Choose Note Type"),
help="_notes", current=current, parent=self.widget,
buttons=[edit], cancel=False)
if not ret.name: if not ret.name:
return return
m = self.deck.models.byName(ret.name) m = self.deck.models.byName(ret.name)

View file

@ -7,6 +7,7 @@ import aqt
from anki.utils import ids2str from anki.utils import ids2str
from aqt.utils import showInfo, showWarning, openHelp, getOnlyText from aqt.utils import showInfo, showWarning, openHelp, getOnlyText
from operator import itemgetter from operator import itemgetter
from anki.hooks import addHook, remHook
class StudyDeck(QDialog): class StudyDeck(QDialog):
def __init__(self, mw, names=None, accept=None, title=None, def __init__(self, mw, names=None, accept=None, title=None,
@ -18,6 +19,7 @@ class StudyDeck(QDialog):
self.form.setupUi(self) self.form.setupUi(self)
self.form.filter.installEventFilter(self) self.form.filter.installEventFilter(self)
self.cancel = cancel self.cancel = cancel
addHook('reset', self.onReset)
if not cancel: if not cancel:
self.form.buttonBox.removeButton( self.form.buttonBox.removeButton(
self.form.buttonBox.button(QDialogButtonBox.Cancel)) self.form.buttonBox.button(QDialogButtonBox.Cancel))
@ -35,7 +37,11 @@ class StudyDeck(QDialog):
if not names: if not names:
names = sorted(self.mw.col.decks.allNames(dyn=dyn)) names = sorted(self.mw.col.decks.allNames(dyn=dyn))
current = self.mw.col.decks.current()['name'] current = self.mw.col.decks.current()['name']
self.origNames = names self.nameFunc = None
self.origNames = names
else:
self.nameFunc = names
self.origNames = names()
self.name = None self.name = None
self.ok = self.form.buttonBox.addButton( self.ok = self.form.buttonBox.addButton(
accept or _("Study"), QDialogButtonBox.AcceptRole) accept or _("Study"), QDialogButtonBox.AcceptRole)
@ -73,6 +79,8 @@ class StudyDeck(QDialog):
return False return False
def redraw(self, filt, focus=None): 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)] self.names = [n for n in self.origNames if self._matches(n, filt)]
l = self.form.list l = self.form.list
l.clear() l.clear()
@ -95,7 +103,14 @@ class StudyDeck(QDialog):
name = name[name.index(c)+1:] name = name[name.index(c)+1:]
return True return True
def onReset(self):
# model updated?
if self.nameFunc:
self.origNames = self.nameFunc()
self.redraw(self.filt, self.focus)
def accept(self): def accept(self):
remHook('reset', self.onReset)
row = self.form.list.currentRow() row = self.form.list.currentRow()
if row < 0: if row < 0:
showInfo(_("Please select something.")) showInfo(_("Please select something."))
@ -104,6 +119,7 @@ class StudyDeck(QDialog):
QDialog.accept(self) QDialog.accept(self)
def reject(self): def reject(self):
remHook('reset', self.onReset)
if not self.cancel: if not self.cancel:
return self.accept() return self.accept()
QDialog.reject(self) QDialog.reject(self)