def test_orthogonal_horizontal_undo(self): """Test orthogonal line constraints bug (#107) """ canvas = Canvas() line = Line() canvas.add(line) assert not line.horizontal assert len(canvas.solver._constraints) == 0 segment = Segment(line, None) segment.split_segment(0) line.orthogonal = True self.assertEqual(2, len(canvas.solver._constraints)) after_ortho = set(canvas.solver._constraints) del undo_list[:] line.horizontal = True self.assertEqual(2, len(canvas.solver._constraints)) undo() self.assertFalse(line.horizontal) self.assertEqual(2, len(canvas.solver._constraints)) line.horizontal = True self.assertTrue(line.horizontal) self.assertEqual(2, len(canvas.solver._constraints))
def test_orthogonal_horizontal_undo(revert_undo, undo_fixture): """Test orthogonal line constraints bug (#107). """ canvas = Canvas() line = Line() canvas.add(line) assert not line.horizontal assert len(canvas.solver._constraints) == 0 segment = Segment(line, None) segment.split_segment(0) line.orthogonal = True assert 2 == len(canvas.solver._constraints) del undo_fixture[2][:] # Clear undo_list line.horizontal = True assert 2 == len(canvas.solver._constraints) undo_fixture[0]() # Call undo assert not line.horizontal assert 2 == len(canvas.solver._constraints) line.horizontal = True assert line.horizontal assert 2 == len(canvas.solver._constraints)
def __init__(self, canvas): Gtk.Window.__init__(self, title="Hello World") #connect the button widget #connect to its clicked signal #add is as child to the top-level window #self.button = Gtk.Button(label="Click Here") #self.button.connect("clicked", self.on_button_clicked) self.label = Gtk.Label() self.view = GtkView() #Gtk widget self.view.painter = DefaultPainter() self.view.canvas = canvas self.win_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) self.add(self.win_box) self.win_box.pack_start(self.label, True, True, 0) self.win_box.pack_start(self.view, True, True, 0) self.b2 = Box(60, 60) self.b2.min_width = 40 self.b2.min_height = 50 self.b2.matrix.translate(170, 170) canvas.add(self.b2) # Draw gaphas line self.line = Line() self.line.matrix.translate(100, 60) canvas.add(self.line) self.line.handles()[1].pos = (30, 30) self.segment = Segment(self.line, canvas) self.segment.split_segment(0)
def test_disconnect_item_by_deleting_element(): b1 = Box() b2 = Box() line = Line() c = Canvas() c.add(b1) c.add(b2) c.add(line) events = [] def callback(): events.append("called") c.connect_item(line, line.handles()[0], b1, b1.ports()[0], callback=callback) assert count(c.get_connections(handle=line.handles()[0])) == 1 c.remove(b1) assert count(c.get_connections(handle=line.handles()[0])) == 0 assert events == ["called"]
def test_orthogonal_horizontal_undo(self): """Test orthogonal line constraints bug (#107) """ canvas = Canvas() line = Line() canvas.add(line) assert not line.horizontal assert len(canvas.solver._constraints) == 0 segment = Segment(line, None) segment.split_segment(0) line.orthogonal = True self.assertEquals(2, len(canvas.solver._constraints)) after_ortho = set(canvas.solver._constraints) del undo_list[:] line.horizontal = True self.assertEquals(2, len(canvas.solver._constraints)) undo() self.assertFalse(line.horizontal) self.assertEquals(2, len(canvas.solver._constraints)) line.horizontal = True self.assertTrue(line.horizontal) self.assertEquals(2, len(canvas.solver._constraints))
def create_canvas(canvas, title): # Setup drawing window window = Gtk.Window() window.set_title(title) window.set_default_size(400, 400) view = GtkView() #Gtk widget view.painter = DefaultPainter() view.canvas = canvas win_box = Gtk.Box( orientation=Gtk.Orientation.HORIZONTAL) #added Gtk box to Gtk window window.add(win_box) win_box.pack_start(view, True, True, 0) #added "view" widget to the box # Draw gaphas box b2 = Box(60, 60) b2.min_width = 40 b2.min_height = 50 b2.matrix.translate(170, 170) canvas.add(b2) # Draw gaphas line line = Line() line.matrix.translate(100, 60) canvas.add(line) line.handles()[1].pos = (30, 30) segment = Segment(line, canvas) segment.split_segment(0) window.show_all() window.connect("destroy", Gtk.main_quit)
def test_constraints_after_merge(self): """Test if constraints are recreated after line merge """ # connect line2 to self.line line2 = Line() self.canvas.add(line2) head = line2.handles()[0] #conn = Connector(line2, head) #sink = conn.glue((25, 25)) #assert sink is not None #conn.connect(sink) self.tool.connect(line2, head, (25, 25)) cinfo = self.canvas.get_connection(head) self.assertEquals(self.line, cinfo.connected) segment = Segment(self.line, self.view) segment.split_segment(0) assert len(self.line.handles()) == 3 c1 = cinfo.constraint segment.merge_segment(0) assert len(self.line.handles()) == 2 h1, h2 = self.line.handles() # connection shall be reconstrained between 1st and 2nd handle cinfo = self.canvas.get_connection(head) self.assertEquals(cinfo.constraint._line[0]._point, h1.pos) self.assertEquals(cinfo.constraint._line[1]._point, h2.pos) self.assertFalse(c1 == cinfo.constraint)
def create_canvas(canvas, title): # Setup drawing window view = GtkView() view.painter = DefaultPainter() view.canvas = canvas window = Gtk.Window() window.set_title(title) window.set_default_size(400, 400) win_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) window.add(win_box) win_box.pack_start(view, True, True, 0) # Draw first gaphas box b1 = Box(60, 60) b1.matrix = (1.0, 0.0, 0.0, 1, 10, 10) canvas.add(b1) # Draw second gaphas box b2 = Box(60, 60) b2.min_width = 40 b2.min_height = 50 b2.matrix.translate(170, 170) canvas.add(b2) # Draw gaphas line line = Line() line.matrix.translate(100, 60) canvas.add(line) line.handles()[1].pos = (30, 30) window.show_all() window.connect("destroy", Gtk.main_quit)
def test_orthogonal_horizontal_undo(self): canvas = Canvas() line = Line() canvas.add(line) assert len(canvas.solver._constraints) == 0 line.orthogonal = True assert len(canvas.solver._constraints) == 2 after_ortho = set(canvas.solver._constraints) del undo_list[:] line.horizontal = True assert len(canvas.solver._constraints) == 2 undo() assert not line.horizontal assert len(canvas.solver._constraints) == 2, canvas.solver._constraints line.horizontal = True assert line.horizontal assert len(canvas.solver._constraints) == 2, canvas.solver._constraints
def test_orthogonal_line_undo(revert_undo, undo_fixture): """Test orthogonal line undo. """ canvas = Canvas() line = Line() canvas.add(line) segment = Segment(line, None) segment.split_segment(0) # Start with no orthogonal constraints assert len(canvas.solver._constraints) == 0 line.orthogonal = True # Check orthogonal constraints assert 2 == len(canvas.solver._constraints) assert 3 == len(line.handles()) undo_fixture[0]() # Call undo assert not line.orthogonal assert 0 == len(canvas.solver._constraints) assert 2 == len(line.handles())
def __init__(self): self.canvas = Canvas() self.box1 = Box() self.canvas.add(self.box1) self.box1.matrix.translate(100, 50) self.box1.width = 40 self.box1.height = 40 self.box1.request_update() self.box2 = Box() self.canvas.add(self.box2) self.box2.matrix.translate(100, 150) self.box2.width = 50 self.box2.height = 50 self.box2.request_update() self.line = Line() self.head = self.line.handles()[0] self.tail = self.line.handles()[-1] self.tail.pos = 100, 100 self.canvas.add(self.line) self.canvas.update_now() self.view = GtkView() self.view.canvas = self.canvas self.win = Gtk.Window() self.win.add(self.view) self.view.show() self.view.update() self.win.show() self.tool = ConnectHandleTool(self.view)
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 test_segment(self): """ """ view = self.view line = Line() self.canvas.add(line) segment = Segment(line, self.view) self.assertEquals(2, len(line.handles())) segment.split((5, 5)) self.assertEquals(3, len(line.handles()))
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_connect_item(self): b1 = Box() b2 = Box() l = Line() c = Canvas() c.add(b1) c.add(b2) c.add(l) c.connect_item(l, l.handles()[0], b1, b1.ports()[0]) assert count(c.get_connections(handle=l.handles()[0])) == 1 # Add the same self.assertRaises(ConnectionError, c.connect_item, l, l.handles()[0], b1, b1.ports()[0]) assert count(c.get_connections(handle=l.handles()[0])) == 1
def test_connect_item(): b1 = Box() b2 = Box() line = Line() c = Canvas() c.add(b1) c.add(b2) c.add(line) c.connect_item(line, line.handles()[0], b1, b1.ports()[0]) assert count(c.get_connections(handle=line.handles()[0])) == 1 # Add the same with pytest.raises(ConnectionError): c.connect_item(line, line.handles()[0], b1, b1.ports()[0]) assert count(c.get_connections(handle=line.handles()[0])) == 1
def test_bounding_box_calculations(self): """ A view created before and after the canvas is populated should contain the same data. """ canvas = Canvas() window1 = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) view1 = GtkView(canvas=canvas) window1.add(view1) view1.realize() window1.show_all() box = Box() box.matrix = (1.0, 0.0, 0.0, 1, 10, 10) canvas.add(box) line = Line() line.fyzzyness = 1 line.handles()[1].pos = (30, 30) #line.split_segment(0, 3) line.matrix.translate(30, 60) canvas.add(line) window2 = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) view2 = GtkView(canvas=canvas) window2.add(view2) window2.show_all() # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration() try: assert view2.get_item_bounding_box(box) assert view1.get_item_bounding_box(box) assert view1.get_item_bounding_box( box) == view2.get_item_bounding_box( box), '%s != %s' % (view1.get_item_bounding_box(box), view2.get_item_bounding_box(box)) assert view1.get_item_bounding_box( line) == view2.get_item_bounding_box( line), '%s != %s' % (view1.get_item_bounding_box(line), view2.get_item_bounding_box(line)) finally: window1.destroy() window2.destroy()
def test_params_errors(self): """Test parameter error exceptions """ line = Line() segment = Segment(line, self.view) # there is only 1 segment self.assertRaises(ValueError, segment.split_segment, -1) line = Line() segment = Segment(line, self.view) self.assertRaises(ValueError, segment.split_segment, 1) line = Line() # can't split into one or less segment :) segment = Segment(line, self.view) self.assertRaises(ValueError, segment.split_segment, 0, 1)
def test_pickle_line(self): item = Line() pickled = pickle.dumps(item) i2 = pickle.loads(pickled) assert i2 assert len(i2.handles()) == 2
def test_disconnect_item_with_callback(self): b1 = Box() b2 = Box() l = Line() c = Canvas() c.add(b1) c.add(b2) c.add(l) events = [] def callback(): events.append('called') c.connect_item(l, l.handles()[0], b1, b1.ports()[0], callback=callback) assert count(c.get_connections(handle=l.handles()[0])) == 1 c.disconnect_item(l, l.handles()[0]) assert count(c.get_connections(handle=l.handles()[0])) == 0 assert events == ['called']
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 __init__(self): self.canvas = Canvas() self.view = GtkView(self.canvas) self.window = Gtk.Window() self.window.add(self.view) self.window.show_all() self.line = Line() self.canvas.add(self.line) self.e1 = Element() self.e2 = Element() self.e3 = Element()
def test_constraints_after_split(self): """Test if constraints are recreated after line split """ # connect line2 to self.line line2 = Line() self.canvas.add(line2) head = line2.handles()[0] self.tool.connect(line2, head, (25, 25)) cinfo = self.canvas.get_connection(head) self.assertEquals(self.line, cinfo.connected) Segment(self.line, self.view).split_segment(0) assert len(self.line.handles()) == 3 h1, h2, h3 = self.line.handles() cinfo = self.canvas.get_connection(head) # connection shall be reconstrained between 1st and 2nd handle self.assertEquals(h1.pos, cinfo.constraint._line[0]._point) self.assertEquals(h2.pos, cinfo.constraint._line[1]._point)
def test_line_projection(self): """Test projection with line's handle on element's side""" line = Line() line.matrix.translate(15, 50) h1, h2 = line.handles() h1.x, h1.y = 0, 0 h2.x, h2.y = 20, 20 box = Box() box.matrix.translate(10, 10) box.width = 40 box.height = 20 h_nw, h_ne, h_se, h_sw = box.handles() canvas = Canvas() canvas.add(line) canvas.add(box) # move line's second handle on box side h2.x, h2.y = 5, -20
def test_disconnect_item_with_callback(): b1 = Box() b2 = Box() line = Line() c = Canvas() c.add(b1) c.add(b2) c.add(line) events = [] def callback(): events.append("called") c.connect_item(line, line.handles()[0], b1, b1.ports()[0], callback=callback) assert count(c.get_connections(handle=line.handles()[0])) == 1 c.disconnect_item(line, line.handles()[0]) assert count(c.get_connections(handle=line.handles()[0])) == 0 assert events == ["called"]
def test_line_guide_horizontal(self): c = Canvas() l = Line() c.add(l) l.handles().append(l._create_handle((20, 20))) l.handles().append(l._create_handle((30, 30))) l.handles().append(l._create_handle((40, 40))) l.horizontal = True l.orthogonal = True c.update_now() guides = list(Guide(l).horizontal()) self.assertEquals(2, len(guides)) self.assertEquals(0.0, guides[0]) self.assertEquals(20.0, guides[1]) guides = list(Guide(l).horizontal()) self.assertEquals(2, len(guides)) self.assertEquals(0.0, guides[0]) self.assertEquals(20.0, guides[1])
def create_canvas(): canvas = Canvas() box = Box() canvas.add(box) box.matrix.translate(100, 50) box.matrix.rotate(50) box2 = Box() canvas.add(box2, parent=box) line = Line() line.handles()[0].visible = False line.handles()[0].connected_to = box line.handles()[0].disconnect = my_disconnect() line.handles()[0].connection_data = 1 canvas.add(line) canvas.update() return canvas
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_bounding_box_calculations(self): """ A view created before and after the canvas is populated should contain the same data. """ canvas = Canvas() window1 = gtk.Window(gtk.WINDOW_TOPLEVEL) view1 = GtkView(canvas=canvas) window1.add(view1) view1.realize() window1.show_all() box = Box() box.matrix = (1.0, 0.0, 0.0, 1, 10,10) canvas.add(box) line = Line() line.fyzzyness = 1 line.handles()[1].pos = (30, 30) #line.split_segment(0, 3) line.matrix.translate(30, 60) canvas.add(line) window2 = gtk.Window(gtk.WINDOW_TOPLEVEL) view2 = GtkView(canvas=canvas) window2.add(view2) window2.show_all() # Process pending (expose) events, which cause the canvas to be drawn. while gtk.events_pending(): gtk.main_iteration() try: assert view2.get_item_bounding_box(box) assert view1.get_item_bounding_box(box) assert view1.get_item_bounding_box(box) == view2.get_item_bounding_box(box), '%s != %s' % (view1.get_item_bounding_box(box), view2.get_item_bounding_box(box)) assert view1.get_item_bounding_box(line) == view2.get_item_bounding_box(line), '%s != %s' % (view1.get_item_bounding_box(line), view2.get_item_bounding_box(line)) finally: window1.destroy() window2.destroy()
class SimpleCanvas(object): """Creates a test canvas object. Adds a view, canvas, and handle connection tool to a test case. Two boxes and a line are added to the canvas as well. """ def __init__(self): self.canvas = Canvas() self.box1 = Box() self.canvas.add(self.box1) self.box1.matrix.translate(100, 50) self.box1.width = 40 self.box1.height = 40 self.box1.request_update() self.box2 = Box() self.canvas.add(self.box2) self.box2.matrix.translate(100, 150) self.box2.width = 50 self.box2.height = 50 self.box2.request_update() self.line = Line() self.head = self.line.handles()[0] self.tail = self.line.handles()[-1] self.tail.pos = 100, 100 self.canvas.add(self.line) self.canvas.update_now() self.view = GtkView() self.view.canvas = self.canvas self.win = Gtk.Window() self.win.add(self.view) self.view.show() self.view.update() self.win.show() self.tool = ConnectHandleTool(self.view)
def test_line_projection(): """Test projection with line's handle on element's side. """ line = Line() line.matrix.translate(15, 50) h1, h2 = line.handles() h1.pos = (0, 0) h2.pos = (20, 20) box = Box() box.matrix.translate(10, 10) box.width = 40 box.height = 20 canvas = Canvas() canvas.add(line) canvas.add(box) # move line's second handle on box side h2.pos = (5, -20)
class SimpleCanvas: """Creates a test canvas object. Adds a view, canvas, and handle connection tool to a test case. Two boxes and a line are added to the canvas as well. """ def __init__(self): self.canvas = Canvas() self.box1 = Box() self.canvas.add(self.box1) self.box1.matrix.translate(100, 50) self.box1.width = 40 self.box1.height = 40 self.box1.request_update() self.box2 = Box() self.canvas.add(self.box2) self.box2.matrix.translate(100, 150) self.box2.width = 50 self.box2.height = 50 self.box2.request_update() self.line = Line() self.head = self.line.handles()[0] self.tail = self.line.handles()[-1] self.tail.pos = 100, 100 self.canvas.add(self.line) self.canvas.update_now() self.view = GtkView() self.view.canvas = self.canvas self.win = Gtk.Window() self.win.add(self.view) self.view.show() self.view.update() self.win.show() self.tool = ConnectHandleTool(self.view)
def test_bounding_box_calculations(view_fixture): """A view created before and after the canvas is populated should contain the same data. """ view_fixture.view.realize() view_fixture.box.matrix = (1.0, 0.0, 0.0, 1, 10, 10) line = Line() line.fuzziness = 1 line.handles()[1].pos = (30, 30) line.matrix.translate(30, 60) view_fixture.canvas.add(line) window2 = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) view2 = GtkView(canvas=view_fixture.canvas) window2.add(view2) window2.show_all() # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration() try: assert view2.get_item_bounding_box(view_fixture.box) assert view_fixture.view.get_item_bounding_box(view_fixture.box) assert view_fixture.view.get_item_bounding_box( view_fixture.box) == view2.get_item_bounding_box( view_fixture.box), ("%s != %s" % ( view_fixture.view.get_item_bounding_box(view_fixture.box), view2.get_item_bounding_box(view_fixture.box), )) assert view_fixture.view.get_item_bounding_box( line) == view2.get_item_bounding_box(line), ("%s != %s" % ( view_fixture.view.get_item_bounding_box(line), view2.get_item_bounding_box(line), )) finally: view_fixture.window.destroy() window2.destroy()
def test_disconnect_item_with_constraint_by_deleting_element(self): b1 = Box() b2 = Box() l = Line() c = Canvas() c.add(b1) c.add(b2) c.add(l) cons = b1.ports()[0].constraint(c, l, l.handles()[0], b1) c.connect_item(l, l.handles()[0], b1, b1.ports()[0], constraint=cons) assert count(c.get_connections(handle=l.handles()[0])) == 1 ncons = len(c.solver.constraints) assert ncons == 5 c.remove(b1) assert count(c.get_connections(handle=l.handles()[0])) == 0 self.assertEquals(2, len(c.solver.constraints))
def test_disconnect_item_with_constraint_by_deleting_element(): b1 = Box() b2 = Box() line = Line() c = Canvas() c.add(b1) c.add(b2) c.add(line) cons = b1.ports()[0].constraint(c, line, line.handles()[0], b1) c.connect_item(line, line.handles()[0], b1, b1.ports()[0], constraint=cons) assert count(c.get_connections(handle=line.handles()[0])) == 1 ncons = len(c.solver.constraints) assert ncons == 5 c.remove(b1) assert count(c.get_connections(handle=line.handles()[0])) == 0 assert 2 == len(c.solver.constraints)
def test_pickle_connect(self): """ Persist a connection. """ canvas = Canvas() box = Box() canvas.add(box) box2 = Box() canvas.add(box2, parent=box) line = Line() line.handles()[0].visible = False line.handles()[0].connected_to = box line.handles()[0].disconnect = my_disconnect() line.handles()[0].connection_data = 1 canvas.add(line) pickled = pickle.dumps(canvas) c2 = pickle.loads(pickled) assert type(canvas._tree.nodes[0]) is Box assert type(canvas._tree.nodes[1]) is Box assert type(canvas._tree.nodes[2]) is Line assert c2.solver line2 = c2._tree.nodes[2] h = line2.handles()[0] assert h.visible == False assert h.connected_to is c2._tree.nodes[0] # connection_data and disconnect have not been persisted assert h.connection_data == 1, h.connection_data assert h.disconnect, h.disconnect assert callable(h.disconnect) assert h.disconnect() is None, h.disconnect()
def test_orthogonal_line_undo(self): """Test orthogonal line undo """ canvas = Canvas() line = Line() canvas.add(line) segment = Segment(line, None) segment.split_segment(0) # start with no orthogonal constraints assert len(canvas.solver._constraints) == 0 line.orthogonal = True # check orthogonal constraints self.assertEqual(2, len(canvas.solver._constraints)) self.assertEqual(3, len(line.handles())) undo() self.assertFalse(line.orthogonal) self.assertEqual(0, len(canvas.solver._constraints)) self.assertEqual(2, len(line.handles()))
def test_orthogonal_line_undo(self): """Test orthogonal line undo """ canvas = Canvas() line = Line() canvas.add(line) segment = Segment(line, None) segment.split_segment(0) # start with no orthogonal constraints assert len(canvas.solver._constraints) == 0 line.orthogonal = True # check orthogonal constraints self.assertEquals(2, len(canvas.solver._constraints)) self.assertEquals(3, len(line.handles())) undo() self.assertFalse(line.orthogonal) self.assertEquals(0, len(canvas.solver._constraints)) self.assertEquals(2, len(line.handles()))
def test_params_errors(self): """Test parameter error exceptions """ line = Line() self.canvas.add(line) segment = Segment(line, self.view) segment.split_segment(0) # no segment -1 self.assertRaises(ValueError, segment.merge_segment, -1) line = Line() self.canvas.add(line) segment = Segment(line, self.view) segment.split_segment(0) # no segment no 2 self.assertRaises(ValueError, segment.merge_segment, 2) line = Line() self.canvas.add(line) segment = Segment(line, self.view) segment.split_segment(0) # can't merge one or less segments :) self.assertRaises(ValueError, segment.merge_segment, 0, 1) line = Line() self.canvas.add(line) self.assertEquals(2, len(line.handles())) segment = Segment(line, self.view) # can't merge line with one segment self.assertRaises(ValueError, segment.merge_segment, 0) line = Line() self.canvas.add(line) segment = Segment(line, self.view) segment.split_segment(0) # 2 segments: no 0 and 1. cannot merge as there are no segments # after segment no 1 self.assertRaises(ValueError, segment.merge_segment, 1) line = Line() self.canvas.add(line) segment = Segment(line, self.view) segment.split_segment(0) # 2 segments: no 0 and 1. cannot merge 3 segments as there are no 3 # segments self.assertRaises(ValueError, segment.merge_segment, 0, 3)
def test_disconnect_item_with_constraint(): b1 = Box() b2 = Box() line = Line() c = Canvas() c.add(b1) c.add(b2) c.add(line) cons = b1.ports()[0].constraint(c, line, line.handles()[0], b1) c.connect_item(line, line.handles()[0], b1, b1.ports()[0], constraint=cons) assert count(c.get_connections(handle=line.handles()[0])) == 1 assert len(c.solver.constraints) == 13 c.disconnect_item(line, line.handles()[0]) assert count(c.get_connections(handle=line.handles()[0])) == 0 assert len(c.solver.constraints) == 12
def test_disconnect_item_with_constraint(self): b1 = Box() b2 = Box() l = Line() c = Canvas() c.add(b1) c.add(b2) c.add(l) cons = b1.ports()[0].constraint(c, l, l.handles()[0], b1) c.connect_item(l, l.handles()[0], b1, b1.ports()[0], constraint=cons) assert count(c.get_connections(handle=l.handles()[0])) == 1 ncons = len(c.solver.constraints) assert ncons == 5 c.disconnect_item(l, l.handles()[0]) assert count(c.get_connections(handle=l.handles()[0])) == 0 assert len(c.solver.constraints) == 4
def test_orthogonal_line_split_segment(self): canvas = Canvas() line = Line() canvas.add(line) assert len(canvas.solver._constraints) == 0 line.orthogonal = True assert len(canvas.solver._constraints) == 2 after_ortho = set(canvas.solver._constraints) assert len(line.handles()) == 3 del undo_list[:] line.split_segment(0) assert len(canvas.solver._constraints) == 3 assert len(line.handles()) == 4 undo() assert len(canvas.solver._constraints) == 2 assert len(line.handles()) == 3 assert canvas.solver._constraints == after_ortho line.split_segment(0) assert len(canvas.solver._constraints) == 3 assert len(line.handles()) == 4 after_split = set(canvas.solver._constraints) del undo_list[:] line.merge_segment(0) assert len(canvas.solver._constraints) == 2 assert len(line.handles()) == 3 undo() assert len(canvas.solver._constraints) == 3 assert len(line.handles()) == 4 assert canvas.solver._constraints == after_split
def test_initial_ports(self): """Test initial ports amount """ line = Line() self.assertEquals(1, len(line.ports()))
def test_initial_ports(revert_undo): """Test initial ports amount. """ line = Line() assert 1 == len(line.ports())