Skip to content

Commit 2001ea4

Browse files
committed
set background label transparency depending on 3D rendering and contour mode
1 parent 3e4ef78 commit 2001ea4

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/motile_tracker/data_views/views/layers/track_labels.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ def __init__(
123123
# Connect click events to node selection
124124
def click(self, _, event):
125125
if (
126-
event.type == "mouse_press"
127-
and self.mode == "pan_zoom"
128-
and not (
129-
self.tracks_viewer.mode == "lineage" and self.viewer.dims.ndisplay == 3
130-
)
126+
event.type == "mouse_press" and self.mode == "pan_zoom"
131127
): # disable selecting in lineage mode in 3D
132128
# differentiate between click and drag
133129
was_click = yield from detect_click(event)
@@ -315,7 +311,7 @@ def update_label_colormap(self, visible: list[int] | str) -> None:
315311

316312
highlighted = set(self.tracks_viewer.selected_nodes)
317313
foreground = self.colormap.color_dict.keys() if visible == "all" else visible
318-
background = (
314+
self.background = (
319315
[]
320316
if visible == "all"
321317
else self.colormap.color_dict.keys() - visible - highlighted
@@ -328,7 +324,13 @@ def update_label_colormap(self, visible: list[int] | str) -> None:
328324
if not self.foreground_contour:
329325
self.filled_labels.extend(foreground)
330326

331-
self.set_opacity(background, self.background_opacity)
327+
# special case: 3D rendering + partially filled contours -> set background opacity
328+
# to 0
329+
if self._slice.slice_input.ndisplay == 3 and self.contour > 0:
330+
self.set_opacity(self.background, 0)
331+
else:
332+
# set normal background opacity
333+
self.set_opacity(self.background, self.background_opacity)
332334
self.set_opacity(foreground, self.foreground_opacity)
333335
self.set_opacity(highlighted, self.highlight_opacity)
334336
self.refresh_colormap()

src/motile_tracker/data_views/views/ortho_views.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ def get_property_names_from_class(layer_cls):
100100
- {"mode", "size", "current_size"},
101101
},
102102
TrackLabels: {
103+
"forward_exclude": {"colormap"},
103104
"reverse_exclude": set(get_property_names_from_class(Labels))
104105
- {
105106
"mode",
106107
"selected_label",
107108
"n_edit_dimensions",
108109
"brush_size",
109-
} # Let TrackLabels handle these properties on its own because it is listening to
110+
}, # Let TrackLabels handle these properties on its own because it is listening to
110111
# them and we do not want to overwrite through reverse syncing.
111112
},
112113
}
@@ -217,6 +218,40 @@ def paint_wrapper(event: Event):
217218
copied_layer.events.paint.connect(paint_wrapper)
218219

219220

221+
def colormap_hook(orig_layer: TrackLabels, copied_layer: Labels) -> None:
222+
"""Hook to sync colormap changes from the original TrackLabels layer to the copied
223+
layers. We need a hook for the special case in which one of the views is showing a 3D
224+
rendering in combination with partially filled contour labels. Since contours are not
225+
rendered in 3D, we want to display the non-filled labels with full opacity instead.
226+
227+
Args:
228+
orig_layer (TrackLabels): TracksLabels layer from which the copied layer is
229+
derived.
230+
copied_layer (ContourLabels): ContourLabels equivalent of the TracksLabels layer.
231+
"""
232+
233+
def sync_colormap(orig_layer: TrackLabels, copied_layer: Labels, event: Event):
234+
"""Sync the colormap from the original TrackLabels instance to the copied
235+
ContourLabels instance. Check the slice ndisplay and contour settings to adjust
236+
background opacity accordingly."""
237+
238+
copied_layer.colormap = orig_layer.colormap
239+
if copied_layer._slice.slice_input.ndisplay == 3 and orig_layer.contour > 0:
240+
copied_layer.set_opacity(orig_layer.background, 0)
241+
else:
242+
copied_layer.set_opacity(
243+
orig_layer.background, orig_layer.background_opacity
244+
)
245+
copied_layer.refresh_colormap()
246+
247+
def update_colormap_wrapper(event: Event):
248+
"""Wrap paint event and send to original layer."""
249+
250+
return sync_colormap(orig_layer, copied_layer, event)
251+
252+
orig_layer.events.colormap.connect(update_colormap_wrapper)
253+
254+
220255
def track_layers_hook(
221256
orig_layer: TrackLabels | TrackPoints, copied_layer: Labels | Points
222257
) -> None:
@@ -265,6 +300,7 @@ def initialize_ortho_views(viewer: Viewer) -> OrthoViewManager:
265300
orth_view_manager.register_layer_hook((TrackLabels, TrackPoints), track_layers_hook)
266301
orth_view_manager.register_layer_hook((TrackLabels), paint_event_hook)
267302
orth_view_manager.register_layer_hook((TrackPoints), point_data_hook)
303+
orth_view_manager.register_layer_hook((TrackLabels), colormap_hook)
268304
orth_view_manager.set_sync_filters(sync_filters)
269305
orth_view_manager.activate_checkboxes = True
270306

0 commit comments

Comments
 (0)