def test_element_glue_on_border_with_secondary_position(
    element, secondary_pos, expected_glue_pos
):
    pos = (50, 50)
    sink = ConnectionSink(element)
    glue_pos = sink.glue(pos, secondary_pos=secondary_pos)

    assert glue_pos == expected_glue_pos
Esempio n. 2
0
    def revert(self, target):
        # Reverse only the diagram level connection.
        # Associations have their own handlers
        connections = target.diagram.connections
        assert connections

        connected = target.diagram.lookup(self.connected.id)
        sink = ConnectionSink(connected)
        sink.port = connected.ports()[self.port_index]

        handle = target.handles()[self.handle_index]
        connector = ConnectorAspect(target, handle, connections)
        connector.connect_handle(sink)
        target.handle(ItemConnected(target, handle, sink.item, sink.port))
Esempio n. 3
0
def test_connect(diagram, comment, commentline, connections):
    sink = ConnectionSink(comment)
    aspect = ConnectorAspect(commentline,
                             commentline.handles()[0], connections)
    aspect.connect(sink)
    cinfo = diagram.connections.get_connection(commentline.handles()[0])
    assert cinfo, cinfo
Esempio n. 4
0
    def glue(self,
             pos: Pos,
             distance: int = GLUE_DISTANCE) -> Optional[ConnectionSinkType]:
        """Glue to an item near a specific point.

        Returns a ConnectionSink or None.
        """
        item = self.item
        handle = self.handle
        view = self.view
        model = view.model
        assert model
        connections = model.connections

        if not handle.connectable:
            return None

        connectable = item_at_point(view,
                                    pos,
                                    distance=distance,
                                    exclude=(item, ))
        if not connectable:
            return None

        connector = Connector(self.item, self.handle, connections)
        sink = ConnectionSink(connectable)

        if connector.glue(sink):
            return sink

        return None
Esempio n. 5
0
def test_allow(commentline, comment, connections):
    aspect = ConnectorAspect(commentline,
                             commentline.handles()[0], connections)
    assert aspect.item is commentline
    assert aspect.handle is commentline.handles()[0]

    sink = ConnectionSink(comment)
    assert aspect.allow(sink)
Esempio n. 6
0
def postload_connect(item: gaphas.Item, handle: gaphas.Handle, target: gaphas.Item):
    """Helper function: when loading a model, handles should be connected as
    part of the `postload` step.

    This function finds a suitable spot on the `target` item to connect
    the handle to.
    """
    connector = ConnectorAspect(item, handle, item.diagram.connections)
    sink = ConnectionSink(target, distance=1e4)
    connector.connect(sink)
Esempio n. 7
0
    def connect(self, line, handle, item):
        """Connect line's handle to an item."""
        diagram = line.diagram
        assert diagram is item.diagram

        sink = ConnectionSink(item, distance=1e4)
        connector = ConnectorAspect(line, handle, diagram.connections)

        connector.connect(sink)

        cinfo = diagram.connections.get_connection(handle)
        assert cinfo.connected is item
        assert cinfo.port
Esempio n. 8
0
def _get_sink(item, handle, target):
    assert item.diagram

    hpos = matrix_i2i(item, target).transform_point(*handle.pos)
    port = None
    dist = 10e6
    for p in target.ports():
        pos, d = p.glue(hpos)
        if not port or d < dist:
            port = p
            dist = d

    return ConnectionSink(target, port)
Esempio n. 9
0
def connect(line, handle, item, port=None):
    """Connect line's handle to an item.

    If port is not provided, then first port is used.
    """
    diagram = line.diagram

    connector = ConnectorAspect(line, handle, diagram.connections)
    sink = ConnectionSink(item, distance=1e4)

    connector.connect(sink)

    cinfo = diagram.connections.get_connection(handle)
    assert cinfo.connected is item
    assert cinfo.port
Esempio n. 10
0
def connect(line, handle, item, port=None):
    """Connect line's handle to an item.

    If port is not provided, then first port is used.
    """
    diagram = line.diagram

    if port is None and len(item.ports()) > 0:
        port = item.ports()[0]

    sink = ConnectionSink(item, port)
    connector = ConnectorAspect(line, handle, diagram.connections)

    connector.connect(sink)

    cinfo = diagram.connections.get_connection(handle)
    assert cinfo.connected is item
    assert cinfo.port is port
def test_element_glue_on_border(element, pos, expected_glue_pos):
    sink = ConnectionSink(element)
    glue_pos = sink.glue(pos)

    assert glue_pos == expected_glue_pos