Pamhyr2/src/View/BoundaryConditionsTemperature/UndoCommand.py

112 lines
3.1 KiB
Python

# UndoCommand.py -- Pamhyr
# Copyright (C) 2023-2025 INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
from copy import deepcopy
from tools import trace, timer
from PyQt5.QtWidgets import (
QMessageBox, QUndoCommand, QUndoStack,
)
from Model.BoundaryConditionsTemperature.BoundaryConditionTemperature \
import BoundaryConditionTemperature
from Model.BoundaryConditionsTemperature.BoundaryConditionsTemperatureList \
import BoundaryConditionsTemperatureList
class SetNodeCommand(QUndoCommand):
def __init__(self, bcs, index, node):
QUndoCommand.__init__(self)
self._bcs = bcs
self._index = index
self._old = self._bcs.get(self._index).node
self._new = node.id if node is not None else None
self._previous = next(
(
bc for i, bc in enumerate(self._bcs.lst)
if i != self._index and bc.node == self._new
),
None
)
def undo(self):
self._bcs.get(self._index).node = self._old
if self._previous is not None:
self._previous.node = self._new
def redo(self):
self._bcs.get(self._index).node = self._new
if self._previous is not None:
self._previous.node = None
class SetNameCommand(QUndoCommand):
def __init__(self, bcs, index, name):
QUndoCommand.__init__(self)
self._bcs = bcs
self._index = index
self._name = name
self._old = self._bcs.get(self._index).name
self._new = self._name
def undo(self):
self._bcs.get(self._index).name = self._old
def redo(self):
self._bcs.get(self._index).name = self._new
class AddCommand(QUndoCommand):
def __init__(self, bcs, index):
QUndoCommand.__init__(self)
self._bcs = bcs
self._index = index
self._new = None
def undo(self):
self._bcs.delete_i([self._index])
def redo(self):
if self._new is None:
self._new = self._bcs.new(self._index)
else:
self._bcs.insert(self._index, self._new)
class DelCommand(QUndoCommand):
def __init__(self, bcs, rows):
QUndoCommand.__init__(self)
self._bcs = bcs
self._rows = rows
self._bc = []
for row in rows:
self._bc.append((row, self._bcs._lst[row]))
self._bc.sort()
def undo(self):
for row, el in self._bc:
self._bcs.insert(row, el)
def redo(self):
self._bcs.delete_i(self._rows)