diff --git a/mne_qt_browser/_pg_figure.py b/mne_qt_browser/_pg_figure.py index bc9b77c1..e1789da1 100644 --- a/mne_qt_browser/_pg_figure.py +++ b/mne_qt_browser/_pg_figure.py @@ -42,6 +42,14 @@ from mne.viz import plot_sensors from mne.viz._figure import BrowserBase from mne.viz.backends._utils import _init_mne_qtapp, _qt_raise_window +from mne.viz.ui_events import ( + ChannelBrowse, + TimeBrowse, + TimeChange, + disable_ui_events, + publish, + subscribe, +) from mne.viz.utils import _figure_agg, _merge_annotations, _simplify_float from pyqtgraph import ( AxisItem, @@ -3972,6 +3980,38 @@ def __init__(self, **kwargs): # disable histogram of epoch PTP amplitude del self.mne.keyboard_shortcuts["h"] + # Subscribe to vertical line change + subscribe(self, "time_change", self._on_time_change_event) + + # Subscribe to time browse + subscribe(self, "time_browse", self._on_time_browse_event) + + # Subscribe to channel browse + # self.mne.plt.sigYRangeChanged.connect(self._on_channel_browse_event) + subscribe(self, "channel_browse", self._on_channel_browse_event) + + def _on_time_change_event(self, event): + """Response to TimeChange event from the event-ui system.""" + with disable_ui_events(self): + self._add_vline(event.time) + + def _on_time_browse_event(self, event): + """Response to TimeBrowse event from the event-ui system.""" + with disable_ui_events(self): + self.mne.plt.setXRange(event.time_start, event.time_end, padding=0) + + def _on_channel_browse_event(self, event): + """Response to ChannelBrowse event from the event-ui system.""" + # Get the indices of the subset in the full set of channels + all_channels = self.mne.ch_names[self.mne.ch_order] + ch_indices = [np.where(all_channels == ch)[0][0] for ch in event.channels] + + # Take the start index and set range + with disable_ui_events(self): + start_idx = ch_indices[0] + n_chans = len(ch_indices) + self.mne.plt.setYRange(start_idx, start_idx + n_chans + 1, padding=0) + def _hidpi_mkPen(self, *args, **kwargs): kwargs["width"] = self._pixel_ratio * kwargs.get("width", 1.0) return mkPen(*args, **kwargs) @@ -4249,11 +4289,13 @@ def _add_vline(self, t): self.mne.vline = VLine(self.mne, t, bounds=(0, self.mne.xmax)) self.mne.vline.sigPositionChangeFinished.connect(self._vline_slot) self.mne.plt.addItem(self.mne.vline) + else: self.mne.vline.setPos(t) self.mne.vline_visible = True self.mne.overview_bar.update_vline() + publish(self, TimeChange(time=t)) def _mouse_moved(self, pos): """Show Crosshair if enabled at mouse move.""" @@ -4348,6 +4390,15 @@ def _xrange_changed(self, _, xrange): # Update annotations self._update_regions_visible() + # Publish event + publish( + self, + TimeBrowse( + time_start=self.mne.t_start, + time_end=self.mne.t_start + self.mne.duration, + ), + ) + def _yrange_changed(self, _, yrange): if not self.mne.butterfly: if not self.mne.fig_selection: @@ -4401,6 +4452,9 @@ def _yrange_changed(self, _, yrange): trace.update_color() trace.update_data() + # Publish to event system + publish(self, ChannelBrowse(channels=self.mne.ch_names[self.mne.picks])) + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # DATA HANDLING # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #