Files
BEdit/src/bedit_gui/controllers/undo_controller.py
2026-07-26 14:46:18 +02:00

50 lines
1.6 KiB
Python

from PySide6.QtCore import QObject
from bedit_gui.documents import Document
from bedit_gui.services.application_logging import get_logger
from bedit_gui.views.main_window import MainWindow
logger = get_logger(__name__)
class UndoController(QObject):
def __init__(self, document: Document, window: MainWindow) -> None:
super().__init__(window)
self.document = document
self.window = window
undo_action = window.ui.actionUndo
redo_action = window.ui.actionRedo
undo_stack = document.undo_stack
undo_action.triggered.connect(self.undo)
redo_action.triggered.connect(self.redo)
undo_stack.canUndoChanged.connect(undo_action.setEnabled)
undo_stack.canRedoChanged.connect(redo_action.setEnabled)
undo_stack.undoTextChanged.connect(self._update_undo_text)
undo_stack.redoTextChanged.connect(self._update_redo_text)
undo_action.setEnabled(undo_stack.canUndo())
redo_action.setEnabled(undo_stack.canRedo())
def undo(self) -> None:
command = self.document.undo_stack.undoText()
self.document.undo_stack.undo()
logger.info("Undo: %s", command)
def redo(self) -> None:
command = self.document.undo_stack.redoText()
self.document.undo_stack.redo()
logger.info("Redo: %s", command)
def _update_undo_text(self, command: str) -> None:
text = f"Undo {command}" if command else "Undo"
self.window.ui.actionUndo.setText(text)
def _update_redo_text(self, command: str) -> None:
text = f"Redo {command}" if command else "Redo"
self.window.ui.actionRedo.setText(text)