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
|
|
|
|
import aqt.editor
|
2020-01-15 04:49:26 +01:00
|
|
|
from aqt import gui_hooks
|
2020-08-16 18:49:51 +02:00
|
|
|
from aqt.main import ResetReason
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2021-01-07 05:24:49 +01:00
|
|
|
from aqt.utils import TR, disable_help_button, restoreGeom, saveGeom, tooltip, tr
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2018-11-28 10:16:23 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class EditCurrent(QDialog):
|
2020-01-15 22:41:23 +01:00
|
|
|
def __init__(self, mw) -> None:
|
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)
|
2020-11-17 08:42:43 +01:00
|
|
|
self.setWindowTitle(tr(TR.EDITING_EDIT_CURRENT))
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
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(
|
2019-12-23 01:34:10 +01:00
|
|
|
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")
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.EditCurrentInit, context=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
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
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def onReset(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# lazy approach for now: throw away edits
|
|
|
|
try:
|
2018-11-28 10:16:23 +01:00
|
|
|
n = self.editor.note
|
2019-12-23 01:34:10 +01:00
|
|
|
n.load() # reload in case the model changed
|
2012-12-21 08:51:59 +01:00
|
|
|
except:
|
|
|
|
# card's been deleted
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2012-12-21 08:51:59 +01:00
|
|
|
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)
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def reopen(self, mw) -> None:
|
2018-11-28 10:16:23 +01:00
|
|
|
tooltip("Please finish editing the existing card first.")
|
|
|
|
self.onReset()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def reject(self) -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
self.saveAndClose()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def saveAndClose(self) -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
self.editor.saveNow(self._saveAndClose)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def _saveAndClose(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2012-12-21 08:51:59 +01:00
|
|
|
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
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def closeWithCallback(self, onsuccess) -> None:
|
|
|
|
def callback() -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
self._saveAndClose()
|
|
|
|
onsuccess()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
self.editor.saveNow(callback)
|