X-Git-Url: https://git.jsancho.org/?p=kivyforms.git;a=blobdiff_plain;f=kivyforms%2Fformcanvas.py;h=0f4c18302cb6a97fd4170a960763da1c642ef77f;hp=8013ef6d442d6ee38f2f3fa0fe86fc568941b523;hb=4a4f9d061b40f5c2527943af31639525d19e7798;hpb=44730c8c03ec9207b2f1f6228acb0e391497246d diff --git a/kivyforms/formcanvas.py b/kivyforms/formcanvas.py index 8013ef6..0f4c183 100644 --- a/kivyforms/formcanvas.py +++ b/kivyforms/formcanvas.py @@ -1,5 +1,4 @@ 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 @@ -110,26 +109,27 @@ class Grabbable(BoxLayout): 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" @@ -162,7 +162,7 @@ class FormCanvas(ButtonBehavior, StackLayout): # 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) @@ -198,21 +198,22 @@ class FormCanvas(ButtonBehavior, StackLayout): 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 @@ -228,9 +229,15 @@ class FormCanvas(ButtonBehavior, StackLayout): 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): + value = getattr(widget, attr) + if type(value) is str: + value = "'" + value + "'" + kv += "{indent}{attr}: {value}\n".format( + indent=indent*len(stack), + attr=attr, + value=value + ) return kv