Skip to content

Commit

Permalink
Move annotationGroup to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Jul 25, 2024
1 parent 0971d86 commit b471542
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 237 deletions.
2 changes: 1 addition & 1 deletion src/app/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {toolList, defaultToolList, toolOptions} from '../tools';
import {binderList} from '../gui/stage';
import {WindowLevel} from '../image/windowLevel';
import {PlaneHelper} from '../image/planeHelper';
import {AnnotationGroup} from '../image/annotation';
import {AnnotationGroup} from '../image/annotationGroup';

// doc imports
/* eslint-disable no-unused-vars */
Expand Down
2 changes: 1 addition & 1 deletion src/app/dataController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {mergeObjects} from '../utils/operator';
// doc imports
/* eslint-disable no-unused-vars */
import {Image} from '../image/image';
import {AnnotationGroup} from '../image/annotation';
import {AnnotationGroup} from '../image/annotationGroup';
/* eslint-enable no-unused-vars */

/**
Expand Down
2 changes: 1 addition & 1 deletion src/app/drawController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AnnotationGroup} from '../image/annotation';
import {AnnotationGroup} from '../image/annotationGroup';
import {
RemoveAnnotationCommand,
UpdateAnnotationCommand
Expand Down
3 changes: 2 additions & 1 deletion src/gui/drawLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {Index} from '../math/index';
import {Vector3D} from '../math/vector';
import {Scalar2D, Scalar3D} from '../math/scalar';
import {PlaneHelper} from '../image/planeHelper';
import {Annotation, AnnotationGroup} from '../image/annotation';
import {Annotation} from '../image/annotation';
import {AnnotationGroup} from '../image/annotationGroup';
import {DrawShapeHandler} from '../tools/drawShapeHandler';
/* eslint-enable no-unused-vars */

Expand Down
228 changes: 0 additions & 228 deletions src/image/annotation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {logger} from '../utils/logger';
import {ListenerHandler} from '../utils/listen';
import {getFlags, replaceFlags} from '../utils/string';
import {CircleFactory} from '../tools/circle';
import {Circle} from '../math/circle';
Expand Down Expand Up @@ -224,230 +223,3 @@ export class Annotation {
return res;
}
}

/**
* Annotation group.
*/
export class AnnotationGroup {
/**
* @type {Annotation[]}
*/
#list;

/**
* Annotation meta data.
*
* @type {Object<string, any>}
*/
#meta = {};

/**
* Listener handler.
*
* @type {ListenerHandler}
*/
#listenerHandler = new ListenerHandler();

/**
* Editable flag.
*
* @type {boolean}
*/
#editable;

/**
* @param {Annotation[]} [list] Optional list, will
* create new if not provided.
*/
constructor(list) {
if (typeof list !== 'undefined') {
this.#list = list;
} else {
this.#list = [];
}
this.#editable = true;
}

/**
* Get the annotation group as an array.
*
* @returns {Annotation[]} The array.
*/
getList() {
return this.#list;
}

/**
* Get the number of annotations of this list.
*
* @returns {number} The number of annotations.
*/
getLength() {
return this.#list.length;
}

/**
* Check if the annotation group is editable.
*
* @returns {boolean} True if editable.
*/
isEditable() {
return this.#editable;
}

/**
* Set the annotation group editability.
*
* @param {boolean} flag True to make the annotation group editable.
*/
setEditable(flag) {
this.#editable = flag;
this.#fireEvent({
type: 'annotationgroupeditablechange',
data: flag
});
}

/**
* Add a new annotation.
*
* @param {Annotation} annotation The annotation to add.
*/
add(annotation) {
this.#list.push(annotation);
this.#fireEvent({
type: 'annotationadd',
data: annotation
});
}

/**
* Update an existing annotation.
*
* @param {Annotation} annotation The annotation to update.
* @param {string[]} [propKeys] Optional properties that got updated.
*/
update(annotation, propKeys) {
const index = this.#list.findIndex((item) => item.id === annotation.id);
if (index !== -1) {
this.#list[index] = annotation;
this.#fireEvent({
type: 'annotationupdate',
data: annotation,
keys: propKeys
});
} else {
logger.warn('Cannot find annotation to update');
}
}

/**
* Remove an annotation.
*
* @param {string} id The id of the annotation to remove.
*/
remove(id) {
const index = this.#list.findIndex((item) => item.id === id);
if (index !== -1) {
const annotation = this.#list.splice(index, 1)[0];
this.#fireEvent({
type: 'annotationremove',
data: annotation
});
} else {
logger.warn('Cannot find annotation to remove');
}
}

/**
* Set the associated view controller.
*
* @param {ViewController} viewController The associated view controller.
*/
setViewController(viewController) {
for (const item of this.#list) {
item.setViewController(viewController);
item.updateQuantification();
}
}

/**
* Find an annotation.
*
* @param {string} id The id of the annotation to find.
* @returns {Annotation|undefined} The found annotation.
*/
find(id) {
return this.#list.find((item) => item.id === id);
}

/**
* Get the meta data.
*
* @returns {Object<string, any>} The meta data.
*/
getMeta() {
return this.#meta;
}

/**
* Check if this list contains a meta data value.
*
* @param {string} key The key to check.
* @returns {boolean} True if the meta data is present.
*/
hasMeta(key) {
return typeof this.#meta[key] !== 'undefined';
}

/**
* Get a meta data value.
*
* @param {string} key The meta data key.
* @returns {string|object} The meta data value.
*/
getMetaValue(key) {
return this.#meta[key];
}

/**
* Set a meta data.
*
* @param {string} key The meta data key.
* @param {string|object} value The value of the meta data.
*/
setMetaValue(key, value) {
this.#meta[key] = value;
}

/**
* Add an event listener to this class.
*
* @param {string} type The event type.
* @param {Function} callback The function associated with the provided
* event type, will be called with the fired event.
*/
addEventListener(type, callback) {
this.#listenerHandler.add(type, callback);
}

/**
* Remove an event listener from this class.
*
* @param {string} type The event type.
* @param {Function} callback The function associated with the provided
* event type.
*/
removeEventListener(type, callback) {
this.#listenerHandler.remove(type, callback);
}

/**
* Fire an event: call all associated listeners with the input event object.
*
* @param {object} event The event to fire.
*/
#fireEvent = (event) => {
this.#listenerHandler.fireEvent(event);
};
}
3 changes: 2 additions & 1 deletion src/image/annotationFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
} from '../dicom/dicomSpatialCoordinate';
import {guid} from '../math/stats';
import {logger} from '../utils/logger';
import {Annotation, AnnotationGroup} from './annotation';
import {Annotation} from './annotation';
import {AnnotationGroup} from './annotationGroup';
import {Line} from '../math/line';
import {Point2D} from '../math/point';

Expand Down
Loading

0 comments on commit b471542

Please sign in to comment.