Example #1
0
class SegmentFixture(object):
    def __init__(self):
        self.canvas = Canvas()
        self.line = Line()
        self.canvas.add(self.line)
        self.view = View(self.canvas)
        self.item = Item()
Example #2
0
class SegmentFixture:
    def __init__(self):
        self.canvas = Canvas()
        self.line = Line()
        self.canvas.add(self.line)
        self.view = View(self.canvas)
        self.item = Item()
Example #3
0
    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
Example #4
0
    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.assertEquals(10, h_se.pos.x) # h_se changed above, should be 10
        self.assertEquals(10, h_se.pos.y)

        self.assertEquals(10, h_ne.pos.x)
        self.assertEquals(10, h_sw.pos.y)
Example #5
0
    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()
Example #6
0
class SegmentTestCase(unittest.TestCase):
    """
    Test aspects for items.
    """
    def setUp(self):
        self.canvas = Canvas()
        self.view = View(self.canvas)

    def test_segment_fails_for_item(self):
        """
        Test if Segment aspect can be applied to Item
        """
        item = Item()
        try:
            s = Segment(item, self.view)
        except TypeError as e:
            pass
        else:
            assert False, 'Should not be reached'

    def test_segment(self):
        """
        """
        view = self.view
        line = Line()
        self.canvas.add(line)
        segment = Segment(line, self.view)
        self.assertEqual(2, len(line.handles()))
        segment.split((5, 5))
        self.assertEqual(3, len(line.handles()))
Example #7
0
    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
Example #8
0
    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))
Example #9
0
    def test_item_removal(self):
        canvas = Canvas()
        view = GtkView(canvas)
        window = gtk.Window(gtk.WINDOW_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()
Example #10
0
    def test_get_item_at_point(self):
        """
        Hover tool only reacts on motion-notify events
        """
        canvas = Canvas()
        view = GtkView(canvas)
        window = gtk.Window(gtk.WINDOW_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()
Example #11
0
    def test_view_registration_2(self):
        """
        Test view registration and destroy when view is destroyed.
        """
        canvas = Canvas()
        view = GtkView(canvas)
        window = gtk.Window(gtk.WINDOW_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 not box._matrix_i2v.has_key(view)
        assert not box._matrix_v2i.has_key(view)
Example #12
0
    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)
Example #13
0
 def test_reparent(self):
     c = Canvas()
     b1 = Box()
     b2 = Box()
     c.add(b1)
     c.add(b2, b1)
     c.reparent(b2, None)
Example #14
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"]
Example #15
0
    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))
Example #16
0
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)
Example #17
0
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())
Example #18
0
def test_reparent():
    c = Canvas()
    b1 = Box()
    b2 = Box()
    c.add(b1)
    c.add(b2, b1)
    c.reparent(b2, None)
Example #19
0
    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()
Example #20
0
class SegmentHandlesTest(unittest.TestCase):
    def setUp(self):
        self.canvas = Canvas()
        self.line = Line()
        self.canvas.add(self.line)
        self.view = View(self.canvas)

    def testHandleFinder(self):
        finder = HandleFinder(self.line, self.view)
        assert type(finder) is SegmentHandleFinder, type(finder)
Example #21
0
class SegmentHandlesTest(unittest.TestCase):

    def setUp(self):
        self.canvas = Canvas()
        self.line = Line()
        self.canvas.add(self.line)
        self.view = View(self.canvas)


    def testHandleFinder(self):
        finder = HandleFinder(self.line, self.view)
        assert type(finder) is SegmentHandleFinder, type(finder)
Example #22
0
class Window:
    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()
Example #23
0
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)
Example #24
0
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)
Example #25
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
Example #26
0
    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))
Example #27
0
    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))
Example #28
0
class ViewFixture(object):
    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()
Example #29
0
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
Example #30
0
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
Example #31
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()
Example #32
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
Example #33
0
    def test_get_handle_at_point(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/1.5)
        canvas.add(box)

        i, h = view.get_handle_at_point((20, 20))
        assert i is box
        assert h is box.handles()[0]
Example #34
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]
Example #35
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
Example #36
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
Example #37
0
    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
Example #38
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)
Example #39
0
    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])
Example #40
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
Example #41
0
    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']
Example #42
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)
Example #43
0
    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])
Example #44
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]
Example #45
0
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
Example #46
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
Example #47
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"]
Example #48
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
Example #49
0
    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']
Example #50
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(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()
Example #51
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)
Example #52
0
    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
Example #53
0
    def test_discard(self):
        """Test removal of an item from Sorted collection"""
        e1 = Element()
        e2 = Element()

        c = Canvas()
        c.add(e1)
        c.add(e2)

        items = Sorted(c)
        items.add(e1)
        items.add(e2)

        assert e1 in items and e2 in items

        items.discard(e1)
        items.discard(e1) # silent discard
        self.assertTrue(e1 not in items)

        items.discard(e2)
        self.assertTrue(e2 not in items)
Example #54
0
    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
Example #55
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)
Example #56
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.assertEquals(2, len(c.solver.constraints))
Example #57
0
class AspectTestCase(unittest.TestCase):
    """
    Test aspects for items.
    """

    def setUp(self):
        self.canvas = Canvas()
        self.view = View(self.canvas)


    def test_selection_select(self):
        """
        Test the Selection role methods
        """
        view = self.view
        item = Item()
        self.canvas.add(item)
        selection = Selection(item, view)
        assert item not in view.selected_items
        selection.select()
        assert item in view.selected_items
        assert item is view.focused_item
        selection.unselect()
        assert item not in view.selected_items
        assert None is view.focused_item


    def test_selection_move(self):
        """
        Test the Selection role methods
        """
        view = self.view
        item = Item()
        self.canvas.add(item)
        inmotion = InMotion(item, view)
        self.assertEquals((1, 0, 0, 1, 0, 0), tuple(item.matrix))
        inmotion.start_move((0, 0))
        inmotion.move((12, 26))
        self.assertEquals((1, 0, 0, 1, 12, 26), tuple(item.matrix))
Example #58
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.assertEquals(110 * count, h_se.pos.x) # h_se changed above, should remain the same
        self.assertEquals(110 * count, float(h_se.pos.y))

        self.assertEquals(110 * count, float(h_ne.pos.x))
        self.assertEquals(110 * count, float(h_sw.pos.y))