AnnotationContent.xaml.cs 12 KB

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