def test_grid_layout(self): """ Test the grid layout helper. """ from enable.layout.api import align, grid from enable.layout.layout_helpers import DefaultSpacing 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.assertTrue(self.c2.position == c2_pos)
def _get_layout(self, container): """ Returns a list of constraits that is meant to be passed to a ConstraintsContainer. """ constraints = [] # NOTE: inequality expressions seem a lil shaky in that it requires # some tweaking to finding a set of constraints that works well ! # But this is much better than manually tweaking padding etc. # Another option is to simply calculate the values of the height etc # and set it as simple inequalities (as opposed to using height as # another variable in the expressions) # FIXME: also, the layouts can prob. be specified as input similar to # the other plot properties. uv_comp = self.plot_contexts.get(LOG_FAMILY_UV) if uv_comp is not None: # uv_comp should exist in container.components assert(uv_comp in container.components) # NOTE: # 1. `>` doesent work but `>=` works ! # 2. adding both >= and <= breaks if `contents_height` is specified # but works if integer values are specified constraints.append( uv_comp.layout_height >= 0.4 * container.contents_height ) # split components into groups. For now, just UV and others other_components = [_comp for _comp in container.components if _comp is not uv_comp] for plot in other_components: # FIXME: just setting `<=` constraint results in plot not showing # up *if* its the only plot in the container # need to figure out the appropriate layout helper function here # constraints.append( # plot.layout_height <= 0.2 * container.contents_height # ) # constraints.append( # plot.layout_height >= 10 # ) pass # NOTE: looks like every comp in the container needs a constraint, # else they get messed up or ignored. # create an ordered (top to bottom) list of components sorted_components = [uv_comp] + other_components constraints.extend([ # all components are in a vertical box vbox(*sorted_components, spacing=50, margins=50), # align widths of all components to be the same align('layout_width', *sorted_components), # align heights of *other* components to be the same align('layout_height', *other_components), ]) return constraints
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_constraint_function(self): """ Test using a function to create constraints. """ from enable.layout.api import hbox, align 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.assertTrue(self.c1.bounds[0] == self.c2.bounds[0])
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_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_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_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_alignment_horizontal(self): """ Test alignment of components horizontally with constraints. """ from enable.layout.api import align 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.assertTrue(pos1[0] + bound1[0] / 2 == self.container.bounds[0] / 2) self.assertTrue(pos2[0] + bound2[0] / 2 == self.container.bounds[0] / 2)
def test_alignment_vertical(self): """ Test alignment of components vertically with constraints. """ from enable.layout.api import align 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.assertTrue( pos1[1] + bound1[1] / 2 == self.container.bounds[1] / 2 ) self.assertTrue( pos2[1] + bound2[1] / 2 == self.container.bounds[1] / 2 )
def test_share_layout(self): """ Test sharing layouts with a child container. """ from enable.api import ConstraintsContainer from enable.layout.api import hbox, align 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.assertTrue(self.c1.bounds[0] == self.c2.bounds[0] != c3.bounds[0]) self.child_container.share_layout = True self.container.relayout() self.assertTrue(self.c1.bounds[0] == self.c2.bounds[0] == c3.bounds[0])