]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - music_sampler/app_blocks/actionlist.py
Lock widget modifications and reuse labels in actionlist/playlist
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / actionlist.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, StringProperty
5 from ..lock import Lock
6
7 __all__ = ["ActionList",
8 "ActionListIcons", "ActionListIcon",
9 "ActionListDescriptions", "ActionListDescription"]
10
11 actionlist_lock = Lock("playlist")
12
13 class ActionList(RelativeLayout):
14 action_title = StringProperty("")
15 action_list = ListProperty([])
16
17 def update_list(self, key, action_descriptions):
18 self.action_title = "actions linked to key {}:".format(key.key_sym)
19 action_list = []
20
21 for [action, status] in action_descriptions:
22 if status == "done":
23 icon = "✓"
24 elif status == "current":
25 icon = "✅"
26 else:
27 icon = " "
28 action_list.append([icon, action])
29 with actionlist_lock:
30 self.action_list = action_list
31
32 class ActionListIcons(StackLayout):
33 def __init__(self, **kwargs):
34 super(ActionListIcons, self).__init__(**kwargs)
35 self.icons = []
36
37 def on_parent(self, instance, parent):
38 parent.bind(action_list=self.update_actionlist_icons)
39
40 def update_actionlist_icons(self, instance, actionlist):
41 icons_length = len(self.icons)
42 index = -1
43 for index, [icon, description] in enumerate(actionlist):
44 if index >= icons_length:
45 icon_label = ActionListIcon(text=icon)
46 self.add_widget(icon_label)
47 self.icons.append(icon_label)
48 else:
49 self.icons[index].text = icon
50
51 if index+1 < icons_length:
52 self.clear_widgets(children=self.icons[index+1:icons_length])
53 del(self.icons[index+1:icons_length])
54
55 class ActionListIcon(Label):
56 def __init__(self, text='', **kwargs):
57 super(ActionListIcon, self).__init__(**kwargs)
58 self.text = text
59
60 def on_parent(self, instance, parent):
61 if parent is not None:
62 parent.bind(font_size=self.update_font_size)
63 parent.bind(labels_height=self.update_height)
64
65 def update_height(self, instance, height):
66 self.height = height
67
68 def update_font_size(self, instance, font_size):
69 self.font_size = font_size
70
71 class ActionListDescriptions(StackLayout):
72 def __init__(self, **kwargs):
73 super(ActionListDescriptions, self).__init__(**kwargs)
74 self.descriptions = []
75
76 def on_parent(self, instance, parent):
77 parent.bind(action_list=self.update_actionlist_descriptions)
78
79 def update_actionlist_descriptions(self, instance, actionlist):
80 descriptions_length = len(self.descriptions)
81 index = -1
82 for index, [icon, description] in enumerate(actionlist):
83 if index >= descriptions_length:
84 description_label = ActionListDescription(text=description)
85 self.add_widget(description_label)
86 self.descriptions.append(description_label)
87 else:
88 self.descriptions[index].text = description
89
90 if index+1 < descriptions_length:
91 self.clear_widgets(
92 children=self.descriptions[index+1:descriptions_length])
93 del(self.descriptions[index+1:descriptions_length])
94
95 class ActionListDescription(Label):
96 def __init__(self, text='', **kwargs):
97 super(ActionListDescription, self).__init__(**kwargs)
98 self.text = text
99
100 def on_parent(self, instance, parent):
101 if parent is not None:
102 parent.bind(font_size=self.update_font_size)
103 parent.bind(labels_height=self.update_height)
104
105 def update_height(self, instance, height):
106 self.height = height
107
108 def update_font_size(self, instance, font_size):
109 self.font_size = font_size
110