Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating proper values for ChangeType parameter in AnnotationModifiedEventDetail. #1821

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions common/reviews/api/tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,8 @@ enum ChangeTypes {
MetadataReferenceModified = "MetadataReferenceModified",
// (undocumented)
StatsUpdated = "StatsUpdated"
// (undocumented)
LabelChange = 'LabelChange',
}

// @public (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions packages/tools/src/enums/ChangeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ enum ChangeTypes {
* so as to cause it to be drawn on the change, or removed if it is no longer visible.
*/
MetadataReferenceModified = 'MetadataReferenceModified',
/**
* Occurs when an annotation label is updated.
*/
LabelChange = 'LabelChange',
}

export default ChangeTypes;
13 changes: 11 additions & 2 deletions packages/tools/src/tools/SculptorTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import type {
ContourAnnotation,
} from '../types';
import { point } from '../utilities/math';
import { Events, ToolModes, AnnotationStyleStates } from '../enums';
import {
Events,
ToolModes,
AnnotationStyleStates,
ChangeTypes,
} from '../enums';
import { triggerAnnotationRenderForViewportIds } from '../utilities/triggerAnnotationRenderForViewportIds';
import {
hideElementCursor,
Expand Down Expand Up @@ -428,7 +433,11 @@ class SculptorTool extends BaseTool {
activeAnnotation.invalidated = true;
}

triggerAnnotationModified(activeAnnotation, element);
triggerAnnotationModified(
activeAnnotation,
element,
ChangeTypes.HandlesUpdated
);
};

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/tools/src/tools/annotation/AngleTool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Events } from '../../enums';
import { ChangeTypes, Events } from '../../enums';
import {
getEnabledElement,
utilities as csUtils,
Expand Down Expand Up @@ -477,6 +477,14 @@ class AngleTool extends AnnotationTool {
const { renderingEngine } = enabledElement;

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

if (annotation.invalidated) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}
};

cancel = (element: HTMLDivElement) => {
Expand Down Expand Up @@ -874,7 +882,7 @@ class AngleTool extends AnnotationTool {
annotation.invalidated = false;

// Dispatching annotation modified
triggerAnnotationModified(annotation, element);
triggerAnnotationModified(annotation, element, ChangeTypes.StatsUpdated);

return cachedStats;
}
Expand Down
19 changes: 14 additions & 5 deletions packages/tools/src/tools/annotation/ArrowAnnotateTool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Events } from '../../enums';
import { ChangeTypes, Events } from '../../enums';
import {
getEnabledElement,
utilities as csUtils,
Expand Down Expand Up @@ -352,8 +352,13 @@ class ArrowAnnotateTool extends AnnotationTool {
const eventDetail = evt.detail;
const { element } = eventDetail;

const { annotation, viewportIdsToRender, newAnnotation, hasMoved } =
this.editData;
const {
annotation,
viewportIdsToRender,
newAnnotation,
hasMoved,
movingTextBox,
} = this.editData;
const { data } = annotation;

if (newAnnotation && !hasMoved) {
Expand Down Expand Up @@ -392,8 +397,12 @@ class ArrowAnnotateTool extends AnnotationTool {

triggerAnnotationRenderForViewportIds(viewportIdsToRender);
});
} else {
triggerAnnotationModified(annotation, element);
} else if (!movingTextBox) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}

this.doneEditMemo();
Expand Down
13 changes: 11 additions & 2 deletions packages/tools/src/tools/annotation/BidirectionalTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
drawLinkedTextBox as drawLinkedTextBoxSvg,
} from '../../drawingSvg';
import { state } from '../../store/state';
import { Events } from '../../enums';
import { ChangeTypes, Events } from '../../enums';
import { getViewportIdsWithToolToRender } from '../../utilities/viewportFilters';
import * as lineSegment from '../../utilities/math/line';
import { getTextBoxCoordsCanvas } from '../../utilities/drawing';
Expand Down Expand Up @@ -568,6 +568,7 @@ class BidirectionalTool extends AnnotationTool {
annotation.invalidated = true;
triggerAnnotationRenderForViewportIds(viewportIdsToRender);

triggerAnnotationModified(annotation, element, ChangeTypes.HandlesUpdated);
this.editData.hasMoved = true;
};

Expand Down Expand Up @@ -620,6 +621,14 @@ class BidirectionalTool extends AnnotationTool {
}

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

if (annotation.invalidated) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}
};

