application.dart 15 KB

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