123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import 'package:fis_common/event/event_type.dart';
- import 'package:fis_measure/interfaces/enums/items.dart';
- import 'package:fis_measure/interfaces/process/calculators/calculator.dart';
- import 'package:fis_measure/interfaces/process/items/item.dart';
- import 'package:fis_measure/interfaces/process/items/item_feature.dart';
- import 'package:fis_measure/interfaces/process/items/item_metas.dart';
- import 'package:fis_measure/interfaces/process/workspace/application.dart';
- import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
- import 'package:flutter/foundation.dart';
- import 'package:get/get.dart';
- import 'item_feature.dart';
- import 'top_item.dart';
- abstract class MeasureItem<T extends MeasureItemFeature> extends IMeasureItem {
- late final ItemMeta _meta;
- late final IMeasureItem? _parent;
- ItemStates _state = ItemStates.waiting;
- ICalculator? _calculator;
- T? _feature;
- bool _repeatableEditable = false;
- final List<T> _measuredFeatures = [];
- final application = Get.find<IApplication>();
- MeasureItem(ItemMeta meta, [IMeasureItem? parent]) {
- _parent = parent;
- _meta = meta;
- featureUpdated = FEventHandler<IMeasureItemFeature?>();
- }
- @override
- String get displayName {
- if (briefAnnotation.isNotEmpty) {
- return briefAnnotation;
- }
- if (description.isNotEmpty) {
- return description;
- }
- return meta.name;
- }
- @override
- String get briefAnnotation => meta.briefAnnotation;
- @override
- String get description => meta.description;
- @override
- T? get feature => _feature;
- @protected
- set feature(T? value) {
- if (value != _feature) {
- _feature = value;
- }
- }
- @override
- ICalculator? get calculator => _calculator;
- @protected
- set calculator(ICalculator? value) {
- if (value != _calculator) {
- _calculator = value;
- }
- }
- @override
- IMeasureItem? get parent => _parent;
- @override
- ItemMeta get meta => _meta;
- @override
- ItemStates get state => _state;
- @protected
- set state(ItemStates value) {
- if (value != _state) {
- _state = value;
- onItemStatesChanged.emit(this, value);
- }
- }
- @override
- double get scaleRatio => application.displayScaleRatio;
- @override
- bool get finishAfterUnactive => true;
- @override
- bool get repeatableEditable => _repeatableEditable;
- set repeatableEditable(bool val) => _repeatableEditable = val;
- @override
- late final FEventHandler<IMeasureItemFeature?> featureUpdated;
- @override
- final FEventHandler<ItemStates?> onItemStatesChanged =
- FEventHandler<ItemStates?>();
- @override
- bool execute(PointInfo args) {
- bool hasFeatureBefore = feature != null;
- bool result = false;
- if (state == ItemStates.waiting ||
- state == ItemStates.running ||
- state == ItemStates.finished ||
- state == ItemStates.idle) {
- switch (args.pointType) {
- case PointInfoType.mouseUp:
- case PointInfoType.mouseDown:
- case PointInfoType.mouseMove:
- result = onExecuteMouse(args);
- break;
- case PointInfoType.touchUp:
- case PointInfoType.touchDown:
- case PointInfoType.touchMove:
- result = onExecuteTouch(args);
- break;
- }
- }
- if (result) {
- /// 未创建测量,不通知刷新
- if (feature == null && !hasFeatureBefore) return result;
- /// 通知刷新
- doFeatureUpdate();
- }
- return result;
- }
- @override
- void clear() {
- feature = null;
- if (calculator != null) {
- calculator!.finishOnce();
- }
- measuredFeatures.clear();
- }
- @override
- void finishOnce() {
- doFeatureFinish();
- }
- @override
- void cancelOnce() {
- if (this is TopMeasureItem) {
- final that = this as TopMeasureItem;
- for (var item in that.childItems) {
- item.cancelOnce();
- item.measuredFeatures.clear();
- }
- that.switchChild(0);
- }
- feature = null;
- state = ItemStates.waiting;
- onCancelingOnce();
- doFeatureUpdate();
- }
- @override
- void update() {
- if (feature == null) return;
- doFeatureUpdate();
- }
- @protected
- void onCancelingOnce() {}
- @protected
- void doFeatureUpdate() {
- featureUpdated.emit(this, feature);
- }
- @protected
- void doFeatureFinish() {
- if (feature != null) {
- feature!.isActive = false;
- if (application.frameData != null) {
- final frameIndex = application.frameData!.index;
- // 快照图像和帧
- feature!.frameIndex = frameIndex;
- feature!.imageBelongSign = application.imageUrl.hashCode;
- // 跨帧测量项需要记录索引给指示器
- _recordCrossFrameIndex(frameIndex);
- }
- measuredFeatures.add(feature!);
- calculator?.finishOnce();
- }
- feature = null;
- state = ItemStates.finished;
- }
- @protected
- void doCalculate() {
- calculator?.calculate();
- }
- @protected
- Future<void> doCalculateAsync() async {
- await calculator?.calculateAsync();
- }
- /// 拖动目标是否超出区域
- @protected
- bool isMoveTargetOutOfRange(PointInfo args) {
- if (args.hostVisualArea == null) {
- return false;
- }
- if (args.hostVisualArea!.displayRegion.containsPoint(args)) {
- return false;
- }
- return true;
- }
- @protected
- bool onExecuteMouse(PointInfo args);
- @protected
- bool onExecuteTouch(PointInfo args);
- @override
- List<T> get measuredFeatures => _measuredFeatures;
- @override
- int assignId() {
- return application.recorder.newRecordId();
- }
- void _recordCrossFrameIndex(int frameIndex) {
- final canRecord = parent != null || !(this is ITopMeasureItem);
- if (canRecord) {
- if (application.crossFrameContext != null) {
- application.crossFrameContext!.recordFrame(frameIndex);
- application.crossFrameAdded.emit(this, frameIndex);
- }
- }
- }
- }
|