application.dart 16 KB

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