AnnotationContent.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using ComPDFKitViewer.AnnotEvent;
  2. using PDF_Office.DataConvert;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Model.BOTA;
  5. using PDF_Office.ViewModels.BOTA;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Forms;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  24. using ListBox = System.Windows.Controls.ListBox;
  25. using MenuItem = System.Windows.Controls.MenuItem;
  26. using UserControl = System.Windows.Controls.UserControl;
  27. namespace PDF_Office.Views.BOTA
  28. {
  29. /// <summary>
  30. /// AnnotationContent.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class AnnotationContent : UserControl
  33. {
  34. private AnnotationContentViewModel viewModel;
  35. public AnnotationContent()
  36. {
  37. InitializeComponent();
  38. viewModel = this.DataContext as AnnotationContentViewModel;
  39. if (AnnotationList.Items.Count < 0)
  40. {
  41. MenuExpandAll.IsEnabled = false;
  42. }
  43. }
  44. private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  45. {
  46. if (e.ClickCount == 1)
  47. {
  48. if (Mouse.LeftButton == e.ButtonState)
  49. {
  50. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  51. }
  52. else if (Mouse.RightButton == e.ButtonState)
  53. {
  54. var pos = e.GetPosition(AnnotationList);
  55. var result = VisualTreeHelper.HitTest(AnnotationList, pos);
  56. if (result != null)
  57. {
  58. ListBoxItem myListBoxItem = sender as System.Windows.Controls.ListBoxItem;
  59. MenuItem copyText = myListBoxItem.ContextMenu.Items[0] as MenuItem;
  60. MenuItem export = myListBoxItem.ContextMenu.Items[1] as MenuItem;
  61. if (AnnotationList.SelectedItems.Count > 1)
  62. {
  63. export.IsEnabled = false;
  64. copyText.IsEnabled = false;
  65. }
  66. else
  67. {
  68. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  69. if (myListBoxItem.DataContext is AnnotationHandlerEventArgs annotation)
  70. {
  71. if (annotation.EventType == AnnotArgsType.AnnotFreeText)
  72. {
  73. copyText.IsEnabled = true;
  74. }
  75. else
  76. {
  77. copyText.IsEnabled = false;
  78. }
  79. }
  80. export.IsEnabled = true;
  81. export.Command = viewModel.ExportCommentsCommand;
  82. }
  83. }
  84. }
  85. }
  86. //else if (e.ClickCount == 2)
  87. //{
  88. // viewModel.DoubleClikCommand.Execute(sender);
  89. //}
  90. }
  91. private void AnnotationList_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  92. {
  93. //AnnotationList.ContextMenu.IsEnabled = false;
  94. }
  95. private async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
  96. {
  97. SetAllExpander(true);
  98. await Task.Delay(1);
  99. for (int i = 0; i < AnnotationList.Items.Count; i++)
  100. {
  101. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  102. ToggleButton button = GetExpandButton(item);
  103. if (button != null && button.Visibility == Visibility.Visible)
  104. {
  105. button.IsChecked = true;
  106. }
  107. }
  108. }
  109. private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
  110. {
  111. for (int i = 0; i < AnnotationList.Items.Count; i++)
  112. {
  113. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  114. ToggleButton button = GetExpandButton(item);
  115. if (button != null && button.Visibility == Visibility.Visible)
  116. {
  117. button.IsChecked = false;
  118. }
  119. }
  120. SetAllExpander(false);
  121. }
  122. private void SetAllExpander(bool isexpand)
  123. {
  124. AnnotationList.Dispatcher.Invoke(() =>
  125. {
  126. //开启虚拟化之后 全部展开和折叠会有问题
  127. var scroller = GetScrollHost(AnnotationList);
  128. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  129. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  130. for (int i = 0; i < count; i++)
  131. {
  132. var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  133. var g = CommonHelper.FindVisualChild<Expander>(item);
  134. if (g != null)
  135. g.IsExpanded = isexpand;
  136. }
  137. });
  138. }
  139. private ScrollViewer GetScrollHost(ListBox listBox)
  140. {
  141. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  142. {
  143. int s = VisualTreeHelper.GetChildrenCount(listBox);
  144. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  145. if (border != null)
  146. {
  147. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  148. }
  149. }
  150. return null;
  151. }
  152. private ToggleButton GetExpandButton(ListBoxItem item)
  153. {
  154. if (item == null) return null;
  155. Border border = VisualTreeHelper.GetChild(item, 0) as Border;
  156. var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
  157. return btn;
  158. }
  159. private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
  160. {
  161. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  162. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  163. v.GroupDescriptions.Clear();
  164. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  165. v.SortDescriptions.Clear();
  166. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  167. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  168. }
  169. private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
  170. {
  171. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  172. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  173. v.GroupDescriptions.Clear();
  174. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  175. v.SortDescriptions.Clear();
  176. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  177. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  178. }
  179. private void MenuPageSort_Click(object sender, RoutedEventArgs e)
  180. {
  181. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  182. v.GroupDescriptions.Clear();
  183. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  184. v.SortDescriptions.Clear();
  185. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  186. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  187. }
  188. private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
  189. {
  190. if (AnnotationList.SelectedItems.Count == 1)
  191. {
  192. if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
  193. {
  194. if (annotation.EventType == AnnotArgsType.AnnotFreeText)
  195. {
  196. StringBuilder Copystr = new StringBuilder();
  197. Copystr.Append(annotation.Content);
  198. System.Windows.Clipboard.SetText(Copystr.ToString());
  199. CustomControl.MessageBoxEx.Show("数据复制成功");
  200. }
  201. }
  202. }
  203. }
  204. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  205. {
  206. List<int> pagelist = new List<int>();
  207. for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
  208. {
  209. AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
  210. pagelist.Add(AnnotationList.Items.IndexOf(annotation));
  211. }
  212. pagelist.Sort();
  213. for (int i = 0; i < pagelist.Count; i++)
  214. {
  215. AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  216. if (annotation == null)
  217. {
  218. continue;
  219. }
  220. viewModel.DeleteCommand.Execute(annotation);
  221. }
  222. }
  223. }
  224. }