]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/app_blocks/playlist.py
Lock widget modifications and reuse labels in actionlist/playlist
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / playlist.py
index b704d4d5077aed6a8f1fa1871f286b912b584ee7..5894995bc71aa66a3b692e89f0460452a10a5d2c 100644 (file)
@@ -4,14 +4,15 @@ from kivy.uix.relativelayout import RelativeLayout
 from kivy.properties import ListProperty
 from kivy.clock import Clock
 from ..helpers import duration_to_min_sec
-
-import math
+from ..lock import Lock
 
 __all__ = ["PlayList",
         "PlayListIcons", "PlayListIcon",
         "PlayListNames", "PlayListName",
         "PlayListTimes", "PlayListTime"]
 
+playlist_lock = Lock("playlist")
+
 class PlayList(RelativeLayout):
     playlist = ListProperty([])
 
@@ -37,7 +38,8 @@ class PlayList(RelativeLayout):
                 playlist.append(["⏸", music_file.name, time_info, False])
             else:
                 playlist.append(["⏵", music_file.name, time_info, True])
-        self.playlist = playlist
+        with playlist_lock:
+            self.playlist = playlist
 
 
 class PlayListIcons(StackLayout):
@@ -49,13 +51,19 @@ class PlayListIcons(StackLayout):
         parent.bind(playlist=self.update_playlist_icons)
 
     def update_playlist_icons(self, instance, playlist):
-        for icon in self.icons:
-            self.remove_widget(icon)
-        self.icons = []
-        for icon, filename, time_info, playing in playlist:
-            icon_label = PlayListIcon(text=icon)
-            self.add_widget(icon_label)
-            self.icons.append(icon_label)
+        icons_length = len(self.icons)
+        index = -1
+        for index, [icon, filename, time_info, playing] in enumerate(playlist):
+            if index >= icons_length:
+                icon_label = PlayListIcon(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 PlayListIcon(Label):
     def __init__(self, text='', **kwargs):
@@ -64,9 +72,14 @@ class PlayListIcon(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 PlayListNames(StackLayout):
     def __init__(self, **kwargs):
@@ -77,13 +90,19 @@ class PlayListNames(StackLayout):
         parent.bind(playlist=self.update_playlist_names)
 
     def update_playlist_names(self, instance, playlist):
-        for name in self.names:
-            self.remove_widget(name)
-        self.names = []
-        for icon, filename, time_info, playing in playlist:
-            name_label = PlayListName(text=filename, is_playing=playing)
-            self.add_widget(name_label)
-            self.names.append(name_label)
+        names_length = len(self.names)
+        index = -1
+        for index, [icon, filename, time_info, playing] in enumerate(playlist):
+            if index >= names_length:
+                name_label = PlayListName(text=filename, is_playing=playing)
+                self.add_widget(name_label)
+                self.names.append(name_label)
+            else:
+                self.names[index].text = filename
+
+        if index+1 < names_length:
+            self.clear_widgets(children=self.names[index+1:names_length])
+            del(self.names[index+1:names_length])
 
 class PlayListName(Label):
     def __init__(self, text='', is_playing=False, **kwargs):
@@ -93,8 +112,14 @@ class PlayListName(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 PlayListTimes(StackLayout):
     def __init__(self, **kwargs):
@@ -105,13 +130,19 @@ class PlayListTimes(StackLayout):
         parent.bind(playlist=self.update_playlist_times)
 
     def update_playlist_times(self, instance, playlist):
-        for time in self.times:
-            self.remove_widget(time)
-        self.times = []
-        for icon, filename, time_info, playing in playlist:
-            time_label = PlayListTime(text=time_info)
-            self.add_widget(time_label)
-            self.times.append(time_label)
+        times_length = len(self.times)
+        index = -1
+        for index, [icon, filename, time_info, playing] in enumerate(playlist):
+            if index >= times_length:
+                time_label = PlayListTime(text=time_info)
+                self.add_widget(time_label)
+                self.times.append(time_label)
+            else:
+                self.times[index].text = time_info
+
+        if index+1 < times_length:
+            self.clear_widgets(children=self.times[index+1:times_length])
+            del(self.times[index+1:times_length])
 
 class PlayListTime(Label):
     def __init__(self, text='', **kwargs):
@@ -120,6 +151,12 @@ class PlayListTime(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