AbstractWidget

Introduction

vtkAbstractWidget is an abstract class to construct a widget.

Methods

activateHandle

Activate a handle, identified by both a state and a representation.
Will also invoke appropriate events.

Argument Type Required Description
locator Yes An object describing the handle to activate.

deactivateAllHandles

Deactivate all the handles on the widget instance.

extend

Method used to decorate a given object (publicAPI+model) with vtkAbstractWidget characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues Yes (default: {})

getBounds

Get the bounds of the widget

getContextVisibility

Get the context visibility.

getHandleVisibility

Returns true if the handles are visible, false otherwise.

getNestedProps

Get all representations of the widget.

getPlaceFactor

Get the place factor.

getRepresentationFromActor

Get the vtkWidgetRepresentation instance associated with the given vtkActor instance.

Argument Type Required Description
actor vtkProp Yes

getRepresentations

Get all the representations of the widget instance.

getViewWidgets

Get all the underlyings view widgets.

getWidgetManager

Get the widget manager associated with the widget instance.

getWidgetState

Get the the state of the widget instance.

grabFocus

Make the widget instance grab the focus.
Should not be called directly or this will lead to unexpected behavior.
To grab the focus on a widget, one should call vtkWidgetManager.grabFocus(widgetInstanceToGiveFocusTo)

hasActor

Returns true if the widget instance holds the given actor, false otherwise.

Argument Type Required Description
actor vtkProp Yes

hasFocus

Returns true if the widget instance holds the focus, false otherwise.

invokeActivateHandle

Invoke the ActivateHandle event with the given payload.

Argument Type Required Description
args Yes The event payload

loseFocus

Make the widget instance release the focus.

newInstance

Method used to create a new instance of vtkAbstractWidget

Argument Type Required Description
initialValues Yes For pre-setting some of its content

onActivateHandle

Register a callback to be invoked when the ActivateHandle event occurs.

Argument Type Required Description
cb EventHandler Yes The callback to register
priority Number No Priority of this subscription

placeWidget

Place a widget at the given bounds.

Argument Type Required Description
bounds Bounds Yes

setContextVisibility

Set the context visibility.

Argument Type Required Description
visible Boolean Yes

setHandleVisibility

Defines if the handles should be visible or not.

Argument Type Required Description
visible Boolean Yes

setPlaceFactor

Set the place factor.

Argument Type Required Description
factor Number Yes The place factor.

setWidgetManager

Set the widget manager associated with the widget instance.

Argument Type Required Description
wm vtkWidgetManager Yes The widget manager instance

updateRepresentationForRender

Update all the widget representations for render.

Argument Type Required Description
renderingType RenderingTypes Yes Default value if RenderingTypes.FRONT_BUFFER

Source

Constants.js
export const WIDGET_PRIORITY = 0.5;

export default { WIDGET_PRIORITY };
index.d.ts
import vtkInteractorObserver from '../../../Rendering/Core/InteractorObserver';
import vtkProp from '../../../Rendering/Core/Prop';
import vtkRenderer from '../../../Rendering/Core/Renderer';
import vtkWidgetManager from '../WidgetManager';
import vtkWidgetRepresentation from '../../Representations/WidgetRepresentation';
import vtkWidgetState from '../WidgetState';
import { Bounds } from '../../../types';
import { RenderingTypes } from '../WidgetManager/Constants';
import { EventHandler, vtkSubscription } from '../../../interfaces';

