CPDFViewerTool.MultiSelectedRect.cs 15 KB

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