]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/app_blocks/actionlist.py
Lock widget modifications and reuse labels in actionlist/playlist
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / actionlist.py
index 70fe3f4f5806e6c1ecf0de7d5a82b1e6934b4a14..51f4b826506383e0b45c7767ee22b6c3c244be34 100644 (file)
@@ -2,13 +2,14 @@ from kivy.uix.label import Label
 from kivy.uix.stacklayout import StackLayout
 from kivy.uix.relativelayout import RelativeLayout
 from kivy.properties import ListProperty, StringProperty
-
-import math
+from ..lock import Lock
 
 __all__ = ["ActionList",
         "ActionListIcons", "ActionListIcon",
         "ActionListDescriptions", "ActionListDescription"]
 
+actionlist_lock = Lock("playlist")
+
 class ActionList(RelativeLayout):
     action_title = StringProperty("")
     action_list = ListProperty([])
@@ -25,7 +26,8 @@ class ActionList(RelativeLayout):
             else:
                 icon = " "
             action_list.append([icon, action])
-        self.action_list = action_list
+        with actionlist_lock:
+            self.action_list = action_list
 
 class ActionListIcons(StackLayout):
     def __init__(self, **kwargs):
@@ -36,13 +38,19 @@ class ActionListIcons(StackLayout):
         parent.bind(action_list=self.update_actionlist_icons)
 
     def update_actionlist_icons(self, instance, actionlist):
-        for icon in self.icons:
-            self.remove_widget(icon)
-        self.icons = []
-        for icon, description in actionlist:
-            icon_label = ActionListIcon(text=icon)
-            self.add_widget(icon_label)
-            self.icons.append(icon_label)
+        icons_length = len(self.icons)
+        index = -1
+        for index, [icon, description] in enumerate(actionlist):
+            if index >= icons_length:
+                icon_label = ActionListIcon(text=icon)
+                self.add_widget(icon_label)
+                self.icons.append(icon_label)
+            else:
+                self.icons[index].text = icon
+
+        if index+1 < icons_length:
+            self.clear_widgets(children=self.icons[index+1:icons_length])
+            del(self.icons[index+1:icons_length])
 
 class ActionListIcon(Label):
     def __init__(self, text='', **kwargs):
@@ -51,8 +59,14 @@ class ActionListIcon(Label):
 
     def on_parent(self, instance, parent):
         if parent is not None:
-            self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size))
-            self.height = parent.parent.labels_height
+            parent.bind(font_size=self.update_font_size)
+            parent.bind(labels_height=self.update_height)
+
+    def update_height(self, instance, height):
+        self.height = height
+
+    def update_font_size(self, instance, font_size):
+        self.font_size = font_size
 
 class ActionListDescriptions(StackLayout):
     def __init__(self, **kwargs):
@@ -63,13 +77,20 @@ class ActionListDescriptions(StackLayout):
         parent.bind(action_list=self.update_actionlist_descriptions)
 
     def update_actionlist_descriptions(self, instance, actionlist):
-        for description in self.descriptions:
-            self.remove_widget(description)
-        self.descriptions = []
-        for icon, description in actionlist:
-            description_label = ActionListDescription(text=description)
-            self.add_widget(description_label)
-            self.descriptions.append(description_label)
+        descriptions_length = len(self.descriptions)
+        index = -1
+        for index, [icon, description] in enumerate(actionlist):
+            if index >= descriptions_length:
+                description_label = ActionListDescription(text=description)
+                self.add_widget(description_label)
+                self.descriptions.append(description_label)
+            else:
+                self.descriptions[index].text = description
+
+        if index+1 < descriptions_length:
+            self.clear_widgets(
+                    children=self.descriptions[index+1:descriptions_length])
+            del(self.descriptions[index+1:descriptions_length])
 
 class ActionListDescription(Label):
     def __init__(self, text='', **kwargs):
@@ -78,7 +99,12 @@ class ActionListDescription(Label):
 
     def on_parent(self, instance, parent):
         if parent is not None:
-            self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size))
-            self.height = parent.parent.labels_height
+            parent.bind(font_size=self.update_font_size)
+            parent.bind(labels_height=self.update_height)
+
+    def update_height(self, instance, height):
+        self.height = height
 
+    def update_font_size(self, instance, font_size):
+        self.font_size = font_size