def connect(self, item, handle, vpos): """ Connect a handle of a item to connectable item. Connectable item is found by `ConnectHandleTool.glue` method. :Parameters: item Connecting item. handle Handle of connecting item. vpos Position to connect to (or near at least) """ connector = Connector(item, handle) # find connectable item and its port sink = self.glue(item, handle, vpos) # no new connectable item, then diconnect and exit if sink: connector.connect(sink) else: cinfo = item.canvas.get_connection(handle) if cinfo: connector.disconnect()
def test_connect(self): sink = ConnectionSink(self.comment, self.comment.ports()[0]) aspect = Connector(self.commentline, self.commentline.handles()[0]) aspect.connect(sink) canvas = self.diagram.canvas cinfo = canvas.get_connection(self.commentline.handles()[0]) assert cinfo, cinfo
def connect(self, item, handle, vpos): """ Connect a handle of a item to connectable item. Connectable item is found by `ConnectHandleTool.glue` method. :Parameters: item Connecting item. handle Handle of connecting item. vpos Position to connect to (or near at least) """ connector = Connector(item, handle) # find connectable item and its port sink = self.glue(item, handle, vpos) # no new connectable item, then diconnect and exit if sink: connector.connect(sink) else: cinfo = item.canvas.get_connection(handle) if cinfo: connector.disconnect()
def test_connect(self): sink = ConnectionSink(self.comment, self.comment.ports()[0]) aspect = Connector(self.commentline, self.commentline.handles()[0]) aspect.connect(sink) canvas = self.diagram.canvas cinfo = canvas.get_connection(self.commentline.handles()[0]) assert cinfo, cinfo
def test_connect(diagram, comment, commentline): sink = ConnectionSink(comment, comment.ports()[0]) aspect = Connector(commentline, commentline.handles()[0]) aspect.connect(sink) canvas = diagram.canvas cinfo = canvas.get_connection(commentline.handles()[0]) assert cinfo, cinfo
def testUndoOnDeletedElement(self): b1 = Box() b2 = Box() line = Line() canvas = Canvas() canvas.add(b1) self.assertEqual(2, len(canvas.solver.constraints)) canvas.add(b2) self.assertEqual(4, len(canvas.solver.constraints)) canvas.add(line) sink = ConnectionSink(b1, b1.ports()[0]) connector = Connector(line, line.handles()[0]) connector.connect(sink) sink = ConnectionSink(b2, b2.ports()[0]) connector = Connector(line, line.handles()[-1]) connector.connect(sink) self.assertEqual(6, len(canvas.solver.constraints)) self.assertEqual(2, len(list(canvas.get_connections(item=line)))) del undo_list[:] # Here disconnect is not invoked! canvas.remove(b2) self.assertEqual(3, len(canvas.solver.constraints)) self.assertEqual(1, len(list(canvas.get_connections(item=line)))) cinfo = canvas.get_connection(line.handles()[0]) self.assertEqual(b1, cinfo.connected) cinfo = canvas.get_connection(line.handles()[-1]) self.assertEqual(None, cinfo) self.assertEqual([], list( canvas.solver.constraints_with_variable( line.handles()[-1].pos.x))) self.assertEqual([], list( canvas.solver.constraints_with_variable( line.handles()[-1].pos.y))) undo() self.assertEqual(6, len(canvas.solver.constraints)) self.assertEqual(2, len(list(canvas.get_connections(item=line)))) cinfo = canvas.get_connection(line.handles()[0]) self.assertEqual(b1, cinfo.connected) cinfo = canvas.get_connection(line.handles()[-1]) self.assertEqual(b2, cinfo.connected)
def _connectItems(self, items, connectorItem): LOG.debug('connect %s', items) handles = connectorItem.handles()[-1], connectorItem.handles()[0] # tail --> head for i in range(2): connector = Connector(connectorItem, handles[i]) sink = ConnectionSink(items[i], items[i].port) connector.connect(sink)
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) sink = _get_sink(item, handle, target) connector.connect(sink)
def test_undo_on_delete_element(revert_undo, undo_fixture): b1 = Box() b2 = Box() line = Line() canvas = Canvas() canvas.add(b1) assert 2 == len(canvas.solver.constraints) canvas.add(b2) assert 4 == len(canvas.solver.constraints) canvas.add(line) sink = ConnectionSink(b1, b1.ports()[0]) connector = Connector(line, line.handles()[0]) connector.connect(sink) sink = ConnectionSink(b2, b2.ports()[0]) connector = Connector(line, line.handles()[-1]) connector.connect(sink) assert 6 == len(canvas.solver.constraints) assert 2 == len(list(canvas.get_connections(item=line))) del undo_fixture[2][:] # Clear undo_list # Here disconnect is not invoked! canvas.remove(b2) assert 3 == len(canvas.solver.constraints) assert 1 == len(list(canvas.get_connections(item=line))) cinfo = canvas.get_connection(line.handles()[0]) assert b1 == cinfo.connected cinfo = canvas.get_connection(line.handles()[-1]) assert cinfo is None assert [] == list( canvas.solver.constraints_with_variable(line.handles()[-1].pos.x)) assert [] == list( canvas.solver.constraints_with_variable(line.handles()[-1].pos.y)) undo_fixture[0]() # Call undo assert 6 == len(canvas.solver.constraints) assert 2 == len(list(canvas.get_connections(item=line))) cinfo = canvas.get_connection(line.handles()[0]) assert b1 == cinfo.connected cinfo = canvas.get_connection(line.handles()[-1]) assert b2 == cinfo.connected
def _postload_connect(self, handle, item): """ Postload connect method. """ from gaphas.aspect import Connector connector = Connector(self, handle) sink = self._get_sink(handle, item) connector.connect(sink)
def testUndoOnDeletedElement(self): b1 = Box() b2 = Box() line = Line() canvas = Canvas() canvas.add(b1) self.assertEquals(6, len(canvas.solver.constraints)) canvas.add(b2) self.assertEquals(12, len(canvas.solver.constraints)) canvas.add(line) sink = ConnectionSink(b1, b1.ports()[0]) connector = Connector(line, line.handles()[0]) connector.connect(sink) sink = ConnectionSink(b2, b2.ports()[0]) connector = Connector(line, line.handles()[-1]) connector.connect(sink) self.assertEquals(14, len(canvas.solver.constraints)) self.assertEquals(2, len(list(canvas.get_connections(item=line)))) del undo_list[:] # Here disconnect is not invoked! canvas.remove(b2) self.assertEquals(7, len(canvas.solver.constraints)) self.assertEquals(1, len(list(canvas.get_connections(item=line)))) cinfo = canvas.get_connection(line.handles()[0]) self.assertEquals(b1, cinfo.connected) cinfo = canvas.get_connection(line.handles()[-1]) self.assertEquals(None, cinfo) self.assertEquals([], list(canvas.solver.constraints_with_variable(line.handles()[-1].pos.x))) self.assertEquals([], list(canvas.solver.constraints_with_variable(line.handles()[-1].pos.y))) undo() self.assertEquals(14, len(canvas.solver.constraints)) self.assertEquals(2, len(list(canvas.get_connections(item=line)))) cinfo = canvas.get_connection(line.handles()[0]) self.assertEquals(b1, cinfo.connected) cinfo = canvas.get_connection(line.handles()[-1]) self.assertEquals(b2, cinfo.connected)
def test_undo_on_delete_element(revert_undo, undo_fixture): b1 = Box() b2 = Box() line = Line() canvas = Canvas() canvas.add(b1) assert 2 == len(canvas.solver.constraints) canvas.add(b2) assert 4 == len(canvas.solver.constraints) canvas.add(line) sink = ConnectionSink(b1, b1.ports()[0]) connector = Connector(line, line.handles()[0]) connector.connect(sink) sink = ConnectionSink(b2, b2.ports()[0]) connector = Connector(line, line.handles()[-1]) connector.connect(sink) assert 6 == len(canvas.solver.constraints) assert 2 == len(list(canvas.get_connections(item=line))) del undo_fixture[2][:] # Clear undo_list # Here disconnect is not invoked! canvas.remove(b2) assert 3 == len(canvas.solver.constraints) assert 1 == len(list(canvas.get_connections(item=line))) cinfo = canvas.get_connection(line.handles()[0]) assert b1 == cinfo.connected cinfo = canvas.get_connection(line.handles()[-1]) assert cinfo is None assert [] == list(canvas.solver.constraints_with_variable(line.handles()[-1].pos.x)) assert [] == list(canvas.solver.constraints_with_variable(line.handles()[-1].pos.y)) undo_fixture[0]() # Call undo assert 6 == len(canvas.solver.constraints) assert 2 == len(list(canvas.get_connections(item=line))) cinfo = canvas.get_connection(line.handles()[0]) assert b1 == cinfo.connected cinfo = canvas.get_connection(line.handles()[-1]) assert b2 == cinfo.connected
def connect(self, line, handle, item, port=None): """ Connect line's handle to an item. If port is not provided, then first port is used. """ canvas = line.canvas if port is None and len(item.ports()) > 0: port = item.ports()[0] sink = ConnectionSink(item, port) connector = Connector(line, handle) connector.connect(sink)
def connect(self, line, handle, item, port=None): """ Connect line's handle to an item. If port is not provided, then first port is used. """ canvas = line.canvas if port is None and len(item.ports()) > 0: port = item.ports()[0] sink = ConnectionSink(item, port) connector = Connector(line, handle) connector.connect(sink)
def connect(line, handle, item, port=None): """Connect line's handle to an item. If port is not provided, then first port is used. """ canvas = line.canvas assert canvas is item.canvas if port is None and len(item.ports()) > 0: port = item.ports()[0] sink = ConnectionSink(item, port) connector = Connector(line, handle) connector.connect(sink) cinfo = canvas.get_connection(handle) assert cinfo.connected is item assert cinfo.port is port
def connect(self, line, handle, item, port=None): """ Connect line's handle to an item. If port is not provided, then first port is used. """ canvas = line.canvas assert line.canvas is item.canvas if port is None and len(item.ports()) > 0: port = item.ports()[0] sink = ConnectionSink(item, port) connector = Connector(line, handle) connector.connect(sink) cinfo = canvas.get_connection(handle) self.assertSame(cinfo.connected, item) self.assertSame(cinfo.port, port)
def test_remove_connected_item(self): """Test adding canvas constraint""" canvas = Canvas() from gaphas.aspect import Connector, ConnectionSink l1 = Line() canvas.add(l1) b1 = Box() canvas.add(b1) number_cons1 = len(canvas.solver.constraints) b2 = Box() canvas.add(b2) number_cons2 = len(canvas.solver.constraints) conn = Connector(l1, l1.handles()[0]) sink = ConnectionSink(b1, b1.ports()[0]) conn.connect(sink) assert canvas.get_connection(l1.handles()[0]) conn = Connector(l1, l1.handles()[1]) sink = ConnectionSink(b2, b2.ports()[0]) conn.connect(sink) assert canvas.get_connection(l1.handles()[1]) self.assertEquals(number_cons2 + 2, len(canvas.solver.constraints)) canvas.remove(b1) # Expecting a class + line connected at one end only self.assertEquals(number_cons1 + 1, len(canvas.solver.constraints))
def test_remove_connected_item(): """Test adding canvas constraint. """ canvas = Canvas() from gaphas.aspect import Connector, ConnectionSink l1 = Line() canvas.add(l1) b1 = Box() canvas.add(b1) number_cons1 = len(canvas.solver.constraints) b2 = Box() canvas.add(b2) number_cons2 = len(canvas.solver.constraints) conn = Connector(l1, l1.handles()[0]) sink = ConnectionSink(b1, b1.ports()[0]) conn.connect(sink) assert canvas.get_connection(l1.handles()[0]) conn = Connector(l1, l1.handles()[1]) sink = ConnectionSink(b2, b2.ports()[0]) conn.connect(sink) assert canvas.get_connection(l1.handles()[1]) assert number_cons2 + 2 == len(canvas.solver.constraints) canvas.remove(b1) # Expecting a class + line connected at one end only assert number_cons1 + 1 == len(canvas.solver.constraints)