AnnotationContent.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using ComPDFKitViewer.AnnotEvent;
  2. using PDF_Master.DataConvert;
  3. using PDF_Master.EventAggregators;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model.BOTA;
  6. using PDF_Master.ViewModels.BOTA;
  7. using Prism.Events;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Forms;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. using System.Windows.Shapes;
  25. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  26. using ListBox = System.Windows.Controls.ListBox;
  27. using MenuItem = System.Windows.Controls.MenuItem;
  28. using UserControl = System.Windows.Controls.UserControl;
  29. namespace PDF_Master.Views.BOTA
  30. {
  31. /// <summary>
  32. /// AnnotationContent.xaml 的交互逻辑
  33. /// </summary>
  34. public partial class AnnotationContent : UserControl
  35. {
  36. private AnnotationContentViewModel viewModel;
  37. private string unicode;
  38. public AnnotationContent(IEventAggregator eventAggregator)
  39. {
  40. InitializeComponent();
  41. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  42. viewModel = this.DataContext as AnnotationContentViewModel;
  43. if (AnnotationList.Items.Count < 0)
  44. {
  45. MenuExpandAll.IsEnabled = false;
  46. }
  47. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  48. }
  49. private void CleanSelectAll(CleanSelectAllArgs obj)
  50. {
  51. AnnotationList.SelectedIndex = -1;
  52. }
  53. private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  54. {
  55. if (e.ClickCount == 1)
  56. {
  57. //if (Mouse.LeftButton == e.ButtonState)
  58. //{
  59. // viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  60. //}
  61. if (Mouse.RightButton == e.ButtonState)
  62. {
  63. var pos = e.GetPosition(AnnotationList);
  64. var result = VisualTreeHelper.HitTest(AnnotationList, pos);
  65. if (result != null)
  66. {
  67. ListBoxItem myListBoxItem = sender as System.Windows.Controls.ListBoxItem;
  68. MenuItem copyText = myListBoxItem.ContextMenu.Items[0] as MenuItem;
  69. MenuItem import = myListBoxItem.ContextMenu.Items[1] as MenuItem;
  70. MenuItem export = myListBoxItem.ContextMenu.Items[2] as MenuItem;
  71. if (AnnotationList.SelectedItems.Count > 1)
  72. {
  73. import.IsEnabled = false;
  74. export.IsEnabled = false;
  75. copyText.IsEnabled = false;
  76. }
  77. else
  78. {
  79. //直接右键菜单不需要选中Viewer中注释
  80. //viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(sender);
  81. if (myListBoxItem.DataContext is AnnotationHandlerEventArgs annotation)
  82. {
  83. //文本、高亮、下划线、删除线、便签
  84. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  85. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  86. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  87. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  88. annotation.EventType == AnnotArgsType.AnnotSticky)
  89. {
  90. copyText.IsEnabled = true;
  91. }
  92. else
  93. {
  94. copyText.IsEnabled = false;
  95. }
  96. }
  97. import.IsEnabled = true;
  98. import.Command = viewModel.ImportCommentsCommand;
  99. export.IsEnabled = true;
  100. export.Command = viewModel.ExportCommentsCommand;
  101. }
  102. }
  103. //拦截响应选中事件
  104. e.Handled = true;
  105. }
  106. }
  107. }
  108. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  109. {
  110. if (Mouse.LeftButton == e.ButtonState)
  111. {
  112. viewModel.AddNotesCommand.Execute(sender);
  113. }
  114. }
  115. private async void MenuExpandAll_Click(object sender, RoutedEventArgs e)
  116. {
  117. SetAllExpander(true);
  118. await Task.Delay(1);
  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 = true;
  126. }
  127. }
  128. }
  129. private void MenuCollapseAll_Click(object sender, RoutedEventArgs e)
  130. {
  131. for (int i = 0; i < AnnotationList.Items.Count; i++)
  132. {
  133. ListBoxItem item = AnnotationList.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  134. ToggleButton button = GetExpandButton(item);
  135. if (button != null && button.Visibility == Visibility.Visible)
  136. {
  137. button.IsChecked = false;
  138. }
  139. }
  140. SetAllExpander(false);
  141. }
  142. private void SetAllExpander(bool isexpand)
  143. {
  144. AnnotationList.Dispatcher.Invoke(() =>
  145. {
  146. //开启虚拟化之后 全部展开和折叠会有问题
  147. var scroller = GetScrollHost(AnnotationList);
  148. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  149. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  150. for (int i = 0; i < count; i++)
  151. {
  152. var item = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  153. var g = CommonHelper.FindVisualChild<Expander>(item);
  154. if (g != null)
  155. g.IsExpanded = isexpand;
  156. }
  157. });
  158. }
  159. private ScrollViewer GetScrollHost(ListBox listBox)
  160. {
  161. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  162. {
  163. int s = VisualTreeHelper.GetChildrenCount(listBox);
  164. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  165. if (border != null)
  166. {
  167. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  168. }
  169. }
  170. return null;
  171. }
  172. private ToggleButton GetExpandButton(ListBoxItem item)
  173. {
  174. if (item == null) return null;
  175. Border border = VisualTreeHelper.GetChild(item, 0) as Border;
  176. var btn = CommonHelper.FindVisualChild<ToggleButton>(border);
  177. return btn;
  178. }
  179. private void MenuTimeRightSort_Click(object sender, RoutedEventArgs e)
  180. {
  181. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  182. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  183. v.GroupDescriptions.Clear();
  184. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  185. v.SortDescriptions.Clear();
  186. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  187. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  188. }
  189. private void MenuTimeBackSort_Click(object sender, RoutedEventArgs e)
  190. {
  191. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  192. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  193. v.GroupDescriptions.Clear();
  194. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  195. v.SortDescriptions.Clear();
  196. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  197. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  198. }
  199. private void MenuPageSort_Click(object sender, RoutedEventArgs e)
  200. {
  201. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationList.ItemsSource);
  202. v.GroupDescriptions.Clear();
  203. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  204. v.SortDescriptions.Clear();
  205. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  206. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  207. }
  208. private void MenuItemCopyText_Click(object sender, RoutedEventArgs e)
  209. {
  210. if (AnnotationList.SelectedItems.Count == 1)
  211. {
  212. if (AnnotationList.SelectedItem is AnnotationHandlerEventArgs annotation)
  213. {
  214. //文本、高亮、下划线、删除线、便签
  215. if (annotation.EventType == AnnotArgsType.AnnotFreeText ||
  216. annotation.EventType == AnnotArgsType.AnnotHighlight ||
  217. annotation.EventType == AnnotArgsType.AnnotUnderline ||
  218. annotation.EventType == AnnotArgsType.AnnotStrikeout ||
  219. annotation.EventType == AnnotArgsType.AnnotSticky)
  220. {
  221. StringBuilder Copystr = new StringBuilder();
  222. if (annotation.MarkupContent != null)
  223. {
  224. Copystr.Append(annotation.MarkupContent);
  225. Copystr.Append(Environment.NewLine);
  226. }
  227. if (annotation.Content != null)
  228. {
  229. Copystr.Append(annotation.Content);
  230. }
  231. System.Windows.Clipboard.SetText(Copystr.ToString());
  232. //CustomControl.MessageBoxEx.Show("数据复制成功");
  233. }
  234. }
  235. }
  236. }
  237. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  238. {
  239. List<int> pagelist = new List<int>();
  240. if (AnnotationList.SelectedItems.Count > 1)
  241. {
  242. for (int i = 0; i < AnnotationList.SelectedItems.Count; i++)
  243. {
  244. AnnotationHandlerEventArgs annotation = AnnotationList.SelectedItems[i] as AnnotationHandlerEventArgs;
  245. pagelist.Add(AnnotationList.Items.IndexOf(annotation));
  246. }
  247. pagelist.Sort();
  248. for (int i = 0; i < pagelist.Count; i++)
  249. {
  250. AnnotationHandlerEventArgs annotation = AnnotationList.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  251. if (annotation == null)
  252. {
  253. continue;
  254. }
  255. viewModel.DeleteCommand.Execute(annotation);
  256. }
  257. }
  258. else
  259. {
  260. AnnotationHandlerEventArgs annotation = (AnnotationHandlerEventArgs)(sender as MenuItem).DataContext;
  261. if (annotation == null)
  262. {
  263. return;
  264. }
  265. viewModel.DeleteCommand.Execute(annotation);
  266. }
  267. }
  268. private void BtnMore_Initialized(object sender, EventArgs e)
  269. {
  270. BtnMore.ContextMenu = null;
  271. }
  272. private void BtnMore_Click(object sender, RoutedEventArgs e)
  273. {
  274. MenuMore.PlacementTarget = BtnMore;
  275. MenuMore.IsOpen = true;
  276. }
  277. private void AnnotationList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  278. {
  279. if (AnnotationList.SelectedItems.Count > 1)
  280. {
  281. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItems);
  282. }
  283. else if (AnnotationList.SelectedItems.Count == 1)
  284. {
  285. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute(AnnotationList.SelectedItem);
  286. }
  287. }
  288. private void AnnotationList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  289. {
  290. var a1 = (sender as ListBox).SelectedItems.Count;
  291. var a2 = (e.OriginalSource as ListBox).SelectedItems.Count;
  292. var a3 = (e.Source as ListBox).SelectedItems.Count;
  293. if (a1 == 1)
  294. {
  295. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItems[0]);
  296. }
  297. else if (a1 > 1)
  298. {
  299. #region 阅读视图 多选
  300. //Dictionary<int, List<int>> selectedItemDics = new Dictionary<int, List<int>>();
  301. //List<AnnotationHandlerEventArgs> eventArgs = new List<AnnotationHandlerEventArgs>();
  302. //List<int> ints = new List<int>();
  303. //foreach (var item in (sender as ListBox).SelectedItems)
  304. //{
  305. // if (item is AnnotationHandlerEventArgs annotation)
  306. // {
  307. // if (!selectedItemDics.ContainsKey(annotation.PageIndex))
  308. // {
  309. // ints.Clear();
  310. // ints.Add(annotation.AnnotIndex);
  311. // selectedItemDics.Add(annotation.PageIndex, ints);
  312. // }
  313. // else
  314. // {
  315. // ints.Add(annotation.AnnotIndex);
  316. // selectedItemDics[annotation.PageIndex] = ints;
  317. // }
  318. // }
  319. //}
  320. #endregion 阅读视图 多选
  321. viewModel.ListBoxItemPreviewMouseLeftButtonDown.Execute((sender as ListBox).SelectedItems[0]);
  322. }
  323. }
  324. private void AnnotationList_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  325. {
  326. if (AnnotationList.SelectedItems == null)
  327. {
  328. return;
  329. }
  330. if (e.Key == Key.Escape)
  331. {
  332. AnnotationList.SelectedItems.Clear();
  333. }
  334. }
  335. private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  336. {
  337. var pos = e.GetPosition(this);
  338. var result = VisualTreeHelper.HitTest(this, pos);
  339. if (result != null)
  340. {
  341. //点击按钮和listboxitem时 不清空多选 其余区域取消多选
  342. var item = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  343. if (item != null)
  344. {
  345. return;
  346. }
  347. var btn = CommonHelper.FindVisualParent<System.Windows.Controls.Button>(result.VisualHit);
  348. if (btn != null)
  349. {
  350. return;
  351. }
  352. AnnotationList.SelectedIndex = -1;
  353. }
  354. }
  355. }
  356. }