All files / tools/src/stateManagement/segmentation/config styleHelpers.ts

68.75% Statements 11/16
75% Branches 3/4
50% Functions 4/8
68.75% Lines 11/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                                                                                                                    212x     212x 212x 212x 424x       212x                               4x         4x     4x   4x 8x                                                                                            
import type SegmentationRepresentations from '../../../enums/SegmentationRepresentations';
import type { ContourStyle } from '../../../types/ContourTypes';
import type { LabelmapStyle } from '../../../types/LabelmapTypes';
import type { SurfaceStyle } from '../../../types/SurfaceTypes';
import { getSegmentations } from '../getSegmentations';
import { getViewportSegmentations } from '../getViewportSegmentations';
import { triggerSegmentationRender } from '../SegmentationRenderingEngine';
import { segmentationStyle } from '../SegmentationStyle';
import type { RepresentationStyle } from '../SegmentationStyle';
import { triggerSegmentationRepresentationModified } from '../triggerSegmentationEvents';
 
type BaseSpecifier = {
  viewportId?: string;
  segmentationId?: string;
  segmentIndex?: number;
};
 
type SpecifierWithType<T extends SegmentationRepresentations> =
  BaseSpecifier & {
    type: T;
  };
 
type StyleForType<T extends SegmentationRepresentations> =
  T extends SegmentationRepresentations.Labelmap
    ? LabelmapStyle
    : T extends SegmentationRepresentations.Contour
      ? ContourStyle
      : T extends SegmentationRepresentations.Surface
        ? SurfaceStyle
        : never;
 
/**
 * Get the style for a given segmentation representation.
 * @param specifier The specifier object containing the viewportId, segmentationId, type, and segmentIndex.
 * @returns The style for the given segmentation representation.
 */
function getStyle<T extends SegmentationRepresentations>(
  specifier: SpecifierWithType<T>
): StyleForType<T>;
function getStyle(
  specifier: BaseSpecifier & { type?: SegmentationRepresentations }
): RepresentationStyle {
  return segmentationStyle.getStyle(specifier);
}
 
/**
 * Set the style for a given segmentation representation.
 * @param specifier The specifier object containing the viewportId, segmentationId, type, and segmentIndex.
 * @param style The style to set for the given segmentation representation.
 */
function setStyle<T extends SegmentationRepresentations>(
  specifier: SpecifierWithType<T>,
  style: StyleForType<T>
): void;
function setStyle(
  specifier: BaseSpecifier & { type: SegmentationRepresentations },
  style: RepresentationStyle
): void {
  segmentationStyle.setStyle(specifier, style);
 
  // if only type is provided, we need to trigger a render for all segmentations in the viewport
  Eif (!specifier.viewportId && !specifier.segmentationId) {
    const segmentations = getSegmentations();
    segmentations.forEach((segmentation) => {
      triggerSegmentationRender(segmentation.segmentationId);
    });
  }
 
  triggerSegmentationRepresentationModified(
    specifier.viewportId,
    specifier.segmentationId,
    specifier.type
  );
}
 
/**
 * Set the renderInactiveSegmentations flag for a specific viewport.
 * @param viewportId The ID of the viewport.
 * @param renderInactiveSegmentations Whether to render inactive segmentations.
 */
function setRenderInactiveSegmentations(
  viewportId: string,
  renderInactiveSegmentations: boolean
): void {
  segmentationStyle.setRenderInactiveSegmentations(
    viewportId,
    renderInactiveSegmentations
  );
 
  triggerSegmentationRender(viewportId);
 
  // get all the segmentations for the viewport
  const segmentations = getViewportSegmentations(viewportId);
 
  segmentations.forEach((segmentation) => {
    triggerSegmentationRepresentationModified(
      viewportId,
      segmentation.segmentationId
    );
  });
}
 
/**
 * Get the renderInactiveSegmentations flag for a specific viewport.
 * @param viewportId The ID of the viewport.
 * @returns Whether to render inactive segmentations.
 */
function getRenderInactiveSegmentations(viewportId: string): boolean {
  return segmentationStyle.getRenderInactiveSegmentations(viewportId);
}
 
/**
 * Reset the segmentation style to the global style.
 */
function resetToGlobalStyle(): void {
  segmentationStyle.resetToGlobalStyle();
  triggerSegmentationRender();
}
 
/**
 * Checks if there is a non-global style for a given specifier.
 * @param specifier - The specifier object containing viewportId, segmentationId, type, and segmentIndex.
 * @returns True if there is a non-global style, false otherwise.
 */
function hasCustomStyle(specifier: {
  viewportId?: string;
  segmentationId?: string;
  type?: SegmentationRepresentations;
  segmentIndex?: number;
}): boolean {
  return segmentationStyle.hasCustomStyle(specifier);
}
 
export {
  getStyle,
  setStyle,
  setRenderInactiveSegmentations,
  getRenderInactiveSegmentations,
  resetToGlobalStyle,
  hasCustomStyle,
};