AnnotationContent.xaml.cs 16 KB

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