item.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import 'package:fis_common/event/event_type.dart';
  2. import 'package:fis_measure/interfaces/enums/items.dart';
  3. import 'package:fis_measure/interfaces/process/calculators/calculator.dart';
  4. import 'package:fis_measure/interfaces/process/items/item.dart';
  5. import 'package:fis_measure/interfaces/process/items/item_feature.dart';
  6. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  7. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  8. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  9. import 'package:flutter/foundation.dart';
  10. import 'package:get/get.dart';
  11. import 'item_feature.dart';
  12. import 'top_item.dart';
  13. abstract class MeasureItem<T extends MeasureItemFeature> extends IMeasureItem {
  14. late final ItemMeta _meta;
  15. late final IMeasureItem? _parent;
  16. ItemStates _state = ItemStates.waiting;
  17. ICalculator? _calculator;
  18. T? _feature;
  19. bool _repeatableEditable = false;
  20. final List<T> _measuredFeatures = [];
  21. final application = Get.find<IApplication>();
  22. MeasureItem(ItemMeta meta, [IMeasureItem? parent]) {
  23. _parent = parent;
  24. _meta = meta;
  25. featureUpdated = FEventHandler<IMeasureItemFeature?>();
  26. }
  27. @override
  28. String get displayName {
  29. if (briefAnnotation.isNotEmpty) {
  30. return briefAnnotation;
  31. }
  32. if (description.isNotEmpty) {
  33. return description;
  34. }
  35. return meta.name;
  36. }
  37. @override
  38. String get briefAnnotation => meta.briefAnnotation;
  39. @override
  40. String get description => meta.description;
  41. @override
  42. T? get feature => _feature;
  43. @protected
  44. set feature(T? value) {
  45. if (value != _feature) {
  46. _feature = value;
  47. }
  48. }
  49. @override
  50. ICalculator? get calculator => _calculator;
  51. @protected
  52. set calculator(ICalculator? value) {
  53. if (value != _calculator) {
  54. _calculator = value;
  55. }
  56. }
  57. @override
  58. IMeasureItem? get parent => _parent;
  59. @override
  60. ItemMeta get meta => _meta;
  61. @override
  62. ItemStates get state => _state;
  63. @protected
  64. set state(ItemStates value) {
  65. if (value != _state) {
  66. _state = value;
  67. onItemStatesChanged.emit(this, value);
  68. }
  69. }
  70. @override
  71. double get scaleRatio => application.displayScaleRatio;
  72. @override
  73. bool get finishAfterUnactive => true;
  74. @override
  75. bool get repeatableEditable => _repeatableEditable;
  76. set repeatableEditable(bool val) => _repeatableEditable = val;
  77. @override
  78. late final FEventHandler<IMeasureItemFeature?> featureUpdated;
  79. @override
  80. final FEventHandler<ItemStates?> onItemStatesChanged =
  81. FEventHandler<ItemStates?>();
  82. @override
  83. bool execute(PointInfo args) {
  84. bool hasFeatureBefore = feature != null;
  85. bool result = false;
  86. if (state == ItemStates.waiting ||
  87. state == ItemStates.running ||
  88. state == ItemStates.finished ||
  89. state == ItemStates.idle) {
  90. switch (args.pointType) {
  91. case PointInfoType.mouseUp:
  92. case PointInfoType.mouseDown:
  93. case PointInfoType.mouseMove:
  94. result = onExecuteMouse(args);
  95. break;
  96. case PointInfoType.touchUp:
  97. case PointInfoType.touchDown:
  98. case PointInfoType.touchMove:
  99. result = onExecuteTouch(args);
  100. break;
  101. }
  102. }
  103. if (result) {
  104. /// 未创建测量,不通知刷新
  105. if (feature == null && !hasFeatureBefore) return result;
  106. /// 通知刷新
  107. doFeatureUpdate();
  108. }
  109. return result;
  110. }
  111. @override
  112. void clear() {
  113. feature = null;
  114. if (calculator != null) {
  115. calculator!.finishOnce();
  116. }
  117. measuredFeatures.clear();
  118. }
  119. @override
  120. void finishOnce() {
  121. doFeatureFinish();
  122. }
  123. @override
  124. void cancelOnce() {
  125. if (this is TopMeasureItem) {
  126. final that = this as TopMeasureItem;
  127. for (var item in that.childItems) {
  128. item.cancelOnce();
  129. item.measuredFeatures.clear();
  130. }
  131. that.switchChild(0);
  132. }
  133. feature = null;
  134. state = ItemStates.waiting;
  135. onCancelingOnce();
  136. doFeatureUpdate();
  137. }
  138. @override
  139. void update() {
  140. if (feature == null) return;
  141. doFeatureUpdate();
  142. }
  143. @protected
  144. void onCancelingOnce() {}
  145. @protected
  146. void doFeatureUpdate() {
  147. featureUpdated.emit(this, feature);
  148. }
  149. @protected
  150. void doFeatureFinish() {
  151. if (feature != null) {
  152. feature!.isActive = false;
  153. if (application.frameData != null) {
  154. final frameIndex = application.frameData!.index;
  155. // 快照图像和帧
  156. feature!.frameIndex = frameIndex;
  157. feature!.imageBelongSign = application.imageUrl.hashCode;
  158. // 跨帧测量项需要记录索引给指示器
  159. _recordCrossFrameIndex(frameIndex);
  160. }
  161. measuredFeatures.add(feature!);
  162. calculator?.finishOnce();
  163. }
  164. feature = null;
  165. state = ItemStates.finished;
  166. }
  167. @protected
  168. void doCalculate() {
  169. calculator?.calculate();
  170. }
  171. @protected
  172. Future<void> doCalculateAsync() async {
  173. await calculator?.calculateAsync();
  174. }
  175. /// 拖动目标是否超出区域
  176. @protected
  177. bool isMoveTargetOutOfRange(PointInfo args) {
  178. if (args.hostVisualArea == null) {
  179. return false;
  180. }
  181. if (args.hostVisualArea!.displayRegion.containsPoint(args)) {
  182. return false;
  183. }
  184. return true;
  185. }
  186. @protected
  187. bool onExecuteMouse(PointInfo args);
  188. @protected
  189. bool onExecuteTouch(PointInfo args);
  190. @override
  191. List<T> get measuredFeatures => _measuredFeatures;
  192. @override
  193. int assignId() {
  194. return application.recorder.newRecordId();
  195. }
  196. void _recordCrossFrameIndex(int frameIndex) {
  197. final canRecord = parent != null || !(this is ITopMeasureItem);
  198. if (canRecord) {
  199. if (application.crossFrameContext != null) {
  200. application.crossFrameContext!.recordFrame(frameIndex);
  201. application.crossFrameAdded.emit(this, frameIndex);
  202. }
  203. }
  204. }
  205. }