anki/aqt/editcurrent.py
Arthur-Milchior b8523944ff The note in curentEdit is not changed until the window is closed.
https://anki.tenderapp.com/discussions/ankidesktop/31105-reopening-an-edit-window
I added a «reopen» method, which I believe may be useful in other
cases too. I check whether the method exists before calling it, so
there should be no compatibility problem.
2018-11-28 10:24:59 +01:00

82 lines
2.5 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 *
from aqt.utils import tooltip
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):
QDialog.__init__(self, None, Qt.Window)
mw.setupDialogGC(self)
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.form.buttonBox.button(QDialogButtonBox.Close).setShortcut(
QKeySequence("Ctrl+Return"))
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self)
self.editor.card = self.mw.reviewer.card
self.editor.setNote(self.mw.reviewer.card.note(), focusTo=0)
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.editor.note
n.load()#reload in case the model changed
except:
# card's been deleted
remHook("reset", self.onReset)
self.editor.setNote(None)
self.mw.reset()
aqt.dialogs.markClosed("EditCurrent")
self.close()
return
self.editor.setNote(n)
def reopen(self,mw):
tooltip("Please finish editing the existing card first.")
self.onReset()
def reject(self):
self.saveAndClose()
def saveAndClose(self):
self.editor.saveNow(self._saveAndClose)
def _saveAndClose(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.editor.cleanup()
self.mw.moveToState("review")
saveGeom(self, "editcurrent")
aqt.dialogs.markClosed("EditCurrent")
QDialog.reject(self)
def closeWithCallback(self, onsuccess):
def callback():
self._saveAndClose()
onsuccess()
self.editor.saveNow(callback)