def on_button_release(self,event): """ This handles and 'drag' part of any connection, after the on_button_press of this tool returns a True (i.e by clicking on an Line Handle) the on_button_release is triggered on mouse button release. Note that this handles both the new connections and modified connections. Hint: ConnectorTool is a child of this class. Uncommenting all the print statements should give a fair idea how this tool works. """ line = self.grabbed_item handle = self.grabbed_handle glueitem,glueport,gluepos = self.view.get_port_at_point((event.x,event.y),distance = 10,exclude = [line]) line = self.grabbed_item handle = self.grabbed_handle try: if glueport and hasattr(glueport,"point"): #print 'READY_CONNECTION_CHECK_PASSED_1' conn = Connector(line,handle) sink = ConnectionSink(glueitem,glueport) conn.connect_port(sink) self.ungrab_handle() #print 'READY_CONNECTION_CHECK_PASSED_2' except Exception as e: print 'Connection Failed, Disconnect/Connect the last Connection again: /n',e finally: self.toggle_highlight_ports() return super(BlockConnectTool, self).on_button_release(event)
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 on_button_release(self, event): """ This handles and 'drag' part of any connection, after the on_button_press of this tool returns a True (i.e by clicking on an Line Handle) the on_button_release is triggered on mouse button release. Note that this handles both the new connections and modified connections. Hint: ConnectorTool is a child of this class. Uncommenting all the print statements should give a fair idea how this tool works. """ line = self.grabbed_item handle = self.grabbed_handle glueitem, glueport, gluepos = self.view.get_port_at_point( (event.x, event.y), distance=10, exclude=[line]) line = self.grabbed_item handle = self.grabbed_handle try: if glueport and hasattr(glueport, "point"): #print 'READY_CONNECTION_CHECK_PASSED_1' conn = Connector(line, handle) sink = ConnectionSink(glueitem, glueport) conn.connect_port(sink) self.ungrab_handle() #print 'READY_CONNECTION_CHECK_PASSED_2' except Exception as e: print 'Connection Failed, Disconnect/Connect the last Connection again: /n', e finally: self.toggle_highlight_ports() return super(BlockConnectTool, self).on_button_release(event)
def test_allow(self): aspect = Connector(self.commentline, self.commentline.handles()[0]) assert aspect.item is self.commentline assert aspect.handle is self.commentline.handles()[0] sink = ConnectionSink(self.comment, self.comment.ports()[0]) assert aspect.allow(sink)
def test_allow(commentline, comment): aspect = Connector(commentline, commentline.handles()[0]) assert aspect.item is commentline assert aspect.handle is commentline.handles()[0] sink = ConnectionSink(comment, comment.ports()[0]) assert aspect.allow(sink)
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 _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 _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.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 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 on_button_press(self,event): glueitem,glueport,gluepos = self.view.get_port_at_point((event.x,event.y),distance = 10,exclude = []) line,handle = self.view.get_handle_at_point((event.x,event.y)) try: if glueport and hasattr(glueport,"point"): self.toggle_highlight_ports(glueport.portinstance) self.line = self._create_line((event.x, event.y)) self._new_item = self.line h_glue = self.line.handles()[self._handle_index_glued] conn = Connector(self.line,h_glue) sink = ConnectionSink(glueitem,glueport) conn.connect_port(sink) h_drag = self.line.handles()[self._handle_index_dragged] self.grab_handle(self.line,h_drag) return True except Exception as e: print 'Connection Failed, Disconnect/Connect the last Connection again: /n',e
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 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 on_button_press(self, event): glueitem, glueport, gluepos = self.view.get_port_at_point( (event.x, event.y), distance=10, exclude=[]) line, handle = self.view.get_handle_at_point((event.x, event.y)) try: if glueport and hasattr(glueport, "point"): self.toggle_highlight_ports(glueport.portinstance) self.line = self._create_line((event.x, event.y)) self._new_item = self.line h_glue = self.line.handles()[self._handle_index_glued] conn = Connector(self.line, h_glue) sink = ConnectionSink(glueitem, glueport) conn.connect_port(sink) h_drag = self.line.handles()[self._handle_index_dragged] self.grab_handle(self.line, h_drag) return True except Exception as e: print 'Connection Failed, Disconnect/Connect the last Connection again: /n', e
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_iconnect(self): """ Test basic glue functionality using CommentItem and CommentLine items. """ element_factory = Application.get_service('element_factory') diagram = element_factory.create(UML.Diagram) comment = diagram.create(CommentItem, subject=element_factory.create(UML.Comment)) #assert comment.height == 50 #assert comment.width == 100 actor = diagram.create(ActorItem, subject=element_factory.create(UML.Actor)) actor.matrix.translate(200, 200) diagram.canvas.update_matrix(actor) line = diagram.create(CommentLineItem) view = self.get_diagram_view(diagram) assert view, 'View should be available here' tool = ConnectHandleTool(view) # select handle: handle = line.handles()[-1] tool._grabbed_item = line tool._grabbed_handle = handle # Should glue to (238, 248) handle.pos = 245, 248 item = tool.glue(line, handle, (245, 248)) self.assertTrue(item is not None) self.assertEquals((238, 248), view.canvas.get_matrix_i2c(line).transform_point( handle.x, handle.y)) handle.pos = 245, 248 tool.connect(line, handle, (245, 248)) cinfo = diagram.canvas.get_connection(handle) self.assertTrue(cinfo.constraint is not None) self.assertTrue(cinfo.connected is actor, cinfo.connected) self.assertEquals( (238, 248), view.get_matrix_i2v(line).transform_point(handle.x, handle.y)) Connector(line, handle).disconnect() #tool.disconnect(line, handle) cinfo = diagram.canvas.get_connection(handle) self.assertTrue(cinfo is None)
def glue(self, pos, distance=None): """ Glue to an item near a specific point. Returns a ConnectionSink or None. """ item = self.item handle = self.handle view = self.view if distance is None: distance = self.GLUE_DISTANCE if not handle.connectable: return None state_v, port, glue_pos = view.get_port_at_point( pos, distance=distance, exclude=(item, ), exclude_port_fun=self._exclude_port) # check if item and found item can be connected on closest port if port is not None: assert state_v is not None connector = Connector(self.item, self.handle) sink = ConnectionSink(state_v, port) if connector.allow(sink): # transform coordinates from view space to the item space and # update position of item's handle v2i = view.get_matrix_v2i(item).transform_point handle.pos = v2i(*glue_pos) return sink state_l = view.get_state_at_point(pos, distance=distance) state_v = state_l if state_v is not None and not state_v.model.state.is_root_state: return ConnectionSink(state_v, None) return None
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 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)
def test_iconnect(self): """ Test basic glue functionality using CommentItem and CommentLine items. """ element_factory = Application.get_service("element_factory") diagram = element_factory.create(UML.Diagram) self.event_manager.handle(DiagramShow(diagram)) comment = diagram.create(CommentItem, subject=element_factory.create(UML.Comment)) actor = diagram.create(ActorItem, subject=element_factory.create(UML.Actor)) actor.matrix.translate(200, 200) diagram.canvas.update_matrix(actor) line = diagram.create(CommentLineItem) view = self.get_diagram_view(diagram) assert view, "View should be available here" comment_bb = view.get_item_bounding_box(comment) # select handle: handle = line.handles()[-1] tool = ConnectHandleTool(view=view) tool.grab_handle(line, handle) handle.pos = (comment_bb.x, comment_bb.y) item = tool.glue(line, handle, handle.pos) assert item is not None tool.connect(line, handle, handle.pos) cinfo = diagram.canvas.get_connection(handle) assert cinfo.constraint is not None assert cinfo.connected is actor, cinfo.connected Connector(line, handle).disconnect() cinfo = diagram.canvas.get_connection(handle) assert cinfo is None
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_aspect_type(self): aspect = Connector(self.commentline, self.commentline.handles()[0]) assert type(aspect) is DiagramItemConnector
def test_aspect_type(commentline): aspect = Connector(commentline, commentline.handles()[0]) assert isinstance(aspect, DiagramItemConnector)