mirror of https://gitlab.com/pamhyr/pamhyr2
179 lines
4.2 KiB
Python
179 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from copy import deepcopy
|
|
from tools import trace, timer
|
|
|
|
from PyQt5.QtWidgets import (
|
|
QMessageBox, QUndoCommand, QUndoStack,
|
|
)
|
|
|
|
from Model.BoundaryCondition.BoundaryCondition import BoundaryCondition
|
|
from Model.BoundaryCondition.BoundaryConditionList import BoundaryConditionList
|
|
|
|
class SetNameCommand(QUndoCommand):
|
|
def __init__(self, lst, index, new_value):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._index = index
|
|
self._old = self._lst[self._index].name
|
|
self._new = new_value
|
|
|
|
def undo(self):
|
|
self._lst[self._index].name = self._old
|
|
|
|
def redo(self):
|
|
self._lst[self._index].name = self._new
|
|
|
|
class SetNodeCommand(QUndoCommand):
|
|
def __init__(self, lst, index, node):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._index = index
|
|
self._old = self._lst[index].node
|
|
self._new = node
|
|
|
|
def undo(self):
|
|
self._lst[self._index].node = self._old
|
|
|
|
def redo(self):
|
|
self._lst[self._index].node = self._new
|
|
|
|
class SetTypeCommand(QUndoCommand):
|
|
def __init__(self, lst, index, _type):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._index = index
|
|
self._type = _type
|
|
self._old = self._lst[index]
|
|
self._new = self._lst[index].convert(self._type)
|
|
|
|
def undo(self):
|
|
self._lst[self._index] = self._old
|
|
|
|
def redo(self):
|
|
self._lst[self._index] = self._new
|
|
|
|
class AddCommand(QUndoCommand):
|
|
def __init__(self, lst, index):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._index = index
|
|
self._new = None
|
|
|
|
def undo(self):
|
|
self._lst.delete_i([self._index])
|
|
|
|
def redo(self):
|
|
if self._new is None:
|
|
self._new = self._lst.new(self._index)
|
|
else:
|
|
self._lst.insert(self._index, self._new)
|
|
|
|
class DelCommand(QUndoCommand):
|
|
def __init__(self, lst, rows):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._rows = rows
|
|
|
|
self._bc = []
|
|
for row in rows:
|
|
self._bc.append((row, self._lst[row]))
|
|
self._bc.sort()
|
|
|
|
def undo(self):
|
|
for row, el in self._bc:
|
|
self._lst.insert(row, el)
|
|
|
|
def redo(self):
|
|
self._lst.delete_i(self._rows)
|
|
|
|
class SortCommand(QUndoCommand):
|
|
def __init__(self, lst, _reverse):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._reverse = _reverse
|
|
|
|
self._old = self._lst.copy()
|
|
self._indexes = None
|
|
|
|
def undo(self):
|
|
ll = self._lst.copy()
|
|
self._lst.sort(
|
|
key=lambda x: self._indexes[ll.index(x)]
|
|
)
|
|
|
|
def redo(self):
|
|
self._lst.sort(
|
|
reverse=self._reverse,
|
|
key=lambda x: x.name
|
|
)
|
|
if self._indexes is None:
|
|
self._indexes = list(
|
|
map(
|
|
lambda p: self._old.index(p),
|
|
self._lst
|
|
)
|
|
)
|
|
self._old = None
|
|
|
|
|
|
class MoveCommand(QUndoCommand):
|
|
def __init__(self, lst, up, i):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._up = up == "up"
|
|
self._i = i
|
|
|
|
def undo(self):
|
|
if self._up:
|
|
self._lst.move_up(self._i)
|
|
else:
|
|
self._lst.move_down(self._i)
|
|
|
|
def redo(self):
|
|
if self._up:
|
|
self._lst.move_up(self._i)
|
|
else:
|
|
self._lst.move_down(self._i)
|
|
|
|
|
|
class PasteCommand(QUndoCommand):
|
|
def __init__(self, lst, row, bc):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._row = row
|
|
self._bc = deepcopy(bc)
|
|
self._bc.reverse()
|
|
|
|
def undo(self):
|
|
self._lst.delete(self._bc)
|
|
|
|
def redo(self):
|
|
for bc in self._bc:
|
|
self._lst.insert(self._row, bc)
|
|
|
|
|
|
class DuplicateCommand(QUndoCommand):
|
|
def __init__(self, lst, rows, bc):
|
|
QUndoCommand.__init__(self)
|
|
|
|
self._lst = lst
|
|
self._rows = rows
|
|
self._bc = deepcopy(bc)
|
|
self._bc.reverse()
|
|
|
|
def undo(self):
|
|
self._lst.delete(self._bc)
|
|
|
|
def redo(self):
|
|
for profile in self._profiles:
|
|
self._lst.insert(self._rows[0], profile)
|