예제 #1
0
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)
예제 #2
0
    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.assertEqual(2, len(c.solver.constraints))
예제 #3
0
def test_get_handle_at_point_at_pi_div_2(view_fixture):
    box = Box()
    box.min_width = 20
    box.min_height = 30
    box.matrix.translate(20, 20)
    box.matrix.rotate(math.pi / 2)
    view_fixture.canvas.add(box)
    box.matrix.translate(20, 20)
    box.matrix.rotate(math.pi / 2)

    i, h = view_fixture.view.get_handle_at_point((20, 20))
    assert i is box
    assert h is box.handles()[0]
예제 #4
0
 def test_placement(self):
     tab = self.tab
     diagram = self.diagram
     from gaphas import Element
     from gaphas.examples import Box
     box = Box()
     diagram.canvas.add(box)
     diagram.canvas.update_now()
     tab.view.request_update([box])
     
     from gaphor.diagram.comment import CommentItem
     comment = self.diagram.create(CommentItem, subject=self.element_factory.create(uml2.Comment))
     self.assertEquals(len(self.element_factory.lselect()), 2)
예제 #5
0
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"]
예제 #6
0
    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()
예제 #7
0
    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()
예제 #8
0
    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()
예제 #9
0
    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]
예제 #10
0
    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
예제 #11
0
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
예제 #12
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)
예제 #13
0
    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]
예제 #14
0
    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
예제 #15
0
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)
예제 #16
0
    def test_resize_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]

        # to see how many solver was called:
        # GAPHAS_TEST_COUNT=3 nosetests -s --with-prof --profile-restrict=gaphas gaphas/tests/test_element.py | grep -e '\<solve\>' -e dirty

        count = getenv('GAPHAS_TEST_COUNT')
        if count:
            count = int(count)
        else:
            count = 1

        for i in range(count):
            h_se.pos.x += 100  # h.se.{x,y} = 10, now
            h_se.pos.y += 100
            box.request_update()
            canvas.update()

        self.assertEqual(
            110 * count,
            h_se.pos.x)  # h_se changed above, should remain the same
        self.assertEqual(110 * count, float(h_se.pos.y))

        self.assertEqual(110 * count, float(h_ne.pos.x))
        self.assertEqual(110 * count, float(h_sw.pos.y))
예제 #17
0
def create_canvas(c=None):
    if not c:
        c = Canvas()
    b = MyBox()
    b.min_width = 20
    b.min_height = 30
    b.matrix = (1.0, 0.0, 0.0, 1, 20, 20)
    b.width = b.height = 40
    c.add(b)

    bb = Box()
    bb.matrix = (1.0, 0.0, 0.0, 1, 10, 10)
    c.add(bb, parent=b)

    fl = FatLine()
    fl.height = 50
    fl.matrix.translate(100, 100)
    c.add(fl)

    circle = Circle()
    h1, h2 = circle.handles()
    circle.radius = 20
    circle.matrix.translate(50, 100)
    c.add(circle)

    # AJM: extra boxes:
    bb = Box()
    bb.matrix.rotate(math.pi / 1.567)
    c.add(bb, parent=b)
    # for i in xrange(10):
    #     bb = Box()
    #     print('box', bb)
    #     bb.matrix.rotate(math.pi/4.0 * i / 10.0)
    #     c.add(bb, parent=b)

    b = PortoBox(60, 60)
    b.min_width = 40
    b.min_height = 50
    b.matrix.translate(55, 55)
    c.add(b)

    t = UnderlineText()
    t.matrix.translate(70, 30)
    c.add(t)

    t = MyText("Single line")
    t.matrix.translate(70, 70)
    c.add(t)

    l = MyLine()
    c.add(l)
    l.handles()[1].pos = (30, 30)
    segment = Segment(l, view=None)
    segment.split_segment(0, 3)
    l.matrix.translate(30, 60)
    l.orthogonal = True

    off_y = 0
    for align_x in (-1, 0, 1):
        for align_y in (-1, 0, 1):
            t = MyText(
                f"Aligned text {align_x:d}/{align_y:d}",
                align_x=align_x,
                align_y=align_y,
            )
            t.matrix.translate(120, 200 + off_y)
            off_y += 30
            c.add(t)

    t = MyText("Multiple\nlines", multiline=True)
    t.matrix.translate(70, 100)
    c.add(t)

    return c
예제 #18
0
 def __init__(self):
     self.canvas = Canvas()
     self.box = Box()
     self.handles = self.box.handles()
예제 #19
0
 def __init__(self):
     self.canvas = Canvas()
     self.box = Box()
     self.box2 = Box()
     self.line = Line()