Hi,
I’m porting my plugin reverso importer to the lastest anki version.
While everything seems to work smoothly there’s a last issue I don’t really catch correctly and is the way the UI is to be refreshed. I understand that I should use CollectionOp, but I have not really understood how to change my code.
Current code, that adds a reverso card from a list of selected card is as follows:
def setReverseField(browser, nids):
mw = browser.mw
mw.checkpoint("add_reverse")
mw.progress.start()
browser.model.beginReset()
cnt = 0
optional_anki_reverse_name = _("Add Reverse")
for nid in nids:
note = mw.col.get_note(nid)
if 'reverse' in note and not note['reverse']:
note['reverse'] = "1"
cnt += 1
note.flush()
if optional_anki_reverse_name in note and not note[optional_anki_reverse_name]:
note[optional_anki_reverse_name] = "1"
cnt += 1
note.flush()
browser.model.endReset()
mw.requireReset()
mw.progress.finish()
mw.reset()
if cnt:
tooltip("<b>Added</b> {0} reversed cards.".format(cnt), parent=browser)
else:
msg = "No card found to reverse.\nOnly cards with field 'reverse' or '{}' are looked for"
tooltip(msg.format(optional_anki_reverse_name), parent=browser)
def onBatchEdit(browser):
nids = browser.selectedNotes()
if not nids:
tooltip("No cards selected.")
return
setReverseField(browser, nids)
and onBatchEdit
is called by the callback of the menu entry.
I don’t mind to undo the operation, but in case it must be a single undo for the whole collection.
Any hint is appreciated.
Sandro