ohif-viewer/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js
2017-06-14 09:19:49 +02:00

266 lines
8.1 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import { $ } from 'meteor/jquery';
import { _ } from 'meteor/underscore';
import { OHIF } from 'meteor/ohif:core';
import { toolManager } from './toolManager';
import { switchToImageRelative } from './switchToImageRelative';
import { switchToImageByIndex } from './switchToImageByIndex';
import { viewportUtils } from './viewportUtils';
import { panelNavigation } from './panelNavigation';
import { WLPresets } from './WLPresets';
// TODO: add this to namespace definitions
Meteor.startup(function() {
OHIF.viewer.loadIndicatorDelay = 200;
OHIF.viewer.defaultTool = 'wwwc';
OHIF.viewer.refLinesEnabled = true;
OHIF.viewer.isPlaying = {};
OHIF.viewer.cine = {
framesPerSecond: 24,
loop: true
};
OHIF.viewer.defaultHotkeys = {
// Tool hotkeys
defaultTool: 'ESC',
zoom: 'Z',
wwwc: 'W',
pan: 'P',
angle: 'A',
stackScroll: 'S',
magnify: 'M',
length: '',
annotate: '',
dragProbe: '',
ellipticalRoi: '',
rectangleRoi: '',
spine: '',
// Viewport hotkeys
flipH: 'H',
flipV: 'V',
rotateR: 'R',
rotateL: 'L',
invert: 'I',
zoomIn: '',
zoomOut: '',
zoomToFit: '',
resetViewport: '',
clearTools: '',
// Viewport navigation hotkeys
scrollDown: 'DOWN',
scrollUp: 'UP',
scrollLastImage: '',
scrollFirstImage: '',
previousDisplaySet: 'PAGEUP',
nextDisplaySet: 'PAGEDOWN',
nextPanel: 'RIGHT',
previousPanel: 'LEFT',
// Miscellaneous hotkeys
toggleOverlayTags: 'SHIFT',
toggleCinePlay: 'SPACE',
toggleCineDialog: '',
// Preset hotkeys
WLPresetSoftTissue: '1',
WLPresetLung: '2',
WLPresetLiver: '3',
WLPresetBone: '4',
WLPresetBrain: '5'
};
// For now
OHIF.viewer.hotkeys = OHIF.viewer.defaultHotkeys;
// Create commands context for viewer
const contextName = 'viewer';
OHIF.commands.createContext(contextName);
// Create a function that returns true if the active viewport is empty
const isActiveViewportEmpty = () => {
const activeViewport = Session.get('activeViewport') || 0;
return $('.imageViewerViewport').eq(activeViewport).hasClass('empty');
};
// Functions to register the tool switching commands
const registerToolCommands = map => _.each(map, (commandName, toolId) => {
OHIF.commands.register(contextName, toolId, {
name: commandName,
action: toolManager.setActiveTool,
params: toolId
});
});
// Register the default tool command
OHIF.commands.register(contextName, 'defaultTool', {
name: 'Default Tool',
action: () => toolManager.setActiveTool(toolManager.getDefaultTool())
});
// Register the tool switching commands
registerToolCommands({
wwwc: 'Window W/L',
zoom: 'Zoom',
angle: 'Angle Measurement',
dragProbe: 'Pixel Probe',
ellipticalRoi: 'Elliptical Probe',
rectangleRoi: 'Rectangle',
magnify: 'Magnify',
annotate: 'Annotate',
stackScroll: 'Scroll Stack',
pan: 'Pan',
length: 'Length Measurement',
spine: 'Spine Labels',
wwwcRegion: 'W/L by Region'
});
// Functions to register the viewport commands
const registerViewportCommands = map => _.each(map, (commandName, commandId) => {
OHIF.commands.register(contextName, commandId, {
name: commandName,
action: viewportUtils[commandId],
disabled: isActiveViewportEmpty
});
});
// Register the viewport commands
registerViewportCommands({
zoomIn: 'Zoom In',
zoomOut: 'Zoom Out',
zoomToFit: 'Zoom to Fit',
invert: 'Invert',
flipH: 'Flip Horizontally',
flipV: 'Flip Vertically',
rotateR: 'Rotate Right',
rotateL: 'Rotate Left',
resetViewport: 'Reset',
clearTools: 'Clear Tools'
});
// Functions to register the preset switching commands
const registerWLPresetCommands = map => _.each(map, (commandName, presetName) => {
OHIF.commands.register(contextName, presetName, {
name: commandName,
action: WLPresets.applyWLPresetToActiveElement,
params: presetName.replace('WLPreset', '')
});
});
// Register the preset switching commands
registerWLPresetCommands({
WLPresetSoftTissue: 'W/L Preset: Soft Tissue',
WLPresetLung: 'W/L Preset: Lung',
WLPresetLiver: 'W/L Preset: Liver',
WLPresetBone: 'W/L Preset: Bone',
WLPresetBrain: 'W/L Preset: Brain'
});
// Register viewport navigation commands
OHIF.commands.set(contextName, {
scrollDown: {
name: 'Scroll Down',
action: () => !isActiveViewportEmpty() && switchToImageRelative(1)
},
scrollUp: {
name: 'Scroll Up',
action: () => !isActiveViewportEmpty() && switchToImageRelative(-1)
},
scrollFirstImage: {
name: 'Scroll to First Image',
action: () => !isActiveViewportEmpty() && switchToImageByIndex(0)
},
scrollLastImage: {
name: 'Scroll to Last Image',
action: () => !isActiveViewportEmpty() && switchToImageByIndex(-1)
},
previousDisplaySet: {
name: 'Previous Series',
action: () => OHIF.viewerbase.layoutManager.moveDisplaySets(false),
disabled: () => !OHIF.viewerbase.layoutManager.canMoveDisplaySets(false)
},
nextDisplaySet: {
name: 'Next Series',
action: () => OHIF.viewerbase.layoutManager.moveDisplaySets(true),
disabled: () => !OHIF.viewerbase.layoutManager.canMoveDisplaySets(true)
},
nextPanel: {
name: 'Next Panel',
action: () => panelNavigation.loadNextActivePanel()
},
previousPanel: {
name: 'Previous Panel',
action: () => panelNavigation.loadPreviousActivePanel()
}
}, true);
// Register miscellaneous commands
OHIF.commands.set(contextName, {
toggleOverlayTags: {
name: 'Toggle Image Annotations',
action() {
const $dicomTags = $('.imageViewerViewportOverlay .dicomTag');
$dicomTags.toggle($dicomTags.eq(0).css('display') === 'none');
}
},
toggleCinePlay: {
name: 'Play/Pause Cine',
action: viewportUtils.toggleCinePlay,
disabled: OHIF.viewerbase.viewportUtils.hasMultipleFrames
},
toggleCineDialog: {
name: 'Show/Hide Cine Controls',
action: viewportUtils.toggleCineDialog,
disabled: OHIF.viewerbase.viewportUtils.hasMultipleFrames
}
}, true);
OHIF.viewer.hotkeyFunctions = {};
OHIF.viewer.loadedSeriesData = {};
});
// Define a jQuery reverse function
$.fn.reverse = [].reverse;
/**
* Overrides OHIF's refLinesEnabled
* @param {Boolean} refLinesEnabled True to enable and False to disable
*/
function setOHIFRefLines(refLinesEnabled) {
OHIF.viewer.refLinesEnabled = refLinesEnabled;
}
/**
* Overrides OHIF's hotkeys
* @param {Object} hotkeys Object with hotkeys mapping
*/
function setOHIFHotkeys(hotkeys) {
OHIF.viewer.hotkeys = hotkeys;
}
/**
* Binds all hotkeys keydown events to the tasks defined in
* OHIF.viewer.hotkeys or a given param
* @param {Object} hotkeys hotkey and task mapping (not required). If not given, uses OHIF.viewer.hotkeys
*/
function enableHotkeys(hotkeys) {
const definitions = hotkeys || OHIF.viewer.hotkeys;
OHIF.hotkeys.set('viewer', definitions, true);
OHIF.context.set('viewer');
}
/**
* Export functions inside hotkeyUtils namespace.
*/
const hotkeyUtils = {
setOHIFRefLines, /* @TODO: find a better place for this... */
setOHIFHotkeys,
enableHotkeys
};
export { hotkeyUtils };