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

94.44% Statements 34/36
63.15% Branches 12/19
100% Functions 7/7
94.44% Lines 34/36

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 143 144                  103x 103x                               4044x 4044x               4258x 4258x               36192x   36192x 10856x           25336x   25336x     14540x                           33992x 33992x 10224x           23768x 23768x 36x           23732x   23732x             23732x 4091x           19641x   19641x             19641x       19641x                 9900x 9900x 36x     9864x 31410x   31410x 21012x 21012x              
import { utilities as cstUtils } from '@cornerstonejs/tools';
import i18n from '@ohif/i18n';
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: i18n.t('SegmentationPanel: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 ?? i18n.t('SegmentationPanel:No segmentations available'),
          };
        }
 
        const activeSegmentation = segmentationService.getActiveSegmentation(viewportId);
        if (!Object.keys(activeSegmentation.segments).length) {
          return {
            disabled: true,
            disabledText: i18n.t('SegmentationPanel:Add segment to enable this tool'),
          };
        }
 
        const toolGroup = toolGroupService.getToolGroupForViewport(viewportId);
 
        Iif (!toolGroup) {
          return {
            disabled: true,
            disabledText: disabledText ?? i18n.t('SegmentationPanel: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 ?? i18n.t('SegmentationPanel:Not available on the current viewport'),
          };
        }
 
        const isPrimaryActive = toolNames
          ? toolNames.includes(toolGroup.getActivePrimaryMouseButtonTool())
          : toolGroup.getActivePrimaryMouseButtonTool() === toolName;
 
        return {
          disabled: false,
          isActive: isPrimaryActive,
        };
      },
    },
    {
      name: 'evaluate.cornerstone.segmentation.synchronizeDrawingRadius',
      evaluate: ({ button, radiusOptionId }) => {
        const toolGroupIds = toolGroupService.getToolGroupIds();
        if (!toolGroupIds?.length) {
          return;
        }
 
        for (const toolGroupId of toolGroupIds) {
          const brushSize = cstUtils.segmentation.getBrushSizeForToolGroup(toolGroupId);
 
          if (brushSize) {
            const option = toolbarService.getOptionById(button, radiusOptionId);
            option.value = brushSize;
          }
        }
      },
    },
  ];
}