AnnotationContent.xaml.cs 11 KB

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