AnnotToolContentViewModel.Command.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. break;
  153. }
  154. PDFViewer.SetToolParam(annot);
  155. //TODO: 设计已重新调整为:修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  156. //if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  157. //{
  158. // foreach (var item in ToolExpandDict)
  159. // {
  160. // if (item.Value == e.AnnotItemsList[0].EventType)
  161. // {
  162. // FindAnnotTypeKey(item.Key, ref annot);
  163. // break;
  164. // }
  165. // }
  166. //}
  167. //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏
  168. if (isRightMenuAddAnnot)
  169. {
  170. ShowPropertyPanel(false);
  171. }
  172. else
  173. {
  174. ShowPropertyPanel();
  175. }
  176. }
  177. }
  178. }
  179. else
  180. {
  181. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  182. }
  183. }
  184. //在注释工具的状态下,右键菜单
  185. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  186. {
  187. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  188. return;
  189. switch (e.CommandType)
  190. {
  191. case CommandType.Context:
  192. if (e.AnnotEventArgsList.Count > 0)
  193. {
  194. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  195. {
  196. //绑定标记密文处右键菜单
  197. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  198. }
  199. else
  200. {
  201. e.PopupMenu = SelectAnnotContextMenu(sender);
  202. }
  203. if (e.PopupMenu != null)
  204. {
  205. e.Handle = true;
  206. }
  207. }
  208. else
  209. {
  210. if (e.PressOnSelectedText)
  211. {
  212. e.PopupMenu = NoneSelectAnnotContextMenu(sender, e);
  213. if (e.PopupMenu != null)
  214. {
  215. e.Handle = true;
  216. }
  217. }
  218. else
  219. {
  220. e.PopupMenu = ViewerContextMenu();
  221. if (e.PopupMenu != null)
  222. {
  223. e.Handle = true;
  224. }
  225. }
  226. }
  227. break;
  228. }
  229. }
  230. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  231. {
  232. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  233. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  234. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  235. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent; ;
  236. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  237. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  238. {
  239. e.PopupMenu = popMenu;
  240. e.Handle = true;
  241. if (popMenu != null && popMenu.Items.Count == 5)
  242. {
  243. //复制
  244. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  245. menuItem.CommandTarget = PDFViewer;
  246. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  247. //导出
  248. menuItem = popMenu.Items[1] as MenuItem;
  249. menuItem.CommandTarget = PDFViewer;
  250. if (menuItem.Items.Count == 3)
  251. {
  252. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  253. menuItem1.CommandTarget = PDFViewer;
  254. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  255. menuItem1 = menuItem.Items[1] as MenuItem;
  256. menuItem1.CommandTarget = PDFViewer;
  257. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  258. menuItem1 = menuItem.Items[2] as MenuItem;
  259. menuItem1.CommandTarget = PDFViewer;
  260. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  261. }
  262. //裁剪
  263. menuItem = popMenu.Items[2] as MenuItem;
  264. menuItem.CommandTarget = PDFViewer;
  265. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  266. //缩放至所选区域
  267. menuItem = popMenu.Items[3] as MenuItem;
  268. menuItem.CommandTarget = PDFViewer;
  269. menuItem.Visibility = Visibility.Collapsed;
  270. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  271. //打印
  272. menuItem = popMenu.Items[4] as MenuItem;
  273. menuItem.CommandTarget = PDFViewer;
  274. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  275. }
  276. }
  277. }
  278. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  279. {
  280. switch (e.Key)
  281. {
  282. case "CloseSnap":
  283. {
  284. #region to do
  285. //var item = PDFViewerTab.SelectedItem as TabItem;
  286. //if (item == null)
  287. //{
  288. // ClearSelectedToolPanel();
  289. // return;
  290. //}
  291. //Grid grid = item.Content as Grid;
  292. //if (grid == null || grid.Children.Count == 0)
  293. //{
  294. // ClearSelectedToolPanel();
  295. // return;
  296. //}
  297. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  298. //if (pdfViewer == null)
  299. //{
  300. // ClearSelectedToolPanel();
  301. // return;
  302. //}
  303. #endregion to do
  304. switch (PDFViewer.MouseMode)
  305. {
  306. case MouseModes.SelectTextTool:
  307. break;
  308. default:
  309. BtnSelecttoolIsChecked = false;
  310. break;
  311. }
  312. }
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  319. {
  320. if (e != null && e.Count > 0)
  321. {
  322. for (int i = 0; i < e.Count; i++)
  323. {
  324. AnnotEditEvent editEvent = e[i];
  325. switch (editEvent.EditAction)
  326. {
  327. case ActionType.Add:
  328. BOTAContentViewModel bOTAContentViewModel = null;
  329. BOTAContent bOTAContent = null;
  330. bool isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  331. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  332. {
  333. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  334. if (viewModel != null)
  335. {
  336. int pageindex = editEvent.PageIndex;
  337. int annotindex = editEvent.AnnotIndex;
  338. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  339. }
  340. }
  341. break;
  342. case ActionType.Del:
  343. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  344. if (isTabItemAnnotation)
  345. {
  346. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  347. if (viewModel != null)
  348. {
  349. int pageindex = editEvent.PageIndex;
  350. int annotindex = editEvent.AnnotIndex;
  351. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  352. }
  353. }
  354. break;
  355. case ActionType.Modify:
  356. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  357. if (isTabItemAnnotation)
  358. {
  359. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  360. if (viewModel != null)
  361. {
  362. int pageindex = editEvent.PageIndex;
  363. int annotindex = editEvent.AnnotIndex;
  364. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  365. }
  366. }
  367. break;
  368. case ActionType.TextEdit:
  369. break;
  370. default:
  371. break;
  372. }
  373. }
  374. }
  375. }
  376. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  377. {
  378. }
  379. #endregion PDFViewer事件
  380. #region BindingEvent事件
  381. private void AnnotProperty_DefaultStored(object sender, object e)
  382. {
  383. }
  384. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  385. {
  386. if (e != null)
  387. {
  388. AnnotHandlerEventArgs annotArgs = null;
  389. foreach (AnnotArgsType argsType in e.Keys)
  390. {
  391. switch (argsType)
  392. {
  393. case AnnotArgsType.AnnotSquare:
  394. annotArgs = GetRect();
  395. break;
  396. case AnnotArgsType.AnnotCircle:
  397. annotArgs = GetCircle();
  398. break;
  399. case AnnotArgsType.AnnotLine:
  400. var LineTag = e[argsType] as string;
  401. annotArgs = GetArrowLine(LineTag);
  402. break;
  403. }
  404. if (annotArgs != null)
  405. {
  406. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  407. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  408. PDFViewer.SetToolParam(annotArgs);
  409. }
  410. ShowPropertyPanel();
  411. }
  412. }
  413. }
  414. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  415. {
  416. if (e != null)
  417. {
  418. foreach (AnnotArgsType argsType in e.Keys)
  419. {
  420. switch (argsType)
  421. {
  422. case AnnotArgsType.AnnotHighlight:
  423. if (e[argsType] is Color)
  424. {
  425. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  426. }
  427. if (e[argsType] is double)
  428. {
  429. HighLightOpacity = (double)e[argsType];
  430. }
  431. break;
  432. case AnnotArgsType.AnnotUnderline:
  433. if (e[argsType] is Color)
  434. {
  435. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  436. }
  437. if (e[argsType] is double)
  438. {
  439. underLineOpacity = (double)e[argsType];
  440. }
  441. break;
  442. case AnnotArgsType.AnnotSquiggly:
  443. if (e[argsType] is Color)
  444. {
  445. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  446. }
  447. if (e[argsType] is double)
  448. {
  449. SquigglyOpacity = (double)e[argsType];
  450. }
  451. break;
  452. case AnnotArgsType.AnnotStrikeout:
  453. if (e[argsType] is Color)
  454. {
  455. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  456. }
  457. if (e[argsType] is double)
  458. {
  459. StrikeoutOpacity = (double)e[argsType];
  460. }
  461. break;
  462. case AnnotArgsType.AnnotFreehand:
  463. if (e[argsType] is Color)
  464. {
  465. // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]);
  466. }
  467. if (e[argsType] is double)
  468. {
  469. // FreehandPath.Opacity = (double)e[argsType];
  470. }
  471. break;
  472. case AnnotArgsType.AnnotErase:
  473. if (e[argsType] is ToggleButton)
  474. {
  475. ToggleButton clickBtn = e[argsType] as ToggleButton;
  476. if (clickBtn.IsChecked == true)
  477. {
  478. if (clickBtn.Tag.ToString() == "PenBtn")
  479. {
  480. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  481. btn.Tag = "Freehand"; btn.IsChecked = true;
  482. BtnMyTools_Click(btn);
  483. break;
  484. }
  485. EraseArgs eraseArgs = new EraseArgs();
  486. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  487. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  488. eraseArgs.Thickness = 10;
  489. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  490. if (annotProperty != null)
  491. {
  492. eraseArgs.Thickness = annotProperty.Thickness;
  493. }
  494. PDFViewer.ClearSelectAnnots(false);
  495. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  496. PDFViewer.SetToolParam(eraseArgs);
  497. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  498. annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
  499. annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
  500. annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
  501. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
  502. }
  503. }
  504. break;
  505. }
  506. }
  507. }
  508. }
  509. #endregion BindingEvent事件
  510. }
  511. }