application.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. import 'package:fis_common/logger/logger.dart';
  2. import 'package:fis_measure/interfaces/date_types/rect_region.dart';
  3. import 'package:fis_measure/interfaces/enums/annotation.dart';
  4. import 'package:fis_measure/interfaces/enums/operate.dart';
  5. import 'package:fis_measure/interfaces/process/annotations/annotation.dart';
  6. import 'package:fis_measure/interfaces/process/items/item.dart';
  7. import 'package:fis_measure/interfaces/process/items/item_feature.dart';
  8. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  9. import 'package:fis_measure/interfaces/process/visuals/visual_area.dart';
  10. import 'package:fis_measure/interfaces/process/visuals/visual.dart';
  11. import 'package:fis_measure/interfaces/process/viewports/viewport.dart';
  12. import 'package:fis_measure/interfaces/process/modes/mode.dart';
  13. import 'package:fis_common/event/event_type.dart';
  14. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  15. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  16. import 'package:fis_measure/interfaces/process/workspace/recorder.dart';
  17. import 'package:fis_measure/process/annotations/arrow_annotation.dart';
  18. import 'package:fis_measure/process/annotations/input_annotation.dart';
  19. import 'package:fis_measure/process/annotations/label_annotation.dart';
  20. import 'package:fis_measure/process/items/factory.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 'dart:math';
  26. import 'recorder.dart';
  27. import 'visual_loader.dart';
  28. class Application implements IApplication {
  29. // ignore: constant_identifier_names
  30. static const C_VID_THIRDPART_NAME = "ThirdPart";
  31. late VidUsProbe _probe;
  32. VidUsImage? _frame;
  33. List<IVisual>? _visuals;
  34. IMeasureItem? _activeMeasureItem;
  35. IAnnotationItem? _activeAnnotationItem;
  36. bool _canOperate = false;
  37. Size _displaySize = Size.zero;
  38. bool _isAdaptiveCarotid2D = false;
  39. bool _isSingleFrame = true;
  40. Size _carotid2DSize = Size.zero;
  41. MeasureOperateType _currOpType = MeasureOperateType.measure;
  42. final List<IMeasureItem> _measureItems = [];
  43. final Set<IAnnotationItem> _annotationItems = {};
  44. late final _recorder = MeasureRecorder(this);
  45. Application(VidUsProbe probe) {
  46. _probe = probe;
  47. currentModeChanged = FEventHandler<IMode>();
  48. visualAreaChanged = FEventHandler<IVisualArea>();
  49. canMeasureChanged = FEventHandler<bool>();
  50. activeMeasureItemChanged = FEventHandler<IMeasureItem?>();
  51. activeAnnotationItemChanged = FEventHandler<IAnnotationItem?>();
  52. updateRenderReady = FEventHandler<void>();
  53. operateTypeChanged = FEventHandler<MeasureOperateType>();
  54. visualsLoaded = FEventHandler<void>();
  55. displaySizeChanged = FEventHandler<Size>();
  56. }
  57. @override
  58. bool get canMeasure => _canOperate;
  59. @override
  60. set canMeasure(bool value) {
  61. if (value != _canOperate) {
  62. _canOperate = value;
  63. _doCanMeasureChanged();
  64. }
  65. }
  66. /// 是否扇形探头
  67. bool get isProbeConvex => probe.type == VidUsProbeType.Convex;
  68. @override
  69. VidUsProbe get probe => _probe;
  70. @override
  71. String get applicationName => _probe.application.applicationName;
  72. @override
  73. String get categoryName => _probe.application.applicationCategoryName;
  74. @override
  75. bool get isThirdPart => probe.name == C_VID_THIRDPART_NAME;
  76. @override
  77. IMode get currentMode => currentVisualArea.mode;
  78. @override
  79. IViewPort get currentViewPort => currentVisualArea.viewport!;
  80. @override
  81. IVisual get currentVisual => visuals.firstWhere((e) => e.activeArea != null);
  82. @override
  83. IVisualArea get currentVisualArea => currentVisual.activeArea!;
  84. @override
  85. VidUsImage? get frameData => _frame;
  86. @override
  87. List<IVisual> get visuals => _visuals!;
  88. @override
  89. List<IMeasureItem> get measureItems => _measureItems;
  90. @override
  91. Set<IAnnotationItem> get annotationItems => _annotationItems;
  92. @override
  93. MeasureOperateType get currentOperateType => _currOpType;
  94. @override
  95. Size get displaySize => _displaySize;
  96. @override
  97. set displaySize(Size value) {
  98. if (value != _displaySize) {
  99. _displaySize = value;
  100. displaySizeChanged.emit(this, value);
  101. }
  102. }
  103. @override
  104. bool get isSingleFrame => _isSingleFrame;
  105. @override
  106. set isSingleFrame(bool value) {
  107. if (value != _isSingleFrame) {
  108. _isSingleFrame = value;
  109. }
  110. }
  111. @override
  112. double get displayScaleRatio {
  113. if (isAdaptiveCarotid2D) {
  114. final firstScale = min(displaySize.width / frameData!.width,
  115. displaySize.height / frameData!.height);
  116. final secondScale = min(frameData!.width / carotid2DSize.width,
  117. frameData!.height / carotid2DSize.height);
  118. return firstScale * secondScale;
  119. }
  120. if (frameData != null) {
  121. return min(displaySize.width / frameData!.width,
  122. displaySize.height / frameData!.height);
  123. }
  124. return 1.0;
  125. }
  126. @override
  127. List<IMode> get avaliableModes {
  128. final modes = <IMode>[];
  129. for (var visual in visuals) {
  130. modes.addAll(visual.modes);
  131. }
  132. return modes;
  133. }
  134. @override
  135. bool get isAdaptiveCarotid2D => _isAdaptiveCarotid2D;
  136. @override
  137. set isAdaptiveCarotid2D(bool value) {
  138. if (value != _isAdaptiveCarotid2D) {
  139. _isAdaptiveCarotid2D = value;
  140. //[Carotid] ✅如果颈动脉2D图像超出范围需要缩放,利用 layoutRegion 参数改变缩放比
  141. if (value) {
  142. final scale = min(frameData!.width / carotid2DSize.width,
  143. frameData!.height / carotid2DSize.height);
  144. currentVisualArea.layoutRegion = RectRegion.fill(0, 0, scale, scale);
  145. } else {
  146. currentVisualArea.layoutRegion = RectRegion.fill(0, 0, 1, 1);
  147. }
  148. }
  149. }
  150. @override
  151. Size get carotid2DSize => _carotid2DSize;
  152. @override
  153. set carotid2DSize(Size value) {
  154. if (value != _carotid2DSize) {
  155. _carotid2DSize = value;
  156. }
  157. }
  158. @override
  159. IMeasureItem? get activeMeasureItem => _activeMeasureItem;
  160. set activeMeasureItem(IMeasureItem? value) {
  161. if (value != _activeMeasureItem) {
  162. // 解绑失活测量项事件监听
  163. _activeMeasureItem?.featureUpdated
  164. .removeListener(_onActiveMeasureItemFeatureUpdated);
  165. _activeMeasureItem = value;
  166. if (_activeMeasureItem != null) {
  167. _measureItems.add(_activeMeasureItem!);
  168. // 添加活动测量项事件监听
  169. _activeMeasureItem!.featureUpdated
  170. .addListener(_onActiveMeasureItemFeatureUpdated);
  171. }
  172. // 通知更新事件
  173. activeMeasureItemChanged.emit(this, value);
  174. _updateRender();
  175. }
  176. }
  177. @override
  178. IAnnotationItem? get activeAnnotationItem => _activeAnnotationItem;
  179. set activeAnnotationItem(IAnnotationItem? value) {
  180. if (value != _activeAnnotationItem) {
  181. // 解绑失活注释项事件监听
  182. _activeAnnotationItem?.featureUpdated
  183. .removeListener(_onActiveAnnotationItemFeatureUpdated);
  184. _activeAnnotationItem = value;
  185. if (_activeAnnotationItem != null) {
  186. _annotationItems.add(_activeAnnotationItem!);
  187. // 添加活动注释项事件监听
  188. _activeAnnotationItem!.featureUpdated
  189. .addListener(_onActiveAnnotationItemFeatureUpdated);
  190. }
  191. // 通知更新事件
  192. activeAnnotationItemChanged.emit(this, value);
  193. _updateRender();
  194. }
  195. }
  196. /// 是否注释模式工作中
  197. bool get isAnnotationWorking =>
  198. currentOperateType == MeasureOperateType.annotation;
  199. @override
  200. IMeasureRecorder get recorder => _recorder;
  201. @override
  202. late final FEventHandler<IMode> currentModeChanged;
  203. @override
  204. late final FEventHandler<IVisualArea> visualAreaChanged;
  205. @override
  206. late final FEventHandler<bool> canMeasureChanged;
  207. @override
  208. late final FEventHandler<IMeasureItem?> activeMeasureItemChanged;
  209. @override
  210. late final FEventHandler<IAnnotationItem?> activeAnnotationItemChanged;
  211. @override
  212. late final FEventHandler<void> updateRenderReady;
  213. @override
  214. late final FEventHandler<MeasureOperateType> operateTypeChanged;
  215. @override
  216. late final FEventHandler<void> visualsLoaded;
  217. @override
  218. late final FEventHandler<Size> displaySizeChanged;
  219. @override
  220. void loadFrame(VidUsImage frame, [bool clearable = true]) {
  221. bool isSameIndex = _frame?.index == frame.index;
  222. _frame = frame;
  223. if (isSameIndex == false) {
  224. if (clearable) {
  225. _clearFrameCache();
  226. if (canMeasure) {
  227. loadVisuals();
  228. }
  229. }
  230. //如果clearable为false,需要手动调用loadCarotidVisuals加载visuals
  231. }
  232. }
  233. final FEventHandler<Offset> mobileTouchEvent = FEventHandler();
  234. @override
  235. PointInfo createPointInfo(Offset offset, PointInfoType type) {
  236. if (frameData == null) {
  237. throw NullThrownError();
  238. }
  239. final width = displaySize.width;
  240. final height = displaySize.height;
  241. final x = offset.dx / width;
  242. final y = offset.dy / height;
  243. final percentOffset = Offset(x, y);
  244. final info = PointInfo.fromOffset(percentOffset, type);
  245. final matchArea = _attchVisualArea(info);
  246. if (matchArea != null) {
  247. if (matchArea != currentVisualArea) {
  248. bool focusAreaChanged = _handleAreaSwitch(matchArea, info);
  249. // if (focusAreaChanged) {
  250. // // 焦点区域发生变更,不继续执行操作
  251. // return info;
  252. // }
  253. return info;
  254. }
  255. }
  256. info.hostVisualArea ??= currentVisualArea; // 未切换区域则沿用当前区域
  257. if (isAnnotationWorking) {
  258. activeAnnotationItem?.execute(info);
  259. } else {
  260. activeMeasureItem?.execute(info);
  261. if (type == PointInfoType.touchMove) {
  262. mobileTouchEvent.emit(this, offset); // 传出移动事件
  263. }
  264. }
  265. return info;
  266. }
  267. @override
  268. void switchItem(ItemMeta meta) {
  269. _updateOperateType(MeasureOperateType.measure);
  270. _handleBeforeSwitchItem();
  271. activeMeasureItem = MeasureItemFactory.createItem(meta);
  272. }
  273. @override
  274. void autoStartAgain(ItemMeta meta) {
  275. if (activeMeasureItem == null) return;
  276. final item = activeMeasureItem!;
  277. if (item.feature != null) {
  278. item.finishOnce();
  279. }
  280. activeMeasureItem = MeasureItemFactory.createItem(meta);
  281. }
  282. @override
  283. void switchAnnotation([AnnotationType? type, String? text]) {
  284. _updateOperateType(MeasureOperateType.annotation);
  285. final targetType = type ?? AnnotationType.input;
  286. if (activeAnnotationItem != null &&
  287. activeAnnotationItem!.type == targetType &&
  288. activeAnnotationItem!.text == text) {
  289. return;
  290. }
  291. // activeAnnotationItem?.finishLast();
  292. final cachedItems = annotationItems.toList();
  293. final cachedItemIdx = cachedItems.indexWhere((e) => e.type == targetType);
  294. if (cachedItemIdx > -1) {
  295. activeAnnotationItem = cachedItems[cachedItemIdx];
  296. } else {
  297. switch (targetType) {
  298. case AnnotationType.label:
  299. activeAnnotationItem = LabelAnnotation();
  300. break;
  301. case AnnotationType.input:
  302. activeAnnotationItem = InputAnnotation();
  303. break;
  304. case AnnotationType.arrow:
  305. activeAnnotationItem = ArrowAnnotation();
  306. break;
  307. }
  308. cachedItems.add(activeAnnotationItem!);
  309. }
  310. activeAnnotationItem?.text = text;
  311. activeAnnotationItemChanged.emit(this, activeAnnotationItem);
  312. }
  313. @override
  314. void switchMode(String name) {
  315. for (var area in currentVisual.visualAreas) {
  316. if (area.mode.name == name) {
  317. _changeAcitveArea(area);
  318. }
  319. }
  320. }
  321. @override
  322. void switchVisual(int indicator) {
  323. if (_visuals == null) return;
  324. try {
  325. for (var i = 0; i < _visuals!.length; i++) {
  326. final v = _visuals![i];
  327. if (i == indicator) {
  328. v.visualAreas.first.isActive = true;
  329. } else {
  330. v.setUnAcitve();
  331. }
  332. }
  333. } catch (e) {
  334. logger.e('switch error: $e');
  335. }
  336. }
  337. @override
  338. void undoRecord() {
  339. if (_recorder.undoOnce()) {
  340. _updateRender();
  341. }
  342. }
  343. @override
  344. void clearRecords() {
  345. _recorder.clear();
  346. _updateRender();
  347. }
  348. @protected
  349. List<IVisual> convertVisuals() {
  350. return VisualsLoader(frameData!.visuals).load();
  351. }
  352. void _handleBeforeSwitchItem() {
  353. if (activeMeasureItem == null) return;
  354. final item = activeMeasureItem!;
  355. if (item.feature != null) {
  356. if (item.finishAfterUnactive) {
  357. item.finishOnce();
  358. } else {
  359. _recorder.undoOnce();
  360. }
  361. }
  362. }
  363. void _clearFrameCache() {
  364. _recorder.clear();
  365. _annotationItems.clear();
  366. _clearVisuals();
  367. }
  368. void _updateOperateType(MeasureOperateType type) {
  369. if (currentOperateType == MeasureOperateType.annotation) {
  370. activeAnnotationItem?.finishLast();
  371. }
  372. if (currentOperateType != type) {
  373. _currOpType = type;
  374. operateTypeChanged.emit(this, type);
  375. }
  376. }
  377. void _updateRender() {
  378. updateRenderReady.emit(this, null);
  379. }
  380. void _doCanMeasureChanged() {
  381. canMeasureChanged.emit(this, canMeasure);
  382. _clearFrameCache();
  383. if (canMeasure) {
  384. if (frameData != null) {
  385. loadVisuals();
  386. }
  387. }
  388. }
  389. void _onActiveMeasureItemFeatureUpdated(
  390. Object sender,
  391. IMeasureItemFeature? e,
  392. ) {
  393. _updateRender();
  394. }
  395. void _onActiveAnnotationItemFeatureUpdated(
  396. Object sender,
  397. IAnnotationItemFeature? e,
  398. ) {
  399. _updateRender();
  400. }
  401. @protected
  402. void loadVisuals() {
  403. _clearVisuals();
  404. _visuals = convertVisuals();
  405. // 默认第一个区域为活动域
  406. switchVisual(0);
  407. visualsLoaded.emit(this, null);
  408. }
  409. @override
  410. void loadCarotidVisuals(VidUsImage carotidVid) {
  411. _clearVisuals();
  412. _visuals = VisualsLoader(carotidVid.visuals).load();
  413. switchVisual(0);
  414. visualsLoaded.emit(this, null);
  415. }
  416. void _clearVisuals() {
  417. _visuals = [];
  418. }
  419. IVisualArea? _attchVisualArea(PointInfo point) {
  420. if (currentVisualArea.displayRegion.containsPoint(point)) {
  421. return currentVisualArea;
  422. }
  423. for (var visual in visuals) {
  424. for (var area in visual.visualAreas) {
  425. if (area.displayRegion.containsPoint(point)) {
  426. return area;
  427. }
  428. }
  429. }
  430. return null;
  431. }
  432. bool _handleAreaSwitch(IVisualArea area, PointInfo point) {
  433. if (point.pointType != PointInfoType.mouseDown &&
  434. point.pointType != PointInfoType.touchDown) {
  435. return false;
  436. }
  437. // 点击切换所在区域焦点
  438. // 不同幅或者不同模式类型的才切换
  439. bool needSwitch = false;
  440. if (area.visual.visualData.indicator !=
  441. currentVisual.visualData.indicator) {
  442. needSwitch = true;
  443. } else {
  444. if (!currentVisualArea.displayRegion.containsPoint(point)) {
  445. needSwitch = true;
  446. }
  447. }
  448. if (needSwitch) {
  449. _changeAcitveArea(area);
  450. return true;
  451. }
  452. return false;
  453. }
  454. void _changeAcitveArea(IVisualArea area) {
  455. for (var visual in visuals) {
  456. visual.setUnAcitve();
  457. }
  458. area.isActive = true;
  459. visualAreaChanged.emit(this, area);
  460. }
  461. }