export interface vtkAbstractWidget extends vtkProp, vtkInteractorObserver {
/**
* Get the bounds of the widget
*/
getBounds(): Bounds;

/**
* Get all representations of the widget.
*/
getNestedProps(): vtkWidgetRepresentation[];

/**
* Activate a handle, identified by both a state and a representation.
* Will also invoke appropriate events.
*
* @param locator An object describing the handle to activate.
*/
activateHandle(locator: {
selectedState: vtkWidgetState;
representation: vtkWidgetRepresentation;
}): void;

/**
* Deactivate all the handles on the widget instance.
*/
deactivateAllHandles(): void;

/**
* Returns true if the widget instance holds the given actor, false otherwise.
*
* @param {vtkProp} actor
*/
hasActor(actor: vtkProp): boolean;

/**
* Make the widget instance grab the focus.
* Should not be called directly or this will lead to unexpected behavior.
* To grab the focus on a widget, one should call `vtkWidgetManager.grabFocus(widgetInstanceToGiveFocusTo)`
*/
grabFocus(): void;

/**
* Make the widget instance release the focus.
*/
loseFocus(): void;

/**
* Returns true if the widget instance holds the focus, false otherwise.
*/
hasFocus(): boolean;

/**
* Place a widget at the given bounds.
*
* @param {Bounds} bounds
*/
placeWidget(bounds: Bounds): void;

/**
* Get the place factor.
*/
getPlaceFactor(): number;

/**
* Set the place factor.
*
* @param {Number} factor The place factor.
*/
setPlaceFactor(factor: number): void;

/**
* Get the `vtkWidgetRepresentation` instance associated with the given `vtkActor` instance.
*
* @param {vtkProp} actor
*/
getRepresentationFromActor(actor: vtkProp): vtkWidgetRepresentation;

/**
* Update all the widget representations for render.
*
* @param {RenderingTypes} renderingType Default value if `RenderingTypes.FRONT_BUFFER`
*/
updateRepresentationForRender(renderingType: RenderingTypes): void;

/**
* Get all the underlyings view widgets.
*/
getViewWidgets(): vtkAbstractWidget[];

/**
* Set the context visibility.
*
* @param {Boolean} visible
*/
setContextVisibility(visible: boolean): void;

/**
* Get the context visibility.
*/
getContextVisibility(): boolean;

/**
* Defines if the handles should be visible or not.
*
* @param {Boolean} visible
*/
setHandleVisibility(visible: boolean): void;

/**
* Returns true if the handles are visible, false otherwise.
*/
getHandleVisibility(): boolean;

/**
* Set the widget manager associated with the widget instance.
*
* @param {vtkWidgetManager} wm The widget manager instance
*/
setWidgetManager(wm: vtkWidgetManager): void;

/**
* Get the widget manager associated with the widget instance.
*/
getWidgetManager(): vtkWidgetManager;

/**
* Get all the representations of the widget instance.
*/
getRepresentations(): vtkWidgetRepresentation[];

/**
* Get the the state of the widget instance.
*/
getWidgetState(): vtkWidgetState;

/**
* Register a callback to be invoked when the `ActivateHandle` event occurs.
*
* @param {EventHandler} cb The callback to register
* @param {Number} [priority] Priority of this subscription
*/
onActivateHandle(
cb: EventHandler,
priority?: number
): Readonly<vtkSubscription>;

/**
* Invoke the `ActivateHandle` event with the given payload.
*
* @param args The event payload
*/
invokeActivateHandle(...args: unknown[]): void;
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkAbstractWidget characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param initialValues (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: object
): void;

/**
* Method used to create a new instance of vtkAbstractWidget
*
* @param initialValues For pre-setting some of its content
*/
export function newInstance(initialValues?: object): vtkAbstractWidget;

/**
* vtkAbstractWidget is an abstract class to construct a widget.
*/
export declare const vtkAbstractWidget: {
newInstance: typeof newInstance;
extend: typeof extend;
};

export default vtkAbstractWidget;
index.js
import macro from 'vtk.js/Sources/macros';
import vtkInteractorObserver from 'vtk.js/Sources/Rendering/Core/InteractorObserver';
import vtkProp from 'vtk.js/Sources/Rendering/Core/Prop';

import { RenderingTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';
import { WIDGET_PRIORITY } from 'vtk.js/Sources/Widgets/Core/AbstractWidget/Constants';

// ----------------------------------------------------------------------------

function vtkAbstractWidget(publicAPI, model) {
model.classHierarchy.push('vtkAbstractWidget');
model.actorToRepresentationMap = new WeakMap();

// --------------------------------------------------------------------------
publicAPI.getBounds = model.widgetState.getBounds;
publicAPI.getNestedProps = () => model.representations;
// --------------------------------------------------------------------------

publicAPI.activateHandle = ({ selectedState, representation }) => {
model.widgetState.activateOnly(selectedState);
model.activeState = selectedState;
if (selectedState && selectedState.updateManipulator) {
selectedState.updateManipulator();
}
publicAPI.invokeActivateHandle({ selectedState, representation });
if (publicAPI.updateCursor) {
publicAPI.updateCursor();
}
};

// --------------------------------------------------------------------------

publicAPI.deactivateAllHandles = () => {
model.widgetState.deactivate();
};

// --------------------------------------------------------------------------

publicAPI.hasActor = (actor) => model.actorToRepresentationMap.has(actor);

// --------------------------------------------------------------------------

publicAPI.grabFocus = () => {
model.hasFocus = true;
};
publicAPI.loseFocus = () => {
model.hasFocus = false;
};
publicAPI.hasFocus = () => model.hasFocus;

// --------------------------------------------------------------------------

publicAPI.placeWidget = (bounds) => model.widgetState.placeWidget(bounds);
publicAPI.getPlaceFactor = () => model.widgetState.getPlaceFactor();
publicAPI.setPlaceFactor = (factor) =>
model.widgetState.setPlaceFactor(factor);

// --------------------------------------------------------------------------

publicAPI.getRepresentationFromActor = (actor) =>
model.actorToRepresentationMap.get(actor);

// --------------------------------------------------------------------------

publicAPI.updateRepresentationForRender = (
renderingType = RenderingTypes.FRONT_BUFFER
) => {
for (let i = 0; i < model.representations.length; i++) {
const representation = model.representations[i];
representation.updateActorVisibility(
renderingType,
model.contextVisibility,
model.handleVisibility
);
}
};

publicAPI.getViewWidgets = () => model._factory.getViewWidgets();

// --------------------------------------------------------------------------
// Initialization calls
// --------------------------------------------------------------------------

publicAPI.setPriority(WIDGET_PRIORITY);
}

// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
contextVisibility: true,
handleVisibility: true,
hasFocus: false,
};

/**
* @param {*} publicAPI public methods to populate
* @param {*} model internal values to populate
* @param {object} initialValues Contains at least
* {viewType, _renderer, _camera, _openGLRenderWindow, _factory}
*/
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

vtkProp.extend(publicAPI, model, initialValues);
vtkInteractorObserver.extend(publicAPI, model, initialValues);

macro.setGet(publicAPI, model, [
'contextVisibility',
'handleVisibility',
'_widgetManager',
]);
macro.get(publicAPI, model, [
'representations',
'widgetState',
'activeState', // stores the last activated sub state(handle)
]);
macro.moveToProtected(publicAPI, model, ['widgetManager']);
macro.event(publicAPI, model, 'ActivateHandle');

vtkAbstractWidget(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend, 'vtkAbstractWidget');

// ----------------------------------------------------------------------------

export default { newInstance, extend };