AnnotationContent.xaml.cs 11 KB

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