]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - music_sampler/app_blocks/playlist.py
Use labels stacking to build playlist
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / playlist.py
1 from kivy.uix.label import Label
2 from kivy.uix.stacklayout import StackLayout
3 from kivy.uix.relativelayout import RelativeLayout
4 from kivy.properties import ListProperty
5 from kivy.clock import Clock
6 from ..helpers import duration_to_min_sec
7
8 import math
9
10 __all__ = ["PlayList",
11 "PlayListIcons", "PlayListIcon",
12 "PlayListNames", "PlayListName",
13 "PlayListTimes", "PlayListTime"]
14
15 class PlayList(RelativeLayout):
16 playlist = ListProperty([])
17
18 def __init__(self, **kwargs):
19 super(PlayList, self).__init__(**kwargs)
20 Clock.schedule_interval(self.update_playlist, 0.5)
21
22 def update_playlist(self, dt):
23 if self.parent is None or 'Mapping' not in self.parent.ids:
24 return True
25
26 open_files = self.parent.ids['Mapping'].open_files
27 playlist = []
28 for music_file in open_files.values():
29 if not music_file.is_in_use():
30 continue
31
32 time_info = "{}/{}".format(
33 duration_to_min_sec(music_file.sound_position),
34 duration_to_min_sec(music_file.sound_duration))
35
36 if music_file.is_loaded_paused():
37 playlist.append(["⏸", music_file.name, time_info, False])
38 else:
39 playlist.append(["⏵", music_file.name, time_info, True])
40 self.playlist = playlist
41
42
43 class PlayListIcons(StackLayout):
44 def __init__(self, **kwargs):
45 super(PlayListIcons, self).__init__(**kwargs)
46 self.icons = []
47
48 def on_parent(self, instance, parent):
49 parent.bind(playlist=self.update_playlist_icons)
50
51 def update_playlist_icons(self, instance, playlist):
52 for icon in self.icons:
53 self.remove_widget(icon)
54 self.icons = []
55 for icon, filename, time_info, playing in playlist:
56 icon_label = PlayListIcon(text=icon)
57 self.add_widget(icon_label)
58 self.icons.append(icon_label)
59
60 class PlayListIcon(Label):
61 def __init__(self, text='', **kwargs):
62 super(PlayListIcon, self).__init__(**kwargs)
63 self.text = text
64
65 def on_parent(self, instance, parent):
66 if parent is not None:
67 self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size))
68 self.height = parent.parent.labels_height
69
70
71 class PlayListNames(StackLayout):
72 def __init__(self, **kwargs):
73 super(PlayListNames, self).__init__(**kwargs)
74 self.names = []
75
76 def on_parent(self, instance, parent):
77 parent.bind(playlist=self.update_playlist_names)
78
79 def update_playlist_names(self, instance, playlist):
80 for name in self.names:
81 self.remove_widget(name)
82 self.names = []
83 for icon, filename, time_info, playing in playlist:
84 name_label = PlayListName(text=filename, is_playing=playing)
85 self.add_widget(name_label)
86 self.names.append(name_label)
87
88 class PlayListName(Label):
89 def __init__(self, text='', is_playing=False, **kwargs):
90 super(PlayListName, self).__init__(**kwargs)
91 self.text = text
92 self.bold = is_playing
93
94 def on_parent(self, instance, parent):
95 if parent is not None:
96 self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size))
97 self.height = parent.parent.labels_height
98
99 class PlayListTimes(StackLayout):
100 def __init__(self, **kwargs):
101 super(PlayListTimes, self).__init__(**kwargs)
102 self.times = []
103
104 def on_parent(self, instance, parent):
105 parent.bind(playlist=self.update_playlist_times)
106
107 def update_playlist_times(self, instance, playlist):
108 for time in self.times:
109 self.remove_widget(time)
110 self.times = []
111 for icon, filename, time_info, playing in playlist:
112 time_label = PlayListTime(text=time_info)
113 self.add_widget(time_label)
114 self.times.append(time_label)
115
116 class PlayListTime(Label):
117 def __init__(self, text='', **kwargs):
118 super(PlayListTime, self).__init__(**kwargs)
119 self.text = text
120
121 def on_parent(self, instance, parent):
122 if parent is not None:
123 self.font_size = math.ceil(2 * math.sqrt(parent.parent.parent.key_size))
124 self.height = parent.parent.labels_height
125