anki/qt/aqt/operations/collection.py
Abdo b23f17df27
Drop support for checkpoints (#2742)
* Drop support for checkpoints

* Deprecate .flush()

* Remove .begin/.commit

* Remove rollback() and deprecate save/autosave/reset()

There's no need to commit anymore, as the Rust code is handling
transactions for us.

* Add safer transact() method

This will ensure add-on authors can't accidentally leave a transaction
open, leading to data loss.

---------

Co-authored-by: Damien Elmes <gpg@ankiweb.net>
2023-10-17 12:43:34 +10:00

43 lines
1.4 KiB
Python

# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
from anki.collection import OpChanges, OpChangesAfterUndo, Preferences
from anki.errors import UndoEmpty
from aqt import gui_hooks
from aqt.operations import CollectionOp
from aqt.qt import QWidget
from aqt.utils import showWarning, tooltip, tr
def undo(*, parent: QWidget) -> None:
"Undo the last operation, and refresh the UI."
def on_success(out: OpChangesAfterUndo) -> None:
gui_hooks.state_did_undo(out)
tooltip(tr.undo_action_undone(action=out.operation), parent=parent)
def on_failure(exc: Exception) -> None:
if not isinstance(exc, UndoEmpty):
showWarning(str(exc), parent=parent)
CollectionOp(parent, lambda col: col.undo()).success(on_success).failure(
on_failure
).run_in_background()
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()
def set_preferences(
*, parent: QWidget, preferences: Preferences
) -> CollectionOp[OpChanges]:
return CollectionOp(parent, lambda col: col.set_preferences(preferences))