def init_panels(self): self.amp_list_panel = AmpListPanel(self) self.amp_config = AmpConfigPanel(self) self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.select_amplifier, self.amp_list_panel.amp_list) self.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.channel_update)
class AmpLauncherDialog(wx.Dialog): """Main amplifier laucher dialog.""" def __init__(self, parent, retriever_instance=None, data_file_name="psychopy_data"): super(AmpLauncherDialog, self).__init__(parent, size=(760, 640), title="Amp Launcher", style=wx.DEFAULT_DIALOG_STYLE) self.data_file_name = data_file_name self.amp_info = None self.old_index = None self.amp_manager = None self.connection = obci_connection.OBCIConnection(("127.0.0.1", 12012)) self.retriever = retriever_instance or AmpListRetriever( self.connection) self.retriever_thread = threading.Thread(group=None, target=self.init_info, name="retriever-thread") self.init_panels() self.init_sizer() self.init_buttons() self.Bind(retriever.EVT_RETRIEVER_STARTED, self.retriever_started) self.Bind(retriever.EVT_RETRIEVER_FINISHED, self.retriever_finished) self.retriever_thread.start() def init_info(self): wx.PostEvent(self, retriever.RetrieverStartedEvent()) try: amp_info = self.retriever.fetch_amp_list() except Exception as _: #wx.MessageBox("Failed to fetch a list of amplifiers:\n" + str(e), "Amp Launcher", wx.ICON_WARNING) amp_info = retriever.AmplifierInfo() # empty amp list wx.PostEvent(self, retriever.RetrieverFinishedEvent(amp_info=amp_info)) def retriever_started(self, event): self.amp_list_panel.lock_list() def retriever_finished(self, event): self.amp_info = event.amp_info self.amp_list_panel.set_amp_info(event.amp_info) self.amp_list_panel.unlock_list() def init_panels(self): self.amp_list_panel = AmpListPanel(self) self.amp_config = AmpConfigPanel(self) self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.select_amplifier, self.amp_list_panel.amp_list) self.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.channel_update) def init_buttons(self): self.Bind(wx.EVT_BUTTON, self.run_click, id=wx.ID_OK) self.FindWindowById(wx.ID_OK).Disable() def init_sizer(self): sizer = wx.BoxSizer(wx.VERTICAL) flags0 = wx.SizerFlags().Border(wx.ALL, 12) flags1 = wx.SizerFlags().Border(wx.LEFT | wx.RIGHT | wx.BOTTOM, 12) sizer.AddF(self.amp_list_panel, flags0.Proportion(1).Expand()) sizer.AddF(self.amp_config, flags1.Proportion(1).Expand()) sizer.AddF(self.CreateButtonSizer(wx.OK | wx.CANCEL), flags1.Proportion(0)) self.SetSizer(sizer) def channel_update(self, event): if self.amp_config.Validate(): self.FindWindowById(wx.ID_OK).Enable() else: self.FindWindowById(wx.ID_OK).Disable() def select_amplifier(self, event): new_index = event.GetIndex() if new_index != self.old_index: amp_entry = self.amp_info.get_entry(new_index) self.amp_config.select_amplifier(amp_entry) self.amp_config.presets_panel.on_name_select( None) #reload preset if it is active self.old_index = new_index def disable_editing(self): self.amp_list_panel.disable_editing() self.amp_config.Disable() self.launch_button.Disable() def get_persistent_config(self): return { "save_signal": self.GetParent().exp.settings.params['saveSignal'].val, "save_etr": self.GetParent().exp.settings.params['saveEtr'].val, "obci_data_dir": self.GetParent().exp.settings.params['obciDataDirectory'].val } def run_click(self, event): if not self.amp_config.Validate(): return self.amp_manager = self.start_amplifier() self.show_progress_dialog() event.Skip() def show_progress_dialog(self): self.launching_dialog = LaunchingDialog(self) self.launching_dialog.ShowModal() def start_amplifier(self): amp_config_dict = self.amp_config.get_config() amp_config_dict.update(self.get_persistent_config()) amp_config_dict["data_file_name"] = self.data_file_name manager = amp_manager.AmplifierManager(self.start_handler, amp_config_dict) manager.start_experiment() return manager def start_handler(self, status): """ This may be called from outside of wx context. """ wx.PostEvent(self.launching_dialog, ExperimentReady())
class AmpLauncherDialog(wx.Dialog): """Main amplifier laucher dialog.""" def __init__(self, parent, retriever_instance=None, data_file_name="psychopy_data"): super(AmpLauncherDialog, self).__init__( parent, size=(760, 640), title="Amp Launcher", style=wx.DEFAULT_DIALOG_STYLE) self.data_file_name = data_file_name self.amp_info = None self.old_index = None self.amp_manager = None self.connection = obci_connection.OBCIConnection(("127.0.0.1", 12012)) self.retriever = retriever_instance or AmpListRetriever(self.connection) self.retriever_thread = threading.Thread(group=None, target=self.init_info, name="retriever-thread") self.init_panels() self.init_sizer() self.init_buttons() self.Bind(retriever.EVT_RETRIEVER_STARTED, self.retriever_started) self.Bind(retriever.EVT_RETRIEVER_FINISHED, self.retriever_finished) self.retriever_thread.start() def init_info(self): wx.PostEvent(self, retriever.RetrieverStartedEvent()) try: amp_info = self.retriever.fetch_amp_list() except Exception as _: #wx.MessageBox("Failed to fetch a list of amplifiers:\n" + str(e), "Amp Launcher", wx.ICON_WARNING) amp_info = retriever.AmplifierInfo() # empty amp list wx.PostEvent(self, retriever.RetrieverFinishedEvent(amp_info=amp_info)) def retriever_started(self, event): self.amp_list_panel.lock_list() def retriever_finished(self, event): self.amp_info = event.amp_info self.amp_list_panel.set_amp_info(event.amp_info) self.amp_list_panel.unlock_list() def init_panels(self): self.amp_list_panel = AmpListPanel(self) self.amp_config = AmpConfigPanel(self) self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.select_amplifier, self.amp_list_panel.amp_list) self.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.channel_update) def init_buttons(self): self.Bind(wx.EVT_BUTTON, self.run_click, id=wx.ID_OK) self.FindWindowById(wx.ID_OK).Disable() def init_sizer(self): sizer = wx.BoxSizer(wx.VERTICAL) flags0 = wx.SizerFlags().Border(wx.ALL, 12) flags1 = wx.SizerFlags().Border(wx.LEFT | wx.RIGHT | wx.BOTTOM, 12) sizer.AddF(self.amp_list_panel, flags0.Proportion(1).Expand()) sizer.AddF(self.amp_config, flags1.Proportion(1).Expand()) sizer.AddF(self.CreateButtonSizer(wx.OK | wx.CANCEL), flags1.Proportion(0)) self.SetSizer(sizer) def channel_update(self, event): if self.amp_config.Validate(): self.FindWindowById(wx.ID_OK).Enable() else: self.FindWindowById(wx.ID_OK).Disable() def select_amplifier(self, event): new_index = event.GetIndex() if new_index != self.old_index: amp_entry = self.amp_info.get_entry(new_index) self.amp_config.select_amplifier(amp_entry) self.amp_config.presets_panel.on_name_select(None) #reload preset if it is active self.old_index = new_index def disable_editing(self): self.amp_list_panel.disable_editing() self.amp_config.Disable() self.launch_button.Disable() def get_persistent_config(self): return { "save_signal": self.GetParent().exp.settings.params['saveSignal'].val, "obci_data_dir": self.GetParent().exp.settings.params['obciDataDirectory'].val } def run_click(self, event): if not self.amp_config.Validate(): return self.amp_manager = self.start_amplifier() self.show_progress_dialog() event.Skip() def show_progress_dialog(self): self.launching_dialog = LaunchingDialog(self) self.launching_dialog.ShowModal() def start_amplifier(self): amp_config_dict = self.amp_config.get_config() amp_config_dict.update(self.get_persistent_config()) amp_config_dict["data_file_name"] = self.data_file_name manager = amp_manager.AmplifierManager(self.start_handler, amp_config_dict) manager.start_experiment() return manager def start_handler(self, status): """ This may be called from outside of wx context. """ wx.PostEvent(self.launching_dialog, ExperimentReady())