From 4987ea34d5102cf85e573ab19241dde1178c57d9 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Thu, 17 Jan 2019 23:08:52 +0100 Subject: [PATCH] Extend guiAddCards to enable presetting cards - Made changes to `guiAddCards` to enable presetting the model, deck, etc., very much in the same way that `addNote` does - Changed the README to represent the new changes --- AnkiConnect.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++-- README.md | 23 ++++++++++++++-- meta.json | 1 + 3 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 meta.json diff --git a/AnkiConnect.py b/AnkiConnect.py index c6bb2a4..7fbddfa 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -1025,10 +1025,78 @@ class AnkiConnect: @api() - def guiAddCards(self): - addCards = aqt.dialogs.open('AddCards', self.window()) - addCards.activateWindow() + def guiAddCards(self, note=None): + if note is not None: + collection = self.collection() + + model = collection.models.byName(note['modelName']) + if model is None: + raise Exception('model was not found: {}'.format(note['modelName'])) + + self.collection().models.setCurrent(model) + self.collection().models.update(model) + + deck = collection.decks.byName(note['deckName']) + if deck is None: + raise Exception('deck was not found: {}'.format(note['deckName'])) + + self.collection().decks.select(deck['id']) + + addAndClose = False + if note is not None and 'options' in note: + if 'addAndClose' in note['options']: + addAndClose = note['options']['addAndClose'] + if type(addAndClose) is not bool: + raise Exception('option parameter \'addAndClose\' must be boolean') + + if addAndClose: + # an "AddCards" dialogue, that closes when you add a note + 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")) + # kind of a hack: + # if Anki closes while thise window is open, close it silently + self.silentlyClose = True + + def _addCards(self): + self.editor.saveAddModeVars() + 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() + + addCards = AddCardsAndClose(self.window()) + + else: + addCards = aqt.dialogs.open('AddCards', self.window()) + + addCards.activateWindow() + editor = addCards.editor + ankiNote = editor.note + + if note is not None and 'fields' in note: + for name, value in note['fields'].items(): + if name in ankiNote: + ankiNote[name] = value + editor.loadNote() + + if note is not None and 'tags' in note: + ankiNote.tags = note['tags'] + editor.updateTags() + + # if Anki does not Focus, the window will not notice that the + # fields are actually filled + addCards.setAndFocusNote(editor.note) @api() def guiReviewActive(self): diff --git a/README.md b/README.md index 27f6309..927796b 100644 --- a/README.md +++ b/README.md @@ -1342,13 +1342,32 @@ guarantee that your application continues to function properly in the future. * **guiAddCards** - Invokes the *Add Cards* dialog. + Invokes the *Add Cards* dialog and presets the note using the given deck and model, with the provided field values + and tags. Invoking it multiple times will open multiple windows. + + The `addAndClose` member inside `options` group can be set to true to create a dialog that closes upon adding the first note. *Sample request*: ```json { "action": "guiAddCards", - "version": 6 + "version": 6, + "params": { + "note": { + "deckName": "Default", + "modelName": "Cloze", + "fields": { + "Text": "The capital of Romania is {{c1::Bucharest}}", + "Extra": "Romania is a country in Europe" + }, + "options": { + "addAndClose": true + }, + "tags": [ + "yomichan" + ] + } + } } ``` diff --git a/meta.json b/meta.json new file mode 100644 index 0000000..4eeaa24 --- /dev/null +++ b/meta.json @@ -0,0 +1 @@ +{"disabled": false} \ No newline at end of file