AnnotationContent.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using PDF_Master.DataConvert;
  6. using PDF_Master.EventAggregators;
  7. using PDF_Master.Helper;
  8. using PDF_Master.Model.BOTA;
  9. using PDF_Master.ViewModels.BOTA;
  10. using Prism.Events;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Annotations;
  20. using System.Windows.Controls;
  21. using System.Windows.Controls.Primitives;
  22. using System.Windows.Data;
  23. using System.Windows.Documents;
  24. using System.Windows.Forms;
  25. using System.Windows.Input;
  26. using System.Windows.Media;
  27. using System.Windows.Media.Imaging;
  28. using System.Windows.Navigation;
  29. using System.Windows.Shapes;
  30. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  31. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  32. using ListBox = System.Windows.Controls.ListBox;
  33. using MenuItem = System.Windows.Controls.MenuItem;
  34. using UserControl = System.Windows.Controls.UserControl;
  35. namespace PDF_Master.Views.BOTA
  36. {
  37. /// <summary>
  38. /// AnnotationContent.xaml 的交互逻辑
  39. /// </summary>
  40. public partial class AnnotationContent : UserControl
  41. {
  42. private AnnotationContentViewModel viewModel;
  43. private string unicode;
  44. public AnnotationContent(IEventAggregator eventAggregator)
  45. {
  46. InitializeComponent();
  47. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  48. viewModel = this.DataContext as AnnotationContentViewModel;
  49. if (AnnotationList.Items.Count < 0)
  50. {
  51. MenuExpandAll.IsEnabled = false;
  52. }
  53. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  54. }
  55. private void CleanSelectAll(CleanSelectAllArgs obj)
  56. {
  57. AnnotationList.SelectedIndex = -1;
  58. }
  59. private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  60. {
  61. if (e.ClickCount == 1)
  62. {
  63. //if (Mouse.LeftButton == e.ButtonState)
  64. //{
  65. // viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  66. //}
  67. if (Mouse.RightButton == e.ButtonState)
  68. {
  69. var pos = e.GetPosition(AnnotationList);
  70. var result = VisualTreeHelper.HitTest(AnnotationList, pos);
  71. if (result != null)
  72. {
  73. ListBoxItem myListBoxItem = sender as System.Windows.Controls.ListBoxItem;
  74. MenuItem copyText = myListBoxItem.ContextMenu.Items[0] as MenuItem;
  75. MenuItem import = myListBoxItem.ContextMenu.Items[1] as MenuItem;
  76. MenuItem export = myListBoxItem.ContextMenu.Items[2] as MenuItem;
  77. if (AnnotationList.SelectedItems.Count > 1)
  78. {
  79. import.IsEnabled = false;
  80. export.IsEnabled = true;
  81. export.Command = viewModel.ExportCommentsCommand;
  82. copyText.IsEnabled = false;
  83. }
  84. else
  85. {
  86. //直接右键菜单不需要选中Viewer中注释
  87. //viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  88. if (myListBoxItem.DataContext is AnnotationHandlerEventArgs annotation)
  89. {
  90. //文本、高亮、下划线、删除线、便签
  91. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  92. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  93. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  94. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  95. annotation.EventType == AnnotArgsType.AnnotSticky)
  96. {
  97. copyText.IsEnabled = true;
  98. }
  99. else
  100. {
  101. copyText.IsEnabled = false;
  102. }
  103. }
  104. import.IsEnabled = true;
  105. import.Command = viewModel.ImportCommentsCommand;
  106. export.IsEnabled = true;
  107. export.Command = viewModel.ExportCommentsCommand;
  108. }
  109. }
  110. //拦截响应选中事件
  111. e.Handled = true;
  112. }
  113. }
  114. }
  115. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  116. {
  117. if (Mouse.LeftButton == e.ButtonState)
  118. {
  119. viewModel.AddNotesCommand.Execute(sender);
  120. }
  121. }
  122. private async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
  123. {
  124. SetAllExpander(true);
  125. await Task.Delay(1);
  126. for (int i = 0; i < AnnotationList.Items.Count; i++)
  127. {
  128. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  129. ToggleButton button = GetExpandButton(item);
  130. if (button != null && button.Visibility == Visibility.Visible)
  131. {
  132. button.IsChecked = true;
  133. }
  134. }
  135. }
  136. private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
  137. {
  138. for (int i = 0; i < AnnotationList.Items.Count; i++)
  139. {
  140. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  141. ToggleButton button = GetExpandButton(item);
  142. if (button != null && button.Visibility == Visibility.Visible)
  143. {
  144. button.IsChecked = false;
  145. }
  146. }
  147. SetAllExpander(false);
  148. }
  149. private void SetAllExpander(bool isexpand)
  150. {
  151. AnnotationList.Dispatcher.Invoke(() =>
  152. {
  153. //开启虚拟化之后 全部展开和折叠会有问题
  154. var scroller = GetScrollHost(AnnotationList);
  155. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  156. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  157. for (int i = 0; i < count; i++)
  158. {
  159. var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  160. var g = CommonHelper.FindVisualChild<Expander>(item);
  161. if (g != null)
  162. g.IsExpanded = isexpand;
  163. }
  164. });
  165. }
  166. private ScrollViewer GetScrollHost(ListBox listBox)
  167. {
  168. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  169. {
  170. int s = VisualTreeHelper.GetChildrenCount(listBox);
  171. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  172. if (border != null)
  173. {
  174. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  175. }
  176. }
  177. return null;
  178. }
  179. private ToggleButton GetExpandButton(ListBoxItem item)
  180. {
  181. if (item == null) return null;
  182. Border border = VisualTreeHelper.GetChild(item, 0) as Border;
  183. var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
  184. return btn;
  185. }
  186. private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
  187. {
  188. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  189. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  190. v.GroupDescriptions.Clear();
  191. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  192. v.SortDescriptions.Clear();
  193. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  194. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  195. }
  196. private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
  197. {
  198. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  199. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  200. v.GroupDescriptions.Clear();
  201. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  202. v.SortDescriptions.Clear();
  203. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  204. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  205. }
  206. private void MenuPageSort_Click(object sender, RoutedEventArgs e)
  207. {
  208. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  209. v.GroupDescriptions.Clear();
  210. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  211. v.SortDescriptions.Clear();
  212. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  213. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  214. }
  215. private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
  216. {
  217. if (AnnotationList.SelectedItems.Count == 1)
  218. {
  219. if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
  220. {
  221. //文本、高亮、下划线、删除线、便签
  222. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  223. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  224. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  225. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  226. annotation.EventType == AnnotArgsType.AnnotSticky)
  227. {
  228. StringBuilder Copystr = new StringBuilder();
  229. if (annotation.MarkupContent != null)
  230. {
  231. Copystr.Append(annotation.MarkupContent);
  232. Copystr.Append(Environment.NewLine);
  233. }
  234. if (annotation.Content != null)
  235. {
  236. Copystr.Append(annotation.Content);
  237. }
  238. System.Windows.Clipboard.SetText(Copystr.ToString());
  239. //CustomControl.MessageBoxEx.Show("数据复制成功");
  240. }
  241. }
  242. }
  243. }
  244. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  245. {
  246. List<int> pagelist = new List<int>();
  247. if (AnnotationList.SelectedItems.Count > 1)
  248. {
  249. for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
  250. {
  251. AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
  252. pagelist.Add(AnnotationList.Items.IndexOf(annotation));
  253. }
  254. pagelist.Sort();
  255. for (int i = 0; i < pagelist.Count; i++)
  256. {
  257. AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  258. if (annotation == null)
  259. {
  260. continue;
  261. }
  262. viewModel.DeleteCommand.Execute(annotation);
  263. }
  264. }
  265. else
  266. {
  267. AnnotationHandlerEventArgs annotation = (AnnotationHandlerEventArgs)(sender as MenuItem).DataContext;
  268. if (annotation == null)
  269. {
  270. return;
  271. }
  272. viewModel.DeleteCommand.Execute(annotation);
  273. }
  274. }
  275. private void BtnMore_Initialized(object sender, EventArgs e)
  276. {
  277. BtnMore.ContextMenu = null;
  278. }
  279. private void BtnMore_Click(object sender, RoutedEventArgs e)
  280. {
  281. MenuMore.PlacementTarget = BtnMore;
  282. MenuMore.IsOpen = true;
  283. }
  284. private void AnnotationList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  285. {
  286. if (AnnotationList.SelectedItems.Count > 1)
  287. {
  288. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItems);
  289. }
  290. else if (AnnotationList.SelectedItems.Count == 1)
  291. {
  292. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItem);
  293. }
  294. }
  295. private void AnnotationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  296. {
  297. var a = AnnotationList.SelectedItems.Count;
  298. var a1 = (sender as ListBox).SelectedItems.Count;
  299. var a2 = (e.OriginalSource as ListBox).SelectedItems.Count;
  300. var a3 = (e.Source as ListBox).SelectedItems.Count;
  301. Trace.WriteLine("a:" + a + " a1:" + a1 + " a2:" + a2 + " a3:" + a3);
  302. //AnnotationList.ScrollIntoView(AnnotationList.Items[0]);
  303. if (a1 == 1)
  304. {
  305. //if(viewModel.ViewContentViewModel.IsPropertyOpen==false)
  306. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItems[0]);
  307. }
  308. else if (a1 > 1)
  309. {
  310. #region 阅读视图 多选
  311. Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
  312. List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
  313. foreach (var item in (sender as ListBox).SelectedItems)
  314. {
  315. if (item is AnnotationHandlerEventArgs annotation)
  316. {
  317. if (eventArgs.Contains(annotation) == false)
  318. {
  319. eventArgs.Add(annotation);
  320. }
  321. }
  322. }
  323. var result = eventArgs.GroupBy(d => d.PageIndex);
  324. foreach (var item in result)
  325. {
  326. if (selectedItemDics.ContainsKey(item.Key) == false)
  327. {
  328. var ai = item.Select(w => w.AnnotIndex).ToList();
  329. selectedItemDics.Add(item.Key, ai);
  330. }
  331. }
  332. #endregion 阅读视图 多选
  333. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(selectedItemDics);
  334. }
  335. }
  336. private void AnnotationList_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  337. {
  338. if (AnnotationList.SelectedItems == null)
  339. {
  340. return;
  341. }
  342. if (e.Key == Key.Escape)
  343. {
  344. AnnotationList.SelectedItems.Clear();
  345. }
  346. }
  347. private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  348. {
  349. var pos = e.GetPosition(this);
  350. var result = VisualTreeHelper.HitTest(this, pos);
  351. if (result != null)
  352. {
  353. //点击按钮和listboxitem时 不清空多选 其余区域取消多选
  354. var item = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  355. if (item != null)
  356. {
  357. return;
  358. }
  359. var btn = CommonHelper.FindVisualParent<System.Windows.Controls.Button>(result.VisualHit);
  360. if (btn != null)
  361. {
  362. return;
  363. }
  364. }
  365. }
  366. private void ListBox_MouseDown(object sender, MouseButtonEventArgs e)
  367. {
  368. AnnotationList.SelectedIndex = -1;
  369. }
  370. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  371. {
  372. var menu = sender as System.Windows.Controls.ContextMenu;
  373. if (menu != null)
  374. {
  375. (menu.Items[0] as MenuItem).Header = viewModel.T_CopyText;
  376. (menu.Items[1] as MenuItem).Header = viewModel.T_Import;
  377. (menu.Items[2] as MenuItem).Header = viewModel.T_Export;
  378. (menu.Items[3] as MenuItem).Header = viewModel.T_Delete;
  379. if (AnnotationList.SelectedItems.Count > 1)
  380. {
  381. (menu.Items[3] as MenuItem).Header = viewModel.T_DeleteAll;
  382. }
  383. }
  384. }
  385. }
  386. }