def align_left_or_top(self, rows, padding, column_or_row, aligned_axis, below_or_right_axis, width_or_height): """ Align columns or rows to the left or top edge """ constraints = [] for i in range(0, len(rows) - 1): row1 = rows[i] row2 = rows[i + 1] if len(row1) > 0 and len(row2) > 0: shape1 = row1[0] shape2 = row2[0] # Width or height of shape1 w_or_h = str(shape1.computed_width() ) if width_or_height == "width" else str( shape1.computed_height()) # Shape1 row is left or top aligned to shape2 row constraints.append( cb.eq(shape1.variables[aligned_axis].id, shape2.variables[aligned_axis].id)) # shape2 row is below or to the right of shape1 row constraints.append( cb.lte( cb.add(shape1.variables[below_or_right_axis].id, cb.add(w_or_h, padding)), shape2.variables[below_or_right_axis].id)) if len(constraints): return cb.and_expr(constraints) return True
def set_container_size_main_axis(self, container, padding, rows_or_columns, width_or_height): """ Constraint for the main axis size of the container """ size = "" num_rows_or_columns = len(rows_or_columns) outside_padding = container.variables.outside_padding.id if container.at_root else "0" for i in range(0, num_rows_or_columns): row_or_column = rows_or_columns[i] if len(row_or_column): spacing = padding if i < num_rows_or_columns - 1 else 0 m_height_or_width = None if width_or_height == "width": m_height_or_width = size_constraint_helpers.get_max_width_constraint( 1, 0, row_or_column) else: m_height_or_width = size_constraint_helpers.get_max_height_constraint( 1, 0, row_or_column) if len(size): size = cb.add(size, cb.add(m_height_or_width, str(spacing))) else: size = cb.add(m_height_or_width, str(spacing)) container_size = cb.add( cb.mult("2", outside_padding), str(container.computed_width()) ) if width_or_height == "width" else str(container.computed_height()) return cb.eq(container_size, size)
def get_row_width(self, row, spacing): """ Constraint to compute the width of a row, including spacing (margin) """ width = "" for i in range(0, len(row)): if i < len(row) - 1: if len(width): width = cb.add( width, cb.add(str(row[i].computed_width()), str(spacing))) else: width = cb.add(str(row[i].computed_width()), str(spacing)) else: if len(width): width = cb.add(width, str(row[i].computed_width())) else: width = str(row[i].computed_width()) return width
def get_column_height(self, column, spacing): """ Constraint to compute the height of a column, including spacing (margin) """ height = "" for i in range(0, len(column)): if i < len(column) - 1: if len(height): height = cb.add( height, cb.add(str(column[i].computed_height()), str(spacing))) else: height = cb.add(str(column[i].computed_height()), str(spacing)) else: if len(height): height = cb.add(height, str(column[i].computed_height())) else: height = str(column[i].computed_height()) return height
def set_container_size_cross_axis(self, container, padding, rows_or_columns, width_or_height): """ Constraint for the cross axis size of the container """ outside_padding = container.variables.outside_padding.id if container.at_root else "0" size = self.get_widest_row_constraint( 1, 0, rows_or_columns, padding ) if width_or_height == "width" else self.get_tallest_column_constraint( 1, 0, rows_or_columns, padding) container_size = cb.add( cb.mult("2", outside_padding), str(container.computed_width()) ) if width_or_height == "width" else str(container.computed_height()) return cb.eq(container_size, size)
def align_rows_or_columns(self, container, padding, rows, column_or_row, aligned_axis, aligned_axis_size, layout_axis, layout_axis_size): """ Align rows or columns elements with a column or row to the alignment of the parent container """ constraints = [] l_index = container.variables.alignment.domain.index("left") c_index = container.variables.alignment.domain.index("center") is_left = cb.eq(container.variables.alignment.id, str(l_index)) is_center = cb.eq(container.variables.alignment.id, str(c_index)) for row in rows: for i in range(len(row) - 1): shape1 = row[i] shape2 = row[i + 1] aligned_axis_size_value = str(shape1.computed_width( )) if aligned_axis_size == "width" else str( shape1.computed_height()) aligned_axis_size_value2 = str(shape2.computed_width( )) if aligned_axis_size == "width" else str( shape2.computed_height()) left_top_aligned = cb.eq(shape1.variables[aligned_axis].id, shape2.variables[aligned_axis].id) right_bottom_aligned = cb.eq( cb.add(shape1.variables[aligned_axis].id, aligned_axis_size_value), cb.add(shape2.variables[aligned_axis].id, aligned_axis_size_value2)) center_aligned = cb.eq( cb.add(shape1.variables[aligned_axis].id, cb.div(aligned_axis_size_value, "2")), cb.add(shape2.variables[aligned_axis].id, cb.div(aligned_axis_size_value2, "2"))) constraints.append( cb.ite( is_left, left_top_aligned, cb.ite(is_center, center_aligned, right_bottom_aligned))) # Shape 2 is exactly to the right of shape 1 or to the bottom if in a column layout_axis_size_value = str(shape1.computed_width( )) if layout_axis_size == "width" else str( shape1.computed_height()) constraints.append( cb.eq( cb.add(shape1.variables[layout_axis].id, cb.add(layout_axis_size_value, padding)), shape2.variables[layout_axis].id)) if len(constraints): return cb.and_expr(constraints) return True