Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

boxlayout: honoring pos_hint, only the one that make sense depending …

…the orientation. closes #698
  • Loading branch information...
commit cfb118cccfef06108916bc52794aad39b9c80e48 1 parent 2f599d1
@tito tito authored
Showing with 114 additions and 19 deletions.
  1. +52 −0 examples/widgets/boxlayout_poshint.py
  2. +62 −19 kivy/uix/boxlayout.py
View
52 examples/widgets/boxlayout_poshint.py
@@ -0,0 +1,52 @@
+from kivy.uix.gridlayout import GridLayout
+from kivy.app import App
+from kivy.lang import Builder
+
+Builder.load_string('''
+<Demo>:
+ cols: 1
+
+ BoxLayout:
+ orientation: 'vertical'
+ Button:
+ size_hint_x: 0.4
+ pos_hint: {'x': 0}
+ text: 'pos_hint: x=0'
+
+ Button:
+ size_hint_x: 0.2
+ pos_hint: {'center_x': 0.5}
+ text: 'pos_hint: center_x=0.5'
+
+ Button:
+ size_hint_x: 0.4
+ pos_hint: {'right': 1}
+ text: 'pos_hint: right=1'
+
+ BoxLayout:
+ Button:
+ size_hint_y: 0.4
+ pos_hint: {'y': 0}
+ text: 'pos_hint: y=0'
+
+ Button:
+ size_hint_y: 0.2
+ pos_hint: {'center_y': .5}
+ text: 'pos_hint: center_y=0.5'
+
+ Button:
+ size_hint_y: 0.4
+ pos_hint: {'top': 1}
+ text: 'pos_hint: top=1'
+''')
+
+class Demo(GridLayout):
+ pass
+
+
+class DemoApp(App):
+ def build(self):
+ return Demo()
+
+if __name__ == '__main__':
+ DemoApp().run()
View
81 kivy/uix/boxlayout.py
@@ -32,6 +32,13 @@
layout.add_widget(btn1)
layout.add_widget(btn2)
+Position hint are also partially working, depending the orientation:
+
+* If the orientation is `vertical`: `x`, `right` and `center_x` will be used
+* If the orientation is `horizontal`: `y`, `top` and `center_y` will be used
+
+You can check the `examples/widgets/boxlayout_poshint.py` for a live example.
+
.. note::
The `size_hint` uses the available space after subtracting all the
@@ -45,6 +52,10 @@
The first button will be 200px wide as specified, the second and third
will be 300px each, e.g., (800-200)*0.5
+
+.. versionchanged:: 1.4.1
+ Added support for `pos_hint`.
+
'''
__all__ = ('BoxLayout', )
@@ -95,9 +106,10 @@ def do_layout(self, *largs):
len_children = len(self.children)
if len_children == 0:
return
- reposition_child = self.reposition_child
- selfx, selfy = self.pos
- selfw, selfh = self.size
+ selfx = self.x
+ selfy = self.y
+ selfw = self.width
+ selfh = self.height
padding = self.padding
spacing = self.spacing
orientation = self.orientation
@@ -109,7 +121,8 @@ def do_layout(self, *largs):
minimum_size_x = padding2 + spacing * (len_children - 1)
minimum_size_y = minimum_size_x
for w in self.children:
- shw, shh = w.size_hint
+ shw = w.size_hint_x
+ shh = w.size_hint_y
if shw is None:
minimum_size_x += w.width
else:
@@ -123,27 +136,57 @@ def do_layout(self, *largs):
x = y = padding
stretch_space = max(0.0, selfw - minimum_size_x)
for c in reversed(self.children):
- shw, shh = c.size_hint
- c_pos = selfx + x, selfy + y
- c_size = list(c.size)
+ shw = c.size_hint_x
+ shh = c.size_hint_y
+ w = c.width
+ h = c.height
+ cx = selfx + x
+ cy = selfy + y
+
if shw:
- #its sizehint * available space
- c_size[0] = stretch_space * shw / stretch_weight_x
+ w = stretch_space * shw / stretch_weight_x
if shh:
- c_size[1] = shh * (selfh - padding2)
- reposition_child(c, pos=c_pos, size=c_size)
- x += c_size[0] + spacing
+ h = shh * (selfh - padding2)
+
+ for key, value in c.pos_hint.iteritems():
+ if key == 'y':
+ cy = y + value * h
+ elif key == 'top':
+ cy = y + selfh - value * h
+ elif key == 'center_y':
+ cy = y + selfh / 2. - (value * h)
+ c.x = cx
+ c.y = cy
+ c.width = w
+ c.height = h
+ x += w + spacing
if orientation == 'vertical':
x = y = padding
stretch_space = max(0.0, selfh - minimum_size_y)
for c in self.children:
- shw, shh = c.size_hint
- c_pos = selfx + x, selfy + y
- c_size = list(c.size)
+ shw = c.size_hint_x
+ shh = c.size_hint_y
+ w = c.width
+ h = c.height
+ cx = selfx + x
+ cy = selfy + y
+
if shh:
- c_size[1] = stretch_space * shh / stretch_weight_y
+ h = stretch_space * shh / stretch_weight_y
if shw:
- c_size[0] = shw * (selfw - padding2)
- reposition_child(c, pos=c_pos, size=c_size)
- y += c_size[1] + spacing
+ w = shw * (selfw - padding2)
+
+ for key, value in c.pos_hint.iteritems():
+ if key == 'x':
+ cx = x + value * w
+ elif key == 'right':
+ cx = x + selfw - value * w
+ elif key == 'center_x':
+ cx = x + selfw / 2. - (value * w)
+
+ c.x = cx
+ c.y = cy
+ c.width = w
+ c.height = h
+ y += h + spacing
Please sign in to comment.
Something went wrong with that request. Please try again.