Add support for Anki 2.0

This commit is contained in:
Henrik Giesel 2019-01-24 08:36:11 +01:00
parent daecf30408
commit 8e6f688e23
3 changed files with 1352 additions and 20 deletions

View File

@ -31,7 +31,7 @@ import sys
from operator import itemgetter
from time import time
from unicodedata import normalize
from random import choices
from random import choice
from string import ascii_letters
@ -48,6 +48,7 @@ TICK_INTERVAL = 25
URL_TIMEOUT = 10
URL_UPGRADE = 'https://raw.githubusercontent.com/FooSoft/anki-connect/master/AnkiConnect.py'
ANKI21 = anki.version.startswith('2.1')
#
# Helpers
@ -1056,25 +1057,44 @@ class AnkiConnect:
if closeAfterAdding:
randomString = ''.join(choices(ascii_letters, k=10))
randomString = ''.join(choice(ascii_letters) for _ in range(10))
windowName = 'AddCardsAndClose' + randomString
class AddCardsAndClose(aqt.addcards.AddCards):
if ANKI21:
class AddCardsAndClose(aqt.addcards.AddCards):
def __init__(self, mw):
super().__init__(mw)
self.addButton.setText("Add and Close")
self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return"))
def __init__(self, mw):
super().__init__(mw)
self.addButton.setText("Add and Close")
self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return"))
def _addCards(self):
super()._addCards()
self.reject()
def _addCards(self):
super()._addCards()
self.reject()
def _reject(self):
savedMarkClosed = aqt.dialogs.markClosed
aqt.dialogs.markClosed = lambda _: savedMarkClosed(windowName)
super()._reject()
aqt.dialogs.markClosed = savedMarkClosed
def _reject(self):
savedMarkClosed = aqt.dialogs.markClosed
aqt.dialogs.markClosed = lambda _: savedMarkClosed(windowName)
super()._reject()
aqt.dialogs.markClosed = savedMarkClosed
else:
class AddCardsAndClose(aqt.addcards.AddCards):
def __init__(self, mw):
super(AddCardsAndClose, self).__init__(mw)
self.addButton.setText("Add and Close")
self.addButton.setShortcut(aqt.qt.QKeySequence("Ctrl+Return"))
def addCards(self):
super(AddCardsAndClose, self).addCards()
self.reject()
def reject(self):
savedClose = aqt.dialogs.close
aqt.dialogs.close = lambda _: savedClose(windowName)
super(AddCardsAndClose, self).reject()
aqt.dialogs.close = savedClose
aqt.dialogs._dialogs[windowName] = [AddCardsAndClose, None]
addCards = aqt.dialogs.open(windowName, self.window())
@ -1095,7 +1115,8 @@ class AnkiConnect:
# 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 ANKI21:
addCards.setAndFocusNote(editor.note)
elif note is not None:
currentWindow = aqt.dialogs._dialogs['AddCards'][1]
@ -1120,10 +1141,9 @@ class AnkiConnect:
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)
aqt.dialogs.open('AddCards', self.window())
if ANKI21:
addCards.setAndFocusNote(editor.note)
if currentWindow is not None:
currentWindow.closeWithCallback(openNewWindow)

1303
__init__.py Normal file

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,15 @@ class TestGui(unittest.TestCase):
# guiAddCards
util.invoke('guiAddCards')
# guiAddCards with preset
util.invoke('createDeck', deck='test')
note = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'tags': ['tag1']}
util.invoke('guiAddCards', note=note)
# guiAddCards with preset and closeAfterAdding
noteWithOption = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'options': { 'closeAfterAdding': True }, 'tags': ['tag1']}
util.invoke('guiAddCards', note=noteWithOption)
# guiCurrentCard
# util.invoke('guiCurrentCard')