anki/qt/aqt/editcurrent.py

81 lines
2.8 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from typing import Optional
import aqt.editor
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 *
from aqt.utils import restoreGeom, saveGeom, tr
2019-12-20 10:19:03 +01:00
class EditCurrent(QMainWindow):
def __init__(self, mw: aqt.AnkiQt) -> None:
super().__init__(None, Qt.WindowType.Window)
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())
self.setMinimumHeight(400)
self.setMinimumWidth(250)
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,
)
self.editor.card = self.mw.reviewer.card
self.editor.set_note(self.mw.reviewer.card.note(), focusTo=0)
restoreGeom(self, "editcurrent")
close_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Close)
close_button.setShortcut(QKeySequence("Ctrl+Return"))
# qt5.14+ doesn't handle numpad enter on Windows
self.compat_add_shorcut = QShortcut(QKeySequence("Ctrl+Enter"), self)
qconnect(self.compat_add_shorcut.activated, close_button.click)
gui_hooks.operation_did_execute.append(self.on_operation_did_execute)
self.show()
def on_operation_did_execute(
self, changes: OpChanges, handler: Optional[object]
) -> None:
if changes.note_text and handler is not self.editor:
# reload note
note = self.editor.note
try:
note.load()
except NotFoundError:
# note's been deleted
self.cleanup()
self.close()
return
self.editor.set_note(note)
def cleanup(self) -> None:
gui_hooks.operation_did_execute.remove(self.on_operation_did_execute)
self.editor.cleanup()
saveGeom(self, "editcurrent")
aqt.dialogs.markClosed("EditCurrent")
def reopen(self, mw: aqt.AnkiQt) -> None:
if card := self.mw.reviewer.card:
self.editor.set_note(card.note())
2019-12-23 01:34:10 +01:00
def closeEvent(self, evt: QCloseEvent) -> None:
self.editor.call_after_note_saved(self.cleanup)
def _saveAndClose(self) -> None:
self.cleanup()
self.mw.deferred_delete_and_garbage_collect(self)
self.close()
def closeWithCallback(self, onsuccess: Callable[[], None]) -> None:
2021-02-01 14:28:21 +01:00
def callback() -> None:
self._saveAndClose()
onsuccess()
2019-12-23 01:34:10 +01:00
self.editor.call_after_note_saved(callback)
onReset = on_operation_did_execute