From bbd085337b453de040e217c8a60be8e2cd5c8be4 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Sat, 8 Oct 2011 16:05:23 +0100 Subject: [PATCH] gridlayout: make minimum_size private to prevent confusion, and fix issue when cols/rows are changed in runtime --- kivy/uix/gridlayout.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/kivy/uix/gridlayout.py b/kivy/uix/gridlayout.py index aa5849637..6bd8d0322 100644 --- a/kivy/uix/gridlayout.py +++ b/kivy/uix/gridlayout.py @@ -80,8 +80,10 @@ Next, let's fix the row height to a specific size:: __all__ = ('GridLayout', 'GridLayoutException') from kivy.clock import Clock +from kivy.logger import Logger from kivy.uix.layout import Layout -from kivy.properties import NumericProperty, BooleanProperty, DictProperty +from kivy.properties import NumericProperty, BooleanProperty, DictProperty, \ + BoundedNumericProperty from math import ceil @@ -109,15 +111,23 @@ class GridLayout(Layout): 0. ''' - cols = NumericProperty(None) + cols = BoundedNumericProperty(None, min=0, allow_none=True) '''Number of columns in the grid + .. versionadded:: 1.0.8 + Change from NumericProperty to BoundedNumericProperty. You cannot set a + negative value anymore. + :data:`cols` is a :class:`~kivy.properties.NumericProperty`, default to 0. ''' - rows = NumericProperty(None) + rows = BoundedNumericProperty(None, min=0, allow_none=True) '''Number of rows in the grid + .. versionadded:: 1.0.8 + Change from NumericProperty to BoundedNumericProperty. You cannot set a + negative value anymore. + :data:`rows` is a :class:`~kivy.properties.NumericProperty`, default to 0. ''' @@ -180,6 +190,7 @@ class GridLayout(Layout): def __init__(self, **kwargs): self._cols = self._rows = None self._trigger_layout = Clock.create_trigger(self.do_layout, -1) + self._minimum_size = [0, 0] super(GridLayout, self).__init__(**kwargs) self.bind( @@ -187,7 +198,8 @@ class GridLayout(Layout): row_default_height = self._trigger_layout, col_force_default = self._trigger_layout, row_force_default = self._trigger_layout, - minimum_size = self._trigger_layout, + cols = self._trigger_layout, + rows = self._trigger_layout, spacing = self._trigger_layout, padding = self._trigger_layout, children = self._trigger_layout, @@ -234,6 +246,8 @@ class GridLayout(Layout): # if no cols or rows are set, we can't calculate minimum size. # the grid must be contrained at least on one side if not current_cols and not current_rows: + Logger.warning('%r have no cols or rows set, ' + 'layout are not triggered.' % self) return None if current_cols is None: current_cols = int(ceil(len_children / float(current_rows))) @@ -270,8 +284,6 @@ class GridLayout(Layout): shh = c.size_hint_y w = c.width h = c.height - if isinstance(c, Layout): - w, h = c.minimum_size # compute minimum size / maximum stretch needed if shw is None: @@ -302,7 +314,7 @@ class GridLayout(Layout): self._rows_sh = rows_sh # finally, set the minimum size - self.minimum_size = (width, height) + self._minimum_size = (width, height) def do_layout(self, *largs): self.update_minimum_size() @@ -320,7 +332,6 @@ class GridLayout(Layout): padding = self.padding spacing = self.spacing selfx = self.x - selfy = self.y selfw = self.width selfh = self.height @@ -333,7 +344,7 @@ class GridLayout(Layout): cols = self._cols[:] cols_sh = self._cols_sh cols_weigth = sum([x for x in cols_sh if x]) - strech_w = max(0, selfw - self.minimum_size[0]) + strech_w = max(0, selfw - self._minimum_size[0]) for index in xrange(len(cols)): # if the col don't have strech information, nothing to do col_stretch = cols_sh[index] @@ -354,7 +365,7 @@ class GridLayout(Layout): rows = self._rows[:] rows_sh = self._rows_sh rows_weigth = sum([x for x in rows_sh if x]) - strech_h = max(0, selfh - self.minimum_size[1]) + strech_h = max(0, selfh - self._minimum_size[1]) for index in xrange(len(rows)): # if the row don't have strech information, nothing to do row_stretch = rows_sh[index]