]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/app_blocks/playlist.py
Use labels stacking to build playlist
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / playlist.py
diff --git a/music_sampler/app_blocks/playlist.py b/music_sampler/app_blocks/playlist.py
new file mode 100644 (file)
index 0000000..b704d4d
--- /dev/null
@@ -0,0 +1,125 @@
+from kivy.uix.label import Label
+from kivy.uix.stacklayout import StackLayout
+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
+
+__all__ = ["PlayList",
+        "PlayListIcons", "PlayListIcon",
+        "PlayListNames", "PlayListName",
+        "PlayListTimes", "PlayListTime"]
+
+class PlayList(RelativeLayout):
+    playlist = ListProperty([])
+
+    def __init__(self, **kwargs):
+        super(PlayList, self).__init__(**kwargs)
+        Clock.schedule_interval(self.update_playlist, 0.5)
+
+    def update_playlist(self, dt):
+        if self.parent is None or 'Mapping' not in self.parent.ids:
+            return True
+
+        open_files = self.parent.ids['Mapping'].open_files
+        playlist = []
+        for music_file in open_files.values():
+            if not music_file.is_in_use():
+                continue
+
+            time_info = "{}/{}".format(
+                    duration_to_min_sec(music_file.sound_position),
+                    duration_to_min_sec(music_file.sound_duration))
+
+            if music_file.is_loaded_paused():
+                playlist.append(["⏸", music_file.name, time_info, False])
+            else:
+                playlist.append(["⏵", music_file.name, time_info, True])
+        self.playlist = playlist
+
+
+class PlayListIcons(StackLayout):
+    def __init__(self, **kwargs):
+        super(PlayListIcons, self).__init__(**kwargs)
+        self.icons = []
+
+    def on_parent(self, instance, parent):
+        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)
+
+class PlayListIcon(Label):
+    def __init__(self, text='', **kwargs):
+        super(PlayListIcon, self).__init__(**kwargs)
+        self.text = text
+
+    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
+
+
+class PlayListNames(StackLayout):
+    def __init__(self, **kwargs):
+        super(PlayListNames, self).__init__(**kwargs)
+        self.names = []
+
+    def on_parent(self, instance, parent):
+        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)
+
+class PlayListName(Label):
+    def __init__(self, text='', is_playing=False, **kwargs):
+        super(PlayListName, self).__init__(**kwargs)
+        self.text = text
+        self.bold = is_playing
+
+    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
+
+class PlayListTimes(StackLayout):
+    def __init__(self, **kwargs):
+        super(PlayListTimes, self).__init__(**kwargs)
+        self.times = []
+
+    def on_parent(self, instance, parent):
+        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)
+
+class PlayListTime(Label):
+    def __init__(self, text='', **kwargs):
+        super(PlayListTime, self).__init__(**kwargs)
+        self.text = text
+
+    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
+