Equation editor undo
This commit is contained in:
53
src/bedit_gui/commands/equation_text_command.py
Normal file
53
src/bedit_gui/commands/equation_text_command.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from PySide6.QtGui import QUndoCommand
|
||||
|
||||
from bedit_core.models import Component, EquationImplementation
|
||||
|
||||
|
||||
class ChangeEquationTextCommand(QUndoCommand):
|
||||
COMMAND_ID = 1001
|
||||
|
||||
def __init__(self, document: object, component: Component, section: str, text: list[str], edit_id: int) -> None:
|
||||
super().__init__(self._command_text(section))
|
||||
implementation = component.implementation
|
||||
if not isinstance(implementation, EquationImplementation):
|
||||
raise TypeError("equation text can only be changed on an equation component")
|
||||
|
||||
self.document = document
|
||||
self.component = component
|
||||
self.section = section
|
||||
self.old_text = list(getattr(implementation, section))
|
||||
self.new_text = list(text)
|
||||
self.edit_id = edit_id
|
||||
|
||||
def id(self) -> int:
|
||||
return self.COMMAND_ID
|
||||
|
||||
def mergeWith(self, other: QUndoCommand) -> bool:
|
||||
if not isinstance(other, ChangeEquationTextCommand):
|
||||
return False
|
||||
if other.component is not self.component or other.section != self.section or other.edit_id != self.edit_id:
|
||||
return False
|
||||
self.new_text = list(other.new_text)
|
||||
return True
|
||||
|
||||
def redo(self) -> None:
|
||||
self._set_text(self.new_text)
|
||||
|
||||
def undo(self) -> None:
|
||||
self._set_text(self.old_text)
|
||||
|
||||
def _set_text(self, text: list[str]) -> None:
|
||||
implementation = self.component.implementation
|
||||
assert isinstance(implementation, EquationImplementation)
|
||||
setattr(implementation, self.section, list(text))
|
||||
self.document.equation_text_changed.emit(self.component, self.section)
|
||||
|
||||
@staticmethod
|
||||
def _command_text(section: str) -> str:
|
||||
return {
|
||||
"declarations": "Edit declarations",
|
||||
"initial_equations": "Edit initial equations",
|
||||
"equations": "Edit equations",
|
||||
}[section]
|
||||
Reference in New Issue
Block a user