AnnotationContent.xaml.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ImTools;
  6. using PDF_Master.DataConvert;
  7. using PDF_Master.EventAggregators;
  8. using PDF_Master.Helper;
  9. using PDF_Master.Model.BOTA;
  10. using PDF_Master.ViewModels;
  11. using PDF_Master.ViewModels.BOTA;
  12. using Prism.Events;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel;
  16. using System.Diagnostics;
  17. using System.Linq;
  18. using System.Runtime.InteropServices.Expando;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Annotations;
  23. using System.Windows.Controls;
  24. using System.Windows.Controls.Primitives;
  25. using System.Windows.Data;
  26. using System.Windows.Documents;
  27. using System.Windows.Forms;
  28. using System.Windows.Input;
  29. using System.Windows.Media;
  30. using System.Windows.Media.Imaging;
  31. using System.Windows.Navigation;
  32. using System.Windows.Shapes;
  33. using static Dropbox.Api.Paper.ListPaperDocsSortBy;
  34. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  35. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  36. using ListBox = System.Windows.Controls.ListBox;
  37. using MenuItem = System.Windows.Controls.MenuItem;
  38. using UserControl = System.Windows.Controls.UserControl;
  39. namespace PDF_Master.Views.BOTA
  40. {
  41. /// <summary>
  42. /// AnnotationContent.xaml 的交互逻辑
  43. /// </summary>
  44. public partial class AnnotationContent : UserControl
  45. {
  46. public AnnotationContentViewModel viewModel;
  47. private string unicode;
  48. public AnnotationContent(IEventAggregator eventAggregator)
  49. {
  50. InitializeComponent();
  51. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  52. viewModel = this.DataContext as AnnotationContentViewModel;
  53. if (AnnotationList.Items.Count < 0)
  54. {
  55. MenuExpandAll.IsEnabled = false;
  56. }
  57. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  58. eventAggregator.GetEvent<RefreshAnnotationEvent>().Subscribe(ExpandGroupHeader, e => e.Unicode == unicode);
  59. }
  60. public async void ExpandGroupHeader(RefreshAnnotationArgs obj)
  61. {
  62. if (obj.IsAll)
  63. {
  64. ExpandAllGroupHeader(obj);
  65. }
  66. else
  67. {
  68. //需要时间加载完 listbox所有的控件模板
  69. await Task.Delay(500);
  70. ExpandOneGroupHeader(obj);
  71. }
  72. }
  73. private async void ExpandAllGroupHeader(RefreshAnnotationArgs obj)
  74. {
  75. try
  76. {
  77. foreach (var item in AnnotationList.Items)
  78. {
  79. var groups = AnnotationList.Items.Groups;
  80. for (int i = 0; i < groups.Count; i++)
  81. {
  82. if (groups[i] is CollectionViewGroup group)
  83. {
  84. if (group.Items.Contains(item))
  85. {
  86. await Task.Delay(5);
  87. var scroller = GetScrollHost(AnnotationList);
  88. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  89. if (stackpanel != null)
  90. {
  91. GroupItemExpanded(stackpanel, i);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. catch (Exception ex) { }
  99. }
  100. public async void ExpandOneGroupHeader(RefreshAnnotationArgs obj)
  101. {
  102. if (obj.annotHandlerArgs == null)
  103. {
  104. if (obj.Pageindex != -1 && obj.AnnotIndex != -1)
  105. {
  106. List<AnnotationHandlerEventArgs> annotations = AnnotationList.Items.Cast<AnnotationHandlerEventArgs>().ToList();
  107. obj.annotHandlerArgs = annotations.FirstOrDefault(x => x.PageIndex == obj.Pageindex && x.AnnotIndex == obj.AnnotIndex);
  108. }
  109. }
  110. if (obj.annotHandlerArgs != null)
  111. {
  112. try
  113. {
  114. var groups = AnnotationList.Items.Groups;
  115. if (groups != null && groups.Count > 0)
  116. {
  117. for (int i = 0; i < groups.Count; i++)
  118. {
  119. if (groups[i] is CollectionViewGroup group)
  120. {
  121. if (group.Items.Contains(obj.annotHandlerArgs))
  122. {
  123. var scroller = GetScrollHost(AnnotationList);
  124. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  125. if (stackpanel != null)
  126. {
  127. GroupItemExpanded(stackpanel, i);
  128. }
  129. else
  130. {
  131. ItemsPresenter itemsPresenter = CommonHelper.FindVisualChild<ItemsPresenter>(scroller);
  132. if (itemsPresenter != null)
  133. {
  134. var tt = VisualTreeHelper.GetChild(itemsPresenter, i);
  135. int count = VisualTreeHelper.GetChildrenCount(tt);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if (obj.IsDel)
  143. {
  144. viewModel.IsModified = true;
  145. }
  146. await Task.Delay(10);//不加延时 会有不滚动的现象
  147. SelectedItems(obj);
  148. }
  149. catch (Exception ex) { }
  150. }
  151. }
  152. private void GroupItemExpanded(StackPanel stackpanel, int i)
  153. {
  154. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  155. if (count > 0)
  156. {
  157. if (VisualTreeHelper.GetChild(stackpanel, i) is GroupItem groupItem)
  158. {
  159. var expander = CommonHelper.FindVisualChild<Expander>(groupItem);
  160. if (expander != null)
  161. {
  162. if (expander.IsExpanded == false)
  163. {
  164. expander.IsExpanded = true;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. private async void SelectedItems(RefreshAnnotationArgs obj)
  171. {
  172. if (obj.IsSelect && obj.annotHandlerArgs != null)
  173. {
  174. if (viewModel.ViewContentViewModel.IsMultiSelected == false)
  175. {
  176. if (AnnotationList.SelectedItems.IndexOf(obj.annotHandlerArgs) == -1)
  177. {
  178. viewModel.IsModified = false;
  179. AnnotationList.SelectedItem = obj.annotHandlerArgs;
  180. AnnotationList.SelectedIndex = AnnotationList.Items.IndexOf(obj.annotHandlerArgs);
  181. await Task.Delay(10);//不加延时 会有不滚动的现象
  182. AnnotationList.ScrollIntoView(AnnotationList.SelectedItem);
  183. }
  184. }
  185. }
  186. }
  187. private void CleanSelectAll(CleanSelectAllArgs obj)
  188. {
  189. AnnotationList.SelectedIndex = -1;
  190. }
  191. private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  192. {
  193. if (e.ClickCount == 1)
  194. {
  195. //if (Mouse.LeftButton == e.ButtonState)
  196. //{
  197. // viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  198. //}
  199. if (Mouse.RightButton == e.ButtonState)
  200. {
  201. var pos = e.GetPosition(AnnotationList);
  202. var result = VisualTreeHelper.HitTest(AnnotationList, pos);
  203. if (result != null)
  204. {
  205. ListBoxItem myListBoxItem = sender as System.Windows.Controls.ListBoxItem;
  206. MenuItem copyText = myListBoxItem.ContextMenu.Items[0] as MenuItem;
  207. MenuItem import = myListBoxItem.ContextMenu.Items[1] as MenuItem;
  208. MenuItem export = myListBoxItem.ContextMenu.Items[2] as MenuItem;
  209. if (AnnotationList.SelectedItems.Count > 1)
  210. {
  211. import.IsEnabled = false;
  212. export.IsEnabled = true;
  213. export.Command = viewModel.ExportCommentsCommand;
  214. copyText.IsEnabled = false;
  215. }
  216. else
  217. {
  218. //直接右键菜单不需要选中Viewer中注释
  219. //viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  220. if (myListBoxItem.DataContext is AnnotationHandlerEventArgs annotation)
  221. {
  222. //文本、高亮、下划线、删除线、便签
  223. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  224. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  225. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  226. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  227. annotation.EventType == AnnotArgsType.AnnotSticky)
  228. {
  229. copyText.IsEnabled = true;
  230. }
  231. else
  232. {
  233. copyText.IsEnabled = false;
  234. }
  235. }
  236. import.IsEnabled = true;
  237. import.Command = viewModel.ImportCommentsCommand;
  238. export.IsEnabled = true;
  239. export.Command = viewModel.ExportCommentsCommand;
  240. }
  241. }
  242. //拦截响应选中事件
  243. e.Handled = true;
  244. }
  245. }
  246. }
  247. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  248. {
  249. if (Mouse.LeftButton == e.ButtonState)
  250. {
  251. viewModel.AddNotesCommand.Execute(sender);
  252. }
  253. }
  254. private async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
  255. {
  256. SetAllExpander(true);
  257. await Task.Delay(1);
  258. for (int i = 0; i < AnnotationList.Items.Count; i++)
  259. {
  260. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  261. if (item != null)
  262. {
  263. ToggleButton button = GetExpandButton(item);
  264. if (button != null && button.Visibility == Visibility.Visible)
  265. {
  266. button.IsChecked = true;
  267. }
  268. }
  269. }
  270. }
  271. private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
  272. {
  273. for (int i = 0; i < AnnotationList.Items.Count; i++)
  274. {
  275. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  276. ToggleButton button = GetExpandButton(item);
  277. if (button != null && button.Visibility == Visibility.Visible)
  278. {
  279. button.IsChecked = false;
  280. }
  281. }
  282. SetAllExpander(false);
  283. }
  284. private void SetAllExpander(bool isexpand)
  285. {
  286. AnnotationList.Dispatcher.Invoke(() =>
  287. {
  288. //开启虚拟化之后 全部展开和折叠会有问题
  289. var scroller = GetScrollHost(AnnotationList);
  290. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  291. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  292. for (int i = 0; i < count; i++)
  293. {
  294. var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  295. var g = CommonHelper.FindVisualChild<Expander>(item);
  296. if (g != null)
  297. g.IsExpanded = isexpand;
  298. }
  299. });
  300. }
  301. private ScrollViewer GetScrollHost(ListBox listBox)
  302. {
  303. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  304. {
  305. int s = VisualTreeHelper.GetChildrenCount(listBox);
  306. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  307. if (border != null)
  308. {
  309. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  310. }
  311. }
  312. return null;
  313. }
  314. private ToggleButton GetExpandButton(ListBoxItem item)
  315. {
  316. if (item == null) return null;
  317. Border border = VisualTreeHelper.GetChild(item, 0) as Border;
  318. var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
  319. return btn;
  320. }
  321. private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
  322. {
  323. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  324. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  325. v.GroupDescriptions.Clear();
  326. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  327. v.SortDescriptions.Clear();
  328. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  329. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  330. }
  331. private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
  332. {
  333. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  334. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  335. v.GroupDescriptions.Clear();
  336. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  337. v.SortDescriptions.Clear();
  338. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  339. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  340. }
  341. private void MenuPageSort_Click(object sender, RoutedEventArgs e)
  342. {
  343. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  344. v.GroupDescriptions.Clear();
  345. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  346. v.SortDescriptions.Clear();
  347. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  348. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  349. }
  350. private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
  351. {
  352. //没有权限解密时,拦截复制文本操作
  353. if (!viewModel.ViewContentViewModel.CheckPermissionBeforeOption())
  354. {
  355. return;
  356. }
  357. if (AnnotationList.SelectedItems.Count == 1)
  358. {
  359. if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
  360. {
  361. //文本、高亮、下划线、删除线、便签
  362. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  363. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  364. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  365. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  366. annotation.EventType == AnnotArgsType.AnnotSticky)
  367. {
  368. StringBuilder Copystr = new StringBuilder();
  369. if (annotation.MarkupContent != null)
  370. {
  371. Copystr.Append(annotation.MarkupContent);
  372. Copystr.Append(Environment.NewLine);
  373. }
  374. if (annotation.Content != null)
  375. {
  376. Copystr.Append(annotation.Content);
  377. }
  378. System.Windows.Clipboard.SetText(Copystr.ToString());
  379. //CustomControl.MessageBoxEx.Show("数据复制成功");
  380. }
  381. }
  382. }
  383. }
  384. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  385. {
  386. List<int> pagelist = new List<int>();
  387. if (AnnotationList.SelectedItems.Count > 1)
  388. {
  389. //for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
  390. //{
  391. // AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
  392. // pagelist.Add(AnnotationList.Items.IndexOf(annotation));
  393. //}
  394. //pagelist.Sort();
  395. //for (int i = 0; i < pagelist.Count; i++)
  396. //{
  397. // AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  398. // if (annotation == null)
  399. // {
  400. // continue;
  401. // }
  402. // viewModel.DeleteCommand.Execute(annotation);
  403. //}
  404. Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
  405. List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
  406. foreach (var item in AnnotationList.SelectedItems)
  407. {
  408. if (item is AnnotationHandlerEventArgs annotation)
  409. {
  410. if (eventArgs.Contains(annotation) == false)
  411. {
  412. eventArgs.Add(annotation);
  413. }
  414. }
  415. }
  416. var result = eventArgs.GroupBy(d => d.PageIndex);
  417. foreach (var item in result)
  418. {
  419. if (selectedItemDics.ContainsKey(item.Key) == false)
  420. {
  421. var ai = item.Select(w => w.AnnotIndex).ToList();
  422. selectedItemDics.Add(item.Key, ai);
  423. }
  424. }
  425. viewModel.DeleteCommand.Execute(selectedItemDics);
  426. }
  427. else
  428. {
  429. AnnotationHandlerEventArgs annotation = (AnnotationHandlerEventArgs)(sender as MenuItem).DataContext;
  430. if (annotation == null)
  431. {
  432. return;
  433. }
  434. viewModel.DeleteCommand.Execute(annotation);
  435. }
  436. }
  437. private void BtnMore_Initialized(object sender, EventArgs e)
  438. {
  439. BtnMore.ContextMenu = null;
  440. }
  441. private void BtnMore_Click(object sender, RoutedEventArgs e)
  442. {
  443. MenuMore.PlacementTarget = BtnMore;
  444. MenuMore.IsOpen = true;
  445. }
  446. private void AnnotationList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  447. {
  448. var pos = e.GetPosition(AnnotationList);
  449. var result = VisualTreeHelper.HitTest(AnnotationList, pos);
  450. if (result != null)
  451. {
  452. //获取当前鼠标指针下的容器
  453. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  454. if (listBoxItem == null)
  455. {
  456. viewModel.ViewContentViewModel.mainViewModel.AnnotMouseLeftButtonDown = false;
  457. }
  458. }
  459. }
  460. private void AnnotationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  461. {
  462. var selectedItems = (sender as ListBox).SelectedItems.Cast<AnnotationHandlerEventArgs>().ToList();
  463. if (selectedItems.Count == 1)
  464. {
  465. if (viewModel.IsModified)
  466. {
  467. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(selectedItems[0]);
  468. }
  469. viewModel.IsModified = true;
  470. }
  471. else if (selectedItems.Count > 1)
  472. {
  473. #region 阅读视图 多选
  474. Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
  475. List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
  476. foreach (var item in selectedItems)
  477. {
  478. if (item is AnnotationHandlerEventArgs annotation)
  479. {
  480. if (eventArgs.Contains(annotation) == false)
  481. {
  482. eventArgs.Add(annotation);
  483. }
  484. }
  485. }
  486. var result = eventArgs.GroupBy(d => d.PageIndex);
  487. foreach (var item in result)
  488. {
  489. if (selectedItemDics.ContainsKey(item.Key) == false)
  490. {
  491. var ai = item.Select(w => w.AnnotIndex).ToList();
  492. selectedItemDics.Add(item.Key, ai);
  493. }
  494. }
  495. #endregion 阅读视图 多选
  496. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(selectedItemDics);
  497. }
  498. }
  499. private void AnnotationList_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  500. {
  501. if (AnnotationList.SelectedItems == null)
  502. {
  503. return;
  504. }
  505. if (e.Key == Key.Escape)
  506. {
  507. //Windows:BOTA列表,移除Esc取消选中逻辑
  508. //AnnotationList.SelectedItems.Clear();
  509. }
  510. }
  511. private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  512. {
  513. var pos = e.GetPosition(this);
  514. var result = VisualTreeHelper.HitTest(this, pos);
  515. if (result != null)
  516. {
  517. //点击按钮和listboxitem时 不清空多选 其余区域取消多选
  518. var item = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  519. if (item != null)
  520. {
  521. return;
  522. }
  523. var btn = CommonHelper.FindVisualParent<System.Windows.Controls.Button>(result.VisualHit);
  524. if (btn != null)
  525. {
  526. return;
  527. }
  528. }
  529. }
  530. private void ListBox_MouseDown(object sender, MouseButtonEventArgs e)
  531. {
  532. AnnotationList.SelectedIndex = -1;
  533. }
  534. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  535. {
  536. var menu = sender as System.Windows.Controls.ContextMenu;
  537. if (menu != null)
  538. {
  539. (menu.Items[0] as MenuItem).Header = viewModel.T_CopyText;
  540. (menu.Items[1] as MenuItem).Header = viewModel.T_Import;
  541. (menu.Items[2] as MenuItem).Header = viewModel.T_Export;
  542. (menu.Items[3] as MenuItem).Header = viewModel.T_Delete;
  543. if (AnnotationList.SelectedItems.Count > 1)
  544. {
  545. (menu.Items[3] as MenuItem).Header = viewModel.T_DeleteAll;
  546. }
  547. }
  548. }
  549. }
  550. }