CPDFAnnotationListUI.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. using System.Collections.Specialized;
  14. using ComPDFKitViewer;
  15. using ComPDFKit.Controls.PDFControl;
  16. using static ComPDFKit.Controls.PDFControlUI.CPDFAnnotationListUI;
  17. namespace ComPDFKit.Controls.PDFControlUI
  18. {
  19. public class ShowReplyListCommand : ICommand
  20. {
  21. public event EventHandler CanExecuteChanged;
  22. public bool CanExecute(object parameter)
  23. {
  24. return true;
  25. }
  26. public void Execute(object parameter)
  27. {
  28. if (parameter is AnnotationBindData data)
  29. {
  30. data.IsReplyListVisible = !data.IsReplyListVisible;
  31. }
  32. }
  33. }
  34. public class ShowReplyInputCommand : ICommand
  35. {
  36. public event EventHandler CanExecuteChanged;
  37. public bool CanExecute(object parameter)
  38. {
  39. return true;
  40. }
  41. public void Execute(object parameter)
  42. {
  43. if (parameter is AnnotationBindData data)
  44. {
  45. data.IsReplyInputVisible = !data.IsReplyInputVisible;
  46. }
  47. }
  48. }
  49. internal class AnnotationBindData : INotifyPropertyChanged
  50. {
  51. public PDFViewControl pdfViewer { get; set; }
  52. public AnnotParam annotationData { get; set; }
  53. public string ReplyCount
  54. {
  55. get => ReplyList.Count.ToString();
  56. }
  57. private bool _isReplyListVisible;
  58. public bool IsReplyListVisible
  59. {
  60. get { return _isReplyListVisible; }
  61. set
  62. {
  63. _isReplyListVisible = value;
  64. OnPropertyChanged(nameof(IsReplyListVisible));
  65. }
  66. }
  67. private bool _isReplyInputVisible;
  68. public bool IsReplyInputVisible
  69. {
  70. get { return _isReplyInputVisible; }
  71. set
  72. {
  73. _isReplyInputVisible = value;
  74. OnPropertyChanged(nameof(IsReplyInputVisible));
  75. }
  76. }
  77. public int PageIndex { get; set; }
  78. public int AnnotIndex { get => annotationData.AnnotIndex; }
  79. public string Author
  80. {
  81. get => annotationData.Author;
  82. }
  83. public string Note
  84. {
  85. get => annotationData.Content;
  86. }
  87. private CPDFAnnotation _annotation;
  88. public CPDFAnnotation Annotation
  89. {
  90. get
  91. {
  92. CPDFAnnotation annotCore = pdfViewer?.GetCPDFViewer()?.GetAnnotForIndex(annotationData.PageIndex, annotationData.AnnotIndex)?.GetAnnotData()?.Annot;
  93. return annotCore;
  94. }
  95. }
  96. private ObservableCollection<ReplyData> _replyList = new ObservableCollection<ReplyData>();
  97. public ObservableCollection<ReplyData> ReplyList
  98. {
  99. get => _replyList;
  100. set
  101. {
  102. if (_replyList != value)
  103. {
  104. if (_replyList != null)
  105. {
  106. // Unsubscribe from the previous collection
  107. _replyList.CollectionChanged -= ReplyList_CollectionChanged;
  108. }
  109. _replyList = value;
  110. if (_replyList != null)
  111. {
  112. // Subscribe to the new collection
  113. _replyList.CollectionChanged += ReplyList_CollectionChanged;
  114. }
  115. OnPropertyChanged(nameof(ReplyList));
  116. }
  117. }
  118. }
  119. private ObservableCollection<AnnotationBindData> annotationList = new ObservableCollection<AnnotationBindData>();
  120. private void ReplyList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  121. {
  122. // Notify that the ReplyCount has changed when the collection changes
  123. OnPropertyChanged(nameof(ReplyCount));
  124. }
  125. public BindAnnotationResult BindProperty { get; set; }
  126. public AnnotationBindData()
  127. {
  128. BindProperty = new BindAnnotationResult();
  129. }
  130. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  131. public event PropertyChangedEventHandler PropertyChanged;
  132. protected void OnPropertyChanged(string propertyName)
  133. {
  134. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  135. }
  136. }
  137. public partial class CPDFAnnotationListUI : UserControl
  138. {
  139. public event EventHandler<CPDFAnnotationState> ReplyStatusChanged;
  140. public class ReplyData
  141. {
  142. public CPDFAnnotation ReplyAnnotation { get; set; }
  143. public string Author
  144. {
  145. get => ReplyAnnotation.GetAuthor();
  146. set => ReplyAnnotation.SetAuthor(value);
  147. }
  148. public string Date
  149. {
  150. get
  151. {
  152. try
  153. {
  154. if (Regex.IsMatch(ReplyAnnotation.GetCreationDate(), "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  155. {
  156. string dateStr = Regex.Match(ReplyAnnotation.GetCreationDate(), "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  157. return (dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + ", " + dateStr.Substring(8, 2) + ":" +
  158. dateStr.Substring(10, 2));
  159. }
  160. else
  161. {
  162. return string.Empty;
  163. }
  164. }
  165. catch (Exception)
  166. {
  167. return string.Empty;
  168. }
  169. }
  170. set => ReplyAnnotation.SetCreationDate(value);
  171. }
  172. public string Content
  173. {
  174. get => ReplyAnnotation.GetContent();
  175. set => ReplyAnnotation.SetContent(value);
  176. }
  177. }
  178. public class BindAnnotationResult : INotifyPropertyChanged
  179. {
  180. private ObservableCollection<ReplyData> _replyList = new ObservableCollection<ReplyData>();
  181. public ObservableCollection<ReplyData> ReplyList
  182. {
  183. get => _replyList;
  184. set
  185. {
  186. if (_replyList != value)
  187. {
  188. if (_replyList != null)
  189. {
  190. // Unsubscribe from the previous collection
  191. _replyList.CollectionChanged -= ReplyList_CollectionChanged;
  192. }
  193. _replyList = value;
  194. if (_replyList != null)
  195. {
  196. // Subscribe to the new collection
  197. _replyList.CollectionChanged += ReplyList_CollectionChanged; ;
  198. }
  199. OnPropertyChanged(nameof(ReplyList));
  200. }
  201. }
  202. }
  203. private void ReplyList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  204. {
  205. OnPropertyChanged(nameof(ReplyCount));
  206. }
  207. public int PageIndex { get; set; }
  208. public int AnnotIndex { get => annotationData.AnnotIndex; }
  209. public string Author
  210. {
  211. get => annotationData.Author;
  212. }
  213. public string ReplyCount
  214. {
  215. get => ReplyList.Count.ToString();
  216. }
  217. public string CreateDate
  218. {
  219. get
  220. {
  221. if (Regex.IsMatch(annotationData.CreateTime, "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  222. {
  223. string dateStr = Regex.Match(annotationData.CreateTime, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  224. return (dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + ", " + dateStr.Substring(8, 2) + ":" +
  225. dateStr.Substring(10, 2));
  226. }
  227. else
  228. {
  229. return String.Empty;
  230. }
  231. }
  232. }
  233. public string Note
  234. {
  235. get => annotationData.Content;
  236. }
  237. public C_ANNOTATION_TYPE CurrentAnnotationType
  238. {
  239. get => annotationData.CurrentType;
  240. }
  241. public AnnotParam annotationData { get; set; }
  242. private CPDFAnnotation _annotation;
  243. public CPDFAnnotation Annotation
  244. {
  245. get
  246. {
  247. CPDFAnnotation annotCore = pdfViewer?.GetCPDFViewer()?.GetAnnotForIndex(annotationData.PageIndex, annotationData.AnnotIndex)?.GetAnnotData()?.Annot;
  248. return annotCore;
  249. }
  250. }
  251. public PDFViewControl pdfViewer { get; set; }
  252. public CPDFAnnotationState ReplyState { get; set; }
  253. public bool IsMarkState { get; set; }
  254. public BindAnnotationResult()
  255. {
  256. ReplyList.CollectionChanged += ReplyList_CollectionChanged;
  257. }
  258. public event PropertyChangedEventHandler PropertyChanged;
  259. protected void OnPropertyChanged(string propertyName)
  260. {
  261. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  262. }
  263. }
  264. private ObservableCollection<AnnotationBindData> annotationList = new ObservableCollection<AnnotationBindData>();
  265. public event EventHandler<object> AnnotationSelectionChanged;
  266. public event EventHandler<Dictionary<int, List<int>>> DeleteItemHandler;
  267. private ContextMenu popContextMenu;
  268. private bool enableSelectEvent = true;
  269. public CPDFAnnotationListUI()
  270. {
  271. InitializeComponent();
  272. ICollectionView groupView = CollectionViewSource.GetDefaultView(annotationList);
  273. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationBindData.ShowPageIndex)));
  274. AnnotationList.PreviewMouseRightButtonUp += ((sender, args) => { SetContextMenu(); });
  275. }
  276. private void SetContextMenu()
  277. {
  278. if (AnnotationList.SelectedIndex == -1)
  279. {
  280. return;
  281. }
  282. popContextMenu = new ContextMenu();
  283. AnnotationBindData data = annotationList[AnnotationList.SelectedIndex];
  284. MenuItem deleteMenu = new MenuItem();
  285. deleteMenu.Header = LanguageHelper.BotaManager.GetString("Menu_Delete");
  286. deleteMenu.Click -= DeleteMenu_Click;
  287. deleteMenu.Click += DeleteMenu_Click;
  288. popContextMenu.Items.Add(deleteMenu);
  289. MenuItem deleteAllMenu = new MenuItem();
  290. deleteAllMenu.Header = LanguageHelper.BotaManager.GetString("Menu_DeleteAll");
  291. deleteAllMenu.Click -= DeleteAllMenu_Click;
  292. deleteAllMenu.Click += DeleteAllMenu_Click;
  293. popContextMenu.Items.Add(deleteAllMenu);
  294. MenuItem replyMenu = new MenuItem();
  295. replyMenu.Header = LanguageHelper.BotaManager.GetString("Menu_AddReply");
  296. replyMenu.Click += (sender, e) =>
  297. {
  298. if (AnnotationList != null && AnnotationList.SelectedIndex >= 0)
  299. {
  300. data.IsReplyInputVisible = true;
  301. }
  302. };
  303. popContextMenu.Items.Add(replyMenu);
  304. MenuItem showReplyMenu = new MenuItem();
  305. if (data.IsReplyListVisible)
  306. {
  307. showReplyMenu.Header = LanguageHelper.BotaManager.GetString("Menu_FoldReply");
  308. }
  309. else
  310. {
  311. showReplyMenu.Header = LanguageHelper.BotaManager.GetString("Menu_ExpandReply");
  312. }
  313. showReplyMenu.Click += (sender, e) =>
  314. {
  315. if (AnnotationList != null && AnnotationList.SelectedIndex >= 0)
  316. {
  317. data.IsReplyListVisible = !data.IsReplyListVisible;
  318. }
  319. };
  320. popContextMenu.Items.Add(showReplyMenu);
  321. AnnotationList.ContextMenu = popContextMenu;
  322. }
  323. public void DeleteAllAnnot()
  324. {
  325. try
  326. {
  327. Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
  328. foreach (AnnotationBindData bindData in annotationList)
  329. {
  330. if (delDict.ContainsKey(bindData.BindProperty.PageIndex) == false)
  331. {
  332. delDict[bindData.BindProperty.PageIndex] = new List<int>();
  333. }
  334. delDict[bindData.BindProperty.PageIndex].Add(bindData.BindProperty.AnnotIndex);
  335. }
  336. if (delDict.Count > 0)
  337. {
  338. DeleteItemHandler?.Invoke(this, delDict);
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. return;
  344. }
  345. }
  346. public void DeleteAllReply()
  347. {
  348. try
  349. {
  350. foreach (var data in annotationList)
  351. {
  352. foreach (var replyData in data.BindProperty.ReplyList)
  353. {
  354. replyData.ReplyAnnotation.RemoveAnnot();
  355. }
  356. }
  357. }
  358. catch (Exception ex)
  359. {
  360. return;
  361. }
  362. }
  363. public void ExpandAllReply(bool isExpand)
  364. {
  365. foreach (AnnotationBindData data in annotationList)
  366. {
  367. data.IsReplyListVisible = isExpand;
  368. }
  369. }
  370. private void DeleteAllMenu_Click(object sender, RoutedEventArgs e)
  371. {
  372. try
  373. {
  374. Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
  375. foreach (AnnotationBindData bindData in annotationList)
  376. {
  377. if (delDict.ContainsKey(bindData.BindProperty.PageIndex) == false)
  378. {
  379. delDict[bindData.BindProperty.PageIndex] = new List<int>();
  380. }
  381. delDict[bindData.BindProperty.PageIndex].Add(bindData.BindProperty.AnnotIndex);
  382. }
  383. if (delDict.Count > 0)
  384. {
  385. DeleteItemHandler?.Invoke(this, delDict);
  386. }
  387. }
  388. catch (Exception ex)
  389. {
  390. }
  391. }
  392. private void DeleteMenu_Click(object sender, RoutedEventArgs e)
  393. {
  394. try
  395. {
  396. if (AnnotationList != null && AnnotationList.SelectedIndex >= 0)
  397. {
  398. AnnotationBindData bindData = annotationList[AnnotationList.SelectedIndex];
  399. Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
  400. delDict[bindData.BindProperty.PageIndex] = new List<int>()
  401. {
  402. bindData.BindProperty.AnnotIndex
  403. };
  404. DeleteItemHandler?.Invoke(this, delDict);
  405. }
  406. }
  407. catch (Exception ex)
  408. {
  409. }
  410. }
  411. public void SetAnnotationList(List<BindAnnotationResult> results)
  412. {
  413. annotationList.Clear();
  414. AnnotationList.ContextMenu = null;
  415. if (results == null || results.Count == 0)
  416. {
  417. AnnotationList.ItemsSource = null;
  418. NoContentText.Visibility = Visibility.Visible;
  419. return;
  420. }
  421. foreach (BindAnnotationResult item in results)
  422. {
  423. annotationList.Add(new AnnotationBindData()
  424. {
  425. BindProperty = item
  426. });
  427. }
  428. AnnotationList.ItemsSource = annotationList;
  429. if (annotationList.Count > 0)
  430. {
  431. AnnotationList.ContextMenu = popContextMenu;
  432. }
  433. AnnotationList.Visibility = Visibility.Visible;
  434. NoContentText.Visibility = Visibility.Collapsed;
  435. }
  436. private void AnnotationListControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  437. {
  438. if (enableSelectEvent)
  439. {
  440. try
  441. {
  442. if (e.AddedItems[0] is AnnotationBindData annotationBindData)
  443. {
  444. AnnotationSelectionChanged?.Invoke(this, (annotationBindData).BindProperty);
  445. }
  446. }
  447. catch { }
  448. }
  449. }
  450. public void CancelSelected()
  451. {
  452. AnnotationList.SelectedIndex = -1;
  453. }
  454. public void SelectAnnotationChanged(int annotationIndex = -1)
  455. {
  456. AnnotationList.SelectedIndex = annotationIndex;
  457. }
  458. public void SelectAnnotationChanged(int pageIIndex, int annotIndex)
  459. {
  460. if (annotationList != null && annotationList.Count > 0)
  461. {
  462. for (int i = 0; i < annotationList.Count; i++)
  463. {
  464. AnnotationBindData data = annotationList[i];
  465. if (data.BindProperty.PageIndex == pageIIndex && data.BindProperty.AnnotIndex == annotIndex)
  466. {
  467. enableSelectEvent = false;
  468. AnnotationList.SelectedIndex = i;
  469. enableSelectEvent = true;
  470. break;
  471. }
  472. }
  473. }
  474. }
  475. private void AnnotationList_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  476. {
  477. try
  478. {
  479. if (popContextMenu.Items[0] is MenuItem checkMenu)
  480. {
  481. if (checkMenu != null)
  482. {
  483. checkMenu.IsEnabled = true;
  484. }
  485. if (AnnotationList != null && AnnotationList.SelectedIndex == -1)
  486. {
  487. checkMenu.IsEnabled = false;
  488. }
  489. }
  490. }
  491. catch (Exception ex)
  492. {
  493. }
  494. }
  495. private void AnnotationList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  496. {
  497. CancelSelected();
  498. }
  499. private void ToggleButton_Checked(object sender, RoutedEventArgs e)
  500. {
  501. ReplyStatusChanged?.Invoke(sender, CPDFAnnotationState.C_ANNOTATION_MARKED);
  502. }
  503. private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
  504. {
  505. ReplyStatusChanged?.Invoke(sender, CPDFAnnotationState.C_ANNOTATION_UNMARKED);
  506. }
  507. private void StatusControl_ReplyStatusChanged(object sender, CPDFAnnotationState e)
  508. {
  509. ReplyStatusChanged?.Invoke(sender, e);
  510. }
  511. }
  512. }