2020-06-25 17:00:42 -04:00
|
|
|
import React, {
|
|
|
|
|
createContext,
|
|
|
|
|
useCallback,
|
|
|
|
|
useContext,
|
|
|
|
|
useEffect,
|
|
|
|
|
useReducer,
|
|
|
|
|
} from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2020-05-23 22:15:21 -04:00
|
|
|
|
2022-07-27 16:39:04 +00:00
|
|
|
import viewportLabels from '../utils/viewportLabels';
|
|
|
|
|
|
2020-06-26 13:29:10 -04:00
|
|
|
const DEFAULT_STATE = {
|
2021-05-28 14:32:48 -04:00
|
|
|
numRows: null,
|
|
|
|
|
numCols: null,
|
2022-07-27 16:39:04 +00:00
|
|
|
layoutType: 'grid',
|
2020-06-30 20:49:37 -04:00
|
|
|
viewports: [
|
2022-07-27 16:39:04 +00:00
|
|
|
{
|
|
|
|
|
displaySetInstanceUIDs: [],
|
|
|
|
|
viewportOptions: {},
|
|
|
|
|
displaySetOptions: [{}],
|
|
|
|
|
x: 0, // left
|
|
|
|
|
y: 0, // top
|
|
|
|
|
width: 100,
|
|
|
|
|
height: 100,
|
|
|
|
|
viewportLabel: null,
|
|
|
|
|
},
|
2020-06-30 20:49:37 -04:00
|
|
|
],
|
2020-06-26 13:29:10 -04:00
|
|
|
activeViewportIndex: 0,
|
2022-11-16 14:19:52 -05:00
|
|
|
cachedLayout: {},
|
2020-06-26 13:29:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ViewportGridContext = createContext(DEFAULT_STATE);
|
2020-05-23 22:15:21 -04:00
|
|
|
|
2022-07-27 16:39:04 +00:00
|
|
|
/**
|
|
|
|
|
* Given the flatten index, and rows and column, it returns the
|
|
|
|
|
* row and column index
|
|
|
|
|
*/
|
|
|
|
|
const unravelIndex = (index, numRows, numCols) => {
|
|
|
|
|
const row = Math.floor(index / numCols);
|
|
|
|
|
const col = index % numCols;
|
|
|
|
|
return { row, col };
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
export function ViewportGridProvider({ children, service }) {
|
|
|
|
|
const viewportGridReducer = (state, action) => {
|
|
|
|
|
switch (action.type) {
|
2020-06-30 15:53:57 -03:00
|
|
|
case 'SET_ACTIVE_VIEWPORT_INDEX': {
|
2020-06-30 20:49:37 -04:00
|
|
|
return { ...state, ...{ activeViewportIndex: action.payload } };
|
2020-06-30 15:53:57 -03:00
|
|
|
}
|
2020-06-25 17:00:42 -04:00
|
|
|
case 'SET_DISPLAYSET_FOR_VIEWPORT': {
|
2022-09-15 23:23:17 -04:00
|
|
|
const payload = action.payload;
|
|
|
|
|
const { viewportIndex, displaySetInstanceUIDs } = payload;
|
2022-09-20 23:40:26 -04:00
|
|
|
|
|
|
|
|
// Note: there should be no inheritance happening at this level,
|
|
|
|
|
// we can't assume the new displaySet can inherit the previous
|
|
|
|
|
// displaySet's or viewportOptions at all. For instance, dragging
|
|
|
|
|
// and dropping a SEG/RT displaySet without any viewportOptions
|
|
|
|
|
// or displaySetOptions should not inherit the previous displaySet's
|
|
|
|
|
// which might have been a PDF Viewport. The viewport itself
|
|
|
|
|
// will deal with inheritance if required. Here is just a simple
|
|
|
|
|
// provider.
|
|
|
|
|
const viewportOptions = payload.viewportOptions || {};
|
|
|
|
|
const displaySetOptions = payload.displaySetOptions || [{}];
|
|
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
const viewports = state.viewports.slice();
|
2020-06-30 20:49:37 -04:00
|
|
|
|
2022-11-21 10:44:19 -05:00
|
|
|
if (!viewportOptions.viewportId) {
|
|
|
|
|
viewportOptions.viewportId = `viewport-${viewportIndex}`;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 16:39:04 +00:00
|
|
|
// merge the displaySetOptions and viewportOptions and displaySetInstanceUIDs
|
|
|
|
|
// into the viewport object at the given index
|
|
|
|
|
viewports[viewportIndex] = {
|
|
|
|
|
...viewports[viewportIndex],
|
|
|
|
|
displaySetInstanceUIDs,
|
|
|
|
|
viewportOptions,
|
|
|
|
|
displaySetOptions,
|
|
|
|
|
viewportLabel: viewportLabels[viewportIndex],
|
|
|
|
|
};
|
2020-06-30 20:49:37 -04:00
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
return { ...state, ...{ viewports } };
|
2020-06-25 17:00:42 -04:00
|
|
|
}
|
|
|
|
|
case 'SET_LAYOUT': {
|
2022-11-16 14:19:52 -05:00
|
|
|
const {
|
|
|
|
|
numCols,
|
|
|
|
|
numRows,
|
|
|
|
|
layoutOptions,
|
|
|
|
|
layoutType = 'grid',
|
|
|
|
|
} = action.payload;
|
2022-07-27 16:39:04 +00:00
|
|
|
|
|
|
|
|
// If empty viewportOptions, we use numRow and numCols to calculate number of viewports
|
|
|
|
|
const numPanes = layoutOptions.length || numRows * numCols;
|
2020-06-25 17:00:42 -04:00
|
|
|
const viewports = state.viewports.slice();
|
|
|
|
|
const activeViewportIndex =
|
|
|
|
|
state.activeViewportIndex >= numPanes ? 0 : state.activeViewportIndex;
|
|
|
|
|
|
|
|
|
|
while (viewports.length < numPanes) {
|
|
|
|
|
viewports.push({});
|
|
|
|
|
}
|
|
|
|
|
while (viewports.length > numPanes) {
|
|
|
|
|
viewports.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 16:39:04 +00:00
|
|
|
for (let i = 0; i < numPanes; i++) {
|
|
|
|
|
let xPos, yPos, w, h;
|
|
|
|
|
|
|
|
|
|
if (layoutOptions && layoutOptions[i]) {
|
|
|
|
|
({ x: xPos, y: yPos, width: w, height: h } = layoutOptions[i]);
|
|
|
|
|
} else {
|
|
|
|
|
const { row, col } = unravelIndex(i, numRows, numCols);
|
|
|
|
|
w = 1 / numCols;
|
|
|
|
|
h = 1 / numRows;
|
|
|
|
|
xPos = col * w;
|
|
|
|
|
yPos = row * h;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
viewports[i].width = w;
|
|
|
|
|
viewports[i].height = h;
|
|
|
|
|
viewports[i].x = xPos;
|
|
|
|
|
viewports[i].y = yPos;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
return {
|
|
|
|
|
...state,
|
2022-07-27 16:39:04 +00:00
|
|
|
...{
|
|
|
|
|
activeViewportIndex,
|
|
|
|
|
numCols,
|
|
|
|
|
numRows,
|
|
|
|
|
layoutType,
|
|
|
|
|
viewports,
|
|
|
|
|
},
|
2020-06-25 17:00:42 -04:00
|
|
|
};
|
|
|
|
|
}
|
2020-07-03 19:18:59 +02:00
|
|
|
case 'RESET': {
|
|
|
|
|
return {
|
2021-05-28 14:32:48 -04:00
|
|
|
numCols: null,
|
|
|
|
|
numRows: null,
|
2022-07-27 16:39:04 +00:00
|
|
|
layoutType: 'grid',
|
2020-07-03 19:18:59 +02:00
|
|
|
activeViewportIndex: 0,
|
2021-05-28 14:32:48 -04:00
|
|
|
viewports: [
|
|
|
|
|
{
|
2022-07-27 16:39:04 +00:00
|
|
|
displaySetInstanceUIDs: [],
|
|
|
|
|
displaySetOptions: [],
|
|
|
|
|
viewportOptions: {},
|
|
|
|
|
x: 0, // left
|
|
|
|
|
y: 0, // top
|
|
|
|
|
width: 100,
|
|
|
|
|
height: 100,
|
2021-05-28 14:32:48 -04:00
|
|
|
},
|
|
|
|
|
],
|
2022-11-16 14:19:52 -05:00
|
|
|
cachedLayout: {},
|
2020-07-03 19:18:59 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
// The SET_CACHE_LAYOUT action can be used for caching a layout
|
|
|
|
|
// for instance double clicking a viewport to maximize it.
|
|
|
|
|
// and then restoring the previous layout when the viewport is
|
|
|
|
|
// double clicked again.
|
2020-07-03 19:12:33 +02:00
|
|
|
case 'SET_CACHED_LAYOUT': {
|
2022-11-16 14:19:52 -05:00
|
|
|
const { cacheId, cachedLayout } = action.payload;
|
|
|
|
|
|
|
|
|
|
// deep copy the cachedLayout into the state
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
cachedLayout: {
|
|
|
|
|
...state.cachedLayout,
|
|
|
|
|
[cacheId]: JSON.parse(JSON.stringify(cachedLayout)),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case 'RESTORE_CACHED_LAYOUT': {
|
|
|
|
|
const cacheId = action.payload;
|
|
|
|
|
|
|
|
|
|
if (!state.cachedLayout[cacheId]) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`No cached layout found for cacheId: ${cacheId}. Ignoring...`
|
|
|
|
|
);
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cachedLayout = state.cachedLayout;
|
|
|
|
|
return { ...state.cachedLayout[cacheId], cachedLayout };
|
2020-07-03 19:12:33 +02:00
|
|
|
}
|
2022-11-16 14:19:52 -05:00
|
|
|
|
2020-07-06 14:28:21 +02:00
|
|
|
case 'SET': {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
...action.payload,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
default:
|
|
|
|
|
return action.payload;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [viewportGridState, dispatch] = useReducer(
|
2020-06-26 13:29:10 -04:00
|
|
|
viewportGridReducer,
|
2020-06-30 15:53:57 -03:00
|
|
|
DEFAULT_STATE
|
2020-06-25 17:00:42 -04:00
|
|
|
);
|
|
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
const getState = useCallback(() => {
|
|
|
|
|
return viewportGridState;
|
|
|
|
|
}, [viewportGridState]);
|
2020-08-19 16:24:07 -03:00
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
const setActiveViewportIndex = useCallback(
|
|
|
|
|
index => dispatch({ type: 'SET_ACTIVE_VIEWPORT_INDEX', payload: index }),
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
2020-08-19 16:24:07 -03:00
|
|
|
|
2022-07-27 16:39:04 +00:00
|
|
|
const setDisplaySetsForViewport = useCallback(
|
|
|
|
|
({
|
|
|
|
|
viewportIndex,
|
|
|
|
|
displaySetInstanceUIDs,
|
2022-09-15 23:23:17 -04:00
|
|
|
viewportOptions,
|
|
|
|
|
displaySetOptions,
|
2022-07-27 16:39:04 +00:00
|
|
|
}) =>
|
2020-06-25 17:00:42 -04:00
|
|
|
dispatch({
|
|
|
|
|
type: 'SET_DISPLAYSET_FOR_VIEWPORT',
|
|
|
|
|
payload: {
|
|
|
|
|
viewportIndex,
|
2022-07-27 16:39:04 +00:00
|
|
|
displaySetInstanceUIDs,
|
|
|
|
|
viewportOptions,
|
|
|
|
|
displaySetOptions,
|
2020-06-25 17:00:42 -04:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
|
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
const setDisplaySetsForViewports = useCallback(
|
|
|
|
|
viewports => {
|
|
|
|
|
viewports.forEach(data => {
|
|
|
|
|
setDisplaySetsForViewport(data);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[setDisplaySetsForViewport]
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
const setLayout = useCallback(
|
2022-07-27 16:39:04 +00:00
|
|
|
({ layoutType, numRows, numCols, layoutOptions = [] }) =>
|
2020-06-25 17:00:42 -04:00
|
|
|
dispatch({
|
|
|
|
|
type: 'SET_LAYOUT',
|
|
|
|
|
payload: {
|
2022-07-27 16:39:04 +00:00
|
|
|
layoutType,
|
2020-06-25 17:00:42 -04:00
|
|
|
numRows,
|
2022-07-27 16:39:04 +00:00
|
|
|
numCols,
|
|
|
|
|
layoutOptions,
|
2020-06-25 17:00:42 -04:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
|
|
|
|
|
2020-07-03 19:18:59 +02:00
|
|
|
const reset = useCallback(
|
|
|
|
|
() =>
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'RESET',
|
|
|
|
|
payload: {},
|
|
|
|
|
}),
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
2020-08-19 16:24:07 -03:00
|
|
|
|
2020-07-03 19:12:33 +02:00
|
|
|
const setCachedLayout = useCallback(
|
|
|
|
|
payload =>
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'SET_CACHED_LAYOUT',
|
|
|
|
|
payload,
|
|
|
|
|
}),
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
|
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
const restoreCachedLayout = useCallback(
|
|
|
|
|
cacheId => {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'RESTORE_CACHED_LAYOUT',
|
|
|
|
|
payload: cacheId,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
|
|
|
|
|
2020-07-06 14:28:21 +02:00
|
|
|
const set = useCallback(
|
|
|
|
|
payload =>
|
|
|
|
|
dispatch({
|
|
|
|
|
type: 'SET',
|
|
|
|
|
payload,
|
|
|
|
|
}),
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
/**
|
2022-11-16 14:19:52 -05:00
|
|
|
* Sets the implementation of ViewportGridService that can be used by extensions.
|
2020-06-25 17:00:42 -04:00
|
|
|
*
|
|
|
|
|
* @returns void
|
|
|
|
|
*/
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (service) {
|
|
|
|
|
service.setServiceImplementation({
|
|
|
|
|
getState,
|
|
|
|
|
setActiveViewportIndex,
|
2022-07-27 16:39:04 +00:00
|
|
|
setDisplaySetsForViewport,
|
2022-11-16 14:19:52 -05:00
|
|
|
setDisplaySetsForViewports,
|
2020-06-25 17:00:42 -04:00
|
|
|
setLayout,
|
2020-07-03 19:18:59 +02:00
|
|
|
reset,
|
2022-11-24 16:28:10 -05:00
|
|
|
onModeExit: reset,
|
2020-07-03 19:12:33 +02:00
|
|
|
setCachedLayout,
|
2022-11-16 14:19:52 -05:00
|
|
|
restoreCachedLayout,
|
2020-07-06 14:28:21 +02:00
|
|
|
set,
|
2020-06-25 17:00:42 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
getState,
|
|
|
|
|
service,
|
|
|
|
|
setActiveViewportIndex,
|
2022-07-27 16:39:04 +00:00
|
|
|
setDisplaySetsForViewport,
|
2022-11-16 14:19:52 -05:00
|
|
|
setDisplaySetsForViewports,
|
2020-06-25 17:00:42 -04:00
|
|
|
setLayout,
|
2020-07-03 19:18:59 +02:00
|
|
|
reset,
|
2020-07-03 19:12:33 +02:00
|
|
|
setCachedLayout,
|
2022-11-16 14:19:52 -05:00
|
|
|
restoreCachedLayout,
|
2020-07-06 14:28:21 +02:00
|
|
|
set,
|
2020-06-25 17:00:42 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const api = {
|
2021-06-15 10:19:45 -04:00
|
|
|
getState,
|
2022-11-16 14:19:52 -05:00
|
|
|
setActiveViewportIndex: index => service.setActiveViewportIndex(index), // run it through the service itself since we want to publish events
|
2022-07-27 16:39:04 +00:00
|
|
|
setDisplaySetsForViewport,
|
2022-11-16 14:19:52 -05:00
|
|
|
setDisplaySetsForViewports,
|
2020-06-25 17:00:42 -04:00
|
|
|
setLayout,
|
2020-07-03 19:12:33 +02:00
|
|
|
setCachedLayout,
|
2022-11-16 14:19:52 -05:00
|
|
|
restoreCachedLayout,
|
2020-07-06 14:28:21 +02:00
|
|
|
reset,
|
|
|
|
|
set,
|
2020-06-25 17:00:42 -04:00
|
|
|
};
|
|
|
|
|
|
2020-05-23 22:15:21 -04:00
|
|
|
return (
|
2020-06-25 17:00:42 -04:00
|
|
|
<ViewportGridContext.Provider value={[viewportGridState, api]}>
|
2020-05-23 22:15:21 -04:00
|
|
|
{children}
|
|
|
|
|
</ViewportGridContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 17:00:42 -04:00
|
|
|
ViewportGridProvider.propTypes = {
|
|
|
|
|
children: PropTypes.any,
|
|
|
|
|
service: PropTypes.shape({
|
|
|
|
|
setServiceImplementation: PropTypes.func,
|
|
|
|
|
}).isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-23 22:15:21 -04:00
|
|
|
export const useViewportGrid = () => useContext(ViewportGridContext);
|