]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/mapping.py
Merge branch 'load_action'
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / mapping.py
index 9e40d401852438e410f662cbf6544f70781e5b99..a526ad2244485174fb03a97a1a3491dc5d184cd3 100644 (file)
@@ -22,8 +22,7 @@ class Mapping(RelativeLayout):
         'configuring',
         'configured',
         'loading',
-        'loaded',
-        'failed'
+        'loaded'
     ]
 
     TRANSITIONS = [
@@ -32,11 +31,6 @@ class Mapping(RelativeLayout):
             'source': 'initial',
             'dest': 'configuring'
         },
-        {
-            'trigger': 'fail',
-            'source': 'configuring',
-            'dest': 'failed'
-        },
         {
             'trigger': 'success',
             'source': 'configuring',
@@ -48,11 +42,6 @@ class Mapping(RelativeLayout):
             'source': 'configured',
             'dest': 'loading'
         },
-        {
-            'trigger': 'fail',
-            'source': 'loading',
-            'dest': 'failed'
-        },
         {
             'trigger': 'success',
             'source': 'loading',
@@ -74,17 +63,18 @@ class Mapping(RelativeLayout):
         self.running = []
         self.wait_ids = {}
         self.open_files = {}
+        self.is_leaving_application = False
 
         Machine(model=self, states=self.STATES,
                 transitions=self.TRANSITIONS, initial='initial',
-                ignore_invalid_triggers=True, queued=True)
+                auto_transitions=False, queued=True)
         super(Mapping, self).__init__(**kwargs)
         self.keyboard = Window.request_keyboard(self.on_keyboard_closed, self)
         self.keyboard.bind(on_key_down=self.on_keyboard_down)
 
-        self.configure()
+        self.configure(initial=True)
 
-    def on_enter_configuring(self):
+    def on_enter_configuring(self, initial=True):
         if Config.builtin_mixing:
             self.mixer = Mixer()
         else:
@@ -94,9 +84,9 @@ class Mapping(RelativeLayout):
             self.key_config, self.open_files = self.parse_config()
         except Exception as e:
             error_print("Error while loading configuration: {}".format(e),
-                    with_trace=True, exit=True)
-        else:
-            self.success()
+                    with_trace=False, exit=initial)
+
+        self.success()
 
     def on_enter_loading(self):
         for key in self.keys:
@@ -127,13 +117,14 @@ class Mapping(RelativeLayout):
         elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
             self.leave_application()
             sys.exit()
-        elif 'ctrl' in modifiers and keycode[0] == 114:
-            threading.Thread(name="MSReload", target=self.reload).start()
+        elif 'ctrl' in modifiers and keycode[0] == 114 and self.is_loaded():
+            self.reload(initial=False)
         return True
 
     def leave_application(self):
         self.keyboard.unbind(on_key_down=self.on_keyboard_down)
         self.stop_all_running()
+        self.is_leaving_application = True
         for music in self.open_files.values():
             music.stop()
         for thread in threading.enumerate():
@@ -167,13 +158,20 @@ class Mapping(RelativeLayout):
 
     # Callbacks
     def key_loaded_callback(self):
+        if hasattr(self, 'finished_loading'):
+            return
+
+        opacity = int(Config.load_all_musics)
+
         result = self.all_keys_ready()
         if result == "success":
-            self.ready_color = [0, 1, 0, 1]
+            self.ready_color = [0, 1, 0, opacity]
+            self.finished_loading = True
         elif result == "partial":
-            self.ready_color = [1, 0, 0, 1]
+            self.ready_color = [1, 0, 0, opacity]
+            self.finished_loading = True
         else:
-            self.ready_color = [1, 165/255, 0, 1]
+            self.ready_color = [1, 165/255, 0, opacity]
 
     ## Some global actions
     def stop_all_running(self, except_key=None, key_start_time=0):
@@ -350,13 +348,11 @@ class Mapping(RelativeLayout):
         try:
             config = yaml.safe_load(stream)
         except Exception as e:
-            error_print("Error while loading config file: {}".format(e),
-                    exit=True)
+            raise Exception("Error while loading config file: {}".format(e)) from e
         stream.close()
 
         if not isinstance(config, dict):
-            error_print("Top level config is supposed to be a hash",
-                    exit=True)
+            raise Exception("Top level config is supposed to be a hash")
 
         if 'aliases' in config and isinstance(config['aliases'], dict):
             aliases = config['aliases']