ProfileUndoCommand.py 3.55 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

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

    def redo(self):
        self._profile.insert(self._index)

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(self._profile.point(row))
        self._points.reverse()

    def undo(self):
        row = self._rows[0]
        for point in self._points:
            self._profile.insert_point(row, point)

    def redo(self):
        row = self._rows[0]
        for _ in self._rows:
            self._profile.delete(row)

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._profiles)):
            self._profile.delete(self._row)

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