|
| 1 | +from trame_server import Server |
| 2 | + |
| 3 | +from trame_slicer.core import SlicerApp |
| 4 | +from trame_slicer.segmentation import SegmentationEffectLogicalOperators |
| 5 | + |
| 6 | +from ...ui import ( |
| 7 | + LogicalOperatorsEffectUI, |
| 8 | + LogicalOperatorsSegmentationMode, |
| 9 | + LogicalOperatorsState, |
| 10 | + SegmentEditorUI, |
| 11 | + SegmentState, |
| 12 | +) |
| 13 | +from .base_segmentation_logic import BaseEffectLogic |
| 14 | + |
| 15 | + |
| 16 | +class LogicalOperatorsEffectLogic(BaseEffectLogic[LogicalOperatorsState, SegmentationEffectLogicalOperators]): |
| 17 | + def __init__(self, server: Server, slicer_app: SlicerApp): |
| 18 | + super().__init__(server, slicer_app, LogicalOperatorsState, SegmentationEffectLogicalOperators) |
| 19 | + self.segmentation_editor.active_segment_id_changed.connect(self._update_available_segments) |
| 20 | + |
| 21 | + def set_ui(self, ui: SegmentEditorUI): |
| 22 | + self.set_effect_ui(ui.get_effect_ui(SegmentationEffectLogicalOperators)) |
| 23 | + |
| 24 | + def set_effect_ui(self, logical_operators_ui: LogicalOperatorsEffectUI): |
| 25 | + logical_operators_ui.apply_clicked.connect(self._on_apply_clicked) |
| 26 | + |
| 27 | + def _update_available_segments(self, *_args, **_kwargs): |
| 28 | + self.data.available_segments = [ |
| 29 | + SegmentState( |
| 30 | + name=segment_properties.name, |
| 31 | + color=segment_properties.color_hex, |
| 32 | + segment_id=segment_id, |
| 33 | + is_visible=self.segmentation_editor.get_segment_visibility(segment_id), |
| 34 | + ) |
| 35 | + for segment_id, segment_properties in self.segmentation_editor.get_all_segment_properties().items() |
| 36 | + ] |
| 37 | + if self.data.reference_segment_id not in [segment.segment_id for segment in self.data.available_segments]: |
| 38 | + self.data.reference_segment_id = None |
| 39 | + |
| 40 | + def _on_apply_clicked(self): |
| 41 | + if not self.is_active(): |
| 42 | + return |
| 43 | + |
| 44 | + match self.data.logical_operator: |
| 45 | + case LogicalOperatorsSegmentationMode.ADD: |
| 46 | + self.effect.add(self.data.reference_segment_id) |
| 47 | + case LogicalOperatorsSegmentationMode.COPY: |
| 48 | + self.effect.copy(self.data.reference_segment_id) |
| 49 | + case LogicalOperatorsSegmentationMode.SUBTRACT: |
| 50 | + self.effect.subtract(self.data.reference_segment_id) |
| 51 | + case LogicalOperatorsSegmentationMode.INTERSECT: |
| 52 | + self.effect.intersect(self.data.reference_segment_id) |
| 53 | + case LogicalOperatorsSegmentationMode.INVERT: |
| 54 | + self.effect.invert() |
| 55 | + case LogicalOperatorsSegmentationMode.CLEAR: |
| 56 | + self.effect.clear() |
| 57 | + case LogicalOperatorsSegmentationMode.FILL: |
| 58 | + self.effect.fill() |
0 commit comments