]>
Commit | Line | Data |
---|---|---|
4b2d79ca | 1 | from kivy.uix.widget import Widget |
2e404903 IB |
2 | from kivy.properties import AliasProperty, BooleanProperty, \ |
3 | ListProperty, StringProperty | |
4b2d79ca IB |
4 | from kivy.uix.behaviors import ButtonBehavior |
5 | ||
e55b29bb | 6 | from .action import Action |
6ebe6247 | 7 | from .helpers import debug_print |
be27763f | 8 | import time |
34382290 | 9 | import threading |
e55b29bb | 10 | from transitions.extensions import HierarchicalMachine as Machine |
4b2d79ca | 11 | |
ba219325 | 12 | class KeyMachine(Widget): |
e55b29bb IB |
13 | STATES = [ |
14 | 'initial', | |
15 | 'configuring', | |
16 | 'configured', | |
17 | 'loading', | |
18 | 'failed', | |
19 | { | |
20 | 'name': 'loaded', | |
34382290 IB |
21 | 'children': [ |
22 | 'no_config', | |
23 | 'no_actions', | |
24 | 'running', | |
25 | 'protecting_repeat' | |
26 | ] | |
e55b29bb IB |
27 | } |
28 | ] | |
29 | ||
30 | TRANSITIONS = [ | |
31 | { | |
32 | 'trigger': 'configure', | |
33 | 'source': 'initial', | |
34 | 'dest': 'configuring' | |
35 | }, | |
36 | { | |
37 | 'trigger': 'fail', | |
38 | 'source': 'configuring', | |
8ba7d831 IB |
39 | 'dest': 'failed', |
40 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
41 | }, |
42 | { | |
43 | 'trigger': 'success', | |
44 | 'source': 'configuring', | |
45 | 'dest': 'configured', | |
46 | 'after': 'load' | |
47 | }, | |
48 | { | |
49 | 'trigger': 'no_config', | |
50 | 'source': 'configuring', | |
51 | 'dest': 'loaded_no_config', | |
8ba7d831 | 52 | 'after': 'key_loaded_callback' |
e55b29bb IB |
53 | }, |
54 | { | |
55 | 'trigger': 'load', | |
56 | 'source': 'configured', | |
57 | 'dest': 'loading' | |
58 | }, | |
59 | { | |
60 | 'trigger': 'fail', | |
61 | 'source': 'loading', | |
8ba7d831 IB |
62 | 'dest': 'failed', |
63 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
64 | }, |
65 | { | |
66 | 'trigger': 'success', | |
67 | 'source': 'loading', | |
8ba7d831 IB |
68 | 'dest': 'loaded', |
69 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
70 | }, |
71 | { | |
72 | 'trigger': 'no_actions', | |
73 | 'source': 'loading', | |
74 | 'dest': 'loaded_no_actions', | |
8ba7d831 | 75 | 'after': 'key_loaded_callback' |
e55b29bb IB |
76 | }, |
77 | { | |
78 | 'trigger': 'reload', | |
ab47d2a1 | 79 | 'source': ['loaded','failed'], |
8ba7d831 IB |
80 | 'dest': 'configuring', |
81 | 'after': 'key_loaded_callback' | |
e55b29bb IB |
82 | }, |
83 | { | |
84 | 'trigger': 'run', | |
85 | 'source': 'loaded', | |
86 | 'dest': 'loaded_running', | |
b17aed6a | 87 | 'after': ['run_actions', 'finish'], |
6c42e32d IB |
88 | # if a child, like loaded_no_actions, has no transitions, then it |
89 | # is bubbled to the parent, and we don't want that. | |
e55b29bb IB |
90 | 'conditions': ['is_loaded'] |
91 | }, | |
92 | { | |
93 | 'trigger': 'finish', | |
94 | 'source': 'loaded_running', | |
34382290 IB |
95 | 'dest': 'loaded_protecting_repeat' |
96 | }, | |
97 | { | |
98 | 'trigger': 'repeat_protection_finished', | |
99 | 'source': 'loaded_protecting_repeat', | |
e55b29bb | 100 | 'dest': 'loaded' |
34382290 | 101 | }, |
e55b29bb IB |
102 | ] |
103 | ||
ba219325 IB |
104 | state = StringProperty("") |
105 | ||
106 | def __init__(self, key, **kwargs): | |
107 | self.key = key | |
108 | ||
109 | Machine(model=self, states=self.STATES, | |
110 | transitions=self.TRANSITIONS, initial='initial', | |
111 | ignore_invalid_triggers=True, queued=True) | |
112 | super(KeyMachine, self).__init__(**kwargs) | |
113 | ||
114 | # Machine states / events | |
115 | def is_loaded_or_failed(self): | |
116 | return self.is_loaded(allow_substates=True) or self.is_failed() | |
117 | ||
118 | def is_loaded_inactive(self): | |
119 | return self.is_loaded_no_config() or self.is_loaded_no_actions() | |
120 | ||
121 | def on_enter_configuring(self): | |
122 | if self.key.key_sym in self.key.parent.key_config: | |
123 | self.key.config = self.key.parent.key_config[self.key.key_sym] | |
124 | ||
125 | self.key.actions = [] | |
126 | for key_action in self.key.config['actions']: | |
127 | self.key.add_action(key_action[0], **key_action[1]) | |
128 | ||
129 | if 'description' in self.key.config['properties']: | |
130 | self.key.set_description(self.key.config['properties']['description']) | |
131 | else: | |
132 | self.key.unset_description() | |
133 | if 'color' in self.key.config['properties']: | |
134 | self.key.set_color(self.key.config['properties']['color']) | |
135 | else: | |
136 | self.key.unset_color() | |
137 | self.success() | |
138 | else: | |
139 | self.no_config() | |
140 | ||
141 | def on_enter_loading(self): | |
142 | if len(self.key.actions) > 0: | |
143 | for action in self.key.actions: | |
144 | action.load() | |
145 | else: | |
146 | self.no_actions() | |
147 | ||
148 | def run_actions(self, modifiers): | |
149 | self.key.parent.parent.ids['KeyList'].append(self.key.key_sym) | |
150 | debug_print("running actions for {}".format(self.key.key_sym)) | |
151 | start_time = time.time() | |
06ed113d | 152 | self.key.parent.start_running(self.key, start_time) |
ba219325 | 153 | for self.key.current_action in self.key.actions: |
06ed113d | 154 | if self.key.parent.keep_running(self.key, start_time): |
ba219325 IB |
155 | self.key.list_actions() |
156 | self.key.current_action.run(start_time) | |
157 | self.key.list_actions(last_action_finished=True) | |
158 | ||
06ed113d | 159 | self.key.parent.finished_running(self.key, start_time) |
ba219325 IB |
160 | |
161 | def on_enter_loaded_protecting_repeat(self, modifiers): | |
814c30c6 | 162 | if self.key.repeat_delay > 0: |
ba219325 | 163 | self.key.protecting_repeat_timer = threading.Timer( |
814c30c6 | 164 | self.key.repeat_delay, |
ba219325 IB |
165 | self.key.repeat_protection_finished) |
166 | self.key.protecting_repeat_timer.start() | |
167 | else: | |
168 | self.key.repeat_protection_finished() | |
169 | ||
170 | # Callbacks | |
171 | def key_loaded_callback(self): | |
172 | self.key.parent.key_loaded_callback() | |
173 | ||
174 | ||
175 | class Key(ButtonBehavior, Widget): | |
176 | ||
4b2d79ca | 177 | key_sym = StringProperty(None) |
e55b29bb | 178 | custom_color = ListProperty([0, 1, 0]) |
4b2d79ca IB |
179 | description_title = StringProperty("") |
180 | description = ListProperty([]) | |
ba219325 | 181 | machine_state = StringProperty("") |
4b2d79ca | 182 | |
635dea02 | 183 | def get_alias_line_cross_color(self): |
ea97edb3 IB |
184 | if not self.is_failed() and ( |
185 | not self.is_loaded(allow_substates=True)\ | |
186 | or self.is_loaded_running()\ | |
187 | or self.is_loaded_protecting_repeat()): | |
635dea02 IB |
188 | return [120/255, 120/255, 120/255, 1] |
189 | else: | |
190 | return [0, 0, 0, 0] | |
191 | ||
192 | def set_alias_line_cross_color(self): | |
193 | pass | |
194 | ||
195 | line_cross_color = AliasProperty( | |
196 | get_alias_line_cross_color, | |
197 | set_alias_line_cross_color, | |
ba219325 | 198 | bind=['machine_state']) |
635dea02 | 199 | |
1094ab1a IB |
200 | def get_alias_line_color(self): |
201 | if self.is_loaded_running(): | |
202 | return [0, 0, 0, 1] | |
203 | else: | |
204 | return [120/255, 120/255, 120/255, 1] | |
205 | ||
206 | def set_alias_line_color(self): | |
207 | pass | |
208 | ||
209 | line_color = AliasProperty(get_alias_line_color, set_alias_line_color, | |
ba219325 | 210 | bind=['machine_state']) |
1094ab1a | 211 | |
e55b29bb IB |
212 | def get_alias_color(self): |
213 | if self.is_loaded_inactive(): | |
4b2d79ca | 214 | return [1, 1, 1, 1] |
34382290 IB |
215 | elif self.is_loaded_protecting_repeat(): |
216 | return [*self.custom_color, 100/255] | |
70cfb266 IB |
217 | elif self.is_loaded_running(): |
218 | return [*self.custom_color, 100/255] | |
e55b29bb IB |
219 | elif self.is_loaded(allow_substates=True): |
220 | return [*self.custom_color, 1] | |
221 | elif self.is_failed(): | |
222 | return [0, 0, 0, 1] | |
be27763f | 223 | else: |
e55b29bb IB |
224 | return [*self.custom_color, 100/255] |
225 | def set_alias_color(self): | |
4b2d79ca IB |
226 | pass |
227 | ||
e55b29bb | 228 | color = AliasProperty(get_alias_color, set_alias_color, |
ba219325 IB |
229 | bind=['machine_state', 'custom_color']) |
230 | ||
231 | def __getattr__(self, name): | |
232 | if hasattr(self.machine, name): | |
233 | return getattr(self.machine, name) | |
234 | else: | |
235 | raise AttributeError | |
236 | ||
237 | def machine_state_changed(self, instance, machine_state): | |
238 | self.machine_state = self.machine.state | |
be27763f | 239 | |
4b2d79ca | 240 | def __init__(self, **kwargs): |
be27763f | 241 | self.actions = [] |
b17aed6a | 242 | self.current_action = None |
ba219325 IB |
243 | self.machine = KeyMachine(self) |
244 | self.machine.bind(state=self.machine_state_changed) | |
b17aed6a | 245 | |
e55b29bb | 246 | super(Key, self).__init__(**kwargs) |
be27763f | 247 | |
e55b29bb | 248 | # Kivy events |
4b2d79ca | 249 | def on_key_sym(self, key, key_sym): |
e55b29bb IB |
250 | if key_sym != "": |
251 | self.configure() | |
252 | ||
253 | def on_press(self): | |
254 | self.list_actions() | |
4b2d79ca | 255 | |
e55b29bb IB |
256 | # This one cannot be in the Machine state since it would be queued to run |
257 | # *after* the loop is ended... | |
258 | def interrupt(self): | |
259 | self.current_action.interrupt() | |
260 | ||
261 | # Callbacks | |
262 | def callback_action_ready(self, action, success): | |
263 | if not success: | |
264 | self.fail() | |
265 | elif all(action.is_loaded_or_failed() for action in self.actions): | |
266 | self.success() | |
267 | ||
268 | # Setters | |
b86db9f1 | 269 | def set_description(self, description): |
4b2d79ca IB |
270 | if description[0] is not None: |
271 | self.description_title = str(description[0]) | |
ab47d2a1 | 272 | self.description = [] |
2e404903 | 273 | for desc in description[1 :]: |
d479af33 IB |
274 | if desc is None: |
275 | self.description.append("") | |
276 | else: | |
4b2d79ca | 277 | self.description.append(str(desc).replace(" ", " ")) |
b86db9f1 | 278 | |
b3e624bb IB |
279 | def unset_description(self): |
280 | self.description_title = "" | |
281 | self.description = [] | |
282 | ||
b86db9f1 | 283 | def set_color(self, color): |
4b2d79ca | 284 | color = [x / 255 for x in color] |
4b2d79ca | 285 | self.custom_color = color |
be27763f | 286 | |
b3e624bb IB |
287 | def unset_color(self): |
288 | self.custom_color = [0, 1, 0] | |
289 | ||
814c30c6 IB |
290 | # Helpers |
291 | @property | |
292 | def repeat_delay(self): | |
293 | if 'repeat_delay' in self.key.config['properties']: | |
294 | return self.key.config['properties']['repeat_delay'] | |
295 | else: | |
296 | return 0 | |
297 | ||
e55b29bb | 298 | # Actions handling |
be27763f IB |
299 | def add_action(self, action_name, **arguments): |
300 | self.actions.append(Action(action_name, self, **arguments)) | |
301 | ||
b17aed6a IB |
302 | def list_actions(self, last_action_finished=False): |
303 | not_running = (not self.is_loaded_running()) | |
304 | current_action_seen = False | |
305 | action_descriptions = [] | |
306 | for action in self.actions: | |
307 | if not_running: | |
308 | state = "inactive" | |
309 | elif last_action_finished: | |
310 | state = "done" | |
311 | elif current_action_seen: | |
312 | state = "pending" | |
313 | elif action == self.current_action: | |
314 | current_action_seen = True | |
315 | state = "current" | |
316 | else: | |
317 | state = "done" | |
318 | action_descriptions.append([action.description(), state]) | |
319 | self.parent.parent.ids['ActionList'].update_list( | |
320 | self, | |
321 | action_descriptions) |