Fix bugs when opening multiple windows

This commit is contained in:
Henrik Giesel 2019-01-24 07:07:53 +01:00
parent d08d2510e7
commit daecf30408

View File

@ -31,6 +31,8 @@ import sys
from operator import itemgetter from operator import itemgetter
from time import time from time import time
from unicodedata import normalize from unicodedata import normalize
from random import choices
from string import ascii_letters
# #
@ -1043,7 +1045,6 @@ class AnkiConnect:
self.collection().models.setCurrent(model) self.collection().models.setCurrent(model)
self.collection().models.update(model) self.collection().models.update(model)
closeAfterAdding = False closeAfterAdding = False
if note is not None and 'options' in note: if note is not None and 'options' in note:
if 'closeAfterAdding' in note['options']: if 'closeAfterAdding' in note['options']:
@ -1051,8 +1052,13 @@ class AnkiConnect:
if type(closeAfterAdding) is not bool: if type(closeAfterAdding) is not bool:
raise Exception('option parameter \'closeAfterAdding\' must be boolean') raise Exception('option parameter \'closeAfterAdding\' must be boolean')
addCards = 'foobar'
if closeAfterAdding: if closeAfterAdding:
# an "AddCards" dialogue, that closes when you add a note
randomString = ''.join(choices(ascii_letters, k=10))
windowName = 'AddCardsAndClose' + randomString
class AddCardsAndClose(aqt.addcards.AddCards): class AddCardsAndClose(aqt.addcards.AddCards):
def __init__(self, mw): def __init__(self, mw):
@ -1061,42 +1067,21 @@ class AnkiConnect:
self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return")) self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return"))
def _addCards(self): def _addCards(self):
self.editor.saveAddModeVars() super()._addCards()
note = self.editor.note
note = self.addNote(note)
if not note:
return
aqt.utils.tooltip(_("Added"), period=500)
# stop anything playing
anki.sound.clearAudioQueue()
self.onReset(keep=True)
self.mw.col.autosave()
self.reject() self.reject()
def _reject(self): def _reject(self):
anki.hooks.remHook('reset', self.onReset) savedMarkClosed = aqt.dialogs.markClosed
anki.hooks.remHook('currentModelChanged', self.onModelChange) aqt.dialogs.markClosed = lambda _: savedMarkClosed(windowName)
anki.sound.clearAudioQueue() super()._reject()
self.removeTempNote(self.editor.note) aqt.dialogs.markClosed = savedMarkClosed
self.editor.cleanup()
self.modelChooser.cleanup()
self.deckChooser.cleanup()
self.mw.maybeReset()
aqt.utils.saveGeom(self, "add")
aqt.dialogs.markClosed("AddCardsAndClose")
aqt.qt.QDialog.reject(self)
aqt.dialogs._dialogs['AddCardsAndClose'] = [AddCardsAndClose, None] aqt.dialogs._dialogs[windowName] = [AddCardsAndClose, None]
addCards = aqt.dialogs.open('AddCardsAndClose', self.window()) addCards = aqt.dialogs.open(windowName, self.window())
else: editor = addCards.editor
addCards = aqt.dialogs.open('AddCards', self.window()) ankiNote = editor.note
addCards.activateWindow()
editor = addCards.editor
ankiNote = editor.note
if note is not None:
if 'fields' in note: if 'fields' in note:
for name, value in note['fields'].items(): for name, value in note['fields'].items():
if name in ankiNote: if name in ankiNote:
@ -1107,9 +1092,47 @@ class AnkiConnect:
ankiNote.tags = note['tags'] ankiNote.tags = note['tags']
editor.updateTags() editor.updateTags()
# if Anki does not Focus, the window will not notice that the # if Anki does not Focus, the window will not notice that the
# fields are actually filled # fields are actually filled
addCards.setAndFocusNote(editor.note) aqt.dialogs.open(windowName, self.window())
addCards.setAndFocusNote(editor.note)
elif note is not None:
currentWindow = aqt.dialogs._dialogs['AddCards'][1]
def openNewWindow():
addCards = aqt.dialogs.open('AddCards', self.window())
editor = addCards.editor
ankiNote = editor.note
# we have to fill out the card in the callback
# otherwise we can't assure, the new window is open
if 'fields' in note:
for name, value in note['fields'].items():
if name in ankiNote:
ankiNote[name] = value
editor.loadNote()
if 'tags' in note:
ankiNote.tags = note['tags']
editor.updateTags()
addCards.activateWindow()
# if Anki does not Focus, the window will not notice that the
# fields are actually filled
aqt.dialogs.open(windowName, self.window())
addCards.setAndFocusNote(editor.note)
if currentWindow is not None:
currentWindow.closeWithCallback(openNewWindow)
else:
openNewWindow()
else:
addCards = aqt.dialogs.open('AddCards', self.window())
addCards.activateWindow()
@api() @api()
def guiReviewActive(self): def guiReviewActive(self):