Pamhyr2/src/View/Scenarios/UndoCommand.py

116 lines
3.2 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 (
QUndoCommand, QUndoStack,
)
class AddScenariosCommand(QUndoCommand):
def __init__(self, study, pos):
QUndoCommand.__init__(self)
self._study = study
self._pos = pos
self._new = None
def undo(self):
self._study.scenarios.delete(self._new.id)
self._study.reload_from_scenario(self._new.parent)
def redo(self):
if self._new is None:
self._new = self._study.new_scenario_from_current()
self._new.set_pos(self._pos.x(), self._pos.y())
else:
self._new.set_as_not_deleted()
self._study.reload_from_scenario(self._new)
class SelectScenariosCommand(QUndoCommand):
def __init__(self, study, new):
QUndoCommand.__init__(self)
self._study = study
self._old = study.status.scenario
self._new = new
def undo(self):
self._study.reload_from_scenario(self._old)
def redo(self):
self._study.reload_from_scenario(self._new)
class DeleteScenariosCommand(QUndoCommand):
def __init__(self, study, scenario):
QUndoCommand.__init__(self)
self._study = study
self._scenario = scenario
self._reload = study.status.scenario == scenario
def undo(self):
self._scenario.set_as_not_deleted()
if self._reload:
self._study.reload_from_scenario(self._scenario)
def redo(self):
if self._reload:
self._study.reload_from_scenario(self._scenario.parent)
self._scenario.set_as_deleted()
class DuplicateScenariosCommand(QUndoCommand):
def __init__(self, study):
QUndoCommand.__init__(self)
self._study = study
self._new = None
def undo(self):
self._study.scenarios.delete(self._new.id)
self._study.reload_from_scenario(self._new.parent)
def redo(self):
if self._new is None:
self._new = self._study.duplicate_current_scenario()
else:
self._new.set_as_not_deleted()
self._study.reload_from_scenario(self._new)
class SetCommand(QUndoCommand):
def __init__(self, scenario, column, new_value):
QUndoCommand.__init__(self)
self._el = scenario
self._column = column
self._old = self._el[self._column]
self._new = new_value
def undo(self):
self._el[self._column] = self._old
def redo(self):
self._el[self._column] = self._new