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