CPDFAnnoationListUI.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.ComponentModel;
  7. using System.Text.RegularExpressions;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Input;
  12. using ComPDFKit.Controls.Helper;
  13. namespace ComPDFKit.Controls.PDFControlUI
  14. {
  15. public partial class CPDFAnnoationListUI : UserControl
  16. {
  17. public class BindAnnotationResult : INotifyPropertyChanged
  18. {
  19. public int PageIndex { get; set; }
  20. public int AnnotIndex { get => annotationData.AnnotIndex; }
  21. public string CreateDate
  22. {
  23. get
  24. {
  25. if (Regex.IsMatch(annotationData.CreateTime, "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  26. {
  27. string dateStr = Regex.Match(annotationData.CreateTime, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  28. return (dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + ", " + dateStr.Substring(8, 2) + ":" +
  29. dateStr.Substring(10, 2));
  30. }
  31. else
  32. {
  33. return String.Empty;
  34. }
  35. }
  36. }
  37. public string Note
  38. {
  39. get => annotationData.Content;
  40. }
  41. public C_ANNOTATION_TYPE CurrentAnnotationType
  42. {
  43. get => annotationData.CurrentType;
  44. }
  45. public AnnotParam annotationData { get; set; }
  46. public event PropertyChangedEventHandler PropertyChanged;
  47. protected void OnPropertyChanged(string propertyName)
  48. {
  49. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  50. }
  51. }
  52. internal class AnnotationBindData
  53. {
  54. public BindAnnotationResult BindProperty { get; set; }
  55. public AnnotationBindData()
  56. {
  57. BindProperty = new BindAnnotationResult();
  58. }
  59. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  60. }
  61. private ObservableCollection<AnnotationBindData> annotationList = new ObservableCollection<AnnotationBindData>();
  62. public event EventHandler<object> AnnotationSelectionChanged;
  63. public event EventHandler<Dictionary<int, List<int>>> DeleteItemHandler;
  64. private ContextMenu popContextMenu;
  65. private bool enableSelectEvent = true;
  66. public CPDFAnnoationListUI()
  67. {
  68. InitializeComponent();
  69. ICollectionView groupView = CollectionViewSource.GetDefaultView(annotationList);
  70. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationBindData.ShowPageIndex)));
  71. popContextMenu = new ContextMenu();
  72. MenuItem deleteMenu = new MenuItem();
  73. deleteMenu.Header = LanguageHelper.BotaManager.GetString("Menu_Delete");
  74. deleteMenu.Click -= DeleteMenu_Click;
  75. deleteMenu.Click += DeleteMenu_Click;
  76. popContextMenu.Items.Add(deleteMenu);
  77. MenuItem deleteAllMenu = new MenuItem();
  78. deleteAllMenu.Header = LanguageHelper.BotaManager.GetString("Menu_DeleteAll");
  79. deleteAllMenu.Click -= DeleteAllMenu_Click;
  80. deleteAllMenu.Click += DeleteAllMenu_Click;
  81. popContextMenu.Items.Add(deleteAllMenu);
  82. }
  83. private void DeleteAllMenu_Click(object sender, RoutedEventArgs e)
  84. {
  85. try
  86. {
  87. Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
  88. foreach (AnnotationBindData bindData in annotationList)
  89. {
  90. if (delDict.ContainsKey(bindData.BindProperty.PageIndex) == false)
  91. {
  92. delDict[bindData.BindProperty.PageIndex] = new List<int>();
  93. }
  94. delDict[bindData.BindProperty.PageIndex].Add(bindData.BindProperty.AnnotIndex);
  95. }
  96. if (delDict.Count > 0)
  97. {
  98. DeleteItemHandler?.Invoke(this, delDict);
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. }
  104. }
  105. private void DeleteMenu_Click(object sender, RoutedEventArgs e)
  106. {
  107. try
  108. {
  109. if (AnnotationList != null && AnnotationList.SelectedIndex >= 0)
  110. {
  111. AnnotationBindData bindData = annotationList[AnnotationList.SelectedIndex];
  112. Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
  113. delDict[bindData.BindProperty.PageIndex] = new List<int>()
  114. {
  115. bindData.BindProperty.AnnotIndex
  116. };
  117. DeleteItemHandler?.Invoke(this, delDict);
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. }
  123. }
  124. public void SetAnnotationList(List<BindAnnotationResult> results)
  125. {
  126. annotationList.Clear();
  127. AnnotationList.ContextMenu = null;
  128. if (results == null || results.Count == 0)
  129. {
  130. AnnotationList.ItemsSource = null;
  131. NoContentText.Visibility = Visibility.Visible;
  132. return;
  133. }
  134. foreach (BindAnnotationResult item in results)
  135. {
  136. annotationList.Add(new AnnotationBindData()
  137. {
  138. BindProperty = item
  139. });
  140. }
  141. AnnotationList.ItemsSource = annotationList;
  142. if (annotationList.Count > 0)
  143. {
  144. AnnotationList.ContextMenu = popContextMenu;
  145. }
  146. AnnotationList.Visibility = Visibility.Visible;
  147. NoContentText.Visibility = Visibility.Collapsed;
  148. }
  149. private void AnnotationListControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  150. {
  151. if (enableSelectEvent)
  152. {
  153. try
  154. {
  155. AnnotationSelectionChanged?.Invoke(this, (e.AddedItems[0] as AnnotationBindData).BindProperty);
  156. }
  157. catch { }
  158. }
  159. }
  160. public void CancelSelected()
  161. {
  162. AnnotationList.SelectedIndex = -1;
  163. }
  164. public void SelectAnnotationChanged(int annotationIndex = -1)
  165. {
  166. AnnotationList.SelectedIndex = annotationIndex;
  167. }
  168. public void SelectAnnotationChanged(int pageIIndex, int annotIndex)
  169. {
  170. if (annotationList != null && annotationList.Count > 0)
  171. {
  172. for (int i = 0; i < annotationList.Count; i++)
  173. {
  174. AnnotationBindData data = annotationList[i];
  175. if (data.BindProperty.PageIndex == pageIIndex && data.BindProperty.AnnotIndex == annotIndex)
  176. {
  177. enableSelectEvent = false;
  178. AnnotationList.SelectedIndex = i;
  179. enableSelectEvent = true;
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. private void AnnotationList_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  186. {
  187. try
  188. {
  189. MenuItem checkMenu = popContextMenu.Items[0] as MenuItem;
  190. if (checkMenu != null)
  191. {
  192. checkMenu.IsEnabled = true;
  193. }
  194. if (AnnotationList != null && AnnotationList.SelectedIndex == -1)
  195. {
  196. checkMenu.IsEnabled = false;
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. }
  202. }
  203. private void AnnotationList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  204. {
  205. CancelSelected();
  206. }
  207. }
  208. }