Switch toolState usage since ES6 modules are read-only

This commit is contained in:
Erik Ziegler 2017-06-13 09:59:01 +02:00
parent ddef3c4710
commit 27296a8bca
12 changed files with 308 additions and 309 deletions

View File

@ -16,8 +16,6 @@ import * as cornerstoneMath from 'cornerstone-math';
import * as cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader';
import * as dicomParser from 'dicom-parser';
debugger;
export {
cornerstone,
cornerstoneTools,

View File

@ -1,99 +1,93 @@
import { Viewerbase } from 'meteor/ohif:viewerbase';
(function(cornerstoneTools) {
// Delete a lesion if Ctrl+D or DELETE is pressed while a lesion is selected
var keys = {
D: 68,
DELETE: 46
};
'use strict';
// Delete a lesion if Ctrl+D or DELETE is pressed while a lesion is selected
var keys = {
D: 68,
DELETE: 46
};
function removeMeasurementTimepoint(data, index, toolType, element) {
var imageId = data.imageId;
if (!imageId) {
var enabledElement = cornerstone.getEnabledElement(element);
imageId = enabledElement.image.imageId;
}
cornerstoneTools.removeToolState(element, toolType, data);
cornerstone.updateImage(element);
function removeMeasurementTimepoint(data, index, toolType, element) {
var imageId = data.imageId;
if (!imageId) {
var enabledElement = cornerstone.getEnabledElement(element);
imageId = enabledElement.image.imageId;
}
// TODO = Check if we have the same function already in Cornerstone Tools
function getNearbyToolData(element, coords, toolTypes) {
var allTools = Viewerbase.toolManager.getTools();
var pointNearTool = false;
var touchDevice = Viewerbase.helpers.isTouchDevice();
var nearbyTool = {},
nearbyToolIndex,
nearbyToolType;
cornerstoneTools.removeToolState(element, toolType, data);
cornerstone.updateImage(element);
}
toolTypes.forEach(function(toolType) {
var toolData = cornerstoneTools.getToolState(element, toolType);
if (!toolData) {
return;
// TODO = Check if we have the same function already in Cornerstone Tools
function getNearbyToolData(element, coords, toolTypes) {
var allTools = Viewerbase.toolManager.getTools();
var pointNearTool = false;
var touchDevice = Viewerbase.helpers.isTouchDevice();
var nearbyTool = {},
nearbyToolIndex,
nearbyToolType;
toolTypes.forEach(function(toolType) {
var toolData = cornerstoneTools.getToolState(element, toolType);
if (!toolData) {
return;
}
for (var i = 0; i < toolData.data.length; i++) {
var data = toolData.data[i];
var toolInterface;
if (touchDevice) {
toolInterface = allTools[toolType].touch;
} else {
toolInterface = allTools[toolType].mouse;
}
for (var i = 0; i < toolData.data.length; i++) {
var data = toolData.data[i];
var toolInterface;
if (touchDevice) {
toolInterface = allTools[toolType].touch;
} else {
toolInterface = allTools[toolType].mouse;
}
if (toolInterface.pointNearTool(element, data, coords)) {
pointNearTool = true;
nearbyTool.tool = data;
nearbyTool.index = i;
nearbyTool.toolType = toolType;
break;
}
if (toolInterface.pointNearTool(element, data, coords)) {
pointNearTool = true;
nearbyTool.tool = data;
nearbyTool.index = i;
nearbyTool.toolType = toolType;
break;
}
}
if (pointNearTool === true) {
return false;
}
if (pointNearTool === true) {
return false;
}
});
return pointNearTool ? nearbyTool : undefined;
}
function keyDownCallback(e, eventData) {
var keyCode = eventData.which;
if (keyCode === keys.DELETE ||
(keyCode === keys.D && eventData.event.ctrlKey === true)) {
var toolTypes = [ 'bidirectional', 'nonTarget', 'length', 'targetCR', 'targetUN', 'targetEX'];
var nearbyToolData = getNearbyToolData(eventData.element, eventData.currentPoints.canvas, toolTypes);
if (!nearbyToolData) {
return;
}
const dialogSettings = {
title: 'Delete measurements',
message: 'Are you sure you want to delete this measurement?'
};
// TODO= Refactor this so the confirmation dialog is an
// optional settable callback in the tool's configuration
OHIF.ui.showDialog('dialogConfirm', dialogSettings).then(() => {
removeMeasurementTimepoint(nearbyToolData.tool,
nearbyToolData.index,
nearbyToolData.toolType,
eventData.element
);
});
return pointNearTool ? nearbyTool : undefined;
}
}
function keyDownCallback(e, eventData) {
var keyCode = eventData.which;
if (keyCode === keys.DELETE ||
(keyCode === keys.D && eventData.event.ctrlKey === true)) {
var toolTypes = [ 'bidirectional', 'nonTarget', 'length', 'targetCR', 'targetUN', 'targetEX'];
var nearbyToolData = getNearbyToolData(eventData.element, eventData.currentPoints.canvas, toolTypes);
if (!nearbyToolData) {
return;
}
const dialogSettings = {
title: 'Delete measurements',
message: 'Are you sure you want to delete this measurement?'
};
// TODO= Refactor this so the confirmation dialog is an
// optional settable callback in the tool's configuration
OHIF.ui.showDialog('dialogConfirm', dialogSettings).then(() => {
removeMeasurementTimepoint(nearbyToolData.tool,
nearbyToolData.index,
nearbyToolData.toolType,
eventData.element
);
});
}
}
// module/private exports
cornerstoneTools.deleteLesionKeyboardTool = cornerstoneTools.keyboardTool(keyDownCallback);
})(cornerstoneTools);
// module/private exports
cornerstoneTools.deleteLesionKeyboardTool = cornerstoneTools.keyboardTool(keyDownCallback);

View File

@ -1,229 +1,222 @@
function drawLine(context, startPoint, endPoint, config) {
context.moveTo(startPoint.x, startPoint.y);
context.lineTo(endPoint.x, endPoint.y);
}
(function($, cornerstone, cornerstoneTools) {
function drawVerticalScalebarIntervals (context, config) {
let i = 0;
'use strict';
while (config.verticalLine.start.y + i * config.verticalMinorTick <= config.vscaleBounds.bottomRight.y) {
function drawLine(context, startPoint, endPoint, config) {
context.moveTo(startPoint.x, startPoint.y);
context.lineTo(endPoint.x, endPoint.y);
}
function drawVerticalScalebarIntervals (context, config) {
let i = 0;
while (config.verticalLine.start.y + i * config.verticalMinorTick <= config.vscaleBounds.bottomRight.y) {
const startPoint = {
x: config.verticalLine.start.x,
y: config.verticalLine.start.y + i * config.verticalMinorTick
};
const endPoint = {
x: 0,
y: config.verticalLine.start.y + i * config.verticalMinorTick
};
if (i % 5 === 0) {
endPoint.x = config.verticalLine.start.x - config.majorTickLength;
} else {
endPoint.x = config.verticalLine.start.x - config.minorTickLength;
}
drawLine(context, startPoint, endPoint, config);
i++;
}
}
function drawHorizontalScalebarIntervals (context, config) {
let i = 0;
while (config.horizontalLine.start.x + i * config.horizontalMinorTick <= config.hscaleBounds.bottomRight.x) {
const startPoint = {
x: config.horizontalLine.start.x + i * config.horizontalMinorTick,
y: config.horizontalLine.start.y
};
const endPoint = {
x: config.horizontalLine.start.x + i * config.horizontalMinorTick,
y: 0
};
if (i % 5 === 0) {
endPoint.y = config.horizontalLine.start.y - config.majorTickLength;
} else {
endPoint.y = config.horizontalLine.start.y - config.minorTickLength;
}
drawLine(context, startPoint, endPoint, config);
i++;
}
}
function drawVerticalScalebar(context, config) {
const startPoint = {
x: config.verticalLine.start.x,
y: config.verticalLine.start.y
};
const endPoint = {
x: config.verticalLine.end.x,
y: config.verticalLine.end.y
y: config.verticalLine.start.y + i * config.verticalMinorTick
};
context.beginPath();
context.strokeStyle = config.color;
context.lineWidth = config.lineWidth;
const endPoint = {
x: 0,
y: config.verticalLine.start.y + i * config.verticalMinorTick
};
if (i % 5 === 0) {
endPoint.x = config.verticalLine.start.x - config.majorTickLength;
} else {
endPoint.x = config.verticalLine.start.x - config.minorTickLength;
}
drawLine(context, startPoint, endPoint, config);
drawVerticalScalebarIntervals(context, config);
context.stroke();
i++;
}
}
function drawHorizontalScalebarIntervals (context, config) {
let i = 0;
while (config.horizontalLine.start.x + i * config.horizontalMinorTick <= config.hscaleBounds.bottomRight.x) {
function drawHorizontalScalebar(context, config) {
const startPoint = {
x: config.horizontalLine.start.x,
x: config.horizontalLine.start.x + i * config.horizontalMinorTick,
y: config.horizontalLine.start.y
};
const endPoint = {
x: config.horizontalLine.end.x,
y: config.horizontalLine.end.y
x: config.horizontalLine.start.x + i * config.horizontalMinorTick,
y: 0
};
if (i % 5 === 0) {
endPoint.y = config.horizontalLine.start.y - config.majorTickLength;
} else {
endPoint.y = config.horizontalLine.start.y - config.minorTickLength;
}
drawLine(context, startPoint, endPoint, config);
drawHorizontalScalebarIntervals(context, config);
i++;
}
}
function drawVerticalScalebar(context, config) {
const startPoint = {
x: config.verticalLine.start.x,
y: config.verticalLine.start.y
};
const endPoint = {
x: config.verticalLine.end.x,
y: config.verticalLine.end.y
};
context.beginPath();
context.strokeStyle = config.color;
context.lineWidth = config.lineWidth;
drawLine(context, startPoint, endPoint, config);
drawVerticalScalebarIntervals(context, config);
context.stroke();
}
function drawHorizontalScalebar(context, config) {
const startPoint = {
x: config.horizontalLine.start.x,
y: config.horizontalLine.start.y
};
const endPoint = {
x: config.horizontalLine.end.x,
y: config.horizontalLine.end.y
};
drawLine(context, startPoint, endPoint, config);
drawHorizontalScalebarIntervals(context, config);
}
function drawScalebars(context, config){
context.shadowColor = config.shadowColor;
context.shadowBlur = config.shadowBlur;
context.strokeStyle = config.color;
context.lineWidth = config.lineWidth;
context.beginPath();
drawVerticalScalebar(context, config);
drawHorizontalScalebar(context, config);
context.stroke();
}
// Computes the max bound for scales on the image
function computeScaleBounds(eventData, canvasSize, imageSize, horizontalReduction, verticalReduction) {
let canvasBounds = {
left: 0,
top: 0,
width: canvasSize.width,
height: canvasSize.height
};
const hReduction = horizontalReduction * Math.min(1000, canvasSize.width);
const vReduction = verticalReduction * Math.min(1000, canvasSize.height);
canvasBounds = {
left: canvasBounds.left + hReduction,
top: canvasBounds.top + vReduction,
width: canvasBounds.width - 2 * hReduction,
height: canvasBounds.height - 2 * vReduction
};
return {
topLeft: {
x: canvasBounds.left,
y: canvasBounds.top
},
bottomRight: {
x: canvasBounds.left + canvasBounds.width,
y: canvasBounds.top + canvasBounds.height
}
};
}
function onImageRendered(e, eventData) {
// Check whether pixel spacing is defined
if (!eventData.image.rowPixelSpacing || !eventData.image.columnPixelSpacing) {
return;
}
function drawScalebars(context, config){
context.shadowColor = config.shadowColor;
context.shadowBlur = config.shadowBlur;
context.strokeStyle = config.color;
context.lineWidth = config.lineWidth;
context.beginPath();
drawVerticalScalebar(context, config);
drawHorizontalScalebar(context, config);
context.stroke();
const viewport = cornerstone.getViewport(eventData.enabledElement.element);
if (!viewport) {
return;
}
// Computes the max bound for scales on the image
function computeScaleBounds(eventData, canvasSize, imageSize, horizontalReduction, verticalReduction) {
const canvasSize = {
width: eventData.enabledElement.canvas.width,
height: eventData.enabledElement.canvas.height
};
const imageSize = {
width: eventData.enabledElement.image.width ,
height: eventData.enabledElement.image.height
};
let canvasBounds = {
left: 0,
top: 0,
width: canvasSize.width,
height: canvasSize.height
};
// Distance between intervals is 10mm
const verticalIntervalScale = (10.0 / eventData.enabledElement.image.rowPixelSpacing) * eventData.viewport.scale;
const horizontalIntervalScale = (10.0 / eventData.enabledElement.image.rowPixelSpacing) * eventData.viewport.scale;
const hReduction = horizontalReduction * Math.min(1000, canvasSize.width);
const vReduction = verticalReduction * Math.min(1000, canvasSize.height);
canvasBounds = {
left: canvasBounds.left + hReduction,
top: canvasBounds.top + vReduction,
width: canvasBounds.width - 2 * hReduction,
height: canvasBounds.height - 2 * vReduction
};
// 0.1 and 0.05 gives margin to horizontal and vertical lines
const hscaleBounds = computeScaleBounds(eventData, canvasSize, imageSize, 0.25, 0.05);
const vscaleBounds = computeScaleBounds(eventData, canvasSize, imageSize, 0.05, 0.15);
return {
topLeft: {
x: canvasBounds.left,
y: canvasBounds.top
if (!canvasSize.width || !canvasSize.height || !imageSize.width || !imageSize.height || !hscaleBounds || !vscaleBounds) {
return;
}
const config = {
hscaleBounds,
vscaleBounds,
verticalMinorTick: verticalIntervalScale,
horizontalMinorTick: horizontalIntervalScale,
minorTickLength: 12.5,
majorTickLength: 25,
verticalLine: {
start: {
x: vscaleBounds.bottomRight.x,
y: vscaleBounds.topLeft.y
},
bottomRight: {
x: canvasBounds.left + canvasBounds.width,
y: canvasBounds.top + canvasBounds.height
end: {
x: vscaleBounds.bottomRight.x,
y: vscaleBounds.bottomRight.y
}
};
}
function onImageRendered(e, eventData) {
// Check whether pixel spacing is defined
if (!eventData.image.rowPixelSpacing || !eventData.image.columnPixelSpacing) {
return;
}
const viewport = cornerstone.getViewport(eventData.enabledElement.element);
if (!viewport) {
return;
}
const canvasSize = {
width: eventData.enabledElement.canvas.width,
height: eventData.enabledElement.canvas.height
};
const imageSize = {
width: eventData.enabledElement.image.width ,
height: eventData.enabledElement.image.height
};
// Distance between intervals is 10mm
const verticalIntervalScale = (10.0 / eventData.enabledElement.image.rowPixelSpacing) * eventData.viewport.scale;
const horizontalIntervalScale = (10.0 / eventData.enabledElement.image.rowPixelSpacing) * eventData.viewport.scale;
// 0.1 and 0.05 gives margin to horizontal and vertical lines
const hscaleBounds = computeScaleBounds(eventData, canvasSize, imageSize, 0.25, 0.05);
const vscaleBounds = computeScaleBounds(eventData, canvasSize, imageSize, 0.05, 0.15);
if (!canvasSize.width || !canvasSize.height || !imageSize.width || !imageSize.height || !hscaleBounds || !vscaleBounds) {
return;
}
const config = {
hscaleBounds: hscaleBounds,
vscaleBounds: vscaleBounds,
verticalMinorTick: verticalIntervalScale,
horizontalMinorTick: horizontalIntervalScale,
minorTickLength: 12.5,
majorTickLength: 25,
verticalLine: {
start: {
x: vscaleBounds.bottomRight.x,
y: vscaleBounds.topLeft.y
},
end: {
x: vscaleBounds.bottomRight.x,
y: vscaleBounds.bottomRight.y
}
},
horizontalLine: {
start: {
x: hscaleBounds.topLeft.x,
y: hscaleBounds.bottomRight.y
},
horizontalLine: {
start: {
x: hscaleBounds.topLeft.x,
y: hscaleBounds.bottomRight.y
},
end: {
x: hscaleBounds.bottomRight.x,
y: hscaleBounds.bottomRight.y
}
},
color: 'white', // TODO: fix this later
lineWidth: cornerstoneTools.toolStyle.getToolWidth(),
shadowColor: 'black',
shadowBlur: 4
};
end: {
x: hscaleBounds.bottomRight.x,
y: hscaleBounds.bottomRight.y
}
},
color: 'white', // TODO: fix this later
lineWidth: cornerstoneTools.toolStyle.getToolWidth(),
shadowColor: 'black',
shadowBlur: 4
};
const context = eventData.enabledElement.canvas.getContext('2d');
context.setTransform(1, 0, 0, 1, 0, 0);
context.save();
const context = eventData.enabledElement.canvas.getContext('2d');
context.setTransform(1, 0, 0, 1, 0, 0);
context.save();
drawScalebars(context, config);
context.restore();
}
drawScalebars(context, config);
context.restore();
}
///////// END IMAGE RENDERING ///////
///////// END IMAGE RENDERING ///////
function disable(element) {
// TODO: displayTool does not have cornerstone.updateImage(element) method to hide tool
$(element).off('CornerstoneImageRendered', onImageRendered);
cornerstone.updateImage(element);
}
function disable(element) {
// TODO: displayTool does not have cornerstone.updateImage(element) method to hide tool
$(element).off('CornerstoneImageRendered', onImageRendered);
cornerstone.updateImage(element);
}
// module exports
cornerstoneTools.scaleOverlayTool = cornerstoneTools.displayTool(onImageRendered);
cornerstoneTools.scaleOverlayTool.disable = disable;
})($, cornerstone, cornerstoneTools);
// module exports
cornerstoneTools.scaleOverlayTool = cornerstoneTools.displayTool(onImageRendered);
cornerstoneTools.scaleOverlayTool.disable = disable;

View File

@ -2,7 +2,7 @@ import { OHIF } from 'meteor/ohif:core';
const removeToolDataWithMeasurementId = (imageId, toolType, measurementId) => {
OHIF.log.info('removeToolDataWithMeasurementId');
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.saveToolState();
// Find any related toolData
if (!toolState[imageId] || !toolState[imageId][toolType]) {
@ -32,6 +32,8 @@ const removeToolDataWithMeasurementId = (imageId, toolType, measurementId) => {
toRemove.forEach(function(index) {
toolData.splice(index, 1);
});
cornerstoneTools.globalImageIdSpecificToolStateManager.restoreToolState(toolState);
};
OHIF.lesiontracker.clearMeasurementTimepointData = (measurementId, timepointId) => {

View File

@ -315,7 +315,8 @@ class MeasurementApi {
const measurementNumber = filter.measurementNumber || entries[0].measurementNumber;
// Synchronize the new data with cornerstone tools
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.saveToolState();
_.each(entries, entry => {
if (toolState[entry.imageId]) {
const toolData = toolState[entry.imageId][entry.toolType];
@ -331,6 +332,8 @@ class MeasurementApi {
}
});
cornerstoneTools.globalImageIdSpecificToolStateManager.restoreToolState(toolState);
// Synchronize the updated measurements with Cornerstone Tools
// toolData to make sure the displayed measurements show 'Target X' correctly
const syncFilter = _.clone(filter);

View File

@ -107,7 +107,6 @@ class MeasurementHandlers {
Collection._c2._simpleSchema.clean(measurement);
// Insert the new measurement into the collection
console.log(measurement);
Collection.update(measurementId, {
$set: measurement
});

View File

@ -10,7 +10,8 @@ import { OHIF } from 'meteor/ohif:core';
function activateTool(measurementData) {
const toolType = measurementData.toolType;
const imageId = measurementData.imageId;
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.saveToolState();
const toolData = toolState[imageId][toolType];
if (!toolData || !toolData.data || !toolData.data.length) {
return;
@ -19,7 +20,11 @@ function activateTool(measurementData) {
// When a measurement is selected, it will be activated in Cornerstone's
// tool data
const tool = toolData.data.find(data => data._id === measurementData._id);
tool.active = true;
if (tool) {
tool.active = true;
}
cornerstoneTools.globalImageIdSpecificToolStateManager.restoreToolState(toolState);
};

View File

@ -1,24 +1,26 @@
import { OHIF } from 'meteor/ohif:core';
import { OHIF } from 'meteor/ohif:core'
/**
* Sets all tool data entries value for 'active' to false
* This is used to remove the active color on entire sets of tools
*/
OHIF.measurements.deactivateAllToolData = () => {
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.saveToolState()
Object.keys(toolState).forEach(imageId => {
const toolData = toolState[imageId];
Object.keys(toolState).forEach(imageId => {
const toolData = toolState[imageId];
Object.keys(toolData).forEach(toolType => {
const specificToolData = toolData[toolType];
if (!specificToolData || !specificToolData.data || !specificToolData.data.length) {
return;
}
Object.keys(toolData).forEach(toolType => {
const specificToolData = toolData[toolType]
if (!specificToolData || !specificToolData.data || !specificToolData.data.length) {
return
}
specificToolData.data.forEach(data => {
data.active = false;
});
});
})
};
specificToolData.data.forEach(data => {
data.active = false
});
});
});
cornerstoneTools.globalImageIdSpecificToolStateManager.restoreToolState(toolState)
}

View File

@ -82,8 +82,6 @@ OHIF.measurements.jumpToRowItem = (rowItem, timepoints) => {
// Deactivate stack synchronizer because it will be re-activated later
OHIF.viewer.stackImagePositionOffsetSynchronizer.deactivate();
console.log('jumpToRowItem');
// Retrieve the timepoints that are currently being displayed in the
// Measurement Table
const numTimepoints = Math.max(timepoints.length, 1);
@ -124,7 +122,7 @@ OHIF.measurements.jumpToRowItem = (rowItem, timepoints) => {
try {
enabledElement = cornerstone.getEnabledElement(element)
} catch(error) {
OHIF.log.warn(error);
continue;
}
if (enabledElement && enabledElement.image) {

View File

@ -4,7 +4,7 @@ import { OHIF } from 'meteor/ohif:core';
OHIF.measurements.syncMeasurementAndToolData = measurement => {
OHIF.log.info('syncMeasurementAndToolData');
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
const toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.saveToolState();
const imageId = measurement.imageId;
const toolType = measurement.toolType;
@ -55,4 +55,6 @@ OHIF.measurements.syncMeasurementAndToolData = measurement => {
// Add the MeasurementData into the toolData for this imageId
toolState[imageId][toolType].data.push(measurement);
cornerstoneTools.globalImageIdSpecificToolStateManager.restoreToolState(toolState);
};

View File

@ -71,4 +71,7 @@ Template.viewerMain.onDestroyed(() => {
// Stop prefetching when we close the viewer
instance.studyPrefetcher.destroy();
// Clear references to all stacks in the StackManager
OHIF.viewerbase.stackManager.clearStacks();
});

View File

@ -10,7 +10,7 @@ import { OHIF } from 'meteor/ohif:core';
*/
export const prepareViewerData = ({ studyInstanceUids, timepointId, timepointsFilter={} }) => {
// Clear the cornerstone tool data to sync the measurements with the measurements API
cornerstoneTools.globalImageIdSpecificToolStateManager = cornerstoneTools.newImageIdSpecificToolStateManager();
cornerstoneTools.globalImageIdSpecificToolStateManager.restoreToolState({});
// Retrieve the studies metadata
const promise = new Promise((resolve, reject) => {
@ -109,7 +109,7 @@ const getDataFromTimepoint = timepoint => {
return {
studyInstanceUids: relatedStudies,
timepointIds: timepointIds
timepointIds
};
};