Esempio n. 1
0
    def _canvas_default(self):
        parent = ConstraintsContainer(bounds=(500,500), padding=20)

        one = Component(id="r", bgcolor=0xFF0000)
        two = Component(id="g", bgcolor=0x00FF00)
        three = Component(id="b", bgcolor=0x0000FF)

        parent.add(one, two, three, self.child_canvas)
        return parent
Esempio n. 2
0
    def _child_canvas_default(self):
        parent = ConstraintsContainer(id="child", share_layout=self.share_layout)

        one = Component(id="c", bgcolor=0x00FFFF)
        two = Component(id="m", bgcolor=0xFF00FF)
        three = Component(id="y", bgcolor=0xFFFF00)
        four = Component(id="k", bgcolor=0x000000)

        parent.add(one, two, three, four)
        return parent
Esempio n. 3
0
    def _create_window(self):
        path = os.path.join(THIS_DIR, 'deepfield.jpg')
        image = Image.from_file(path, resist_width='weak',
                                resist_height='weak')

        container = ConstraintsContainer(bounds=[500, 500])
        container.add(image)
        ratio = float(image.data.shape[1])/image.data.shape[0]
        container.layout_constraints = [
            image.left == container.contents_left,
            image.right == container.contents_right,
            image.top == container.contents_top,
            image.bottom == container.contents_bottom,
            image.layout_width == ratio*image.layout_height,
        ]
        return Window(self, -1, component=container)
    def setUp(self):
        from enable.api import ConstraintsContainer

        self.container = ConstraintsContainer(bounds=[100.0, 100.0])
        self.c1 = Component()
        self.c2 = Component()
        self.container.add(self.c1)
        self.container.add(self.c2)
    def test_share_layout(self):
        """ Test sharing layouts with a child container.

        """
        self.child_container = ConstraintsContainer(bounds=[50, 50])
        c3 = Component()
        self.child_container.add(c3)
        self.container.add(self.child_container)

        self.container.layout_constraints = [
            hbox(self.c1, self.c2, c3),
            align('layout_width', self.c1, self.c2, c3)
        ]

        self.assert_(self.c1.bounds[0] == self.c2.bounds[0] != c3.bounds[0])

        self.child_container.share_layout = True
        self.container.relayout()

        self.assert_(self.c1.bounds[0] == self.c2.bounds[0] == c3.bounds[0])
 def setUp(self):
     self.container = ConstraintsContainer(bounds=[100.0, 100.0])
     self.c1 = Component()
     self.c2 = Component()
     self.container.add(self.c1)
     self.container.add(self.c2)
