All files / extensions/cornerstone-dicom-seg/src getToolbarModule.ts

92.85% Statements 26/28
58.82% Branches 10/17
100% Functions 6/6
92.85% Lines 26/28

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            66x 66x                               2781x 2781x               3772x 3772x               8058x   8058x 2746x           5312x   5312x     1544x                           8479x 8479x 3085x           5394x 5394x 52x           5342x   5342x             5342x 791x           4551x   4551x             4551x       4551x                
import { useUIStateStore } from '@ohif/extension-default';
import LogicalContourOperationsOptions from './components/LogicalContourOperationsOptions';
import SimplifyContourOptions from './components/SimplifyContourOptions';
import SmoothContoursOptions from './components/SmoothContoursOptions';
 
export function getToolbarModule({ servicesManager }: withAppTypes) {
  const { segmentationService, toolbarService, toolGroupService } = servicesManager.services;
  return [
    {
      name: 'cornerstone.SimplifyContourOptions',
      defaultComponent: SimplifyContourOptions,
    },
    {
      name: 'cornerstone.LogicalContourOperationsOptions',
      defaultComponent: LogicalContourOperationsOptions,
    },
    {
      name: 'cornerstone.SmoothContoursOptions',
      defaultComponent: SmoothContoursOptions,
    },
    {
      name: 'cornerstone.isActiveSegmentationUtility',
      evaluate: ({ button }) => {
        const { uiState } = useUIStateStore.getState();
        return {
          isActive: uiState[`activeSegmentationUtility`] === button.id,
        };
      },
    },
    {
      name: 'evaluate.cornerstone.hasSegmentation',
      evaluate: ({ viewportId }) => {
        const segmentations = segmentationService.getSegmentationRepresentations(viewportId);
        return {
          disabled: !segmentations?.length,
        };
      },
    },
    {
      name: 'evaluate.cornerstone.hasSegmentationOfType',
      evaluate: ({ viewportId, segmentationRepresentationType }) => {
        const segmentations = segmentationService.getSegmentationRepresentations(viewportId);
 
        if (!segmentations?.length) {
          return {
            disabled: true,
            disabledText: 'No segmentations available',
          };
        }
 
        if (
          !segmentations.some(segmentation =>
            Boolean(segmentation.type === segmentationRepresentationType)
          )
        ) {
          return {
            disabled: true,
            disabledText: `No ${segmentationRepresentationType} segmentations available`,
          };
        }
      },
    },
    {
      name: 'evaluate.cornerstone.segmentation',
      evaluate: ({ viewportId, button, toolNames, disabledText }) => {
        // Todo: we need to pass in the button section Id since we are kind of
        // forcing the button to have black background since initially
        // it is designed for the toolbox not the toolbar on top
        // we should then branch the buttonSectionId to have different styles
        const segmentations = segmentationService.getSegmentationRepresentations(viewportId);
        if (!segmentations?.length) {
          return {
            disabled: true,
            disabledText: disabledText ?? 'No segmentations available',
          };
        }
 
        const activeSegmentation = segmentationService.getActiveSegmentation(viewportId);
        if (!Object.keys(activeSegmentation.segments).length) {
          return {
            disabled: true,
            disabledText: 'Add segment to enable this tool',
          };
        }
 
        const toolGroup = toolGroupService.getToolGroupForViewport(viewportId);
 
        Iif (!toolGroup) {
          return {
            disabled: true,
            disabledText: disabledText ?? 'Not available on the current viewport',
          };
        }
 
        if (!toolNames) {
          return {
            disabled: false,
            // isActive: false,
          };
        }
 
        const toolName = toolbarService.getToolNameForButton(button);
 
        Iif (!toolGroup.hasTool(toolName) && !toolNames) {
          return {
            disabled: true,
            disabledText: disabledText ?? 'Not available on the current viewport',
          };
        }
 
        const isPrimaryActive = toolNames
          ? toolNames.includes(toolGroup.getActivePrimaryMouseButtonTool())
          : toolGroup.getActivePrimaryMouseButtonTool() === toolName;
 
        return {
          disabled: false,
          isActive: isPrimaryActive,
        };
      },
    },
  ];
}