diff --git a/jdaviz/app.py b/jdaviz/app.py index 63ab909ace..e4bee15ee2 100644 --- a/jdaviz/app.py +++ b/jdaviz/app.py @@ -2286,17 +2286,24 @@ def _on_new_viewer(self, msg, vid=None, name=None, add_layers_to_viewer=False): msg.cls, data=msg.data, show=False) viewer.figure_widget.layout.height = '100%' + linked_by_wcs = self._link_type.lower() == 'wcs' + if hasattr(viewer.state, 'linked_by_wcs'): orientation_plugin = self._jdaviz_helper.plugins.get('Orientation', None) if orientation_plugin is not None: - viewer.state.linked_by_wcs = orientation_plugin.link_type.selected == 'WCS' + linked_by_wcs = orientation_plugin.link_type.selected == 'WCS' elif len(self._viewer_store): # The plugin would only not exist for instances of Imviz where the user has # intentionally removed the Orientation plugin, but in that case we will # adopt "linked_by_wcs" from the first (assuming all are the same) # NOTE: deleting the default viewer is forbidden both by API and UI, but if # for some reason that was the case here, linked_by_wcs will default to False - viewer.state.linked_by_wcs = list(self._viewer_store.values())[0].state.linked_by_wcs # noqa + linked_by_wcs = list(self._viewer_store.values())[0].state.linked_by_wcs # noqa + viewer.state.linked_by_wcs = linked_by_wcs + + if linked_by_wcs: + from jdaviz.configs.imviz.helper import get_wcs_only_layer_labels + viewer.state.wcs_only_layers = get_wcs_only_layer_labels(self) if msg.x_attr is not None: x = msg.data.id[msg.x_attr] @@ -2309,9 +2316,8 @@ def _on_new_viewer(self, msg, vid=None, name=None, add_layers_to_viewer=False): viewer=viewer, vid=vid, name=name, reference=name ) - if add_layers_to_viewer: - ref_data = self._jdaviz_helper.default_viewer._obj.state.reference_data - new_viewer_item['reference_data_label'] = ref_data.label + ref_data = self._jdaviz_helper.default_viewer._obj.state.reference_data + new_viewer_item['reference_data_label'] = ref_data.label new_stack_item = self._create_stack_item( container='gl-stack', diff --git a/jdaviz/configs/imviz/plugins/orientation/orientation.py b/jdaviz/configs/imviz/plugins/orientation/orientation.py index 0b858b48af..1b48034f16 100644 --- a/jdaviz/configs/imviz/plugins/orientation/orientation.py +++ b/jdaviz/configs/imviz/plugins/orientation/orientation.py @@ -7,14 +7,14 @@ from glue.core.subset_group import GroupedSubset import astropy.units as u -from jdaviz.configs.imviz.helper import link_image_data +from jdaviz.configs.imviz.helper import link_image_data, get_wcs_only_layer_labels from jdaviz.configs.imviz.wcs_utils import ( get_compass_info, _get_rotated_nddata_from_label ) from jdaviz.core.events import ( LinkUpdatedMessage, ExitBatchLoadMessage, ChangeRefDataMessage, AstrowidgetMarkersChangedMessage, MarkersPluginUpdate, - SnackbarMessage + SnackbarMessage, AddDataToViewerMessage, NewViewerMessage, ViewerAddedMessage ) from jdaviz.core.custom_traitlets import FloatHandleEmpty from jdaviz.core.registries import tray_registry @@ -127,6 +127,15 @@ def __init__(self, *args, **kwargs): self.hub.subscribe(self, SubsetDeleteMessage, handler=self._on_subset_change) + # self.hub.subscribe(self, NewViewerMessage, + # handler=self._on_new_viewer) + + self.hub.subscribe(self, ViewerAddedMessage, + handler=self._on_viewer_added) + + self.hub.subscribe(self, AddDataToViewerMessage, + handler=self._on_data_add_to_viewer) + self._update_layer_label_default() @property @@ -352,12 +361,36 @@ def add_orientation(self, rotation_angle=None, east_left=None, label=None, def _add_data_to_all_viewers(self, data_label): for viewer_ref in self.viewer.choices: - layers_in_viewer = [ - layer.layer.label for layer in - self.app.get_viewer_by_id(self.viewer.selected).layers - ] - if data_label not in layers_in_viewer: - self.app.add_data_to_viewer(viewer_ref, data_label, visible=False) + layers = [layer.label for layer in self.app.get_viewer_by_id(self.viewer.selected).layers] + if data_label not in layers: + self.app.add_data_to_viewer(viewer_ref, data_label) + + def _on_viewer_added(self, msg): + for data_label in get_wcs_only_layer_labels(self.app): + self._add_data_to_all_viewers(data_label) + + self.orientation.choices = get_wcs_only_layer_labels(self.app) + + if hasattr(self, 'orientation'): + self.viewer.selected = msg._viewer_id + self._change_reference_data( + self.orientation.selected, + viewer_id=msg._viewer_id + ) + + def _on_data_add_to_viewer(self, msg): + if not hasattr(self, 'viewer'): + return + + viewer_id = self.viewer.selected_obj.reference_id + + if viewer_id != 'imviz-0' and self.viewer.selected_obj.state.reference_data is None: + self.app._change_reference_data( + self.orientation.selected, + viewer_id=viewer_id + ) + else: + self.orientation_layer_selected = self.ref_data.label def vue_add_orientation(self, *args, **kwargs): self.add_orientation(set_on_create=True) @@ -423,7 +456,7 @@ def _on_viewer_change(self, msg={}): # don't update choices until viewer is available: if hasattr(self, 'viewer'): viewer = self.app.get_viewer(self.viewer.selected) - self.orientation.choices = viewer.state.wcs_only_layers + self.orientation.choices = get_wcs_only_layer_labels(self.app) self.orientation.selected = self.ref_data.label def create_north_up_east_left(self, label="North-up, East-left", set_on_create=False):