Skip to content

Commit

Permalink
Move logic to LayerSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
javerbukh committed Nov 13, 2023
1 parent f4dfab5 commit ddeb60c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
7 changes: 5 additions & 2 deletions jdaviz/components/plugin_layer_select_tabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
<div>
<v-row>
<v-col v-for="(item, index) in items">
<v-btn :color=item.color @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-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', 'button_selected']
props: ['items', 'selected', 'multiselect']
};
</script>
37 changes: 19 additions & 18 deletions 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 @@ -880,24 +879,26 @@ def _viewer_is_image_viewer(self):
@observe('line_color_value', 'image_color_value')
def color_change_event(self, event):
# Do not run when Plot Options is being instantiated
if not event['old']:
if event['old'] is None:
return
# self.layer_items_tabs = self.layer_items
print(event['name'], event['old'], event['new'])
print(event['owner'].layer_items)
print(event.keys())
print(self.layer_items)
print(self.layer_selected)
layer_names = [x['label'] for x in self.layer_items]
print('BEFORE IF')
if len(layer_names) > 0 and self.layer_selected in layer_names:
print("1")
# if hasattr(self.layer_items[layer_names.index(self.layer_selected)], 'image_color_value'):
print(layer_names, self.layer_selected, layer_names.index(self.layer_selected),
layer_names.index(self.layer_selected))
print(self.layer_items[layer_names.index(self.layer_selected)]['color'])
self.layer_items[layer_names.index(self.layer_selected)]['color'] = event['new']
print(self.layer_items[layer_names.index(self.layer_selected)]['color'])
self.send_state('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)
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 ddeb60c

Please sign in to comment.