anki/aqt/editcurrent.py
Damien Elmes 8e71554ac4 saveNow() now requires a callback
the current code was freezing when clicking on 'cards' in the
browser - it looks like like the javascript callback was never
being called despite calling processEvents(). so we need to
refactor the code to call saveNow() with a callback that does the
subsequent processing.

a lot of the browser code was implicitly calling saveNow() via
beginReset(), so we've had to change all that code to save
immediately before it begins any processing. found a probable bug in
the process - it doesn't look like onRowChange() was saving before
overwriting the note, so theoretically edits could be lost if the
user switched to another card very quickly after typing something.

onSearch() has been split into a GUI-activated onSearchActivated()
that takes care of saving, and a lower level search() that refreshes
the current search. it keeps track of the last search via an instance
variable so that it refreshes properly if a user accidentally adds
some characters to their search without activating the search, then
does something like reverse the sort order.
2016-07-14 20:23:44 +10:00

72 lines
2.2 KiB
Python

# Copyright: Damien Elmes <anki@ichi2.net>
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from aqt.qt import *
import aqt.editor
from aqt.utils import saveGeom, restoreGeom
from anki.hooks import addHook, remHook
from anki.utils import isMac
class EditCurrent(QDialog):
def __init__(self, mw):
if isMac:
# use a separate window on os x so we can a clean menu
QDialog.__init__(self, None, Qt.Window)
else:
QDialog.__init__(self, mw)
QDialog.__init__(self, None, Qt.Window)
self.mw = mw
self.form = aqt.forms.editcurrent.Ui_Dialog()
self.form.setupUi(self)
self.setWindowTitle(_("Edit Current"))
self.setMinimumHeight(400)
self.setMinimumWidth(500)
self.rejected.connect(self.onSave)
self.form.buttonBox.button(QDialogButtonBox.Close).setShortcut(
QKeySequence("Ctrl+Return"))
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self)
self.editor.setNote(self.mw.reviewer.card.note())
restoreGeom(self, "editcurrent")
addHook("reset", self.onReset)
self.mw.requireReset()
self.show()
# reset focus after open
self.editor.web.setFocus()
def onReset(self):
# lazy approach for now: throw away edits
try:
n = self.mw.reviewer.card.note()
n.load()
except:
# card's been deleted
remHook("reset", self.onReset)
self.editor.setNote(None)
self.mw.reset()
aqt.dialogs.close("EditCurrent")
self.close()
return
self.editor.setNote(n)
def onSave(self):
self.editor.saveNow(self._onSave)
def _onSave(self):
remHook("reset", self.onReset)
r = self.mw.reviewer
try:
r.card.load()
except:
# card was removed by clayout
pass
else:
self.mw.reviewer.cardQueue.append(self.mw.reviewer.card)
self.mw.moveToState("review")
saveGeom(self, "editcurrent")
aqt.dialogs.close("EditCurrent")
def canClose(self):
return True