2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
from aqt.qt import *
|
2018-11-28 10:16:23 +01:00
|
|
|
from aqt.utils import tooltip
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt.editor
|
|
|
|
from aqt.utils import saveGeom, restoreGeom
|
|
|
|
from anki.hooks import addHook, remHook
|
|
|
|
from anki.utils import isMac
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2018-11-28 10:16:23 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
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)
|
2019-09-02 01:52:04 +02:00
|
|
|
self.setMinimumWidth(250)
|
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()
|
2019-02-18 23:01:11 +01:00
|
|
|
# reset focus after open, taking care not to retain webview
|
2019-03-04 07:01:10 +01:00
|
|
|
# pylint: disable=unnecessary-lambda
|
2019-02-18 23:01:11 +01:00
|
|
|
self.mw.progress.timer(100, lambda: self.editor.web.setFocus(), False)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onReset(self):
|
|
|
|
# lazy approach for now: throw away edits
|
|
|
|
try:
|
2018-11-28 10:16:23 +01:00
|
|
|
n = self.editor.note
|
|
|
|
n.load()#reload in case the model changed
|
2012-12-21 08:51:59 +01:00
|
|
|
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)
|
|
|
|
|
2018-11-28 10:16:23 +01:00
|
|
|
def reopen(self,mw):
|
|
|
|
tooltip("Please finish editing the existing card first.")
|
|
|
|
self.onReset()
|
|
|
|
|
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)
|