Files
BEdit/src/bedit_gui/commands/simulation_plot_commands.py

117 lines
3.9 KiB
Python

from __future__ import annotations
from collections.abc import Callable
from copy import deepcopy
from PySide6.QtGui import QUndoCommand
from bedit_gui.simulation_models import SimulationPlotSettings, SimulationPlotTab, SimulationRoot
ChangedCallback = Callable[[int], None]
class AddSimulationPlotTabCommand(QUndoCommand):
def __init__(self, root: SimulationRoot, tab: SimulationPlotTab, index: int, changed: ChangedCallback) -> None:
super().__init__("Add plot")
self.root = root
self.tab = tab
self.index = index
self.changed = changed
def redo(self) -> None:
self.root.plot_tabs.insert(self.index, self.tab)
self.changed(self.index)
def undo(self) -> None:
self.root.plot_tabs.pop(self.index)
self.changed(max(0, self.index - 1))
class RemoveSimulationPlotTabCommand(QUndoCommand):
def __init__(self, root: SimulationRoot, index: int, changed: ChangedCallback) -> None:
super().__init__("Remove plot")
self.root = root
self.index = index
self.tab = root.plot_tabs[index]
self.changed = changed
def redo(self) -> None:
self.root.plot_tabs.pop(self.index)
self.changed(min(self.index, len(self.root.plot_tabs) - 1))
def undo(self) -> None:
self.root.plot_tabs.insert(self.index, self.tab)
self.changed(self.index)
class RenameSimulationPlotTabCommand(QUndoCommand):
def __init__(self, root: SimulationRoot, index: int, name: str, changed: ChangedCallback) -> None:
super().__init__("Rename plot")
self.root = root
self.index = index
self.old_name = root.plot_tabs[index].name
self.name = name
self.changed = changed
def redo(self) -> None:
self.root.plot_tabs[self.index].name = self.name
self.changed(self.index)
def undo(self) -> None:
self.root.plot_tabs[self.index].name = self.old_name
self.changed(self.index)
class ChangeSimulationPlotSignalsCommand(QUndoCommand):
def __init__(self, root: SimulationRoot, index: int, signals: list[str], changed: ChangedCallback) -> None:
super().__init__("Change plotted signals")
self.root = root
self.index = index
self.old_signals = list(root.plot_tabs[index].signals)
self.signals = list(signals)
self.changed = changed
def redo(self) -> None:
self.root.plot_tabs[self.index].signals = list(self.signals)
self.changed(self.index)
def undo(self) -> None:
self.root.plot_tabs[self.index].signals = list(self.old_signals)
self.changed(self.index)
class ChangeSimulationPlotXAxisCommand(QUndoCommand):
def __init__(self, root: SimulationRoot, index: int, x_axis: str | None, changed: ChangedCallback) -> None:
super().__init__("Change plot x axis")
self.root = root
self.index = index
self.old_x_axis = root.plot_tabs[index].x_axis
self.x_axis = x_axis
self.changed = changed
def redo(self) -> None:
self.root.plot_tabs[self.index].x_axis = self.x_axis
self.changed(self.index)
def undo(self) -> None:
self.root.plot_tabs[self.index].x_axis = self.old_x_axis
self.changed(self.index)
class ChangeSimulationPlotSettingsCommand(QUndoCommand):
def __init__(self, root: SimulationRoot, index: int, settings: SimulationPlotSettings, changed: ChangedCallback) -> None:
super().__init__("Change plot settings")
self.root = root
self.index = index
self.old_settings = deepcopy(root.plot_tabs[index].settings)
self.settings = deepcopy(settings)
self.changed = changed
def redo(self) -> None:
self.root.plot_tabs[self.index].settings = deepcopy(self.settings)
self.changed(self.index)
def undo(self) -> None:
self.root.plot_tabs[self.index].settings = deepcopy(self.old_settings)
self.changed(self.index)