/**
Expand Down Expand Up @@ -1313,7 +1322,7 @@ class BidirectionalTool extends AnnotationTool {
annotation.invalidated = false;

// Dispatching annotation modified
triggerAnnotationModified(annotation, element);
triggerAnnotationModified(annotation, element, ChangeTypes.StatsUpdated);

return cachedStats;
};
Expand Down
14 changes: 12 additions & 2 deletions packages/tools/src/tools/annotation/CircleROITool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
drawLinkedTextBox as drawLinkedTextBoxSvg,
} from '../../drawingSvg';
import { state } from '../../store/state';
import { Events } from '../../enums';
import { ChangeTypes, Events } from '../../enums';
import { getViewportIdsWithToolToRender } from '../../utilities/viewportFilters';
import { getTextBoxCoordsCanvas } from '../../utilities/drawing';
import getWorldWidthAndHeightFromTwoPoints from '../../utilities/planar/getWorldWidthAndHeightFromTwoPoints';
Expand Down Expand Up @@ -431,6 +431,8 @@ class CircleROITool extends AnnotationTool {
this.editData.hasMoved = true;

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

triggerAnnotationModified(annotation, element, ChangeTypes.HandlesUpdated);
};

_dragModifyCallback = (evt: EventTypes.InteractionEventType): void => {
Expand Down Expand Up @@ -482,6 +484,14 @@ class CircleROITool extends AnnotationTool {
const { renderingEngine } = enabledElement;

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

if (annotation.invalidated) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}
};

_dragHandle = (evt: EventTypes.InteractionEventType): void => {
Expand Down Expand Up @@ -1012,7 +1022,7 @@ class CircleROITool extends AnnotationTool {
annotation.invalidated = false;

// Dispatching annotation modified
triggerAnnotationModified(annotation, element);
triggerAnnotationModified(annotation, element, ChangeTypes.StatsUpdated);

return cachedStats;
};
Expand Down
12 changes: 10 additions & 2 deletions packages/tools/src/tools/annotation/CobbAngleTool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { vec3 } from 'gl-matrix';
import { Events } from '../../enums';
import { ChangeTypes, Events } from '../../enums';
import { getEnabledElement } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';

Expand Down Expand Up @@ -477,6 +477,14 @@ class CobbAngleTool extends AnnotationTool {
const { renderingEngine } = enabledElement;

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

if (annotation.invalidated) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}
};

cancel = (element: HTMLDivElement) => {
Expand Down Expand Up @@ -1024,7 +1032,7 @@ class CobbAngleTool extends AnnotationTool {
annotation.invalidated = false;

// Dispatching annotation modified
triggerAnnotationModified(annotation, element);
triggerAnnotationModified(annotation, element, ChangeTypes.StatsUpdated);

return cachedStats;
}
Expand Down
13 changes: 11 additions & 2 deletions packages/tools/src/tools/annotation/EllipticalROITool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
drawLinkedTextBox as drawLinkedTextBoxSvg,
} from '../../drawingSvg';
import { state } from '../../store/state';
import { Events } from '../../enums';
import { ChangeTypes, Events } from '../../enums';
import { getViewportIdsWithToolToRender } from '../../utilities/viewportFilters';
import { getTextBoxCoordsCanvas } from '../../utilities/drawing';
import getWorldWidthAndHeightFromTwoPoints from '../../utilities/planar/getWorldWidthAndHeightFromTwoPoints';
Expand Down Expand Up @@ -550,6 +550,7 @@ class EllipticalROITool extends AnnotationTool {
this.editData.hasMoved = true;

triggerAnnotationRenderForViewportIds(viewportIdsToRender);
triggerAnnotationModified(annotation, element, ChangeTypes.HandlesUpdated);
};

_dragModifyCallback = (evt: EventTypes.InteractionEventType): void => {
Expand Down Expand Up @@ -601,6 +602,14 @@ class EllipticalROITool extends AnnotationTool {
const { renderingEngine } = enabledElement;

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

if (annotation.invalidated) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}
};

_dragHandle = (evt: EventTypes.InteractionEventType): void => {
Expand Down Expand Up @@ -1163,7 +1172,7 @@ class EllipticalROITool extends AnnotationTool {
annotation.invalidated = false;

// Dispatching annotation modified
triggerAnnotationModified(annotation, element);
triggerAnnotationModified(annotation, element, ChangeTypes.StatsUpdated);

return cachedStats;
};
Expand Down
12 changes: 10 additions & 2 deletions packages/tools/src/tools/annotation/LengthTool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Events } from '../../enums';
import { Events, ChangeTypes } from '../../enums';
import {
getEnabledElement,
utilities as csUtils,
Expand Down Expand Up @@ -481,6 +481,14 @@ class LengthTool extends AnnotationTool {
this.editData.hasMoved = true;

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

if (annotation.invalidated) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}
};

cancel = (element: HTMLDivElement) => {
Expand Down Expand Up @@ -888,7 +896,7 @@ class LengthTool extends AnnotationTool {
annotation.invalidated = false;

// Dispatching annotation modified
triggerAnnotationModified(annotation, element);
triggerAnnotationModified(annotation, element, ChangeTypes.StatsUpdated);

return cachedStats;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/tools/src/tools/annotation/PlanarFreehandROITool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,10 @@ class PlanarFreehandROITool extends ContourSegmentationBaseTool {

if (!this.commonData?.movingTextBox) {
const { data } = annotation;
const { closed } = data.contour;
if (
!data.cachedStats[targetId] ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about !data.cachedStates[targetId]?.areaUnit
The areaUnit should be a non empty string, even for unknown units as that will be pixels

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there is an existing issue that calculateCachedStats is always called for each mouse movement of open freenad annotation. This is due to the areaUnit for open freehand ROIs in cachedStates is always undefined.

As this condition is always true for Open freehand annotations, resulting in the unintended activation of an annotationModified event, we are restricting the second condition for closed freehand only.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should probably check on unit rather than areaUnit, and always make sure we specify unit in it - that should be available for both closed and open contours. I believe it is not currently set for open contours, but it should be.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be an equivalent check for open contours, and I'd really rather have a single check that works for both.

data.cachedStats[targetId].areaUnit == null
(closed && !data.cachedStats[targetId]?.areaUnit)
) {
data.cachedStats[targetId] = {
Modality: null,
Expand Down
12 changes: 10 additions & 2 deletions packages/tools/src/tools/annotation/RectangleROITool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
drawRectByCoordinates as drawRectSvg,
} from '../../drawingSvg';
import { state } from '../../store/state';
import { Events } from '../../enums';
import { ChangeTypes, Events } from '../../enums';
import { getViewportIdsWithToolToRender } from '../../utilities/viewportFilters';
import * as rectangle from '../../utilities/math/rectangle';
import { getTextBoxCoordsCanvas } from '../../utilities/drawing';
Expand Down Expand Up @@ -477,6 +477,14 @@ class RectangleROITool extends AnnotationTool {
const enabledElement = getEnabledElement(element);

triggerAnnotationRenderForViewportIds(viewportIdsToRender);

if (annotation.invalidated) {
triggerAnnotationModified(
annotation,
element,
ChangeTypes.HandlesUpdated
);
}
};

cancel = (element: HTMLDivElement) => {
Expand Down Expand Up @@ -954,7 +962,7 @@ class RectangleROITool extends AnnotationTool {
annotation.invalidated = false;

// Dispatching annotation modified
triggerAnnotationModified(annotation, element);
triggerAnnotationModified(annotation, element, ChangeTypes.StatsUpdated);

return cachedStats;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/tools/src/types/AnnotationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type Annotation = {
[key: string]: unknown;
/** Cached Annotation statistics which is specific to the tool */
cachedStats?: Record<string, unknown>;
/** Label of an annotation */
label?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inconsistent with the label in LengthTool - label belongs to the data element. Note the difference between CS3D and OHIF - in CS3D the data is in the data object, while in OHIF it is at the top level.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The label is already defined inside the data element as you suggested.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, was seeing the nesting wrong. Could you move the handles to it's own type - I think that is sufficiently long to be worth having by itself. That would resolve the issue with having it hard to see the nesting.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I like that a lot better. I never like having to try to manually figure out the indentation to figure out what is what.

};
};

Expand Down
2 changes: 2 additions & 0 deletions packages/tools/src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const roundNumber = utilities.roundNumber;
import normalizeViewportPlane from './normalizeViewportPlane';
import IslandRemoval from './segmentation/islandRemoval';
import { getPixelValueUnits } from './getPixelValueUnits';
import setAnnotationLabel from './setAnnotationLabel';

export {
math,
Expand Down Expand Up @@ -96,4 +97,5 @@ export {
pointInSurroundingSphereCallback,
normalizeViewportPlane,
IslandRemoval,
setAnnotationLabel,
};
12 changes: 12 additions & 0 deletions packages/tools/src/utilities/setAnnotationLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Annotation } from '../types/AnnotationTypes';
import { triggerAnnotationModified } from '../stateManagement/annotation/helpers/state';
import { ChangeTypes } from '../enums';

export default function setAnnotationLabel(
annotation: Annotation,
element: HTMLDivElement,
updatedLabel: string
) {
annotation.data.label = updatedLabel;
triggerAnnotationModified(annotation, element, ChangeTypes.LabelChange);
}