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_item_removal(self): canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) # No gtk main loop, so updates occur instantly assert not canvas.require_update() # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration() assert len(canvas.get_all_items()) == len(view._qtree) view.focused_item = box canvas.remove(box) assert len(canvas.get_all_items()) == 0 assert len(view._qtree) == 0 window.destroy()
def test_get_item_at_point(self): """ Hover tool only reacts on motion-notify events """ canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) # No gtk main loop, so updates occur instantly assert not canvas.require_update() box.width = 50 box.height = 50 # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration() assert len(view._qtree._ids) == 1 assert not view._qtree._bucket.bounds == ( 0, 0, 0, 0), view._qtree._bucket.bounds assert view.get_item_at_point((10, 10)) is box assert view.get_item_at_point((60, 10)) is None window.destroy()
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_reparent(): c = Canvas() b1 = Box() b2 = Box() c.add(b1) c.add(b2, b1) c.reparent(b2, None)
def test_association_end_updates(self): """Test association end navigability connected to a class""" from gaphas.canvas import Canvas canvas = Canvas() c1 = self.create(ClassItem, UML.Class) c2 = self.create(ClassItem, UML.Class) a = self.create(AssociationItem) self.connect(a, a.head, c1) c = self.get_connected(a.head) self.assertTrue(c is c1) self.connect(a, a.tail, c2) c = self.get_connected(a.tail) self.assertTrue(c is c2) assert a.subject.memberEnd, a.subject.memberEnd assert a.subject.memberEnd[0] is a.head_end.subject assert a.subject.memberEnd[1] is a.tail_end.subject assert a.subject.memberEnd[0].name is None dispatcher = self.get_service("element_dispatcher") a.subject.memberEnd[0].name = "blah" self.diagram.canvas.update() assert a.head_end._name == "+ blah", a.head_end.get_name()
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 test_minimal_se(self): """ Test resizing of element by dragging it SE handle. """ canvas = Canvas() box = Box() handles = box.handles() canvas.add(box) h_nw, h_ne, h_se, h_sw = handles assert h_nw is handles[NW] assert h_ne is handles[NE] assert h_sw is handles[SW] assert h_se is handles[SE] h_se.pos.x -= 20 # h.se.{x,y} == -10 h_se.pos.y -= 20 assert h_se.pos.x == h_se.pos.y == -10 box.request_update() canvas.update() self.assertEqual(10, h_se.pos.x) # h_se changed above, should be 10 self.assertEqual(10, h_se.pos.y) self.assertEqual(10, h_ne.pos.x) self.assertEqual(10, h_sw.pos.y)
def test_association_end_updates(self): """Test association end navigability connected to a class""" from gaphas.canvas import Canvas canvas = Canvas() c1 = self.create(ClassItem, uml2.Class) c2 = self.create(ClassItem, uml2.Class) a = self.create(AssociationItem) self.connect(a, a.head, c1) c = self.get_connected(a.head) self.assertTrue(c is c1) self.connect(a, a.tail, c2) c = self.get_connected(a.tail) self.assertTrue(c is c2) assert a.subject.memberEnd, a.subject.memberEnd assert a.subject.memberEnd[0] is a.head_end.subject assert a.subject.memberEnd[1] is a.tail_end.subject assert a.subject.memberEnd[0].name is None dispatcher = self.get_service('element_dispatcher') print((a.subject.memberEnd[0], uml2.Property.name) in dispatcher._handlers) print('*' * 60) a.subject.memberEnd[0].name = 'blah' print('*' * 60) self.diagram.canvas.update() assert a.head_end._name == '+ blah', a.head_end.get_name()
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_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 test_view_registration_2(self): """ Test view registration and destroy when view is destroyed. """ canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() canvas.add(box) assert hasattr(box, '_matrix_i2v') assert hasattr(box, '_matrix_v2i') assert box._matrix_i2v[view] assert box._matrix_v2i[view] assert len(canvas._registered_views) == 1 assert view in canvas._registered_views window.destroy() assert len(canvas._registered_views) == 0 assert view not in box._matrix_i2v assert view not in box._matrix_v2i
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_scroll_adjustments(self): sc = gtk.ScrolledWindow() view = GtkView(Canvas()) sc.add(view) print sc.get_hadjustment(), view.hadjustment assert sc.get_hadjustment() is view.hadjustment assert sc.get_vadjustment() is view.vadjustment
def test_scroll_adjustments_signal(self): def handler(self, hadj, vadj): self.handled = True sc = gtk.ScrolledWindow() view = GtkView(Canvas()) view.connect('set-scroll-adjustments', handler) sc.add(view) assert view.handled
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_update_matrices(): """Test updating of matrices""" c = Canvas() i = Box() ii = Box() c.add(i) c.add(ii, i) i.matrix = (1.0, 0.0, 0.0, 1.0, 5.0, 0.0) ii.matrix = (1.0, 0.0, 0.0, 1.0, 0.0, 8.0) updated = c.update_matrices([i]) assert i._matrix_i2c == cairo.Matrix(1, 0, 0, 1, 5, 0) assert ii._matrix_i2c == cairo.Matrix(1, 0, 0, 1, 5, 8)
def test_update_matrices(self): """Test updating of matrices""" c = Canvas() i = Box() ii = Box() c.add(i) c.add(ii, i) i.matrix = (1.0, 0.0, 0.0, 1.0, 5.0, 0.0) ii.matrix = (1.0, 0.0, 0.0, 1.0, 0.0, 8.0) updated = c.update_matrices([i]) self.assertEquals(i._matrix_i2c, cairo.Matrix(1, 0, 0, 1, 5, 0)) self.assertEquals(ii._matrix_i2c, cairo.Matrix(1, 0, 0, 1, 5, 8))
def __init__(self): self.canvas = Canvas() self.view = GtkView(self.canvas) self.window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) self.window.add(self.view) self.window.show_all() self.box = Box() self.canvas.add(self.box) # No gtk main loop, so updates occur instantly assert not self.canvas.require_update() # Process pending (expose) events, which cause the canvas to be drawn. while Gtk.events_pending(): Gtk.main_iteration()
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_get_handle_at_point(self): canvas = Canvas() view = GtkView(canvas) window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() box = Box() box.min_width = 20 box.min_height = 30 box.matrix.translate(20, 20) box.matrix.rotate(old_div(math.pi, 1.5)) canvas.add(box) i, h = view.get_handle_at_point((20, 20)) assert i is box assert h is box.handles()[0]
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_creation_with_size(self): """ Test if initial size holds when added to a canvas. """ canvas = Canvas() box = Box(150, 153) assert box.width == 150, box.width assert box.height == 153, box.height assert box.handles()[SE].pos.x == 150, box.handles()[SE].pos.x assert box.handles()[SE].pos.y == 153, box.handles()[SE].pos.y canvas.add(box) assert box.width == 150, box.width assert box.height == 153, box.height assert box.handles()[SE].pos.x == 150, box.handles()[SE].pos.x assert box.handles()[SE].pos.y == 153, box.handles()[SE].pos.y
def test_line_guide(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.orthogonal = True c.update_now() guides = list(Guide(l).horizontal()) self.assertEquals(2, len(guides)) self.assertEquals(10.0, guides[0]) self.assertEquals(40.0, guides[1]) guides = list(Guide(l).vertical()) self.assertEquals(2, len(guides)) self.assertEquals(00.0, guides[0]) self.assertEquals(20.0, guides[1])
def test_get_handle_at_point_at_pi_div_2(self): canvas = Canvas() view = GtkView(canvas) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() box = Box() box.min_width = 20 box.min_height = 30 box.matrix.translate(20, 20) box.matrix.rotate(math.pi / 2) canvas.add(box) p = canvas.get_matrix_i2c(box).transform_point(0, 20) p = canvas.get_matrix_c2i(box).transform_point(20, 20) i, h = view.get_handle_at_point((20, 20)) assert i is box assert h is box.handles()[0]
def test_view_registration(self): canvas = Canvas() # Simple views do not register on the canvas view = View(canvas) assert len(canvas._registered_views) == 0 box = Box() canvas.add(box) # By default no complex updating/calculations are done: assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) # GTK view does register for updates though view = GtkView(canvas) assert len(canvas._registered_views) == 1 # No entry, since GtkView is not realized and has no window assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.add(view) window.show_all() # Now everything is realized and updated assert box._matrix_i2v.has_key(view) assert box._matrix_v2i.has_key(view) view.canvas = None assert len(canvas._registered_views) == 0 assert not box._matrix_i2v.has_key(view) assert not box._matrix_v2i.has_key(view) view.canvas = canvas assert len(canvas._registered_views) == 1 assert box._matrix_i2v.has_key(view) assert box._matrix_v2i.has_key(view)
def test_view_registration(view_fixture): canvas = Canvas() # Simple views do not register on the canvas view = View(canvas) assert len(canvas._registered_views) == 0 box = Box() canvas.add(box) # By default no complex updating/calculations are done: assert view not in box._matrix_i2v assert view not in box._matrix_v2i # GTK view does register for updates though view = GtkView(canvas) assert len(canvas._registered_views) == 1 # No entry, since GtkView is not realized and has no window assert view not in box._matrix_i2v assert view not in box._matrix_v2i window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) window.add(view) window.show_all() # Now everything is realized and updated assert view in box._matrix_i2v assert view in box._matrix_v2i view.canvas = None assert len(canvas._registered_views) == 0 assert view not in box._matrix_i2v assert view not in box._matrix_v2i view.canvas = canvas assert len(canvas._registered_views) == 1 assert view in box._matrix_i2v assert view in box._matrix_v2i
def test_scroll_adjustments_signal(self): sc = Gtk.ScrolledWindow() view = GtkView(Canvas()) sc.add(view) assert view.hadjustment assert view.vadjustment assert view.hadjustment.get_value() == 0.0 assert view.hadjustment.get_lower() == 0.0 assert view.hadjustment.get_upper() == 1.0 assert view.hadjustment.get_step_increment() == 0.0 assert view.hadjustment.get_page_increment() == 1.0 assert view.hadjustment.get_page_size() == 1.0 assert view.vadjustment.get_value() == 0.0 assert view.vadjustment.get_lower() == 0.0 assert view.vadjustment.get_upper() == 1.0 assert view.vadjustment.get_step_increment() == 0.0 assert view.vadjustment.get_page_increment() == 1.0 assert view.vadjustment.get_page_size() == 1.0