AnnotToolContentViewModel.Command.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.CustomControl;
  6. using PDF_Office.EventAggregators;
  7. using PDF_Office.Helper;
  8. using PDF_Office.Properties;
  9. using PDF_Office.ViewModels.BOTA;
  10. using PDF_Office.Views.BOTA;
  11. using PDFSettings;
  12. using Prism.Mvvm;
  13. using Prism.Regions;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Controls.Primitives;
  22. using System.Windows.Media;
  23. namespace PDF_Office.ViewModels.Tools
  24. {
  25. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  26. {
  27. #region 事件绑定和解绑
  28. private void BindingEvent()
  29. {
  30. //属性面板与注释工具栏绑定:属性面板的属性变化,会同步注释工具栏的属性值
  31. //比如在属性面板里更改注释颜色,会同时更新工具栏对应的工具颜色
  32. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  33. propertyPanel.DataChanged += AnnotPropertyPanel_DataChanged;
  34. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  35. propertyPanel.DefaultStored += AnnotProperty_DefaultStored;
  36. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  37. propertyPanel.AnnotTypeChanged += AnnotPropertyPanel_AnnotTypeChanged;
  38. }
  39. private void UnBindingEvent()
  40. {
  41. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  42. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  43. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  44. }
  45. private void BindingPDFViewerHandler()
  46. {
  47. //来自PDFViewer的响应事件
  48. if (PDFViewer != null)
  49. {
  50. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  51. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  52. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  53. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  54. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  55. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  56. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  57. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  58. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  59. PDFViewer.SnapshotCommandHandler += PDFViewer_SnapshotCommandHandler;
  60. }
  61. }
  62. private void UnBindingPDFViewerHandler()
  63. {
  64. if (PDFViewer != null)
  65. {
  66. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  67. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  68. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  69. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  70. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  71. }
  72. }
  73. #endregion 事件绑定和解绑
  74. #region PDFViewer事件
  75. //选中和非选中注释,右键菜单
  76. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  77. {
  78. if (e != null)
  79. {
  80. var annot = e.AnnotItemsList[0];
  81. if (annot != null)
  82. {
  83. //IsAnnotCreateReset:是否为创建注释的状态
  84. if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == false)
  85. {
  86. switch (annot.EventType)
  87. {
  88. case AnnotArgsType.AnnotHighlight:
  89. e.IsAnnotCreateReset = false;
  90. GetHighLight(annot as TextHighlightAnnotArgs);
  91. break;
  92. case AnnotArgsType.AnnotUnderline:
  93. GetUnderLine(annot as TextUnderlineAnnotArgs);
  94. break;
  95. case AnnotArgsType.AnnotStrikeout:
  96. GetStrikeout(annot as TextStrikeoutAnnotArgs);
  97. break;
  98. case AnnotArgsType.AnnotSquiggly:
  99. GetSquiggly(annot as TextSquigglyAnnotArgs);
  100. break;
  101. case AnnotArgsType.AnnotFreehand:
  102. GetFreehand(annot as FreehandAnnotArgs);
  103. break;
  104. case AnnotArgsType.AnnotFreeText:
  105. GetFreetext(annot as FreeTextAnnotArgs);
  106. break;
  107. case AnnotArgsType.AnnotSquare:
  108. GetRect(annot as SquareAnnotArgs);
  109. break;
  110. case AnnotArgsType.AnnotCircle:
  111. GetCircle(annot as CircleAnnotArgs);
  112. break;
  113. case AnnotArgsType.AnnotLine:
  114. bool isLine = true;
  115. if (e.Attribs.ContainsKey(AnnotAttrib.LineStart))
  116. {
  117. if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_NONE)
  118. {
  119. isLine = false;
  120. }
  121. }
  122. if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd))
  123. {
  124. if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_NONE)
  125. {
  126. isLine = false;
  127. }
  128. }
  129. if (isLine)
  130. GetArrowLine("Line", annot as LineAnnotArgs);
  131. else
  132. GetArrowLine("Arrow", annot as LineAnnotArgs);
  133. break;
  134. case AnnotArgsType.AnnotLink:
  135. //viewContentViewModel.IsCreateLink = false;
  136. GetLink(annot as LinkAnnotArgs, e);
  137. break;
  138. case AnnotArgsType.AnnotSticky:
  139. GetStickyNote(annot as StickyAnnotArgs);
  140. break;
  141. }
  142. //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值
  143. PDFViewer.SetToolParam(annot);
  144. }
  145. else if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == true)
  146. {
  147. switch (annot.EventType)
  148. {
  149. case AnnotArgsType.AnnotLink:
  150. //viewContentViewModel.IsCreateLink = false;
  151. GetLink(annot as LinkAnnotArgs, e);
  152. PDFViewer.SetToolParam(annot);
  153. break;
  154. }
  155. //TODO: 设计已重新调整为(仅限高亮注释):修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  156. if (annot.EventType != AnnotArgsType.AnnotStrikeout &&
  157. annot.EventType != AnnotArgsType.AnnotUnderline &&
  158. annot.EventType != AnnotArgsType.AnnotHighlight &&
  159. annot.EventType != AnnotArgsType.AnnotSquiggly
  160. )
  161. {
  162. if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  163. {
  164. foreach (var item in ToolExpandDict)
  165. {
  166. if (item.Value == e.AnnotItemsList[0].EventType)
  167. {
  168. annot = null;//新建注释时,回到默认值
  169. FindAnnotTypeKey(item.Key, ref annot);
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. //else
  176. PDFViewer.SetToolParam(annot);
  177. //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏
  178. if (isRightMenuAddAnnot)
  179. {
  180. ShowPropertyPanel(false);
  181. }
  182. else
  183. {
  184. ShowPropertyPanel();
  185. }
  186. }
  187. }
  188. }
  189. else
  190. {
  191. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  192. }
  193. }
  194. //在注释工具的状态下,右键菜单
  195. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  196. {
  197. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  198. return;
  199. switch (e.CommandType)
  200. {
  201. case CommandType.Context:
  202. if (e.AnnotEventArgsList.Count > 0)
  203. {
  204. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  205. {
  206. //绑定标记密文处右键菜单
  207. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  208. }
  209. else
  210. {
  211. e.PopupMenu = SelectAnnotContextMenu(sender);
  212. }
  213. if (e.PopupMenu != null)
  214. {
  215. e.Handle = true;
  216. }
  217. }
  218. else
  219. {
  220. if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection)
  221. {
  222. e.PopupMenu = NoneSelectAnnotContextMenu(sender, e);
  223. if (e.PopupMenu != null)
  224. {
  225. e.Handle = true;
  226. }
  227. }
  228. else
  229. {
  230. e.PopupMenu = ViewerContextMenu();
  231. if (e.PopupMenu != null)
  232. {
  233. e.Handle = true;
  234. }
  235. }
  236. }
  237. break;
  238. }
  239. }
  240. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  241. {
  242. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  243. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  244. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  245. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent; ;
  246. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  247. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  248. {
  249. e.PopupMenu = popMenu;
  250. e.Handle = true;
  251. if (popMenu != null && popMenu.Items.Count == 5)
  252. {
  253. //复制
  254. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  255. menuItem.CommandTarget = PDFViewer;
  256. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  257. //导出
  258. menuItem = popMenu.Items[1] as MenuItem;
  259. menuItem.CommandTarget = PDFViewer;
  260. if (menuItem.Items.Count == 3)
  261. {
  262. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  263. menuItem1.CommandTarget = PDFViewer;
  264. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  265. menuItem1 = menuItem.Items[1] as MenuItem;
  266. menuItem1.CommandTarget = PDFViewer;
  267. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  268. menuItem1 = menuItem.Items[2] as MenuItem;
  269. menuItem1.CommandTarget = PDFViewer;
  270. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  271. }
  272. //裁剪
  273. menuItem = popMenu.Items[2] as MenuItem;
  274. menuItem.CommandTarget = PDFViewer;
  275. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  276. //缩放至所选区域
  277. menuItem = popMenu.Items[3] as MenuItem;
  278. menuItem.CommandTarget = PDFViewer;
  279. menuItem.Visibility = Visibility.Collapsed;
  280. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  281. //打印
  282. menuItem = popMenu.Items[4] as MenuItem;
  283. menuItem.CommandTarget = PDFViewer;
  284. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  285. }
  286. }
  287. }
  288. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  289. {
  290. switch (e.Key)
  291. {
  292. case "CloseSnap":
  293. {
  294. #region to do
  295. //var item = PDFViewerTab.SelectedItem as TabItem;
  296. //if (item == null)
  297. //{
  298. // ClearSelectedToolPanel();
  299. // return;
  300. //}
  301. //Grid grid = item.Content as Grid;
  302. //if (grid == null || grid.Children.Count == 0)
  303. //{
  304. // ClearSelectedToolPanel();
  305. // return;
  306. //}
  307. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  308. //if (pdfViewer == null)
  309. //{
  310. // ClearSelectedToolPanel();
  311. // return;
  312. //}
  313. #endregion to do
  314. switch (PDFViewer.MouseMode)
  315. {
  316. case MouseModes.SelectTextTool:
  317. break;
  318. default:
  319. BtnSelecttoolIsChecked = false;
  320. break;
  321. }
  322. }
  323. break;
  324. default:
  325. break;
  326. }
  327. }
  328. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  329. {
  330. if (e != null && e.Count > 0)
  331. {
  332. for (int i = 0; i < e.Count; i++)
  333. {
  334. AnnotEditEvent editEvent = e[i];
  335. switch (editEvent.EditAction)
  336. {
  337. case ActionType.Add:
  338. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  339. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  340. {
  341. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  342. if (viewModel != null)
  343. {
  344. int pageindex = editEvent.PageIndex;
  345. int annotindex = editEvent.AnnotIndex;
  346. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  347. }
  348. }
  349. break;
  350. case ActionType.Del:
  351. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  352. if (isTabItemAnnotation)
  353. {
  354. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  355. if (viewModel != null)
  356. {
  357. int pageindex = editEvent.PageIndex;
  358. int annotindex = editEvent.AnnotIndex;
  359. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  360. }
  361. }
  362. break;
  363. case ActionType.Modify:
  364. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  365. if (isTabItemAnnotation)
  366. {
  367. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  368. if (viewModel != null)
  369. {
  370. int pageindex = editEvent.PageIndex;
  371. int annotindex = editEvent.AnnotIndex;
  372. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  373. }
  374. }
  375. break;
  376. case ActionType.TextEdit:
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. }
  383. }
  384. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  385. {
  386. }
  387. #endregion PDFViewer事件
  388. #region BindingEvent事件
  389. private void AnnotProperty_DefaultStored(object sender, object e)
  390. {
  391. }
  392. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  393. {
  394. if (e != null)
  395. {
  396. AnnotHandlerEventArgs annotArgs = null;
  397. foreach (AnnotArgsType argsType in e.Keys)
  398. {
  399. switch (argsType)
  400. {
  401. case AnnotArgsType.AnnotSquare:
  402. annotArgs = GetRect();
  403. break;
  404. case AnnotArgsType.AnnotCircle:
  405. annotArgs = GetCircle();
  406. break;
  407. case AnnotArgsType.AnnotLine:
  408. var LineTag = e[argsType] as string;
  409. annotArgs = GetArrowLine(LineTag);
  410. break;
  411. }
  412. if (annotArgs != null)
  413. {
  414. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  415. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  416. PDFViewer.SetToolParam(annotArgs);
  417. }
  418. ShowPropertyPanel();
  419. }
  420. }
  421. }
  422. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  423. {
  424. if (e != null)
  425. {
  426. foreach (AnnotArgsType argsType in e.Keys)
  427. {
  428. switch (argsType)
  429. {
  430. case AnnotArgsType.AnnotHighlight:
  431. if (e[argsType] is Color)
  432. {
  433. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  434. }
  435. if (e[argsType] is double)
  436. {
  437. HighLightOpacity = (double)e[argsType];
  438. }
  439. break;
  440. case AnnotArgsType.AnnotUnderline:
  441. if (e[argsType] is Color)
  442. {
  443. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  444. }
  445. if (e[argsType] is double)
  446. {
  447. underLineOpacity = (double)e[argsType];
  448. }
  449. break;
  450. case AnnotArgsType.AnnotSquiggly:
  451. if (e[argsType] is Color)
  452. {
  453. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  454. }
  455. if (e[argsType] is double)
  456. {
  457. SquigglyOpacity = (double)e[argsType];
  458. }
  459. break;
  460. case AnnotArgsType.AnnotStrikeout:
  461. if (e[argsType] is Color)
  462. {
  463. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  464. }
  465. if (e[argsType] is double)
  466. {
  467. StrikeoutOpacity = (double)e[argsType];
  468. }
  469. break;
  470. case AnnotArgsType.AnnotFreehand:
  471. if (e[argsType] is Color)
  472. {
  473. // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]);
  474. }
  475. if (e[argsType] is double)
  476. {
  477. // FreehandPath.Opacity = (double)e[argsType];
  478. }
  479. break;
  480. case AnnotArgsType.AnnotErase:
  481. if (e[argsType] is ToggleButton)
  482. {
  483. ToggleButton clickBtn = e[argsType] as ToggleButton;
  484. if (clickBtn.IsChecked == true)
  485. {
  486. if (clickBtn.Tag.ToString() == "PenBtn")
  487. {
  488. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  489. btn.Tag = "Freehand"; btn.IsChecked = true;
  490. BtnMyTools_Click(btn);
  491. break;
  492. }
  493. EraseArgs eraseArgs = new EraseArgs();
  494. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  495. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  496. eraseArgs.Thickness = 10;
  497. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  498. if (annotProperty != null)
  499. {
  500. eraseArgs.Thickness = annotProperty.Thickness;
  501. }
  502. PDFViewer.ClearSelectAnnots(false);
  503. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  504. PDFViewer.SetToolParam(eraseArgs);
  505. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  506. annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  507. annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  508. annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  509. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
  510. }
  511. }
  512. break;
  513. }
  514. }
  515. }
  516. }
  517. #endregion BindingEvent事件
  518. }
  519. }