Skip to content

Commit

Permalink
Layers now represented as horizontal buttons
Browse files Browse the repository at this point in the history
Color change and multiselect working

Move logic to LayerSelect
  • Loading branch information
javerbukh committed Nov 14, 2023
1 parent cfe7e25 commit bf22850
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
1 change: 1 addition & 0 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def to_unit(self, data, cid, values, original_units, target_units):
'plugin-subset-select': 'components/plugin_subset_select.vue',
'plugin-viewer-select': 'components/plugin_viewer_select.vue',
'plugin-layer-select': 'components/plugin_layer_select.vue',
'plugin-layer-select-tabs': 'components/plugin_layer_select_tabs.vue',
'plugin-editable-select': 'components/plugin_editable_select.vue',
'plugin-add-results': 'components/plugin_add_results.vue',
'plugin-auto-label': 'components/plugin_auto_label.vue',
Expand Down
20 changes: 20 additions & 0 deletions jdaviz/components/plugin_layer_select_tabs.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<template>
<div>
<v-row>
<v-col v-for="(item, index) in items">
<v-btn :color="!item.mixed_color ? item.color : 'gray'" @click="() => {if (!multiselect){$emit('update:selected', item.label)} else if(selected.indexOf(item.label) === -1) {$emit('update:selected', selected.concat(item.label))} else {$emit('update:selected', selected.filter(select => select != item.label))} }">{{ item.icon }}</v-btn>


</v-col>
</v-row>
<v-row>
<span>{{ selected }}</span>
</v-row>
</div>
</template>

<script>
module.exports = {
props: ['items', 'selected', 'multiselect']
};
</script>
28 changes: 27 additions & 1 deletion jdaviz/configs/default/plugins/plot_options/plot_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.viewer = ViewerSelect(self, 'viewer_items', 'viewer_selected', 'multiselect')
self.layer = LayerSelect(self, 'layer_items', 'layer_selected', 'viewer_selected', 'multiselect') # noqa

self.swatches_palette = [
['#FF0000', '#AA0000', '#550000'],
['#FFD300', '#AAAA00', '#555500'],
Expand Down Expand Up @@ -929,3 +928,30 @@ def _viewer_is_image_viewer(self):

return isinstance(self.viewer.selected_obj, (ImvizImageView, CubevizImageView,
MosvizImageView, MosvizProfile2DView))

@observe('line_color_value', 'image_color_value')
def color_change_event(self, event):
# Do not run when Plot Options is being instantiated
if event['old'] is None:
return
layer_names = [x['label'] for x in self.layer_items]

if isinstance(self.layer_selected, list):
layers_select = self.layer_selected
else:
layers_select = [self.layer_selected]
for layer_select in layers_select:
self.layer_items[layer_names.index(layer_select)]['color'] = event['new']
self.send_state('layer_items')

@observe('layer_selected')
def layer_selected_change_event(self, event):
if event is None or event['old'] is None:
return
print("layer", self.layer_items, self.layer.items)

@observe('viewer_selected')
def viewer_selected_change_event(self, event):
if event is None or event['old'] is None:
return
print("viewer", self.layer_items, self.layer.items)
7 changes: 3 additions & 4 deletions jdaviz/configs/default/plugins/plot_options/plot_options.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@

<!-- LAYER OPTIONS -->
<j-plugin-section-header>Layer options</j-plugin-section-header>
<plugin-layer-select
<plugin-layer-select-tabs
:items="layer_items"
:selected.sync="layer_selected"
:multiselect="multiselect"
:show_if_single_entry="true"
:label="multiselect ? 'Layers': 'Layer'"
:hint="multiselect ? 'Select layers to set options simultaneously' : 'Select the data or subset to set options.'"
label="Layers"
hint="Select the data or subset to set options."
/>

<j-plugin-section-header v-if="layer_selected.length && (line_visible_sync.in_subscribed_states || subset_visible_sync.in_subscribed_states)">Layer Visibility</j-plugin-section-header>
Expand Down
29 changes: 25 additions & 4 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,10 +1255,12 @@ def _get_viewer(self, viewer):
except TypeError:
return self.app.get_viewer_by_id(viewer)

def _layer_to_dict(self, layer):
def _layer_to_dict(self, layer, label_to_color=None, label_mixed_color=None):
d = {"label": layer.layer.label,
"color": layer.state.color,
"icon": self.app.state.layer_icons.get(layer.layer.label)}
"icon": self.app.state.layer_icons.get(layer.layer.label),
"visible": layer.state.bitmap_visible,
"mixed_color": False if not label_mixed_color else label_mixed_color[layer.layer.label]}
return d

def _on_viewer_changed(self, msg=None):
Expand Down Expand Up @@ -1287,10 +1289,29 @@ def _on_layers_changed(self, msg=None):
# same name in different viewers will be randomly assigned within plot_options
# based on which was found _first.
layer_labels = [layer.layer.label for layer in layers]
print("LayerSelect", layer_labels)
print("unique return", np.unique(layer_labels, return_index=True))
_, inds = np.unique(layer_labels, return_index=True)
layers = [layers[i] for i in inds]
layers_unique = [layers[i] for i in inds]
label_to_color = {}
label_mixed_color = {}
for layer in layers:
label = layer.layer.label
color = layer.state.color
# label_to_color tracks all colors per layer label
if label not in label_to_color:
label_to_color[label] = [color]
label_mixed_color[label] = False
else:
# If the color is not yet present, then layers
# with this label have mixed color
if color not in label_to_color[label]:
label_mixed_color[label] = True
label_to_color[label] += [color]

self.items = manual_items + [self._layer_to_dict(layer, label_to_color, label_mixed_color)
for layer in layers_unique]

self.items = manual_items + [self._layer_to_dict(layer) for layer in layers]
self._apply_default_selection()

@cached_property
Expand Down

0 comments on commit bf22850

Please sign in to comment.