Prepare FormCanvas to be used from other widgets
authorJavier Sancho <jsf@jsancho.org>
Wed, 25 Jul 2018 17:00:00 +0000 (19:00 +0200)
committerJavier Sancho <jsf@jsancho.org>
Wed, 25 Jul 2018 17:00:00 +0000 (19:00 +0200)
kivyforms/formcanvas.py

index 8013ef6..a1b7058 100644 (file)
@@ -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,7 +198,7 @@ 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=''):