Fix model bug

This commit is contained in:
Henrik Giesel 2019-05-02 18:33:24 +02:00
parent 8328a0289d
commit e326fd6dca

View File

@ -1065,7 +1065,13 @@ class AnkiConnect:
class AddCardsAndClose(aqt.addcards.AddCards): class AddCardsAndClose(aqt.addcards.AddCards):
def __init__(self, mw): def __init__(self, mw):
# the window must only reset if
# * function `onModelChange` has been called prior
# * window was newly opened
self.modelHasChanged = True
super().__init__(mw) super().__init__(mw)
self.addButton.setText("Add and Close") self.addButton.setText("Add and Close")
self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return")) self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return"))
@ -1076,6 +1082,22 @@ class AnkiConnect:
if len(self.history): if len(self.history):
self.reject() self.reject()
def onModelChange(self):
if self.isActiveWindow():
super().onModelChange()
self.modelHasChanged = True
def onReset(self, model=None, keep=False):
if self.isActiveWindow() or self.modelHasChanged:
super().onReset(model, keep)
self.modelHasChanged = False
else:
# modelchoosers text is changed by a reset hook
# therefore we need to change it back manually
self.modelChooser.models.setText(self.editor.note.model()['name'])
self.modelHasChanged = False
def _reject(self): def _reject(self):
savedMarkClosed = aqt.dialogs.markClosed savedMarkClosed = aqt.dialogs.markClosed
aqt.dialogs.markClosed = lambda _: savedMarkClosed(windowName) aqt.dialogs.markClosed = lambda _: savedMarkClosed(windowName)
@ -1086,7 +1108,9 @@ class AnkiConnect:
class AddCardsAndClose(aqt.addcards.AddCards): class AddCardsAndClose(aqt.addcards.AddCards):
def __init__(self, mw): def __init__(self, mw):
self.modelHasChanged = True
super(AddCardsAndClose, self).__init__(mw) super(AddCardsAndClose, self).__init__(mw)
self.addButton.setText("Add and Close") self.addButton.setText("Add and Close")
self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return")) self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return"))
@ -1097,6 +1121,20 @@ class AnkiConnect:
if len(self.history): if len(self.history):
self.reject() self.reject()
def onModelChange(self):
if self.isActiveWindow():
super(AddCardsAndClose, self).onModelChange()
self.modelHasChanged = True
def onReset(self, model=None, keep=False):
if self.isActiveWindow() or self.modelHasChanged:
super(AddCardsAndClose, self).onReset(model, keep)
self.modelHasChanged = False
else:
self.modelChooser.models.setText(self.editor.note.model()['name'])
self.modelHasChanged = False
def reject(self): def reject(self):
savedClose = aqt.dialogs.close savedClose = aqt.dialogs.close
aqt.dialogs.close = lambda _: savedClose(windowName) aqt.dialogs.close = lambda _: savedClose(windowName)