Skip to content

Commit

Permalink
feat(grid): remove viewportIndex and only rely on viewportId (OHIF#3591)
Browse files Browse the repository at this point in the history
Co-authored-by: Bill Wallace <wayfarer3130@gmail.com>
  • Loading branch information
2 people authored and Sofien-Sellami committed Oct 5, 2023
1 parent 4fc99d3 commit 9970df3
Show file tree
Hide file tree
Showing 83 changed files with 1,308 additions and 1,122 deletions.
2 changes: 1 addition & 1 deletion extensions/_example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const getSopClassHandlerModule = (/* ... */) => {

const getToolbarModule = () => {};

// displaySet, viewportIndex, dataSource
// displaySet, dataSource
const getViewportModule = () => {
const wrappedViewport = props => {
return (
Expand Down
17 changes: 12 additions & 5 deletions extensions/cornerstone-dicom-rt/src/utils/_hydrateRT.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
async function _hydrateRTDisplaySet({
rtDisplaySet,
viewportIndex,
viewportId,
servicesManager,
}) {
const {
Expand All @@ -27,7 +27,7 @@ async function _hydrateRTDisplaySet({
const { viewports } = viewportGridService.getState();

const updatedViewports = hangingProtocolService.getViewportsRequireUpdate(
viewportIndex,
viewportId,
displaySetInstanceUID
);

Expand All @@ -39,8 +39,8 @@ async function _hydrateRTDisplaySet({
// is being displayed, if so we need to update the viewport to use volume viewport
// (if already is not using it) since Cornerstone3D currently only supports
// volume viewport for segmentation
viewports.forEach((viewport, index) => {
if (index === viewportIndex) {
viewports.forEach(viewport => {
if (viewport.viewportId === viewportId) {
return;
}

Expand All @@ -51,9 +51,16 @@ async function _hydrateRTDisplaySet({

if (shouldDisplaySeg) {
updatedViewports.push({
viewportIndex: index,
viewportId: viewport.viewportId,
displaySetInstanceUIDs: viewport.displaySetInstanceUIDs,
viewportOptions: {
// Note: This is a hack to get the grid to re-render the OHIFCornerstoneViewport component
// Used for segmentation hydration right now, since the logic to decide whether
// a viewport needs to render a segmentation lives inside the CornerstoneViewportService
// so we need to re-render (force update via change of the needsRerendering) so that React
// does the diffing and decides we should render this again (although the id and element has not changed)
// so that the CornerstoneViewportService can decide whether to render the segmentation or not.
needsRerendering: true,
initialImageOptions: {
preset: 'middle',
},
Expand Down
13 changes: 5 additions & 8 deletions extensions/cornerstone-dicom-rt/src/utils/promptHydrateRT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ const RESPONSE = {
function promptHydrateRT({
servicesManager,
rtDisplaySet,
viewportIndex,
viewportId,
toolGroupId = 'default',
preHydrateCallbacks,
}) {
const { uiViewportDialogService } = servicesManager.services;

return new Promise(async function(resolve, reject) {
const promptResult = await _askHydrate(
uiViewportDialogService,
viewportIndex
);
const promptResult = await _askHydrate(uiViewportDialogService, viewportId);

if (promptResult === RESPONSE.HYDRATE_SEG) {
preHydrateCallbacks?.forEach(callback => {
Expand All @@ -29,7 +26,7 @@ function promptHydrateRT({

const isHydrated = await hydrateRTDisplaySet({
rtDisplaySet,
viewportIndex,
viewportId,
toolGroupId,
servicesManager,
});
Expand All @@ -39,7 +36,7 @@ function promptHydrateRT({
});
}

function _askHydrate(uiViewportDialogService, viewportIndex) {
function _askHydrate(uiViewportDialogService, viewportId) {
return new Promise(function(resolve, reject) {
const message = 'Do you want to open this Segmentation?';
const actions = [
Expand All @@ -60,7 +57,7 @@ function _askHydrate(uiViewportDialogService, viewportIndex) {
};

uiViewportDialogService.show({
viewportIndex,
viewportId,
type: 'info',
message,
actions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function OHIFCornerstoneRTViewport(props) {
children,
displaySets,
viewportOptions,
viewportIndex,
viewportLabel,
servicesManager,
extensionManager,
Expand All @@ -36,7 +35,9 @@ function OHIFCornerstoneRTViewport(props) {
customizationService,
} = servicesManager.services;

const toolGroupId = `${RT_TOOLGROUP_BASE_NAME}-${viewportIndex}`;
const viewportId = viewportOptions.viewportId;

const toolGroupId = `${RT_TOOLGROUP_BASE_NAME}-${viewportId}`;

// RT viewport will always have a single display set
if (displaySets.length > 1) {
Expand Down Expand Up @@ -67,7 +68,7 @@ function OHIFCornerstoneRTViewport(props) {
// refs
const referencedDisplaySetRef = useRef(null);

const { viewports, activeViewportIndex } = viewportGrid;
const { viewports, activeViewportId } = viewportGrid;

const referencedDisplaySet = rtDisplaySet.getReferenceDisplaySet();
const referencedDisplaySetMetadata = _getReferencedDisplaySetMetadata(
Expand All @@ -93,9 +94,9 @@ function OHIFCornerstoneRTViewport(props) {
};

const storePresentationState = useCallback(() => {
viewportGrid?.viewports.forEach(({ viewportIndex }) => {
viewportGrid?.viewports.forEach(({ viewportId }) => {
commandsManager.runCommand('storePresentation', {
viewportIndex,
viewportId,
});
});
}, [viewportGrid]);
Expand Down Expand Up @@ -124,7 +125,7 @@ function OHIFCornerstoneRTViewport(props) {
onElementDisabled={onElementDisabled}
></Component>
);
}, [viewportIndex, rtDisplaySet, toolGroupId]);
}, [viewportId, rtDisplaySet, toolGroupId]);

const onSegmentChange = useCallback(
direction => {
Expand Down Expand Up @@ -162,15 +163,15 @@ function OHIFCornerstoneRTViewport(props) {

promptHydrateRT({
servicesManager,
viewportIndex,
viewportId,
rtDisplaySet,
preHydrateCallbacks: [storePresentationState],
}).then(isHydrated => {
if (isHydrated) {
setIsHydrated(true);
}
});
}, [servicesManager, viewportIndex, rtDisplaySet, rtIsLoading]);
}, [servicesManager, viewportId, rtDisplaySet, rtIsLoading]);

useEffect(() => {
const { unsubscribe } = segmentationService.subscribe(
Expand Down Expand Up @@ -222,12 +223,12 @@ function OHIFCornerstoneRTViewport(props) {
const onDisplaySetsRemovedSubscription = displaySetService.subscribe(
displaySetService.EVENTS.DISPLAY_SETS_REMOVED,
({ displaySetInstanceUIDs }) => {
const activeViewport = viewports[activeViewportIndex];
const activeViewport = viewports.get(activeViewportId);
if (
displaySetInstanceUIDs.includes(activeViewport.displaySetInstanceUID)
) {
viewportGridService.setDisplaySetsForViewport({
viewportIndex: activeViewportIndex,
viewportId: activeViewportId,
displaySetInstanceUIDs: [],
});
}
Expand Down Expand Up @@ -292,7 +293,7 @@ function OHIFCornerstoneRTViewport(props) {
return (
child &&
React.cloneElement(child, {
viewportIndex,
viewportId,
key: index,
})
);
Expand Down Expand Up @@ -322,7 +323,7 @@ function OHIFCornerstoneRTViewport(props) {
storePresentationState();
const isHydrated = await _hydrateRTDisplaySet({
rtDisplaySet,
viewportIndex,
viewportId,
servicesManager,
});

Expand Down Expand Up @@ -384,7 +385,7 @@ function OHIFCornerstoneRTViewport(props) {

OHIFCornerstoneRTViewport.propTypes = {
displaySets: PropTypes.arrayOf(PropTypes.object),
viewportIndex: PropTypes.number.isRequired,
viewportId: PropTypes.string.isRequired,
dataSource: PropTypes.object,
children: PropTypes.node,
customProps: PropTypes.object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ PanelSegmentation.propTypes = {
commandsManager: PropTypes.shape({
runCommand: PropTypes.func.isRequired,
}),
appConfig: PropTypes.object.isRequired,
servicesManager: PropTypes.shape({
services: PropTypes.shape({
segmentationService: PropTypes.shape({
Expand Down
17 changes: 12 additions & 5 deletions extensions/cornerstone-dicom-seg/src/utils/_hydrateSEG.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
async function _hydrateSEGDisplaySet({
segDisplaySet,
viewportIndex,
viewportId: targetViewportId,
servicesManager,
}) {
const {
Expand All @@ -27,7 +27,7 @@ async function _hydrateSEGDisplaySet({
const { viewports } = viewportGridService.getState();

const updatedViewports = hangingProtocolService.getViewportsRequireUpdate(
viewportIndex,
targetViewportId,
displaySetInstanceUID
);

Expand All @@ -37,8 +37,8 @@ async function _hydrateSEGDisplaySet({
// is being displayed, if so we need to update the viewport to use volume viewport
// (if already is not using it) since Cornerstone3D currently only supports
// volume viewport for segmentation
viewports.forEach((viewport, index) => {
if (index === viewportIndex) {
viewports.forEach((viewport, viewportId) => {
if (targetViewportId === viewportId) {
return;
}

Expand All @@ -49,9 +49,16 @@ async function _hydrateSEGDisplaySet({

if (shouldDisplaySeg) {
updatedViewports.push({
viewportIndex: index,
viewportId,
displaySetInstanceUIDs: viewport.displaySetInstanceUIDs,
viewportOptions: {
// Note: This is a hack to get the grid to re-render the OHIFCornerstoneViewport component
// Used for segmentation hydration right now, since the logic to decide whether
// a viewport needs to render a segmentation lives inside the CornerstoneViewportService
// so we need to re-render (force update via change of the needsRerendering) so that React
// does the diffing and decides we should render this again (although the id and element has not changed)
// so that the CornerstoneViewportService can decide whether to render the segmentation or not.
needsRerendering: true,
initialImageOptions: {
preset: 'middle',
},
Expand Down
13 changes: 5 additions & 8 deletions extensions/cornerstone-dicom-seg/src/utils/promptHydrateSEG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ const RESPONSE = {
function promptHydrateSEG({
servicesManager,
segDisplaySet,
viewportIndex,
viewportId,
preHydrateCallbacks,
}) {
const { uiViewportDialogService } = servicesManager.services;

return new Promise(async function(resolve, reject) {
const promptResult = await _askHydrate(
uiViewportDialogService,
viewportIndex
);
const promptResult = await _askHydrate(uiViewportDialogService, viewportId);

if (promptResult === RESPONSE.HYDRATE_SEG) {
preHydrateCallbacks?.forEach(callback => {
Expand All @@ -28,7 +25,7 @@ function promptHydrateSEG({

const isHydrated = await hydrateSEGDisplaySet({
segDisplaySet,
viewportIndex,
viewportId,
servicesManager,
});

Expand All @@ -37,7 +34,7 @@ function promptHydrateSEG({
});
}

function _askHydrate(uiViewportDialogService, viewportIndex) {
function _askHydrate(uiViewportDialogService, viewportId) {
return new Promise(function(resolve, reject) {
const message = 'Do you want to open this Segmentation?';
const actions = [
Expand All @@ -58,7 +55,7 @@ function _askHydrate(uiViewportDialogService, viewportIndex) {
};

uiViewportDialogService.show({
viewportIndex,
viewportId,
type: 'info',
message,
actions,
Expand Down
Loading

0 comments on commit 9970df3

Please sign in to comment.