]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - music_sampler/app_blocks/playlist.py
Merge branch 'load_action'
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / playlist.py
CommitLineData
d734981b
IB
1from kivy.uix.label import Label
2from kivy.uix.stacklayout import StackLayout
3from kivy.uix.relativelayout import RelativeLayout
4from kivy.properties import ListProperty
a9324e30 5from kivy.clock import Clock, mainthread
d734981b 6from ..helpers import duration_to_min_sec
4d1dfc89 7from ..lock import Lock
d734981b
IB
8
9__all__ = ["PlayList",
10 "PlayListIcons", "PlayListIcon",
11 "PlayListNames", "PlayListName",
12 "PlayListTimes", "PlayListTime"]
13
4d1dfc89
IB
14playlist_lock = Lock("playlist")
15
d734981b
IB
16class PlayList(RelativeLayout):
17 playlist = ListProperty([])
18
19 def __init__(self, **kwargs):
20 super(PlayList, self).__init__(**kwargs)
21 Clock.schedule_interval(self.update_playlist, 0.5)
22
a9324e30 23 @mainthread
d734981b
IB
24 def update_playlist(self, dt):
25 if self.parent is None or 'Mapping' not in self.parent.ids:
26 return True
27
28 open_files = self.parent.ids['Mapping'].open_files
29 playlist = []
30 for music_file in open_files.values():
31 if not music_file.is_in_use():
32 continue
33
34 time_info = "{}/{}".format(
35 duration_to_min_sec(music_file.sound_position),
36 duration_to_min_sec(music_file.sound_duration))
37
38 if music_file.is_loaded_paused():
39 playlist.append(["⏸", music_file.name, time_info, False])
40 else:
41 playlist.append(["⏵", music_file.name, time_info, True])
4d1dfc89
IB
42 with playlist_lock:
43 self.playlist = playlist
d734981b
IB
44
45
46class PlayListIcons(StackLayout):
47 def __init__(self, **kwargs):
48 super(PlayListIcons, self).__init__(**kwargs)
49 self.icons = []
50
51 def on_parent(self, instance, parent):
52 parent.bind(playlist=self.update_playlist_icons)
53
54 def update_playlist_icons(self, instance, playlist):
4d1dfc89
IB
55 icons_length = len(self.icons)
56 index = -1
57 for index, [icon, filename, time_info, playing] in enumerate(playlist):
58 if index >= icons_length:
59 icon_label = PlayListIcon(text=icon)
60 self.add_widget(icon_label)
61 self.icons.append(icon_label)
62 else:
63 self.icons[index].text = icon
64
65 if index+1 < icons_length:
66 self.clear_widgets(children=self.icons[index+1:icons_length])
67 del(self.icons[index+1:icons_length])
d734981b
IB
68
69class PlayListIcon(Label):
70 def __init__(self, text='', **kwargs):
71 super(PlayListIcon, self).__init__(**kwargs)
72 self.text = text
73
74 def on_parent(self, instance, parent):
75 if parent is not None:
4d1dfc89
IB
76 parent.bind(font_size=self.update_font_size)
77 parent.bind(labels_height=self.update_height)
78
79 def update_height(self, instance, height):
80 self.height = height
d734981b 81
4d1dfc89
IB
82 def update_font_size(self, instance, font_size):
83 self.font_size = font_size
d734981b
IB
84
85class PlayListNames(StackLayout):
86 def __init__(self, **kwargs):
87 super(PlayListNames, self).__init__(**kwargs)
88 self.names = []
89
90 def on_parent(self, instance, parent):
91 parent.bind(playlist=self.update_playlist_names)
92
93 def update_playlist_names(self, instance, playlist):
4d1dfc89
IB
94 names_length = len(self.names)
95 index = -1
96 for index, [icon, filename, time_info, playing] in enumerate(playlist):
97 if index >= names_length:
98 name_label = PlayListName(text=filename, is_playing=playing)
99 self.add_widget(name_label)
100 self.names.append(name_label)
101 else:
102 self.names[index].text = filename
103
104 if index+1 < names_length:
105 self.clear_widgets(children=self.names[index+1:names_length])
106 del(self.names[index+1:names_length])
d734981b
IB
107
108class PlayListName(Label):
109 def __init__(self, text='', is_playing=False, **kwargs):
110 super(PlayListName, self).__init__(**kwargs)
111 self.text = text
112 self.bold = is_playing
113
114 def on_parent(self, instance, parent):
115 if parent is not None:
4d1dfc89
IB
116 parent.bind(font_size=self.update_font_size)
117 parent.bind(labels_height=self.update_height)
118
119 def update_height(self, instance, height):
120 self.height = height
121
122 def update_font_size(self, instance, font_size):
123 self.font_size = font_size
d734981b
IB
124
125class PlayListTimes(StackLayout):
126 def __init__(self, **kwargs):
127 super(PlayListTimes, self).__init__(**kwargs)
128 self.times = []
129
130 def on_parent(self, instance, parent):
131 parent.bind(playlist=self.update_playlist_times)
132
133 def update_playlist_times(self, instance, playlist):
4d1dfc89
IB
134 times_length = len(self.times)
135 index = -1
136 for index, [icon, filename, time_info, playing] in enumerate(playlist):
137 if index >= times_length:
138 time_label = PlayListTime(text=time_info)
139 self.add_widget(time_label)
140 self.times.append(time_label)
141 else:
142 self.times[index].text = time_info
143
144 if index+1 < times_length:
145 self.clear_widgets(children=self.times[index+1:times_length])
146 del(self.times[index+1:times_length])
d734981b
IB
147
148class PlayListTime(Label):
149 def __init__(self, text='', **kwargs):
150 super(PlayListTime, self).__init__(**kwargs)
151 self.text = text
152
153 def on_parent(self, instance, parent):
154 if parent is not None:
4d1dfc89
IB
155 parent.bind(font_size=self.update_font_size)
156 parent.bind(labels_height=self.update_height)
157
158 def update_height(self, instance, height):
159 self.height = height
160
161 def update_font_size(self, instance, font_size):
162 self.font_size = font_size
d734981b 163