class ConstraintsContainerTestCase(unittest.TestCase):

    def setUp(self):
        self.container = ConstraintsContainer(bounds=[100.0, 100.0])
        self.c1 = Component()
        self.c2 = Component()
        self.container.add(self.c1)
        self.container.add(self.c2)

    def test_equal_size(self):
        """ Test alignment of widths and heights.

        """
        self.container.layout_constraints = [
            self.c1.layout_width == 10,
            self.c2.layout_width == 10,
            self.c1.layout_height == 10,
            self.c2.layout_height == 10
        ]

        self.assert_(self.c1.bounds == self.c2.bounds)

    def test_hbox_order(self):
        """ Test the order of components in an hbox.

        """
        self.container.layout_constraints = [
            hbox(self.c1, self.c2)
        ]

        dx = self.c2.position[0] - self.c1.position[0]
        self.assert_(dx > 0)

    def test_vbox_order(self):
        """ Test the order of components in a vbox.

        """
        self.container.layout_constraints = [
            vbox(self.c1, self.c2)
        ]

        dy = self.c2.position[1] - self.c1.position[1]
        self.assert_(dy < 0)

    def test_alignment_vertical(self):
        """ Test alignment of components vertically with constraints.

        """
        self.container.layout_constraints = [
            self.c1.layout_height == 10,
            self.c2.layout_height == 10,
            align('v_center', self.container, self.c1, self.c2)
        ]

        pos1 = self.c1.position
        bound1 = self.c1.bounds
        pos2 = self.c2.position
        bound2 = self.c2.bounds

        self.assert_(pos1[1] + bound1[1] / 2 == self.container.bounds[1] / 2)
        self.assert_(pos2[1] + bound2[1] / 2 == self.container.bounds[1] / 2)

    def test_alignment_horizontal(self):
        """ Test alignment of components horizontally with constraints.

        """
        self.container.layout_constraints = [
            self.c1.layout_width == 10,
            self.c2.layout_width == 10,
            align('h_center', self.container, self.c1, self.c2)
        ]

        pos1 = self.c1.position
        bound1 = self.c1.bounds
        pos2 = self.c2.position
        bound2 = self.c2.bounds

        self.assert_(pos1[0] + bound1[0] / 2 == self.container.bounds[0] / 2)
        self.assert_(pos2[0] + bound2[0] / 2 == self.container.bounds[0] / 2)

    def test_constraint_function(self):
        """ Test using a function to create constraints.

        """
        cns = [
            hbox(self.c1, self.c2),
            align('layout_width', self.c1, self.c2)
        ]

        def get_constraints(container):
            return cns

        self.container.layout_constraints = get_constraints

        self.assert_(self.c1.bounds[0] == self.c2.bounds[0])

    def test_invalid_layout(self):
        """ Make sure proper exceptions are thrown with an invalid layout.

        """
        self.assertRaises(TypeError, setattr,
                          self.container.layout_constraints,
                          [hbox(self.c1, spacer, spacer)])

    def test_grid_layout(self):
        """ Test the grid layout helper.

        """
        c3 = Component()
        c4 = Component()

        self.container.add(c3)
        self.container.add(c4)

        self.container.layout_constraints = [
            grid([self.c1, self.c2], [c3, c4]),
            align('layout_width', self.c1, self.c2, c3, c4),
            align('layout_height', self.c1, self.c2, c3, c4)
        ]

        space = DefaultSpacing.ABUTMENT
        c2_pos = [self.c1.position[0] + self.c1.bounds[0] + space,
                  self.c1.position[1]]
        self.assert_(self.c2.position == c2_pos)

    def test_invalid_grid_layout(self):
        """ Test an invalid grid layout.

        """
        self.assertRaises(TypeError, setattr,
                          self.container.layout_constraints,
                          [grid([self.c1, spacer])])

    def test_constraint_strength(self):
        """ Test the strength of constraints.

        """
        self.container.layout_constraints = [
            (self.c1.layout_width == 10) | 'weak',
            (self.c1.layout_width == 20) | 'strong'
        ]

        self.assert_(self.c1.bounds[0] == 20)

    def test_share_layout(self):
        """ Test sharing layouts with a child container.

        """
        self.child_container = ConstraintsContainer(bounds=[50, 50])
        c3 = Component()
        self.child_container.add(c3)
        self.container.add(self.child_container)

        self.container.layout_constraints = [
            hbox(self.c1, self.c2, c3),
            align('layout_width', self.c1, self.c2, c3)
        ]

        self.assert_(self.c1.bounds[0] == self.c2.bounds[0] != c3.bounds[0])

        self.child_container.share_layout = True
        self.container.relayout()

        self.assert_(self.c1.bounds[0] == self.c2.bounds[0] == c3.bounds[0])

    def test_layout_manager_initialize(self):
        """ Ensure that a layout manager can only be initialized once.

        """
        manager = self.container._layout_manager
        self.assertRaises(RuntimeError, manager.initialize, [])

    def test_layout_manager_replace_constraints(self):
        """ Test replacing constraints in the layout manager.

        """
        manager = LayoutManager()
        cns = hbox(self.c1, self.c2).get_constraints(self.container)
        new_cns = vbox(self.c1, self.c2).get_constraints(self.container)

        self.assertRaises(RuntimeError, manager.replace_constraints, cns[0],
                          new_cns[0])

        manager.initialize(cns)
        manager.replace_constraints(cns, new_cns)

        self.assert_(not manager._solver.hasConstraint(cns[0]))
        self.assert_(manager._solver.hasConstraint(new_cns[0]))

    def test_layout_manager_max_size(self):
        """ Test the max_size method of the LayoutManager.

        """
        manager = self.container._layout_manager
        max_size = manager.get_max_size(self.container.layout_width,
                                        self.container.layout_height)
        self.assert_(max_size == (-1, -1))