AnnotationContent.xaml.cs 17 KB

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