anki/qt/aqt/editcurrent.py

83 lines
2.6 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import aqt.editor
2019-03-04 02:58:34 +01:00
from anki.lang import _
2020-01-15 04:49:26 +01:00
from aqt import gui_hooks
2019-12-20 10:19:03 +01:00
from aqt.qt import *
from aqt.utils import restoreGeom, saveGeom, tooltip
class EditCurrent(QDialog):
def __init__(self, mw) -> None:
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(250)
self.form.buttonBox.button(QDialogButtonBox.Close).setShortcut(
2019-12-23 01:34:10 +01:00
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")
2020-01-15 07:53:24 +01:00
gui_hooks.state_did_reset.append(self.onReset)
self.mw.requireReset()
self.show()
# reset focus after open, taking care not to retain webview
2019-03-04 07:01:10 +01:00
# pylint: disable=unnecessary-lambda
self.mw.progress.timer(100, lambda: self.editor.web.setFocus(), False)
def onReset(self) -> None:
# lazy approach for now: throw away edits
try:
n = self.editor.note
2019-12-23 01:34:10 +01:00
n.load() # reload in case the model changed
except:
# card's been deleted
2020-01-15 07:53:24 +01:00
gui_hooks.state_did_reset.remove(self.onReset)
self.editor.setNote(None)
self.mw.reset()
aqt.dialogs.markClosed("EditCurrent")
self.close()
return
self.editor.setNote(n)
2019-12-23 01:34:10 +01:00
def reopen(self, mw):
tooltip("Please finish editing the existing card first.")
self.onReset()
2019-12-23 01:34:10 +01:00
def reject(self):
self.saveAndClose()
def saveAndClose(self):
self.editor.saveNow(self._saveAndClose)
def _saveAndClose(self) -> None:
2020-01-15 07:53:24 +01:00
gui_hooks.state_did_reset.remove(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()
2019-12-23 01:34:10 +01:00
self.editor.saveNow(callback)