diff --git a/ankiqt/ui/addcards.py b/ankiqt/ui/addcards.py index 61ec85c93..72645069f 100644 --- a/ankiqt/ui/addcards.py +++ b/ankiqt/ui/addcards.py @@ -12,6 +12,7 @@ from anki.utils import stripHTML, parseTags from ankiqt.ui.utils import saveGeom, restoreGeom, saveSplitter, restoreSplitter from ankiqt import ui from anki.sound import clearAudioQueue +from anki.hooks import addHook, removeHook class FocusButton(QPushButton): def focusInEvent(self, evt): @@ -36,12 +37,13 @@ class AddCards(QDialog): self.addChooser() self.addButtons() self.setupStatus() - self.modelChanged(self.parent.deck.currentModel) + self.modelChanged() self.addedItems = 0 self.forceClose = False restoreGeom(self, "add") restoreSplitter(self.dialog.splitter, "add") self.show() + addHook('guiReset', self.modelChanged) ui.dialogs.open("AddCards", self) def setupEditor(self): @@ -52,8 +54,7 @@ class AddCards(QDialog): def addChooser(self): self.modelChooser = ui.modelchooser.ModelChooser(self, self.parent, - self.parent.deck, - self.modelChanged) + self.parent.deck) self.dialog.modelArea.setLayout(self.modelChooser) def helpRequested(self): @@ -97,7 +98,7 @@ class AddCards(QDialog): browser.updateSearch() browser.onFact() - def modelChanged(self, model): + def modelChanged(self): oldFact = self.editor.fact # create a new fact fact = self.parent.deck.newFact() @@ -176,6 +177,7 @@ question or answer on all cards."""), parent=self) QDialog.reject(self) def onClose(self): + removeHook('guiReset', self.modelChanged) # stop anything playing clearAudioQueue() if (self.forceClose or self.editor.fieldsAreBlank() or diff --git a/ankiqt/ui/modelchooser.py b/ankiqt/ui/modelchooser.py index 9608ad1ad..6e902232e 100644 --- a/ankiqt/ui/modelchooser.py +++ b/ankiqt/ui/modelchooser.py @@ -9,10 +9,11 @@ from anki import stdmodels from anki.models import * from ankiqt import ui import ankiqt.forms +from anki.hooks import addHook, removeHook class ModelChooser(QHBoxLayout): - def __init__(self, parent, main, deck, onChangeFunc, cards=True, label=True): + def __init__(self, parent, main, deck, onChangeFunc=None, cards=True, label=True): QHBoxLayout.__init__(self) self.parent = parent self.main = main @@ -54,6 +55,7 @@ class ModelChooser(QHBoxLayout): self.connect(self.cards, SIGNAL("clicked()"), self.onCardChange) self.addWidget(self.cards) self.drawCardModels() + addHook('guiReset', self.onModelEdited) def show(self): for i in range(self.count()): @@ -68,6 +70,9 @@ class ModelChooser(QHBoxLayout): onFinish=self.onModelEdited) def onModelEdited(self): + # hack + from ankiqt import mw + self.deck = mw.deck self.drawModels() self.changed(self.deck.currentModel) @@ -79,7 +84,8 @@ class ModelChooser(QHBoxLayout): def changed(self, model): self.deck.addModel(model) - self.onChangeFunc(model) + if self.onChangeFunc: + self.onChangeFunc(model) self.drawCardModels() def drawModels(self):