smishlayev

widgets for row-/colspan grid layout

Sep 18th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1.  
  2. from kivy.uix.widget import Widget
  3. from kivy.properties import BoundedNumericProperty, ListProperty
  4.  
  5. class SpannableWidget(Widget):
  6.     """Widget with rowspan/colspan attributes able to span across rows/cols"""
  7.  
  8.     rowspan = BoundedNumericProperty(1, min=1)
  9.     colspan = BoundedNumericProperty(1, min=1)
  10.  
  11.     # list of widgets, representing extention by y-axis
  12.     _rowspan = ListProperty([])
  13.     # list of widgets, representing extention by x-axis
  14.     _colspan = ListProperty([])
  15.  
  16.     def on_parent(self, instance, parent):
  17.         """Clear extention widget lists when widget is inserted/removed
  18.  
  19.        @param instance: instance, that fired the event
  20.        @param parent: new C{self.parent} value
  21.  
  22.        """
  23.         for _widget in self._rowspan:
  24.             _widget.unbind(height=self.update, y=self.update)
  25.         self._rowspan = []
  26.  
  27.         for _widget in self._colspan:
  28.             _widget.unbind(width=self.update, x=self.update)
  29.         self._colspan = []
  30.  
  31.     def update(self, *largs):
  32.         """Update widget size and position
  33.  
  34.        @param largs: arguments of the property change event (not used)
  35.  
  36.        """
  37.         _widget = None
  38.         _height = self.parent.height
  39.         for _widget in self._rowspan:
  40.             _height += _widget.height
  41.         self.height = _height
  42.         if _widget:
  43.             # Stretch widget vertically specifying its y (lower) bound
  44.             self.y = _widget.y
  45.             pass
  46.         self.width = self.parent.width
  47.         for _widget in self._colspan:
  48.             # Stretch widget horizontally increasing its width
  49.             self.width += _widget.width
  50.         # Align widget with parent (C{Placeholder}) by x axis
  51.         self.x = self.parent.x
  52.  
  53.  
  54. class Span(Widget):
  55.     """Widget taking place for rowspan/colspan of other widgets"""
  56.  
  57.     pass
  58.  
  59.  
  60. class Placeholder(Widget):
  61.     """Widget holding single child
  62.  
  63.    Represents an empty matrix cell.
  64.  
  65.    Used to fill C{terminal.layout.Grid.children} list with empty spaces,
  66.    widgets can be inserted into those later.
  67.  
  68.    """
  69.     def add_widget(self, widget):
  70.         """Insert one child and stub this method"""
  71.         if self.children:
  72.             return
  73.         super(Placeholder, self).add_widget(widget)
  74.         self.bind(
  75.             size=widget.setter("size"),
  76.             pos=widget.setter("pos"))
Advertisement
Add Comment
Please, Sign In to add comment