How to use the gaphas.constraint.Constraint function in gaphas

To help you get started, we’ve selected a few gaphas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DLR-RM / RAFCON / source / rafcon / mvc / mygaphas / constraint.py View on Github external
class KeepRelativePositionConstraint(Constraint):
    def __init__(self, anchor, point):
        super(KeepRelativePositionConstraint, self).__init__(anchor[0], anchor[1], point[0], point[1])

        self.point = point
        self.anchor = anchor

        self._dx = point.x.value - anchor.x.value
        self._dy = point.y.value - anchor.y.value

    def solve_for(self, var):
        _update(self.point.x, self._dx + self.anchor.x)
        _update(self.point.y, self._dy + self.anchor.y)


class KeepPortDistanceConstraint(Constraint):
    def __init__(self, anchor, point, port, port_side_size, incoming):
        super(KeepPortDistanceConstraint, self).__init__(anchor[0], anchor[1], point[0], point[1])
        assert isinstance(port, PortView)

        self.distance = port_side_size

        self.incoming = incoming
        self.point = point
        self.anchor = anchor
        self.port = port

    def solve_for(self, var):
        distance = self.distance
        if self.port.side is SnappedSide.TOP:
            _update(self.point[0], self.anchor[0])
            _update(self.point[1],
github DLR-RM / RAFCON / source / rafcon / gui / mygaphas / constraint.py View on Github external
_update(self.child_nw[1], self.parent_nw[1].value + margin)
            _update(self.child_se[1], self.child_nw[1].value + height)
            updated = True
        # Lower edge (south)
        elif self.parent_se[1].value < self.child_se[1].value + margin - EPSILON:
            height = child_height()
            _update(self.child_se[1], self.parent_se[1].value - margin)
            _update(self.child_nw[1], self.child_se[1].value - height)
            updated = True

        from rafcon.gui.mygaphas.items.state import StateView
        if updated and isinstance(self.child, StateView):
            self.child.update_minimum_size_of_children()


class KeepPointWithinConstraint(Constraint):
    """Ensure that the point is within its parent

    Attributes:
     - parent_nw: NW coordinates of parent
     - parent_se: SE coordinates of parent
     - child: coordinates of child
    """

    def __init__(self, parent_nw, parent_se, child, margin_method=None):
        super(KeepPointWithinConstraint, self).__init__(parent_nw[0], parent_nw[1], parent_se[0], parent_se[1],
                                                        child[0], child[1])
        self.parent_nw = parent_nw
        self.parent_se = parent_se
        self.child = child

        if not margin_method:
github DLR-RM / RAFCON / source / rafcon / gui / mygaphas / constraint.py View on Github external
from gaphas.constraint import Constraint

from rafcon.gui.mygaphas.items.ports import PortView
from rafcon.gui.mygaphas.utils.enums import SnappedSide

from copy import deepcopy

EPSILON = 1e-6


def _update(variable, value):
    if abs(variable.value - value) > EPSILON:
        variable.value = value


class KeepRectangleWithinConstraint(Constraint):
    """Ensure that the children is within its parent

    Attributes:
     - parent_nw: NW coordinates of parent
     - parent_se: SE coordinates of parent
     - child_nw: NW coordinates of child
     - child_se: SE coordinates of child
    """

    def __init__(self, parent_nw, parent_se, child_nw, child_se, child=None, margin_method=None):
        super(KeepRectangleWithinConstraint, self).__init__(parent_nw[0], parent_nw[1], parent_se[0], parent_se[1],
                                                            child_nw[0], child_nw[1], child_se[0], child_se[1])
        self.parent_nw = parent_nw
        self.parent_se = parent_se
        self.child_nw = child_nw
        self.child_se = child_se
github DLR-RM / RAFCON / source / rafcon / gui / mygaphas / constraint.py View on Github external
self.anchor[1].value - distance if self.incoming else self.anchor[1].value + distance)
        elif self.port.side is SnappedSide.BOTTOM:
            _update(self.point[0], self.anchor[0])
            _update(self.point[1],
                    self.anchor[1].value + distance if self.incoming else self.anchor[1].value - distance)
        elif self.port.side is SnappedSide.LEFT:
            _update(self.point[0],
                    self.anchor[0].value - distance if self.incoming else self.anchor[0].value + distance)
            _update(self.point[1], self.anchor[1])
        elif self.port.side is SnappedSide.RIGHT:
            _update(self.point[0],
                    self.anchor[0].value + distance if self.incoming else self.anchor[0].value - distance)
            _update(self.point[1], self.anchor[1])


