AnnotationContent.xaml.cs 17 KB

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