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
This commit is contained in:
Henrik Giesel 2019-01-17 23:08:52 +01:00
parent 1448c8d43f
commit 4987ea34d5
3 changed files with 93 additions and 5 deletions

View File

@ -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):

View File

@ -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"
]
}
}
}
```

1
meta.json Normal file
View File

@ -0,0 +1 @@
{"disabled": false}