application.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import 'package:fis_measure/interfaces/date_types/point.dart';
  2. import 'package:fis_measure/interfaces/enums/annotation.dart';
  3. import 'package:fis_measure/interfaces/enums/operate.dart';
  4. import 'package:fis_measure/interfaces/process/annotations/annotation.dart';
  5. import 'package:fis_measure/interfaces/process/items/item.dart';
  6. import 'package:fis_measure/interfaces/process/items/item_feature.dart';
  7. import 'package:fis_measure/interfaces/process/items/measure_terms.dart';
  8. import 'package:fis_measure/interfaces/process/visuals/visual_area.dart';
  9. import 'package:fis_measure/interfaces/process/visuals/visual.dart';
  10. import 'package:fis_measure/interfaces/process/viewports/viewport.dart';
  11. import 'package:fis_measure/interfaces/process/modes/mode.dart';
  12. import 'package:fis_common/event/event_type.dart';
  13. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  14. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  15. import 'package:fis_measure/process/annotations/arrow_annotation.dart';
  16. import 'package:fis_measure/process/annotations/input_annotation.dart';
  17. import 'package:fis_measure/process/annotations/label_annotation.dart';
  18. import 'package:fis_measure/process/primitives/location.dart';
  19. import 'package:fis_measure/process/primitives/straightline.dart';
  20. import 'package:fis_measure/process/visual/tissue_area.dart';
  21. import 'package:flutter/foundation.dart';
  22. import 'package:flutter/painting.dart';
  23. import 'package:vid/us/vid_us_image.dart';
  24. import 'package:vid/us/vid_us_probe.dart';
  25. import 'package:vid/us/vid_us_unit.dart';
  26. import '../primitives/poyline.dart';
  27. import '../visual/visual.dart';
  28. import 'visual_loader.dart';
  29. class Application implements IApplication {
  30. // ignore: constant_identifier_names
  31. static const C_VID_THIRDPART_NAME = "ThirdPart";
  32. late VidUsProbe _probe;
  33. VidUsImage? _frame;
  34. List<IVisual>? _visuals;
  35. IMeasureItem? _activeMeasureItem;
  36. IAnnotationItem? _activeAnnotationItem;
  37. bool _canOperate = false;
  38. Size _displaySize = Size.zero;
  39. MeasureOperateType _currOpType = MeasureOperateType.measure;
  40. final Set<IMeasureItem> _measureItems = {};
  41. final Set<IAnnotationItem> _annotationItems = {};
  42. Application(VidUsProbe probe) {
  43. _probe = probe;
  44. currentModeChanged = FEventHandler<IMode>();
  45. visualAreaChanged = FEventHandler<IVisualArea>();
  46. canMeasureChanged = FEventHandler<bool>();
  47. activeMeasureItemChanged = FEventHandler<IMeasureItem?>();
  48. activeAnnotationItemChanged = FEventHandler<IAnnotationItem?>();
  49. updateRenderReady = FEventHandler<void>();
  50. operateTypeChanged = FEventHandler<MeasureOperateType>();
  51. }
  52. @override
  53. bool get canMeasure => _canOperate;
  54. @override
  55. set canMeasure(bool value) {
  56. if (value != _canOperate) {
  57. _canOperate = value;
  58. _doCanMeasureChanged();
  59. }
  60. }
  61. @override
  62. bool get canOperate => _canOperate;
  63. @override
  64. set canOperate(bool value) {
  65. if (value != _canOperate) {
  66. _canOperate = value;
  67. _doCanMeasureChanged();
  68. }
  69. }
  70. /// 是否扇形探头
  71. bool get isProbeConvex => probe.type == VidUsProbeType.Convex;
  72. @override
  73. VidUsProbe get probe => _probe;
  74. @override
  75. String get applicationName => _probe.application.applicationName;
  76. @override
  77. String get categoryName => _probe.application.applicationCategoryName;
  78. @override
  79. bool get isThirdPart => probe.name == C_VID_THIRDPART_NAME;
  80. @override
  81. IMode get currentMode => currentVisualArea.mode;
  82. @override
  83. IViewPort get currentViewPort => currentVisualArea.viewport!;
  84. @override
  85. IVisual get currentVisual => visuals.firstWhere((e) => e.activeArea != null);
  86. @override
  87. IVisualArea get currentVisualArea => currentVisual.activeArea!;
  88. @override
  89. VidUsImage? get frameData => _frame;
  90. @override
  91. List<IVisual> get visuals => _visuals!;
  92. @override
  93. Set<IMeasureItem> get measureItems => _measureItems;
  94. @override
  95. Set<IAnnotationItem> get annotationItems => _annotationItems;
  96. @override
  97. MeasureOperateType get currentOperateType => _currOpType;
  98. @override
  99. Size get displaySize => _displaySize;
  100. @override
  101. set displaySize(Size value) {
  102. if (value != _displaySize) {
  103. _displaySize = value;
  104. }
  105. }
  106. @override
  107. double get displayScaleRatio {
  108. if (frameData != null) {
  109. return displaySize.width / frameData!.width;
  110. }
  111. return 1.0;
  112. }
  113. @override
  114. List<IMode> get avaliableModes {
  115. final modes = <IMode>[];
  116. for (var visual in visuals) {
  117. modes.addAll(visual.modes);
  118. }
  119. return modes;
  120. }
  121. @override
  122. IMeasureItem? get activeMeasureItem => _activeMeasureItem;
  123. set activeMeasureItem(IMeasureItem? value) {
  124. if (value != _activeMeasureItem) {
  125. // 解绑失活测量项事件监听
  126. _activeMeasureItem?.featureUpdated
  127. .removeListener(_onActiveMeasureItemFeatureUpdated);
  128. _activeMeasureItem = value;
  129. if (_activeMeasureItem != null) {
  130. _measureItems.add(_activeMeasureItem!);
  131. // 添加活动测量项事件监听
  132. _activeMeasureItem!.featureUpdated
  133. .addListener(_onActiveMeasureItemFeatureUpdated);
  134. }
  135. // 通知更新事件
  136. activeMeasureItemChanged.emit(this, value);
  137. _updateRender();
  138. }
  139. }
  140. @override
  141. IAnnotationItem? get activeAnnotationItem => _activeAnnotationItem;
  142. set activeAnnotationItem(IAnnotationItem? value) {
  143. if (value != _activeAnnotationItem) {
  144. // 解绑失活注释项事件监听
  145. _activeAnnotationItem?.featureUpdated
  146. .removeListener(_onActiveAnnotationItemFeatureUpdated);
  147. _activeAnnotationItem = value;
  148. if (_activeMeasureItem != null) {
  149. _annotationItems.add(_activeAnnotationItem!);
  150. // 添加活动注释项事件监听
  151. _activeAnnotationItem!.featureUpdated
  152. .addListener(_onActiveAnnotationItemFeatureUpdated);
  153. }
  154. // 通知更新事件
  155. activeAnnotationItemChanged.emit(this, value);
  156. _updateRender();
  157. }
  158. }
  159. /// 是否注释模式工作中
  160. bool get isAnnotationWorking =>
  161. currentOperateType == MeasureOperateType.annotation;
  162. @override
  163. late final FEventHandler<IMode> currentModeChanged;
  164. @override
  165. late final FEventHandler<IVisualArea> visualAreaChanged;
  166. @override
  167. late final FEventHandler<bool> canMeasureChanged;
  168. @override
  169. late final FEventHandler<IMeasureItem?> activeMeasureItemChanged;
  170. @override
  171. late final FEventHandler<IAnnotationItem?> activeAnnotationItemChanged;
  172. @override
  173. late final FEventHandler<void> updateRenderReady;
  174. @override
  175. late final FEventHandler<MeasureOperateType> operateTypeChanged;
  176. @override
  177. void loadFrame(VidUsImage frame) {
  178. bool frameLoaded = _frame != null;
  179. _frame = frame;
  180. if (!frameLoaded && canMeasure) {
  181. loadVisuals();
  182. }
  183. }
  184. @override
  185. PointInfo createPointInfo(Offset offset, PointInfoType type) {
  186. if (frameData == null) {
  187. throw NullThrownError();
  188. }
  189. final width = displaySize.width;
  190. final height = displaySize.height;
  191. final x = offset.dx / width;
  192. final y = offset.dy / height;
  193. final percentOffset = Offset(x, y);
  194. final info = PointInfo.fromOffset(percentOffset, type);
  195. final matchArea = _attchVisualArea(info);
  196. if (matchArea != null) {
  197. info.hostVisualArea = matchArea;
  198. bool focusAreaChanged = _handleAreaSwitch(matchArea, info);
  199. if (focusAreaChanged) {
  200. // 焦点区域发生变更,不继续执行操作
  201. return info;
  202. }
  203. } else {
  204. info.hostVisualArea = currentVisualArea;
  205. }
  206. if (isAnnotationWorking) {
  207. activeAnnotationItem?.execute(info);
  208. } else {
  209. activeMeasureItem?.execute(info);
  210. }
  211. return info;
  212. }
  213. @override
  214. void switchItemByName(String name) {
  215. _updateOperateType(MeasureOperateType.measure);
  216. // TODO: create from map
  217. if (name == MeasureTerms.Distance) {
  218. activeMeasureItem = StraightLine.createDistance(
  219. ItemMeta(
  220. MeasureTerms.Distance,
  221. {
  222. "Description": MeasureTerms.Distance,
  223. "BriefDescription": "D",
  224. "Unit": VidUsUnit.cm,
  225. },
  226. ),
  227. );
  228. return;
  229. }
  230. if (name == MeasureTerms.Perimeter) {
  231. activeMeasureItem = PolyLine.createPerimeter(
  232. ItemMeta(
  233. MeasureTerms.Perimeter,
  234. {
  235. "Description": MeasureTerms.Perimeter,
  236. "BriefDescription": MeasureTerms.Perimeter,
  237. "Unit": VidUsUnit.cm,
  238. },
  239. ),
  240. null,
  241. );
  242. return;
  243. }
  244. if (name == MeasureTerms.Area) {
  245. activeMeasureItem = PolyLine.createArea(
  246. ItemMeta(
  247. MeasureTerms.Area,
  248. {
  249. "Description": MeasureTerms.Area,
  250. "BriefDescription": MeasureTerms.Area,
  251. "Unit": VidUsUnit.cm2,
  252. },
  253. ),
  254. null,
  255. );
  256. return;
  257. }
  258. if (name == MeasureTerms.Depth) {
  259. final isProbeConvex = (currentVisualArea as TissueArea).isConvex;
  260. final Location Function(ItemMeta, [IMeasureItem?]) fn = isProbeConvex
  261. ? Location.createTissueConvexDepth
  262. : Location.createTissueDepth;
  263. activeMeasureItem = fn(
  264. ItemMeta(
  265. MeasureTerms.Depth,
  266. {
  267. "Description": MeasureTerms.Depth,
  268. "BriefDescription": MeasureTerms.Depth,
  269. "Unit": VidUsUnit.cm,
  270. },
  271. ),
  272. null,
  273. );
  274. return;
  275. }
  276. }
  277. @override
  278. void switchAnnotation([AnnotationType? type, String? text]) {
  279. _updateOperateType(MeasureOperateType.annotation);
  280. final targetType = type ?? AnnotationType.input;
  281. if (activeAnnotationItem != null &&
  282. activeAnnotationItem!.type == targetType &&
  283. activeAnnotationItem!.text == text) {
  284. return;
  285. }
  286. // activeAnnotationItem?.finishLast();
  287. final cachedItems = annotationItems.toList();
  288. final cachedItemIdx = cachedItems.indexWhere((e) => e.type == targetType);
  289. if (cachedItemIdx > -1) {
  290. activeAnnotationItem = cachedItems[cachedItemIdx];
  291. } else {
  292. switch (targetType) {
  293. case AnnotationType.label:
  294. activeAnnotationItem = LabelAnnotation();
  295. break;
  296. case AnnotationType.input:
  297. activeAnnotationItem = InputAnnotation();
  298. break;
  299. case AnnotationType.arrow:
  300. activeAnnotationItem = ArrowAnnotation();
  301. break;
  302. }
  303. cachedItems.add(activeAnnotationItem!);
  304. }
  305. activeAnnotationItem?.text = text;
  306. activeAnnotationItemChanged.emit(this, activeAnnotationItem);
  307. }
  308. /// 切换模式
  309. void switchMode(String name) {
  310. for (var area in currentVisual.visualAreas) {
  311. if (area.mode.name == name) {
  312. _handleAreaSwitch(area, PointInfo(0, 0, PointInfoType.mouseDown));
  313. }
  314. }
  315. }
  316. @protected
  317. List<IVisual> convertVisuals() {
  318. return VisualsLoader(frameData!.visuals).load();
  319. }
  320. void _updateOperateType(MeasureOperateType type) {
  321. if (currentOperateType == MeasureOperateType.annotation) {
  322. activeAnnotationItem?.finishLast();
  323. }
  324. if (currentOperateType != type) {
  325. _currOpType = type;
  326. operateTypeChanged.emit(this, type);
  327. }
  328. }
  329. void _updateRender() {
  330. updateRenderReady.emit(this, null);
  331. }
  332. void _doCanMeasureChanged() {
  333. canMeasureChanged.emit(this, canMeasure);
  334. _clear();
  335. if (canMeasure) {
  336. if (frameData != null) {
  337. loadVisuals();
  338. }
  339. }
  340. }
  341. void _onActiveMeasureItemFeatureUpdated(
  342. Object sender,
  343. IMeasureItemFeature? e,
  344. ) {
  345. _updateRender();
  346. }
  347. void _onActiveAnnotationItemFeatureUpdated(
  348. Object sender,
  349. IAnnotationItemFeature? e,
  350. ) {
  351. _updateRender();
  352. }
  353. @protected
  354. void loadVisuals() {
  355. _clearVisuals();
  356. _visuals = convertVisuals();
  357. // 默认第一个区域为活动域
  358. _visuals!.first.visualAreas.first.isActive = true;
  359. }
  360. void _clear() {
  361. for (var item in measureItems) {
  362. item.clear();
  363. }
  364. _clearVisuals();
  365. }
  366. void _clearVisuals() {
  367. _visuals = [];
  368. }
  369. IVisualArea? _attchVisualArea(PointInfo point) {
  370. for (var visual in visuals) {
  371. for (var area in visual.visualAreas) {
  372. if (area.displayRegion.containsPoint(point)) {
  373. return area;
  374. }
  375. }
  376. }
  377. return null;
  378. }
  379. bool _handleAreaSwitch(IVisualArea area, PointInfo point) {
  380. if (point.pointType != PointInfoType.mouseDown &&
  381. point.pointType != PointInfoType.touchDown) {
  382. return false;
  383. }
  384. /// 点击切换所在区域焦点
  385. if (area != currentVisualArea) {
  386. for (var visual in visuals) {
  387. visual.setUnAcitve();
  388. }
  389. area.isActive = true;
  390. visualAreaChanged.emit(this, area);
  391. return true;
  392. }
  393. return false;
  394. }
  395. }