AnnotationContent.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. 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 async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
  104. {
  105. SetAllExpander(true);
  106. await Task.Delay(1);
  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 = true;
  114. }
  115. }
  116. }
  117. private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
  118. {
  119. for (int i = 0; i < AnnotationList.Items.Count; i++)
  120. {
  121. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  122. ToggleButton button = GetExpandButton(item);
  123. if (button != null && button.Visibility == Visibility.Visible)
  124. {
  125. button.IsChecked = false;
  126. }
  127. }
  128. SetAllExpander(false);
  129. }
  130. private void SetAllExpander(bool isexpand)
  131. {
  132. AnnotationList.Dispatcher.Invoke(() =>
  133. {
  134. //开启虚拟化之后 全部展开和折叠会有问题
  135. var scroller = GetScrollHost(AnnotationList);
  136. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  137. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  138. for (int i = 0; i < count; i++)
  139. {
  140. var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  141. var g = CommonHelper.FindVisualChild<Expander>(item);
  142. if (g != null)
  143. g.IsExpanded = isexpand;
  144. }
  145. });
  146. }
  147. private ScrollViewer GetScrollHost(ListBox listBox)
  148. {
  149. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  150. {
  151. int s = VisualTreeHelper.GetChildrenCount(listBox);
  152. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  153. if (border != null)
  154. {
  155. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  156. }
  157. }
  158. return null;
  159. }
  160. private ToggleButton GetExpandButton(ListBoxItem item)
  161. {
  162. if (item == null) return null;
  163. Border border = VisualTreeHelper.GetChild(item, 0) as Border;
  164. var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
  165. return btn;
  166. }
  167. private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
  168. {
  169. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  170. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  171. v.GroupDescriptions.Clear();
  172. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  173. v.SortDescriptions.Clear();
  174. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  175. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  176. }
  177. private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
  178. {
  179. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  180. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  181. v.GroupDescriptions.Clear();
  182. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  183. v.SortDescriptions.Clear();
  184. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  185. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  186. }
  187. private void MenuPageSort_Click(object sender, RoutedEventArgs e)
  188. {
  189. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  190. v.GroupDescriptions.Clear();
  191. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  192. v.SortDescriptions.Clear();
  193. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  194. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  195. }
  196. private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
  197. {
  198. if (AnnotationList.SelectedItems.Count == 1)
  199. {
  200. if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
  201. {
  202. //文本、高亮、下划线、删除线、便签
  203. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  204. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  205. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  206. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  207. annotation.EventType == AnnotArgsType.AnnotSticky)
  208. {
  209. StringBuilder Copystr = new StringBuilder();
  210. if (annotation.MarkupContent != null)
  211. {
  212. Copystr.Append(annotation.MarkupContent);
  213. Copystr.Append(Environment.NewLine);
  214. }
  215. if (annotation.Content != null)
  216. {
  217. Copystr.Append(annotation.Content);
  218. }
  219. System.Windows.Clipboard.SetText(Copystr.ToString());
  220. //CustomControl.MessageBoxEx.Show("数据复制成功");
  221. }
  222. }
  223. }
  224. }
  225. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  226. {
  227. List<int> pagelist = new List<int>();
  228. for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
  229. {
  230. AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
  231. pagelist.Add(AnnotationList.Items.IndexOf(annotation));
  232. }
  233. pagelist.Sort();
  234. for (int i = 0; i < pagelist.Count; i++)
  235. {
  236. AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  237. if (annotation == null)
  238. {
  239. continue;
  240. }
  241. viewModel.DeleteCommand.Execute(annotation);
  242. }
  243. }
  244. private void BtnMore_Initialized(object sender, EventArgs e)
  245. {
  246. BtnMore.ContextMenu = null;
  247. }
  248. private void BtnMore_Click(object sender, RoutedEventArgs e)
  249. {
  250. MenuMore.PlacementTarget = BtnMore;
  251. MenuMore.IsOpen = true;
  252. }
  253. private void AnnotationList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  254. {
  255. if (AnnotationList.SelectedItems.Count > 1)
  256. {
  257. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItems);
  258. }
  259. else if (AnnotationList.SelectedItems.Count == 1)
  260. {
  261. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItem);
  262. }
  263. }
  264. private void AnnotationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  265. {
  266. var a1 = (sender as ListBox).SelectedItems.Count;
  267. var a2 = (e.OriginalSource as ListBox).SelectedItems.Count;
  268. var a3 = (e.Source as ListBox).SelectedItems.Count;
  269. if (a1 == 1)
  270. {
  271. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItem);
  272. }
  273. else if (a1 > 1)
  274. {
  275. #region 阅读视图 多选
  276. //Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
  277. //List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
  278. //List<int> ints = new List<int>();
  279. //foreach (var item in (sender as ListBox).SelectedItems)
  280. //{
  281. // if (item is AnnotationHandlerEventArgs annotation)
  282. // {
  283. // if (!selectedItemDics.ContainsKey(annotation.PageIndex))
  284. // {
  285. // ints.Clear();
  286. // ints.Add(annotation.AnnotIndex);
  287. // selectedItemDics.Add(annotation.PageIndex, ints);
  288. // }
  289. // else
  290. // {
  291. // ints.Add(annotation.AnnotIndex);
  292. // selectedItemDics[annotation.PageIndex] = ints;
  293. // }
  294. // }
  295. //}
  296. #endregion 阅读视图 多选
  297. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItems[0]);
  298. }
  299. }
  300. private void AnnotationList_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  301. {
  302. if (AnnotationList.SelectedItems == null)
  303. {
  304. return;
  305. }
  306. if (e.Key == Key.Escape)
  307. {
  308. AnnotationList.SelectedItems.Clear();
  309. }
  310. }
  311. }
  312. }