AnnotationContent.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  40. private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  41. {
  42. if (e.ClickCount == 1)
  43. {
  44. if (Mouse.LeftButton == e.ButtonState)
  45. {
  46. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  47. }
  48. else if (Mouse.RightButton == e.ButtonState)
  49. {
  50. var pos = e.GetPosition(AnnotationList);
  51. var result = VisualTreeHelper.HitTest(AnnotationList, pos);
  52. if (result != null)
  53. {
  54. ListBoxItem myListBoxItem = sender as System.Windows.Controls.ListBoxItem;
  55. MenuItem copyText = myListBoxItem.ContextMenu.Items[0] as MenuItem;
  56. MenuItem export = myListBoxItem.ContextMenu.Items[1] as MenuItem;
  57. if (AnnotationList.SelectedItems.Count > 1)
  58. {
  59. export.IsEnabled = false;
  60. copyText.IsEnabled = false;
  61. }
  62. else
  63. {
  64. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  65. if (myListBoxItem.DataContext is AnnotationHandlerEventArgs annotation)
  66. {
  67. if (annotation.EventType == AnnotArgsType.AnnotFreeText)
  68. {
  69. copyText.IsEnabled = true;
  70. }
  71. else
  72. {
  73. copyText.IsEnabled = false;
  74. }
  75. }
  76. export.IsEnabled = true;
  77. export.Command = viewModel.ExportCommentsCommand;
  78. }
  79. }
  80. }
  81. }
  82. //else if (e.ClickCount == 2)
  83. //{
  84. // viewModel.DoubleClikCommand.Execute(sender);
  85. //}
  86. }
  87. private void AnnotationList_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  88. {
  89. //AnnotationList.ContextMenu.IsEnabled = false;
  90. }
  91. private async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
  92. {
  93. SetAllExpander(true);
  94. await Task.Delay(1);
  95. for (int i = 0; i < AnnotationList.Items.Count; i++)
  96. {
  97. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  98. ToggleButton button = GetExpandButton(item);
  99. if (button != null && button.Visibility == Visibility.Visible)
  100. {
  101. button.IsChecked = true;
  102. }
  103. }
  104. }
  105. private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
  106. {
  107. for (int i = 0; i < AnnotationList.Items.Count; i++)
  108. {
  109. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  110. ToggleButton button = GetExpandButton(item);
  111. if (button != null && button.Visibility == Visibility.Visible)
  112. {
  113. button.IsChecked = false;
  114. }
  115. }
  116. SetAllExpander(false);
  117. }
  118. private void SetAllExpander(bool isexpand)
  119. {
  120. AnnotationList.Dispatcher.Invoke(() =>
  121. {
  122. //开启虚拟化之后 全部展开和折叠会有问题
  123. var scroller = GetScrollHost(AnnotationList);
  124. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  125. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  126. for (int i = 0; i < count; i++)
  127. {
  128. var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  129. var g = CommonHelper.FindVisualChild<Expander>(item);
  130. if (g != null)
  131. g.IsExpanded = isexpand;
  132. }
  133. });
  134. }
  135. private ScrollViewer GetScrollHost(ListBox listBox)
  136. {
  137. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  138. {
  139. int s = VisualTreeHelper.GetChildrenCount(listBox);
  140. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  141. if (border != null)
  142. {
  143. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  144. }
  145. }
  146. return null;
  147. }
  148. private ToggleButton GetExpandButton(ListBoxItem item)
  149. {
  150. if (item == null) return null;
  151. Border border = VisualTreeHelper.GetChild(item, 0) as Border;
  152. var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
  153. return btn;
  154. }
  155. private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
  156. {
  157. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  158. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  159. v.GroupDescriptions.Clear();
  160. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  161. v.SortDescriptions.Clear();
  162. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  163. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  164. }
  165. private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
  166. {
  167. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  168. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  169. v.GroupDescriptions.Clear();
  170. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  171. v.SortDescriptions.Clear();
  172. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  173. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  174. }
  175. private void MenuPageSort_Click(object sender, RoutedEventArgs e)
  176. {
  177. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  178. v.GroupDescriptions.Clear();
  179. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  180. v.SortDescriptions.Clear();
  181. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  182. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  183. }
  184. private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
  185. {
  186. if (AnnotationList.SelectedItems.Count == 1)
  187. {
  188. if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
  189. {
  190. if (annotation.EventType == AnnotArgsType.AnnotFreeText)
  191. {
  192. StringBuilder Copystr = new StringBuilder();
  193. Copystr.Append(annotation.Content);
  194. System.Windows.Clipboard.SetText(Copystr.ToString());
  195. CustomControl.MessageBoxEx.Show("数据复制成功");
  196. }
  197. }
  198. }
  199. }
  200. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  201. {
  202. List<int> pagelist = new List<int>();
  203. for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
  204. {
  205. AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
  206. pagelist.Add(AnnotationList.Items.IndexOf(annotation));
  207. }
  208. pagelist.Sort();
  209. for (int i = 0; i < pagelist.Count; i++)
  210. {
  211. AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  212. if (annotation == null)
  213. {
  214. continue;
  215. }
  216. viewModel.DeleteCommand.Execute(annotation);
  217. }
  218. }
  219. }
  220. }