2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-04-06 02:14:11 +02:00
|
|
|
from typing import Optional
|
2021-03-16 07:39:41 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt.editor
|
2021-03-16 07:39:41 +01:00
|
|
|
from anki.collection import OpChanges
|
|
|
|
from anki.errors import NotFoundError
|
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 *
|
2021-03-26 04:48:26 +01:00
|
|
|
from aqt.utils import disable_help_button, restoreGeom, saveGeom, 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):
|
2021-02-02 15:00:29 +01:00
|
|
|
def __init__(self, mw: aqt.AnkiQt) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
QDialog.__init__(self, None, Qt.WindowType.Window)
|
2021-03-05 04:07:52 +01:00
|
|
|
mw.garbage_collect_on_dialog_finish(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.form = aqt.forms.editcurrent.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2021-03-26 04:48:26 +01:00
|
|
|
self.setWindowTitle(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)
|
2021-10-05 05:53:01 +02:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.StandardButton.Close).setShortcut(
|
2019-12-23 01:34:10 +01:00
|
|
|
QKeySequence("Ctrl+Return")
|
|
|
|
)
|
2022-01-12 05:51:43 +01:00
|
|
|
self.editor = aqt.editor.Editor(
|
|
|
|
self.mw,
|
|
|
|
self.form.fieldsArea,
|
|
|
|
self,
|
2022-01-12 06:01:43 +01:00
|
|
|
editor_mode=aqt.editor.EditorMode.EDIT_CURRENT,
|
2022-01-12 05:51:43 +01:00
|
|
|
)
|
2018-07-23 04:54:26 +02:00
|
|
|
self.editor.card = self.mw.reviewer.card
|
2021-03-16 07:39:41 +01:00
|
|
|
self.editor.set_note(self.mw.reviewer.card.note(), focusTo=0)
|
2012-12-21 08:51:59 +01:00
|
|
|
restoreGeom(self, "editcurrent")
|
2021-03-16 07:39:41 +01:00
|
|
|
gui_hooks.operation_did_execute.append(self.on_operation_did_execute)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
|
|
|
|
2021-04-06 02:14:11 +02:00
|
|
|
def on_operation_did_execute(
|
|
|
|
self, changes: OpChanges, handler: Optional[object]
|
|
|
|
) -> None:
|
2021-06-08 04:09:35 +02:00
|
|
|
if changes.note_text and handler is not self.editor:
|
2021-04-05 06:28:56 +02:00
|
|
|
# reload note
|
|
|
|
note = self.editor.note
|
|
|
|
try:
|
|
|
|
note.load()
|
|
|
|
except NotFoundError:
|
|
|
|
# note's been deleted
|
|
|
|
self.cleanup_and_close()
|
|
|
|
return
|
2021-03-16 07:39:41 +01:00
|
|
|
|
2021-04-05 06:28:56 +02:00
|
|
|
self.editor.set_note(note)
|
2021-03-16 07:39:41 +01:00
|
|
|
|
|
|
|
def cleanup_and_close(self) -> None:
|
|
|
|
gui_hooks.operation_did_execute.remove(self.on_operation_did_execute)
|
|
|
|
self.editor.cleanup()
|
|
|
|
saveGeom(self, "editcurrent")
|
|
|
|
aqt.dialogs.markClosed("EditCurrent")
|
|
|
|
QDialog.reject(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def reopen(self, mw: aqt.AnkiQt) -> None:
|
2021-03-16 07:39:41 +01:00
|
|
|
if card := self.mw.reviewer.card:
|
|
|
|
self.editor.set_note(card.note())
|
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:
|
2021-03-16 13:40:37 +01:00
|
|
|
self.editor.call_after_note_saved(self._saveAndClose)
|
2017-08-16 04:45:33 +02:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def _saveAndClose(self) -> None:
|
2021-03-16 07:39:41 +01:00
|
|
|
self.cleanup_and_close()
|
2013-04-11 12:23:32 +02:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def closeWithCallback(self, onsuccess: Callable[[], None]) -> None:
|
2021-02-01 14:28:21 +01:00
|
|
|
def callback() -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
self._saveAndClose()
|
|
|
|
onsuccess()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
self.editor.call_after_note_saved(callback)
|
2021-03-16 07:39:41 +01:00
|
|
|
|
|
|
|
onReset = on_operation_did_execute
|