/usr/share/pyshared/kivy/uix/gridlayout.py is in python-kivy 1.7.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | '''
Grid Layout
===========
.. only:: html
.. image:: images/gridlayout.gif
:align: right
.. only:: latex
.. image:: images/gridlayout.png
:align: right
.. versionadded:: 1.0.4
:class:`GridLayout` arranges children in a matrix. It takes the available space
and divides it into columns and rows, then adds widgets to the resulting
"cells".
.. versionadded:: 1.0.7
The implementation has changed to use widget size_hint for calculating
column/row sizes. `uniform_width` and `uniform_height` have been removed,
and others properties have added to give you more control.
Background
----------
Unlike many other toolkits, you cannot explicitly place a widget at a specific
column/row. Each child is automatically assigned a position, depending on the
layout configuration and the child's index in the children list.
A GridLayout must always have at least one input constraint:
:data:`GridLayout.cols` or :data:`GridLayout.rows`. If you do not specify cols
or rows, the Layout will throw an exception.
Column Width and Row Height
---------------------------
The column width/row height are determined in 3 steps:
- The initial size is given by the :data:`col_default_width` and
:data:`row_default_height` properties. To customize the size of a single
column or row, use :data:`cols_minimum` or :data:`rows_minimum`.
- Then the `size_hint_x`/`size_hint_y` of the child are taken into account.
If no widgets have a size hint, the maximum size is used for all children.
- You can force the default size by setting the :data:`col_force_default`
or :data:`row_force_default` property. This will force the layout to
ignore the `width` and `size_hint` properties of children and use the
default size.
Usage of GridLayout
-------------------
In the example below, all widgets will get an equal size. By default,
`size_hint` is (1, 1), so a Widget will take the full size of the parent::
layout = GridLayout(cols=2)
layout.add_widget(Button(text='Hello 1'))
layout.add_widget(Button(text='World 1'))
layout.add_widget(Button(text='Hello 2'))
layout.add_widget(Button(text='World 2'))
.. image:: images/gridlayout_1.jpg
Now, let's fix the size of Hello buttons to 100px instead of using size_hint 1::
layout = GridLayout(cols=2)
layout.add_widget(Button(text='Hello 1', size_hint_x=None, width=100))
layout.add_widget(Button(text='World 1'))
layout.add_widget(Button(text='Hello 2', size_hint_x=None, width=100))
layout.add_widget(Button(text='World 2'))
.. image:: images/gridlayout_2.jpg
Next, let's fix the row height to a specific size::
layout = GridLayout(cols=2, row_force_default=True, row_default_height=40)
layout.add_widget(Button(text='Hello 1', size_hint_x=None, width=100))
layout.add_widget(Button(text='World 1'))
layout.add_widget(Button(text='Hello 2', size_hint_x=None, width=100))
layout.add_widget(Button(text='World 2'))
.. image:: images/gridlayout_3.jpg
'''
__all__ = ('GridLayout', 'GridLayoutException')
from kivy.logger import Logger
from kivy.uix.layout import Layout
from kivy.properties import NumericProperty, BooleanProperty, DictProperty, \
BoundedNumericProperty, ReferenceListProperty, VariableListProperty
from math import ceil
class GridLayoutException(Exception):
'''Exception for errors in the grid layout manipulation.
'''
pass
class GridLayout(Layout):
'''Grid layout class. See module documentation for more information.
'''
spacing = VariableListProperty([0, 0], length=2)
'''Spacing between children: [spacing_horizontal, spacing_vertical].
spacing also accepts a one argument form [spacing].
:data:`spacing` is a :class:`~kivy.properties.VariableListProperty`, default to
[0, 0].
'''
padding = VariableListProperty([0, 0, 0, 0])
'''Padding between layout box and children: [padding_left, padding_top,
padding_right, padding_bottom].
padding also accepts a two argument form [padding_horizontal,
padding_vertical] and a one argument form [padding].
.. versionchanged:: 1.7.0
Replaced NumericProperty with VariableListProperty.
:data:`padding` is a :class:`~kivy.properties.VariableListProperty`, default to
[0, 0, 0, 0].
'''
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 = 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.
'''
col_default_width = NumericProperty(0)
'''Default minimum size to use for column.
.. versionadded:: 1.0.7
:data:`col_default_width` is a :class:`~kivy.properties.NumericProperty`,
default to 0.
'''
row_default_height = NumericProperty(0)
'''Default minimum size to use for row.
.. versionadded:: 1.0.7
:data:`row_default_height` is a :class:`~kivy.properties.NumericProperty`,
default to 0.
'''
col_force_default = BooleanProperty(False)
'''If True, ignore the width and size_hint_x of the child, and use the
default column width.
.. versionadded:: 1.0.7
:data:`col_force_default` is a :class:`~kivy.properties.BooleanProperty`,
default to False.
'''
row_force_default = BooleanProperty(False)
'''If True, ignore the height and size_hint_y of the child, and use the
default row height.
.. versionadded:: 1.0.7
:data:`row_force_default` is a :class:`~kivy.properties.BooleanProperty`,
default to False.
'''
cols_minimum = DictProperty({})
'''List of minimum size for each column.
.. versionadded:: 1.0.7
:data:`cols_minimum` is a :class:`~kivy.properties.DictProperty`, default
to {}
'''
rows_minimum = DictProperty({})
'''List of minimum size for each row.
.. versionadded:: 1.0.7
:data:`rows_minimum` is a :class:`~kivy.properties.DictProperty`, default
to {}
'''
minimum_width = NumericProperty(0)
'''Minimum width needed to contain all children.
.. versionadded:: 1.0.8
:data:`minimum_width` is a :class:`kivy.properties.NumericProperty`, default
to 0.
'''
minimum_height = NumericProperty(0)
'''Minimum height needed to contain all children.
.. versionadded:: 1.0.8
:data:`minimum_height` is a :class:`kivy.properties.NumericProperty`,
default to 0.
'''
minimum_size = ReferenceListProperty(minimum_width, minimum_height)
'''Minimum size needed to contain all children.
.. versionadded:: 1.0.8
:data:`minimum_size` is a :class:`~kivy.properties.ReferenceListProperty` of
(:data:`minimum_width`, :data:`minimum_height`) properties.
'''
def __init__(self, **kwargs):
self._cols = self._rows = None
super(GridLayout, self).__init__(**kwargs)
self.bind(
col_default_width=self._trigger_layout,
row_default_height=self._trigger_layout,
col_force_default=self._trigger_layout,
row_force_default=self._trigger_layout,
cols=self._trigger_layout,
rows=self._trigger_layout,
parent=self._trigger_layout,
spacing=self._trigger_layout,
padding=self._trigger_layout,
children=self._trigger_layout,
size=self._trigger_layout,
pos=self._trigger_layout)
def get_max_widgets(self):
if self.cols and not self.rows:
return None
if self.rows and not self.cols:
return None
if not self.cols and not self.rows:
return None
return self.rows * self.cols
def on_children(self, instance, value):
# if that makes impossible to construct things with deffered method,
# migrate this test in do_layout, and/or issue a warning.
smax = self.get_max_widgets()
if smax and len(value) > smax:
raise GridLayoutException(
'Too many children in GridLayout. Increase rows/cols!')
def update_minimum_size(self, *largs):
# the goal here is to calculate the minimum size of every cols/rows
# and determine if they have stretch or not
current_cols = self.cols
current_rows = self.rows
children = self.children
len_children = len(children)
# 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 is not triggered.' % self)
return None
if current_cols is None:
current_cols = int(ceil(len_children / float(current_rows)))
elif current_rows is None:
current_rows = int(ceil(len_children / float(current_cols)))
current_cols = max(1, current_cols)
current_rows = max(1, current_rows)
cols = [self.col_default_width] * current_cols
cols_sh = [None] * current_cols
rows = [self.row_default_height] * current_rows
rows_sh = [None] * current_rows
# update minimum size from the dicts
# FIXME index might be outside the bounds ?
for index, value in self.cols_minimum.iteritems():
cols[index] = value
for index, value in self.rows_minimum.iteritems():
rows[index] = value
# calculate minimum size for each columns and rows
i = len_children - 1
for row in xrange(current_rows):
for col in xrange(current_cols):
# don't go further is we don't have child left
if i < 0:
break
# get initial information from the child
c = children[i]
shw = c.size_hint_x
shh = c.size_hint_y
w = c.width
h = c.height
# compute minimum size / maximum stretch needed
if shw is None:
cols[col] = max(cols[col], w)
else:
cols_sh[col] = max(cols_sh[col], shw)
if shh is None:
rows[row] = max(rows[row], h)
else:
rows_sh[row] = max(rows_sh[row], shh)
# next child
i = i - 1
# calculate minimum width/height needed, starting from padding + spacing
padding_x = self.padding[0] + self.padding[2]
padding_y = self.padding[1] + self.padding[3]
spacing_x, spacing_y = self.spacing
width = padding_x + spacing_x * (current_cols - 1)
height = padding_y + spacing_y * (current_rows - 1)
# then add the cell size
width += sum(cols)
height += sum(rows)
# remember for layout
self._cols = cols
self._rows = rows
self._cols_sh = cols_sh
self._rows_sh = rows_sh
# finally, set the minimum size
self.minimum_size = (width, height)
def do_layout(self, *largs):
self.update_minimum_size()
if self._cols is None:
return
if self.cols is None and self.rows is None:
raise GridLayoutException('Need at least cols or rows constraint.')
children = self.children
len_children = len(children)
if len_children == 0:
return
# speedup
padding_left = self.padding[0]
padding_top = self.padding[1]
spacing_x, spacing_y = self.spacing
selfx = self.x
selfw = self.width
selfh = self.height
# resolve size for each column
if self.col_force_default:
cols = [self.col_default_width] * len(self._cols)
for index, value in self.cols_minimum.iteritems():
cols[index] = value
else:
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_width)
for index in xrange(len(cols)):
# if the col don't have strech information, nothing to do
col_stretch = cols_sh[index]
if col_stretch is None:
continue
# calculate the column stretch, and take the maximum from
# minimum size and the calculated stretch
col_width = cols[index]
col_width = max(col_width, strech_w * col_stretch / cols_weigth)
cols[index] = col_width
# same algo for rows
if self.row_force_default:
rows = [self.row_default_height] * len(self._rows)
for index, value in self.rows_minimum.iteritems():
rows[index] = value
else:
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_height)
for index in xrange(len(rows)):
# if the row don't have strech information, nothing to do
row_stretch = rows_sh[index]
if row_stretch is None:
continue
# calculate the row stretch, and take the maximum from minimum
# size and the calculated stretch
row_height = rows[index]
row_height = max(row_height,
strech_h * row_stretch / rows_weigth)
rows[index] = row_height
# reposition every child
i = len_children - 1
y = self.top - padding_top
for row_height in rows:
x = selfx + padding_left
for col_width in cols:
if i < 0:
break
c = children[i]
c.x = x
c.y = y - row_height
c.width = col_width
c.height = row_height
i = i - 1
x = x + col_width + spacing_x
y -= row_height + spacing_y
|