class PortRectConstraint(Constraint):
    """
    Keeps ports on rectangular path along containing state
    :param rect: Rect (NWpos, SEpos) specifying the path to bind port to
    :param point: Port position
    :param port: Port to bind
    """

    def __init__(self, rect, point, port):
        super(PortRectConstraint, self).__init__(rect[0][0], rect[0][1], rect[1][0], rect[1][1], point[0], point[1])

        self._rect = rect
        self._point = point
        self._initial_pos = deepcopy(point)
        self._port = port

        self._distance_to_border = self._port.port_side_size / 2.
github DLR-RM / RAFCON / source / rafcon / gui / mygaphas / constraint.py View on Github external
"""
        margin = self.margin_method()
        if self.parent_nw[0].value > self.child[0].value - margin:
            _update(self.child[0], self.parent_nw[0].value + margin)
        # Right edge (east)
        if self.parent_se[0].value < self.child[0].value + margin:
            _update(self.child[0], self.parent_se[0].value - margin)
        # Upper edge (north)
        if self.parent_nw[1].value > self.child[1].value - margin:
            _update(self.child[1], self.parent_nw[1].value + margin)
        # Lower edge (south)
        if self.parent_se[1].value < self.child[1].value + margin:
            _update(self.child[1], self.parent_se[1].value - margin)


class KeepRelativePositionConstraint(Constraint):
    def __init__(self, anchor, point):
        super(KeepRelativePositionConstraint, self).__init__(anchor[0], anchor[1], point[0], point[1])

        self.point = point
        self.anchor = anchor

        self._dx = point.x.value - anchor.x.value
        self._dy = point.y.value - anchor.y.value

    def solve_for(self, var):
        _update(self.point.x, self._dx + self.anchor.x)
        _update(self.point.y, self._dy + self.anchor.y)


class KeepPortDistanceConstraint(Constraint):
    def __init__(self, anchor, point, port, distance_func, incoming):
github DLR-RM / RAFCON / source / rafcon / mvc / mygaphas / constraint.py View on Github external
from gaphas.constraint import Constraint

from rafcon.mvc.mygaphas.items.ports import PortView
from rafcon.mvc.mygaphas.utils.enums import SnappedSide

from copy import deepcopy

EPSILON = 1e-6


def _update(variable, value):
    if abs(variable.value - value) > EPSILON:
        variable.value = value


class KeepRectangleWithinConstraint(Constraint):
    """Ensure that the children is within its parent

    Attributes:
     - parent_nw: NW coordinates of parent
     - parent_se: SE coordinates of parent
     - child_nw: NW coordinates of child
     - child_se: SE coordinates of child
    """

    def __init__(self, parent_nw, parent_se, child_nw, child_se, child=None, margin=None):
        super(KeepRectangleWithinConstraint, self).__init__(parent_nw[0], parent_nw[1], parent_se[0], parent_se[1],
                                                            child_nw[0], child_nw[1], child_se[0], child_se[1])
        self.parent_nw = parent_nw
        self.parent_se = parent_se
        self.child_nw = child_nw
        self.child_se = child_se
github DLR-RM / RAFCON / source / rafcon / gui / mygaphas / constraint.py View on Github external
"""Snaps the port to the correct side upon state size change
        """
        px, py = self._point
        nw_x, nw_y, se_x, se_y = self.get_adjusted_border_positions()

        if self._port.side == SnappedSide.RIGHT:
            _update(px, se_x)
        elif self._port.side == SnappedSide.BOTTOM:
            _update(py, se_y)
        elif self._port.side == SnappedSide.LEFT:
            _update(px, nw_x)
        elif self._port.side == SnappedSide.TOP:
            _update(py, nw_y)


class BorderWidthConstraint(Constraint):
    def __init__(self, north_west, south_east, border_width, factor):
        super(BorderWidthConstraint, self).__init__(north_west[0], north_west[1], south_east[0], south_east[1])

        self.nw = north_west
        self.se = south_east
        self.border_width = border_width
        self.factor = factor

    def solve_for(self, var=None):
        width = float(self.se.x) - float(self.nw.x)
        height = float(self.se.y) - float(self.nw.y)
        _update(self.border_width, min(width, height) / self.factor)
github DLR-RM / RAFCON / source / rafcon / mvc / mygaphas / constraint.py View on Github external
self.anchor[1].value - distance if self.incoming else self.anchor[1].value + distance)
        elif self.port.side is SnappedSide.BOTTOM:
            _update(self.point[0], self.anchor[0])
            _update(self.point[1],
                    self.anchor[1].value + distance if self.incoming else self.anchor[1].value - distance)
        elif self.port.side is SnappedSide.LEFT:
            _update(self.point[0],
                    self.anchor[0].value - distance if self.incoming else self.anchor[0].value + distance)
            _update(self.point[1], self.anchor[1])
        elif self.port.side is SnappedSide.RIGHT:
            _update(self.point[0],
                    self.anchor[0].value + distance if self.incoming else self.anchor[0].value - distance)
            _update(self.point[1], self.anchor[1])


class PortRectConstraint(Constraint):
    """
    Keeps ports on rectangular path along containing state
    :param rect: Rect (NWpos, SEpos) specifying the path to bind port to
    :param point: Port position
    :param port: Port to bind
    """

    def __init__(self, rect, point, port):
        super(PortRectConstraint, self).__init__(rect[0][0], rect[0][1], rect[1][0], rect[1][1], point[0], point[1])

        self._rect = rect
        self._point = point
        self._initial_pos = deepcopy(point)
        self._port = port

        self._distance_to_border = self._port.port_side_size / 2.
github DLR-RM / RAFCON / source / rafcon / gui / mygaphas / constraint.py View on Github external
class KeepRelativePositionConstraint(Constraint):
    def __init__(self, anchor, point):
        super(KeepRelativePositionConstraint, self).__init__(anchor[0], anchor[1], point[0], point[1])

        self.point = point
        self.anchor = anchor

        self._dx = point.x.value - anchor.x.value
        self._dy = point.y.value - anchor.y.value

    def solve_for(self, var):
        _update(self.point.x, self._dx + self.anchor.x)
        _update(self.point.y, self._dy + self.anchor.y)


class KeepPortDistanceConstraint(Constraint):
    def __init__(self, anchor, point, port, distance_func, incoming):
        super(KeepPortDistanceConstraint, self).__init__(anchor[0], anchor[1], point[0], point[1])
        assert isinstance(port, PortView)

        self.distance_func = distance_func

        self.incoming = incoming
        self.point = point
        self.anchor = anchor
        self.port = port

    def solve_for(self, var):
        distance = self.distance_func()
        if self.port.side is SnappedSide.TOP:
            _update(self.point[0], self.anchor[0])
            _update(self.point[1],
github DLR-RM / RAFCON / source / rafcon / mvc / mygaphas / constraint.py View on Github external
class KeepPointWithinConstraint(KeepRectangleWithinConstraint):
    """Ensure that the children is within its parent

    Attributes:
     - parent_nw: NW coordinates of parent
     - parent_se: SE coordinates of parent
     - child_pos: coordinates of child
    """

    def __init__(self, parent_nw, parent_se, child_pos, margin=None):
        super(KeepPointWithinConstraint, self).__init__(parent_nw, parent_se, child_pos, child_pos)


class KeepRelativePositionConstraint(Constraint):
    def __init__(self, anchor, point):
        super(KeepRelativePositionConstraint, self).__init__(anchor[0], anchor[1], point[0], point[1])

        self.point = point
        self.anchor = anchor

        self._dx = point.x.value - anchor.x.value
        self._dy = point.y.value - anchor.y.value

    def solve_for(self, var):
        _update(self.point.x, self._dx + self.anchor.x)
        _update(self.point.y, self._dy + self.anchor.y)


class KeepPortDistanceConstraint(Constraint):
    def __init__(self, anchor, point, port, port_side_size, incoming):