CPDFViewerTool.MultiSelectedRect.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using ComPDFKit.Tool.DrawTool;
  2. using ComPDFKit.Tool.Help;
  3. using ComPDFKit.Viewer.Layer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Windows;
  7. using System.Windows.Input;
  8. namespace ComPDFKit.Tool
  9. {
  10. public class MultiSelectedData
  11. {
  12. public int PageIndex { get; set; }
  13. public List<int> MultiObjectIndex { get; set; }
  14. /// <summary>
  15. /// Current multi-select type
  16. /// </summary>
  17. public SelectedType ObjectType { get; set; }
  18. /// <summary>
  19. /// Move offset value of the whole
  20. /// </summary>
  21. public Point MoveOffset { get; set; }
  22. public float ZoomX { get; set; }
  23. public float ZoomY { get; set; }
  24. /// <summary>
  25. /// Multiple selection of movement distance
  26. /// </summary>
  27. public float ChangeX { get; set; }
  28. /// <summary>
  29. /// Multiple selection of movement distance
  30. /// </summary>
  31. public float ChangeY { get; set; }
  32. }
  33. public partial class CPDFViewerTool
  34. {
  35. public event EventHandler<MultiSelectedData> MultiDataChanging;
  36. public event EventHandler<MultiSelectedData> MultiDataChanged;
  37. public int MultiSelectedRectViewTag { get; set; } = -1;
  38. private List<int> editAreaMultiIndex = new List<int>();
  39. private int multiPage = -1;
  40. private bool isOpen = false;
  41. private Key multiKey = Key.LeftCtrl;
  42. private void InsertMultiSelectedRectView()
  43. {
  44. int selectedRectViewIndex = PDFViewer.GetMaxViewIndex();
  45. CustomizeLayer customizeLayer = new CustomizeLayer();
  46. MultiSelectedRect multiSelectedRect = new MultiSelectedRect(GetDefaultDrawParam(), SelectedType.None);
  47. multiSelectedRect.SetDrawMoveType(DrawMoveType.kReferenceLine);
  48. customizeLayer.Children.Add(multiSelectedRect);
  49. multiSelectedRect.DataChanged += MultiSelectedRect_DataChanged;
  50. multiSelectedRect.DataChanging += MultiSelectedRect_DataChanging;
  51. PDFViewer.InsertView(selectedRectViewIndex, customizeLayer);
  52. MultiSelectedRectViewTag = customizeLayer.GetResTag();
  53. //multiSelectedRect.Children.Add(multiSelectedRect);
  54. }
  55. private void MultiSelectedRect_DataChanging(object sender, Point e)
  56. {
  57. MultiSelectedData multiSelectedAnnotData = new MultiSelectedData();
  58. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  59. if (isOpen && multiSelectedRect != null)
  60. {
  61. multiSelectedAnnotData.ZoomX = multiSelectedRect.GetZoomX();
  62. multiSelectedAnnotData.ZoomY = multiSelectedRect.GetZoomY();
  63. multiSelectedAnnotData.ChangeX = multiSelectedRect.GetChangeX();
  64. multiSelectedAnnotData.ChangeY = multiSelectedRect.GetChangeY();
  65. multiSelectedAnnotData.MoveOffset = e;
  66. multiSelectedAnnotData.ObjectType = multiSelectedRect.GetSelectedType();
  67. multiSelectedAnnotData.MultiObjectIndex = new List<int>();
  68. multiSelectedAnnotData.MultiObjectIndex.AddRange(editAreaMultiIndex);
  69. multiSelectedAnnotData.PageIndex = multiPage;
  70. MultiDataChanging?.Invoke(this, multiSelectedAnnotData);
  71. }
  72. }
  73. private void MultiSelectedRect_DataChanged(object sender, Point e)
  74. {
  75. MultiSelectedData multiSelectedAnnotData = new MultiSelectedData();
  76. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  77. if (isOpen && multiSelectedRect != null)
  78. {
  79. multiSelectedAnnotData.ZoomX = multiSelectedRect.GetZoomX();
  80. multiSelectedAnnotData.ZoomY = multiSelectedRect.GetZoomY();
  81. multiSelectedAnnotData.ChangeX = multiSelectedRect.GetChangeX();
  82. multiSelectedAnnotData.ChangeY = multiSelectedRect.GetChangeY();
  83. multiSelectedAnnotData.MoveOffset = e;
  84. multiSelectedAnnotData.ObjectType = multiSelectedRect.GetSelectedType();
  85. multiSelectedAnnotData.MultiObjectIndex = new List<int>();
  86. multiSelectedAnnotData.MultiObjectIndex.AddRange(editAreaMultiIndex);
  87. multiSelectedAnnotData.PageIndex = multiPage;
  88. MultiDataChanged?.Invoke(this, multiSelectedAnnotData);
  89. }
  90. }
  91. /// <summary>
  92. /// Set multiple selection shortcut keys
  93. /// </summary>
  94. /// <param name="multikey"></param>
  95. public void SetMultiSelectKey(Key multikey)
  96. {
  97. multiKey = multikey;
  98. }
  99. private void OpenSelectedMulti(bool open)
  100. {
  101. isOpen = open;
  102. }
  103. public bool HitTestMultiSelectedRect()
  104. {
  105. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  106. if (isOpen && multiSelectedRect != null)
  107. {
  108. if (multiSelectedRect.GetHitControlIndex(Mouse.GetPosition(this)) != PointControlType.None)
  109. {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. public void SelectedMultiRect(Rect selectedRects, Rect MaxRect, SelectedType type)
  116. {
  117. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  118. bool open = isOpen;
  119. if (!Keyboard.IsKeyDown(multiKey))
  120. {
  121. open = false;
  122. }
  123. if (open && multiSelectedRect != null)
  124. {
  125. lastSelectedRect.ClearDraw();
  126. lastSelectedRect.HideDraw();
  127. GetSelectedEditAreaForIndex(out int pageIndex, out int editAreaIndex);
  128. if (multiPage != pageIndex && editAreaMultiIndex.Count > 0)
  129. {
  130. foreach (int item in editAreaMultiIndex)
  131. {
  132. SelectedRect OldRect = GetEditAreaForIndex(multiPage, item);
  133. if (OldRect != null)
  134. {
  135. OldRect.Draw();
  136. }
  137. }
  138. editAreaMultiIndex.Clear();
  139. multiSelectedRect.ClearDraw();
  140. multiSelectedRect.CleanMulitSelectedRect();
  141. multiPage = pageIndex;
  142. }
  143. multiPage = pageIndex;
  144. if (startSelectedRect != null && startSelectedPageIndex == multiPage && !editAreaMultiIndex.Contains(startSelectedIndex))
  145. {
  146. //Add the first selected object
  147. editAreaMultiIndex.Add(startSelectedIndex);
  148. SelectedRect startselectedrect = new SelectedRect(GetDefaultDrawParam(), type);
  149. startselectedrect.SetEditPen(editPen, editHoverPen);
  150. startselectedrect.SetDrawMoveType(DrawMoveType.kReferenceLine);
  151. startselectedrect.SetRect(startSelectedRect.GetRect(), currentZoom);
  152. startselectedrect.SetMaxRect(MaxRect);
  153. editAreaList.Add(startselectedrect, startSelectedEditAreaObject);
  154. multiSelectedRect.Children.Add(startselectedrect);
  155. multiSelectedRect.SetMulitSelectedRect(startselectedrect, startSelectedEditAreaObject.PageIndex, startSelectedEditAreaObject.EditAreaIndex);
  156. multiSelectedRect.SetRect(startselectedrect.GetRect());
  157. multiSelectedRect.SetMaxRect(startselectedrect.GetMaxRect());
  158. multiSelectedRect.Draw();
  159. }
  160. startSelectedRect = null;
  161. startSelectedIndex = -1;
  162. startSelectedPageIndex = -1;
  163. startSelectedEditAreaObject = null;
  164. editAreaMultiIndex.Add(editAreaIndex);
  165. multiSelectedRect.SetSelectedType(type);
  166. SelectedRect selectedRect = new SelectedRect(GetDefaultDrawParam(), type);
  167. selectedRect.SetEditPen(editPen, editHoverPen);
  168. selectedRect.SetDrawMoveType(DrawMoveType.kReferenceLine);
  169. selectedRect.SetRect(selectedRects, currentZoom);
  170. selectedRect.SetMaxRect(MaxRect);
  171. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  172. editAreaList.Add(selectedRect, editAreaObject);
  173. multiSelectedRect.Children.Add(selectedRect);
  174. multiSelectedRect.SetMulitSelectedRect(selectedRect, editAreaObject.PageIndex, editAreaObject.EditAreaIndex);
  175. multiSelectedRect.SetRect(selectedRects);
  176. multiSelectedRect.SetMaxRect(MaxRect);
  177. multiSelectedRect.Draw();
  178. }
  179. else
  180. {
  181. //Remember the first selected object
  182. isOpen = false;
  183. GetSelectedEditAreaForIndex(out int pageIndex, out int editAreaIndex);
  184. SelectedRect selectedRect = new SelectedRect(GetDefaultDrawParam(), type);
  185. selectedRect.SetEditPen(editPen, editHoverPen);
  186. selectedRect.SetDrawMoveType(DrawMoveType.kReferenceLine);
  187. selectedRect.SetRect(selectedRects, currentZoom);
  188. selectedRect.SetMaxRect(MaxRect);
  189. EditAreaObject editAreaObject = GetEditAreaObjectForRect(lastSelectedRect);
  190. if (startSelectedIndex != editAreaIndex || startSelectedPageIndex != pageIndex)
  191. {
  192. startSelectedIndex = editAreaIndex;
  193. startSelectedPageIndex = pageIndex;
  194. startSelectedEditAreaObject = editAreaObject;
  195. startSelectedRect = selectedRect;
  196. editAreaList.Add(startSelectedRect, editAreaObject);
  197. }
  198. }
  199. }
  200. public void HideDrawSelectedMultiRect()
  201. {
  202. foreach (int item in editAreaMultiIndex)
  203. {
  204. SelectedRect OldRect = GetEditAreaForIndex(multiPage, item);
  205. if (OldRect != null)
  206. {
  207. OldRect.HideDraw();
  208. }
  209. }
  210. }
  211. public void CleanSelectedMultiRect()
  212. {
  213. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  214. if (multiSelectedRect != null)
  215. {
  216. multiSelectedRect.Children.Clear();
  217. multiSelectedRect.CleanMulitSelectedRect();
  218. editAreaMultiIndex.Clear();
  219. //Delete Multiple Selection Record List
  220. editAreaList.Clear();
  221. }
  222. }
  223. public void DrawStartSelectedMultiRect()
  224. {
  225. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  226. if (multiSelectedRect != null)
  227. {
  228. Point point = Mouse.GetPosition(this);
  229. multiSelectedRect.Draw();
  230. multiSelectedRect.OnMouseLeftButtonDown(point);
  231. }
  232. }
  233. public void DrawMoveSelectedMultiRect()
  234. {
  235. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  236. if (multiSelectedRect != null&& multiSelectedRect.Children.Count>0)
  237. {
  238. Point point = Mouse.GetPosition(this);
  239. multiSelectedRect.OnMouseMove(point, out bool Tag, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
  240. }
  241. }
  242. public void DrawEndSelectedMultiRect()
  243. {
  244. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  245. if (multiSelectedRect != null)
  246. {
  247. Point point = Mouse.GetPosition(this);
  248. multiSelectedRect.OnMouseLeftButtonUp(point);
  249. }
  250. }
  251. public void ReDrawSelectedMultiRect()
  252. {
  253. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  254. DrawEndFrameSelect();
  255. if (multiSelectedRect != null)
  256. {
  257. multiSelectedRect.ClearDraw();
  258. multiSelectedRect.CleanMulitSelectedRect();
  259. Point point = Mouse.GetPosition(this);
  260. switch (multiSelectedRect.GetSelectedType())
  261. {
  262. case SelectedType.Annot:
  263. SelectAnnot();
  264. break;
  265. case SelectedType.PDFEdit:
  266. SelectPDFEdit(multiSelectedRect);
  267. break;
  268. default:
  269. break;
  270. }
  271. multiSelectedRect.Draw();
  272. }
  273. }
  274. private void SelectAnnot()
  275. {
  276. CleanSelectedRect();
  277. }
  278. private void SelectPDFEdit(MultiSelectedRect multiSelectedRect)
  279. {
  280. //Delete Multiple Selection Box
  281. foreach (int item in editAreaMultiIndex)
  282. {
  283. SelectedRect OldRect = GetEditAreaForIndex(multiPage, item);
  284. if (OldRect != null)
  285. {
  286. multiSelectedRect.SetSelectedType(SelectedType.PDFEdit);
  287. //Optimize logic to prevent overlapping of multiple selected objects
  288. //SelectedRect selectedRect = new SelectedRect(GetDefaultDrawParam(), SelectedType.PDFEdit);
  289. //selectedRect.SetDrawMoveType(DrawMoveType.kReferenceLine);
  290. //selectedRect.SetRect(OldRect.GetRect(), currentZoom);
  291. //selectedRect.SetMaxRect(OldRect.GetMaxRect());
  292. //multiSelectedRect.Children.Add(selectedRect);
  293. multiSelectedRect.SetMulitSelectedRect(OldRect, multiPage,item);
  294. multiSelectedRect.SetRect(OldRect.GetRect());
  295. multiSelectedRect.SetMaxRect(OldRect.GetMaxRect());
  296. OldRect.HideDraw();
  297. }
  298. }
  299. }
  300. }
  301. }