77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
|
|
from PySide6.QtGui import QUndoCommand
|
|
|
|
from bedit_core.models import Component, Parameter, ParameterID
|
|
|
|
|
|
class AddParamCommand(QUndoCommand):
|
|
def __init__(
|
|
self,
|
|
document: object,
|
|
component: Component,
|
|
param_id: ParameterID,
|
|
param: Parameter,
|
|
) -> None:
|
|
super().__init__("Add port")
|
|
self.document = document
|
|
self.component = component
|
|
self.param_id = param_id
|
|
self.param = deepcopy(param)
|
|
|
|
def redo(self) -> None:
|
|
self.component.parameters[self.param_id] = deepcopy(self.param)
|
|
self.document.model_changed.emit(self.document.model)
|
|
|
|
def undo(self) -> None:
|
|
del self.component.parameters[self.param_id]
|
|
self.document.model_changed.emit(self.document.model)
|
|
|
|
|
|
class RemoveParamCommand(QUndoCommand):
|
|
def __init__(
|
|
self,
|
|
document: object,
|
|
component: Component,
|
|
param_id: ParameterID,
|
|
) -> None:
|
|
super().__init__("Remove parameter")
|
|
self.document = document
|
|
self.component = component
|
|
self.param_id = param_id
|
|
self.param = deepcopy(component.parameters[param_id])
|
|
|
|
def redo(self) -> None:
|
|
del self.component.parameters[self.param_id]
|
|
self.document.model_changed.emit(self.document.model)
|
|
|
|
def undo(self) -> None:
|
|
self.component.parameters[self.param_id] = deepcopy(self.param)
|
|
self.document.model_changed.emit(self.document.model)
|
|
|
|
|
|
class ChangeParamCommand(QUndoCommand):
|
|
def __init__(
|
|
self,
|
|
document: object,
|
|
component: Component,
|
|
param_id: ParameterID,
|
|
param: Parameter,
|
|
) -> None:
|
|
super().__init__("Change parameter")
|
|
self.document = document
|
|
self.component = component
|
|
self.param_id = param_id
|
|
self.old_param = deepcopy(component.parameters[param_id])
|
|
self.new_param = deepcopy(param)
|
|
|
|
def redo(self) -> None:
|
|
self.component.parameters[self.param_id] = deepcopy(self.new_param)
|
|
self.document.model_changed.emit(self.document.model)
|
|
|
|
def undo(self) -> None:
|
|
self.component.parameters[self.param_id] = deepcopy(self.old_param)
|
|
self.document.model_changed.emit(self.document.model)
|