from kivy.graphics import Color, Line
-from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
self.attach(touch)
-class FormCanvas(ButtonBehavior, StackLayout):
+class FormCanvas(BoxLayout):
def __init__(self, *args, **kwargs):
super(FormCanvas, self).__init__(*args, **kwargs)
- self.n = 0
- self.orientation = 'lr-tb'
- self.padding = [10, 10, 10, 10]
- self.spacing = [10, 10]
+
+ self._canvas = StackLayout(
+ orientation='lr-tb',
+ padding=[10, 10, 10, 10],
+ spacing=[10, 10]
+ )
+ super(FormCanvas, self).add_widget(self._canvas)
self.widgets_height = 40
self.widgets_size_hint = (1, None)
- def on_press(self, *args):
- self.n += 1
+ def add_widget(self, widget):
g = Grabbable(
height=self.widgets_height,
size_hint=self.widgets_size_hint
)
- g.add_widget(Button(text=str(self.n)))
- self.add_widget(g)
- #print(self.export_to_kv(self))
+ g.add_widget(widget)
+ self._canvas.add_widget(g)
def detach_widget(self, widget):
"Detach grabbable widget from canvas and show widget destination"
# Put destination in the right place
if zone in ('left', 'right') and widget.parent.orientation != 'horizontal':
parent = widget.parent
- box = parent.create_box()
+ box = self.create_box()
parent.add_widget(box, index=widget_idx)
parent.remove_widget(widget)
box.add_widget(widget)
orientation='horizontal',
height=self.widgets_height,
size_hint=self.widgets_size_hint,
- spacing=self.spacing[0]
+ spacing=self._canvas.spacing[0]
)
- def export_to_kv(self, widget, indent=''):
+ def export_to_kv(self):
kv = """StackLayout:
orientation: '{orientation}'
padding: {padding}
spacing: {spacing}
-""".format(orientation=self.orientation, padding=self.padding, spacing=self.spacing)
+""".format(orientation=self._canvas.orientation, padding=self._canvas.padding, spacing=self._canvas.spacing)
indent = ' '
- stack = [self]
+ stack = [self._canvas]
widgets = self.walk(restrict=True)
next(widgets) # the first widget is the FormCanvas
+ next(widgets) # and the second is the inner StackLayout
for widget in widgets:
if not isinstance(widget, Grabbable):
# Look for the widget position inside the tree
stack.append(widget)
# Widget attributes
- kv += """{indent}height: 40
-{indent}size_hint: (1., None)
-{indent}text: '{text}'
-""".format(indent=indent*len(stack), text=widget.text)
+ for attr in ('height', 'size_hint', 'text'):
+ if hasattr(widget, attr):
+ kv += "{indent}{attr}: {value}\n".format(
+ indent=indent*len(stack),
+ attr=attr,
+ value=getattr(widget, attr)
+ )
return kv