measure_tool.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. import 'package:fis_common/logger/logger.dart';
  2. import 'package:fis_i18n/i18n.dart';
  3. import 'package:fis_jsonrpc/rpc.dart';
  4. import 'package:fis_measure/define.dart';
  5. import 'package:fis_measure/interfaces/process/items/item.dart';
  6. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  7. import 'package:fis_measure/interfaces/process/items/terms.dart';
  8. import 'package:fis_measure/interfaces/process/items/types.dart';
  9. import 'package:fis_measure/interfaces/process/player/play_controller.dart';
  10. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  11. import 'package:fis_measure/process/language/measure_language.dart';
  12. import 'package:fis_measure/process/workspace/measure_data_controller.dart';
  13. import 'package:fis_measure/process/workspace/measure_data_helper.dart';
  14. import 'package:fis_measure/process/workspace/measure_handler.dart';
  15. import 'package:fis_measure/utils/prompt_box.dart';
  16. import 'package:fis_measure/view/measure/combo_widget.dart';
  17. import 'package:fis_measure/view/measure/measure_view_controller.dart';
  18. import 'package:fis_measure/view/player/controller.dart';
  19. import 'package:fis_ui/index.dart';
  20. import 'package:fis_ui/interface/interactive_container.dart';
  21. import 'package:flutter/material.dart';
  22. import 'package:get/get.dart';
  23. import 'package:vid/us/vid_us_image.dart';
  24. import 'custom_item_buttons/combo.dart';
  25. /// 测量项页面
  26. class LeftSiderSelectMeasure extends FStatefulWidget
  27. implements FInteractiveContainer {
  28. const LeftSiderSelectMeasure({Key? key}) : super(key: key);
  29. @override
  30. final String pageName = 'LeftSiderSelectMeasure';
  31. @override
  32. FState<LeftSiderSelectMeasure> createState() => LeftSiderSelectMeasureState();
  33. }
  34. class LeftSiderSelectMeasureState extends FState<LeftSiderSelectMeasure> {
  35. /// 数据
  36. IApplication get application => Get.find<IApplication>();
  37. VidPlayerController get playerController =>
  38. Get.find<IPlayerController>() as VidPlayerController;
  39. final measureHandler = Get.find<MeasureHandler>();
  40. final measureData = Get.find<MeasureDataController>();
  41. final measureMetaController = Get.find<MeasureMetaController>();
  42. /// 测量语言包
  43. final measureLanguage = MeasureLanguage();
  44. /// 当前选中的测量项下标
  45. int activeItemIndex = 0;
  46. /// 当前选中的子测量项下标
  47. int activeChildItemIndex = 0;
  48. /// 上一个测量项
  49. int lastActiveItemIndex = 0;
  50. /// Styles
  51. late String fontFamily;
  52. static const Color buttonBackgroundColor = Color.fromRGBO(70, 70, 70, 1);
  53. static const Color buttonBorderColor = Color.fromRGBO(124, 124, 124, 1);
  54. static const Color buttonBorderHighlight = Color.fromRGBO(124, 124, 124, 1);
  55. static const Color buttonTextColor = Colors.white;
  56. static const Color childButtonHighlight = Color.fromRGBO(84, 144, 249, 1);
  57. static const Color childContainerBackground = Color.fromRGBO(60, 60, 60, 1);
  58. static const double buttonBorderHighlightWidth = 1.0;
  59. /// 触发测量项选中事件
  60. void selectItem(int selectIndex) {
  61. if (selectIndex == activeItemIndex) return;
  62. final itemMeta = measureData.curItemMetaList[selectIndex];
  63. if (itemMeta.buyStatus == WorkingItemStatusEnum.Unpaid) {
  64. PromptBox.toast("如需继续使用该测量项,请联系Vinno申请试用或购买。");
  65. return;
  66. }
  67. changeItem(itemMeta);
  68. // setState(() {
  69. // activeItemIndex = selectIndex;
  70. // activeChildItemIndex = 0;
  71. // });
  72. }
  73. /// 触发子测量项选中事件
  74. void selectChildItem(int selectIndex) {
  75. if (selectIndex == activeChildItemIndex) return;
  76. final item = application.activeMeasureItem!;
  77. if (item is ITopMeasureItem) {
  78. item.switchChild(selectIndex);
  79. activeChildItemIndex = selectIndex;
  80. }
  81. setState(() {});
  82. }
  83. /// 切换测量项
  84. void changeItem(ItemMeta itemMeta) {
  85. int itemIndex =
  86. measureData.curItemMetaList.indexWhere((e) => e.name == itemMeta.name);
  87. int childItemIndex = activeChildItemIndex;
  88. try {
  89. // IMeasureItem? item = application.activeMeasureItem;
  90. // if (item != null &&
  91. // item.meta.name == itemMeta.name &&
  92. // item is ITopMeasureItem &&
  93. // item.isCrossFrameMode) {
  94. // // 跨帧测量,测量项无变化时,自动切到下一个子项
  95. // childItemIndex = item.workingChildIndex + 1;
  96. // if (childItemIndex < item.childItems.length) {
  97. // item.switchChild(childItemIndex);
  98. // item.workingChildChanged.addListener(_onWorkingChildChanged);
  99. // if (mounted) {
  100. // setState(() {
  101. // activeChildItemIndex = childItemIndex;
  102. // });
  103. // }
  104. // return;
  105. // }
  106. // }
  107. application.switchItem(itemMeta);
  108. final item = application.activeMeasureItem!;
  109. if (item is ITopMeasureItem) {
  110. item.switchChild(0);
  111. item.workingChildChanged.addListener(_onWorkingChildChanged);
  112. }
  113. } catch (e) {
  114. logger.e("changeItem failed", e);
  115. }
  116. if (mounted) {
  117. setState(() {
  118. activeItemIndex = itemIndex;
  119. activeChildItemIndex = childItemIndex;
  120. });
  121. }
  122. }
  123. /// 子测量项变化事件监听
  124. void _onWorkingChildChanged(sender, int e) {
  125. setState(() {
  126. activeChildItemIndex = e;
  127. });
  128. }
  129. void _onItemMetaListChanged(Object s, dynamic e) {
  130. try {
  131. if (e != null) {
  132. setState(() {});
  133. }
  134. } catch (e) {
  135. debugPrint("ItemMetaListChanged fail " + e.toString());
  136. }
  137. }
  138. /// 首帧加载完成事件监听: 获取当前图像的测量项,从第一帧取
  139. void _onFirstFrameLoaded(Object sender, VidUsImage e) async {
  140. if (!mounted) return;
  141. _getMeasureItemsList(e);
  142. _getAnnotationList();
  143. }
  144. //初始化时尝试加载第一项测量项(如果有的话)
  145. void _loadFirstItem() {
  146. if (measureData.curItemMetaList.isNotEmpty) {
  147. if (_checkAndHostCrossFrameMeasureItem()) {
  148. return;
  149. }
  150. final itemMeta = measureData.curItemMetaList.first;
  151. changeItem(itemMeta);
  152. // setState(() {
  153. // activeItemIndex = 0;
  154. // activeChildItemIndex = 0;
  155. // });
  156. }
  157. }
  158. /// 注释获取
  159. void _getAnnotationList() async {
  160. List<String> annotationList = [];
  161. var measureCommentItemResult =
  162. await MeasureDataHelper.getCommentsByApplicationAsync(
  163. application.applicationName,
  164. application.categoryName,
  165. );
  166. measureData.measureCommentItemResult =
  167. measureCommentItemResult?.commentItems ?? [];
  168. measureCommentItemResult?.commentItems?.forEach((element) {
  169. annotationList.add(element.text ?? '');
  170. });
  171. measureData.annotationList = annotationList;
  172. measureHandler.onAnnotationsLoaded();
  173. }
  174. void _getMeasureItemsList(VidUsImage e) async {
  175. List<String> getModes = [];
  176. for (var element in e.visuals[0].modes) {
  177. getModes.add(element.type.toString().split('.')[1]);
  178. }
  179. measureData.applicationModes = e.visuals[0].modes;
  180. measureData.currentMode = e.visuals[0].modes.first.type.name;
  181. var measureModeSelection = MeasureModeSelection(
  182. application.applicationName,
  183. application.categoryName,
  184. application.isThirdPart ? ['TPPTissue'] : getModes,
  185. );
  186. measureHandler.measureModeChanged = measureModeSelection;
  187. var measureApplicationDTO =
  188. await MeasureDataHelper.getMeasureApplication(measureModeSelection);
  189. if (measureApplicationDTO != null) {
  190. measureData.measureApplicationVersion =
  191. measureApplicationDTO.version ?? '';
  192. measureData.availableModes = measureApplicationDTO.availableModes ?? [];
  193. if (application.isThirdPart) {
  194. measureData.currentMode = 'TPPTissue';
  195. }
  196. measureMetaController.setAvailableModes(measureData.currentMode);
  197. setState(() {});
  198. }
  199. }
  200. /// 测量项列表变化事件监听
  201. void _onCurItemMetaListChanged(sender, e) {
  202. if (measureData.curItemMetaList.isNotEmpty) {
  203. if (_checkAndHostCrossFrameMeasureItem()) {
  204. return;
  205. }
  206. changeItem(measureData.curItemMetaList[0]);
  207. }
  208. // setState(() {
  209. // activeItemIndex = 0;
  210. // activeChildItemIndex = 0;
  211. // });
  212. }
  213. bool _checkAndHostCrossFrameMeasureItem() {
  214. final crossCxt = application.crossFrameContext;
  215. if (crossCxt != null && !crossCxt.isOver) {
  216. final activeItem = application.activeMeasureItem;
  217. if (activeItem != null &&
  218. activeItem is ITopMeasureItem &&
  219. activeItem.isCrossFrameMode) {
  220. // 跨帧时&当前是跨帧测量项,则保持此测量项
  221. changeItem(activeItem.meta);
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. /// 是否显示测量项翻译事件变化
  228. void _onShowItemTransStateChanged(_, e) {
  229. setState(() {});
  230. }
  231. /// 鼠标右键结束测量事件【即切换到空测量项】,此时缓存被结束的测量项,如果再次收到右击事件,恢复被结束的测量项
  232. void _onRightClickFinishMeasure(_, e) {
  233. if (activeItemIndex == -1) {
  234. changeItem(measureData.curItemMetaList[lastActiveItemIndex]);
  235. setState(() {
  236. activeItemIndex = lastActiveItemIndex;
  237. activeChildItemIndex = 0;
  238. });
  239. } else {
  240. lastActiveItemIndex = activeItemIndex;
  241. final emptyItem = ItemMeta(
  242. "EmptyItem",
  243. measureType: MeasureTypes.Empty,
  244. description: "",
  245. outputs: [],
  246. );
  247. changeItem(emptyItem);
  248. setState(() {
  249. activeItemIndex = -1;
  250. activeChildItemIndex = 0;
  251. });
  252. }
  253. }
  254. @override
  255. void initState() {
  256. measureData.curItemMetaListChanged.addListener(_onCurItemMetaListChanged);
  257. measureData.itemMetaListChanged.addListener(_onItemMetaListChanged);
  258. measureData.showItemTransStateChanged
  259. .addListener(_onShowItemTransStateChanged);
  260. measureHandler.onRightClickFinishMeasure
  261. .addListener(_onRightClickFinishMeasure);
  262. WidgetsBinding.instance.addPostFrameCallback((call) {
  263. _loadFirstItem();
  264. playerController.firstFrameLoaded.addListener(_onFirstFrameLoaded);
  265. });
  266. super.initState();
  267. }
  268. @override
  269. dispose() {
  270. super.dispose();
  271. playerController.firstFrameLoaded.removeListener(_onFirstFrameLoaded);
  272. measureData.curItemMetaListChanged
  273. .removeListener(_onCurItemMetaListChanged);
  274. measureData.itemMetaListChanged.removeListener(_onItemMetaListChanged);
  275. measureData.showItemTransStateChanged
  276. .removeListener(_onShowItemTransStateChanged);
  277. measureHandler.onRightClickFinishMeasure
  278. .removeListener(_onRightClickFinishMeasure);
  279. final item = application.activeMeasureItem;
  280. if (item != null && item is ITopMeasureItem) {
  281. item.workingChildChanged.removeListener(_onWorkingChildChanged);
  282. }
  283. }
  284. @override
  285. FWidget build(BuildContext context) {
  286. fontFamily = Theme.of(context).textTheme.labelLarge!.fontFamily!;
  287. return buildItemsListView(measureData.curItemMetaList);
  288. }
  289. /// 构建测量项列表
  290. FWidget buildItemsListView(List<ItemMeta> itemMetaList) {
  291. return FListView(
  292. shrinkWrap: true,
  293. controller: ScrollController(),
  294. children: List.generate(itemMetaList.length, (i) {
  295. return _buildItemContainer(i);
  296. }));
  297. }
  298. /// 构建测量项容器
  299. FWidget _buildItemContainer(int itemMetaIndex) {
  300. final itemMeta = measureData.curItemMetaList[itemMetaIndex];
  301. final isActived = itemMetaIndex == activeItemIndex; // 是否选中
  302. final hasChildItem = itemMeta.childItems.isNotEmpty; //是否为组合测量项
  303. return FContainer(
  304. decoration: BoxDecoration(
  305. borderRadius: BorderRadius.circular(5),
  306. border: isActived && hasChildItem
  307. ? Border.all(
  308. color: buttonBorderHighlight,
  309. width: buttonBorderHighlightWidth)
  310. : null,
  311. ),
  312. margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
  313. child: FStack(
  314. children: [
  315. FColumn(children: [
  316. if (isActived && hasChildItem) ...[
  317. _buildItemTitle(itemMetaIndex),
  318. _buildChildItemContainer(itemMeta),
  319. ] else
  320. FContainer(
  321. width: double.infinity,
  322. child: _buildItemButton(itemMetaIndex),
  323. ),
  324. ]),
  325. // if (itemMeta.buyStatus != null)
  326. // FPositioned(
  327. // top: 0,
  328. // right: 0,
  329. // child: _buildItemBuyStatusName(
  330. // itemMeta.buyStatus ?? WorkingItemStatusEnum.Free,
  331. // ),
  332. // )
  333. ],
  334. ));
  335. }
  336. // 构建测量项是否需要购买
  337. FWidget _buildItemBuyStatusName(WorkingItemStatusEnum buyStatus) {
  338. String name = "";
  339. Color textColor = Colors.white;
  340. switch (buyStatus) {
  341. case WorkingItemStatusEnum.Probation:
  342. name = i18nBook.measure.probation.t;
  343. textColor = Colors.yellow;
  344. break;
  345. case WorkingItemStatusEnum.Unpaid:
  346. name = i18nBook.measure.unpaid.t;
  347. textColor = Colors.red;
  348. break;
  349. case WorkingItemStatusEnum.Purchased:
  350. name = i18nBook.measure.purchased.t;
  351. textColor = childButtonHighlight;
  352. break;
  353. case WorkingItemStatusEnum.Expired:
  354. name = i18nBook.measure.expired.t;
  355. textColor = Colors.red;
  356. break;
  357. default:
  358. break;
  359. }
  360. if (name.isNotEmpty) {
  361. return QuickFWidget(
  362. RawChip(
  363. backgroundColor: textColor.withOpacity(0.8),
  364. label: FText(
  365. name,
  366. style: TextStyle(
  367. color: Colors.white,
  368. fontFamily: fontFamily,
  369. fontSize: 12,
  370. ),
  371. ),
  372. // isEnabled: false,//禁止点选状态
  373. labelPadding: const EdgeInsets.symmetric(horizontal: 4),
  374. shape: OutlinedBorder.lerp(
  375. RoundedRectangleBorder(
  376. borderRadius: BorderRadius.circular(4),
  377. ),
  378. RoundedRectangleBorder(
  379. borderRadius: BorderRadius.circular(4),
  380. ),
  381. 0,
  382. )
  383. // padding: EdgeInsets.only(left: 10, right: 10, top: 5),
  384. ),
  385. );
  386. }
  387. return const FSizedBox();
  388. }
  389. // 构建测量项按钮
  390. FWidget _buildItemButton(int itemIndex) {
  391. final itemMeta = measureData.curItemMetaList[itemIndex];
  392. final transValue = measureLanguage.t('measure', itemMeta.description);
  393. // 非英文环境下显示中英文,否则只显示英文
  394. final ifShowCN = !i18nBook.isCurrentEnglish &&
  395. transValue != itemMeta.description &&
  396. measureData.measureSystemSetting.showAnnotation;
  397. final isActived = itemIndex == activeItemIndex;
  398. return FElevatedButton(
  399. name: itemMeta.name,
  400. businessParent: widget,
  401. onPressed: () => selectItem(itemIndex),
  402. child: FColumn(
  403. mainAxisAlignment: MainAxisAlignment.center,
  404. children: [
  405. if (ifShowCN)
  406. FText(
  407. transValue,
  408. maxLines: 1,
  409. overflow: TextOverflow.ellipsis,
  410. style: TextStyle(
  411. color: isActived ? null : buttonTextColor,
  412. fontFamily: fontFamily,
  413. fontSize: 12,
  414. ),
  415. ),
  416. FText(
  417. itemMeta.description,
  418. style: TextStyle(
  419. color: isActived ? null : buttonTextColor,
  420. ),
  421. ),
  422. ],
  423. ),
  424. style: ElevatedButton.styleFrom(
  425. backgroundColor: isActived ? null : buttonBackgroundColor,
  426. fixedSize: const Size.fromHeight(
  427. 50,
  428. ),
  429. side: BorderSide(
  430. color: isActived ? Colors.transparent : buttonBorderColor,
  431. ),
  432. ),
  433. );
  434. }
  435. // 构建测量项标签【用于组合测量项顶部】
  436. FWidget _buildItemTitle(int itemIndex) {
  437. final itemMeta = measureData.curItemMetaList[itemIndex];
  438. final transValue = measureLanguage.t('measure', itemMeta.description);
  439. // 非英文环境下显示中英文,否则只显示英文
  440. final ifShowCN = !i18nBook.isCurrentEnglish &&
  441. transValue != itemMeta.description &&
  442. measureData.measureSystemSetting.showAnnotation;
  443. return FContainer(
  444. decoration: const BoxDecoration(
  445. border: Border(
  446. bottom: BorderSide(
  447. color: buttonBorderHighlight, width: buttonBorderHighlightWidth),
  448. ),
  449. ),
  450. child: FSizedBox(
  451. height: 50,
  452. width: double.infinity,
  453. child: FColumn(
  454. mainAxisAlignment: MainAxisAlignment.center,
  455. children: [
  456. if (ifShowCN)
  457. FText(
  458. transValue,
  459. maxLines: 1,
  460. overflow: TextOverflow.ellipsis,
  461. style: TextStyle(
  462. color: buttonTextColor,
  463. fontFamily: fontFamily,
  464. fontSize: 12,
  465. ),
  466. ),
  467. FText(
  468. itemMeta.description,
  469. style: const TextStyle(
  470. color: buttonTextColor,
  471. ),
  472. ),
  473. ],
  474. ),
  475. ),
  476. );
  477. }
  478. /// 子测量项容器
  479. FWidget _buildChildItemContainer(ItemMeta itemMeta) {
  480. final activeName = itemMeta.description;
  481. final isSpecial = MeasurespecialsupportedTerms.items.contains(activeName);
  482. if (isSpecial) {
  483. //是否为特殊组合测量项
  484. return const SpecialItemHR();
  485. } else if (CustomComboItemGroup.specialItemTypes
  486. .contains(itemMeta.measureType)) {
  487. return CustomComboItemGroup(
  488. activeIndex: activeChildItemIndex,
  489. businessParent: widget,
  490. itemMeta: itemMeta,
  491. onChildClick: (int childItemIndex) {
  492. selectChildItem(childItemIndex);
  493. },
  494. );
  495. } else {
  496. return FContainer(
  497. decoration: const BoxDecoration(
  498. color: childContainerBackground,
  499. borderRadius: BorderRadius.only(
  500. bottomLeft: Radius.circular(5), bottomRight: Radius.circular(5)),
  501. ),
  502. padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 5),
  503. child: FColumn(
  504. children: (itemMeta.childItems)
  505. .asMap()
  506. .entries
  507. .map(
  508. (e) => _buildChildItemButton(
  509. e.value,
  510. e.key,
  511. ),
  512. )
  513. .toList(),
  514. ),
  515. );
  516. }
  517. }
  518. // 构建子测量项按钮
  519. FWidget _buildChildItemButton(ItemMeta itemMeta, int childItemIndex) {
  520. final isActived = childItemIndex == activeChildItemIndex;
  521. return FContainer(
  522. width: double.infinity,
  523. margin: const EdgeInsetsDirectional.all(5),
  524. child: FElevatedButton(
  525. businessParent: widget,
  526. name: "selectChildItemIdnex:$childItemIndex",
  527. onPressed: () {
  528. selectChildItem(childItemIndex);
  529. },
  530. child: FText(
  531. itemMeta.description,
  532. ),
  533. style: ElevatedButton.styleFrom(
  534. backgroundColor: isActived ? null : buttonBackgroundColor,
  535. side: BorderSide(
  536. color: isActived ? Colors.transparent : buttonBorderColor,
  537. ),
  538. ),
  539. ));
  540. }
  541. }