2012-12-21 08:51:59 +01:00
|
|
|
# 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):
|
2017-08-11 13:06:15 +02:00
|
|
|
QDialog.__init__(self, None, Qt.Window)
|
2017-08-16 04:45:33 +02:00
|
|
|
mw.setupDialogGC(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.form = aqt.forms.editcurrent.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
self.setWindowTitle(_("Edit Current"))
|
|
|
|
self.setMinimumHeight(400)
|
|
|
|
self.setMinimumWidth(500)
|
2014-08-31 21:41:08 +02:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Close).setShortcut(
|
|
|
|
QKeySequence("Ctrl+Return"))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self)
|
2018-07-23 04:54:26 +02:00
|
|
|
self.editor.card = self.mw.reviewer.card
|
2017-08-05 07:15:19 +02:00
|
|
|
self.editor.setNote(self.mw.reviewer.card.note(), focusTo=0)
|
2012-12-21 08:51:59 +01:00
|
|
|
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()
|
2017-08-16 04:45:33 +02:00
|
|
|
aqt.dialogs.markClosed("EditCurrent")
|
2012-12-21 08:51:59 +01:00
|
|
|
self.close()
|
|
|
|
return
|
|
|
|
self.editor.setNote(n)
|
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
def reject(self):
|
|
|
|
self.saveAndClose()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
def saveAndClose(self):
|
|
|
|
self.editor.saveNow(self._saveAndClose)
|
|
|
|
|
|
|
|
def _saveAndClose(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
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)
|
2017-08-16 04:45:33 +02:00
|
|
|
self.editor.cleanup()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.moveToState("review")
|
|
|
|
saveGeom(self, "editcurrent")
|
2017-08-16 04:45:33 +02:00
|
|
|
aqt.dialogs.markClosed("EditCurrent")
|
|
|
|
QDialog.reject(self)
|
2013-04-11 12:23:32 +02:00
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
def closeWithCallback(self, onsuccess):
|
|
|
|
def callback():
|
|
|
|
self._saveAndClose()
|
|
|
|
onsuccess()
|
|
|
|
self.editor.saveNow(callback)
|