Forked from HYCAR-Hydro / airGR
Source project has a limited visibility.
UndoCommand.py 3.66 KiB
# -*- coding: utf-8 -*-

from tools import trace, timer

from PyQt5.QtWidgets import (
    QMessageBox, QUndoCommand, QUndoStack,
)

from Model.Geometry.Profile import Profile


class SetDataCommand(QUndoCommand):
    def __init__(self, profile, index, old_value, new_value):
        QUndoCommand.__init__(self)

        self._profile = profile
        self._index = index
        self._old = old_value
        self._new = new_value

class SetXCommand(SetDataCommand):
    def undo(self):
        self._profile.point(self._index).x = self._old

    def redo(self):
        self._profile.point(self._index).x = self._new

class SetYCommand(SetDataCommand):
    def undo(self):
        self._profile.point(self._index).y = self._old

    def redo(self):
        self._profile.point(self._index).y = self._new

class SetZCommand(SetDataCommand):
    def undo(self):
        self._profile.point(self._index).z = self._old

    def redo(self):
        self._profile.point(self._index).z = self._new

class SetNameCommand(SetDataCommand):
    def undo(self):
        self._profile.point(self._index).name = self._old

    def redo(self):
        self._profile.point(self._index).name = self._new


class AddCommand(QUndoCommand):
    def __init__(self, profile, index):
        QUndoCommand.__init__(self)

        self._profile = profile
        self._index = index
        self._point = None

    def undo(self):
        self._profile.delete([self._index])

    def redo(self):
        if self._point is None:
            self._point = self._profile.insert(self._index)
        else:
            self._profile.insert_point(self._index, self._point)

class DelCommand(QUndoCommand):
    def __init__(self, profile, rows):
        QUndoCommand.__init__(self)

        self._profile = profile
        self._rows = rows

        self._points = []
        for row in rows:
            self._points.append((row, self._profile.point(row)))
        self._points.sort()     # Sort by row index

    def undo(self):
        for row, point in self._points:
            self._profile.insert_point(row, point)

    def redo(self):
        self._profile.delete(self._rows)

class SortCommand(QUndoCommand):
    def __init__(self, profile, column, _reverse):
        QUndoCommand.__init__(self)

        self._profile = profile
        self._column = column
        self._reverse = _reverse

        old = self._profile.points
        self._profile.sort(self._column, self._reverse)
        new = self._profile.points

        self._indexes = list(
            map(
                lambda p: old.index(p),
                new
            )
        )

    def undo(self):
        self._profile.sort_with_indexes(self._indexes)

    def redo(self):
        self._profile.sort(self._column, self._reverse)


class MoveCommand(QUndoCommand):
    def __init__(self, profile, up, i):
        QUndoCommand.__init__(self)

        self._profile = profile
        self._up = up == "up"
        self._i = i

    def undo(self):
        if self._up:
            self._profile.move_up_point(self._i)
        else:
            self._profile.move_down_point(self._i)

    def redo(self):
        if self._up:
            self._profile.move_up_point(self._i)
        else:
            self._profile.move_down_point(self._i)


class PasteCommand(QUndoCommand):
    def __init__(self, profile, row, points):
        QUndoCommand.__init__(self)

        self._profile = profile
        self._row = row
        self._points = points

        self._points.reverse()

    def undo(self):
        for ind in range(len(self._points)):
            self._profile.delete([self._row])

    def redo(self):
        for point in self._points:
            self._profile.insert_point(self._row, point)