2021-04-03 06:38:49 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-17 04:43:34 +02:00
|
|
|
from anki.collection import OpChanges, OpChangesAfterUndo, Preferences
|
2021-04-03 06:38:49 +02:00
|
|
|
from anki.errors import UndoEmpty
|
|
|
|
from aqt import gui_hooks
|
2021-04-06 06:36:13 +02:00
|
|
|
from aqt.operations import CollectionOp
|
2021-04-03 06:38:49 +02:00
|
|
|
from aqt.qt import QWidget
|
2023-10-17 04:43:34 +02:00
|
|
|
from aqt.utils import showWarning, tooltip, tr
|
2021-04-03 06:38:49 +02:00
|
|
|
|
|
|
|
|
2021-04-06 06:36:13 +02:00
|
|
|
def undo(*, parent: QWidget) -> None:
|
2021-04-03 06:38:49 +02:00
|
|
|
"Undo the last operation, and refresh the UI."
|
|
|
|
|
2021-05-08 09:51:36 +02:00
|
|
|
def on_success(out: OpChangesAfterUndo) -> None:
|
|
|
|
gui_hooks.state_did_undo(out)
|
|
|
|
tooltip(tr.undo_action_undone(action=out.operation), parent=parent)
|
|
|
|
|
2021-04-03 06:38:49 +02:00
|
|
|
def on_failure(exc: Exception) -> None:
|
2023-10-17 04:43:34 +02:00
|
|
|
if not isinstance(exc, UndoEmpty):
|
2021-04-03 06:38:49 +02:00
|
|
|
showWarning(str(exc), parent=parent)
|
|
|
|
|
2021-05-08 09:51:36 +02:00
|
|
|
CollectionOp(parent, lambda col: col.undo()).success(on_success).failure(
|
|
|
|
on_failure
|
|
|
|
).run_in_background()
|
2021-04-06 06:36:13 +02:00
|
|
|
|
2021-04-03 06:38:49 +02:00
|
|
|
|
2021-05-19 07:18:39 +02:00
|
|
|
def redo(*, parent: QWidget) -> None:
|
|
|
|
"Redo the last operation, and refresh the UI."
|
|
|
|
|
|
|
|
def on_success(out: OpChangesAfterUndo) -> None:
|
|
|
|
tooltip(tr.undo_action_redone(action=out.operation), parent=parent)
|
|
|
|
|
|
|
|
CollectionOp(parent, lambda col: col.redo()).success(on_success).run_in_background()
|
|
|
|
|
|
|
|
|
2021-08-19 02:47:55 +02:00
|
|
|
def set_preferences(
|
|
|
|
*, parent: QWidget, preferences: Preferences
|
|
|
|
) -> CollectionOp[OpChanges]:
|
|
|
|
return CollectionOp(parent, lambda col: col.set_preferences(preferences))
|