CommonHelper.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Runtime.CompilerServices;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. namespace Compdfkit_Tools.Helper
  14. {
  15. public static class CommonHelper
  16. {
  17. /// <summary>
  18. /// Returns the file size based on the specified file path, with the smallest unit being bytes (B).
  19. /// </summary>
  20. /// <param name="filePath">The path to the file.</param>
  21. /// <returns>
  22. /// The calculated file size, with units in bytes (B), kilobytes (KB), megabytes (MB), or gigabytes (GB).
  23. /// </returns>
  24. public static string GetFileSize(string filePath)
  25. {
  26. FileInfo fileInfo = null;
  27. try
  28. {
  29. fileInfo = new FileInfo(filePath);
  30. }
  31. catch
  32. {
  33. return "0B";
  34. }
  35. if (fileInfo != null && fileInfo.Exists)
  36. {
  37. double fileSize = fileInfo.Length;
  38. if (fileSize > 1024)
  39. {
  40. fileSize = Math.Round(fileSize / 1024, 2);
  41. if (fileSize > 1024)
  42. {
  43. fileSize = Math.Round(fileSize / 1024, 2);
  44. if (fileSize > 1024)
  45. {
  46. fileSize = Math.Round(fileSize / 1024, 2);
  47. return fileSize + " GB";
  48. }
  49. else
  50. {
  51. return fileSize + " MB";
  52. }
  53. }
  54. else
  55. {
  56. return fileSize + " KB";
  57. }
  58. }
  59. else
  60. {
  61. return fileSize + " B";
  62. }
  63. }
  64. return "0B";
  65. }
  66. public static string GetFilePathOrEmpty()
  67. {
  68. string selectedFilePath = string.Empty;
  69. OpenFileDialog openFileDialog = new OpenFileDialog();
  70. openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
  71. if (openFileDialog.ShowDialog() == true)
  72. {
  73. selectedFilePath = openFileDialog.FileName;
  74. }
  75. return selectedFilePath;
  76. }
  77. public static bool GetPagesInRange(ref List<int> pageList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
  78. {
  79. string[] rangeSplit = pageRange.Split(enumerationSeparator);
  80. pageList.Clear();
  81. foreach (string range in rangeSplit)
  82. {
  83. int starttag = 1;
  84. if (inittag)
  85. {
  86. starttag = 0;
  87. }
  88. if (range.Contains("-"))
  89. {
  90. try
  91. {
  92. string[] limits = range.Split(rangeSeparator);
  93. if (limits.Length >= 2 && !string.IsNullOrWhiteSpace(limits[0]) && !string.IsNullOrWhiteSpace(limits[1]))
  94. {
  95. int start = int.Parse(limits[0]);
  96. int end = int.Parse(limits[1]);
  97. if ((start < starttag) || (end > count) || (start > end))
  98. {
  99. return false;
  100. }
  101. for (int i = start; i <= end; ++i)
  102. {
  103. if (pageList.Contains(i))
  104. {
  105. return false;
  106. }
  107. pageList.Add(i - 1);
  108. }
  109. continue;
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. return false;
  115. }
  116. }
  117. int pageNr;
  118. try
  119. {
  120. pageNr = int.Parse(range);
  121. }
  122. catch (Exception)
  123. {
  124. return false;
  125. }
  126. if (pageNr < starttag || pageNr > count)
  127. {
  128. return false;
  129. }
  130. if (pageList.Contains(pageNr))
  131. {
  132. return false;
  133. }
  134. pageList.Add(pageNr - 1);
  135. }
  136. return true;
  137. }
  138. internal static class PageEditHelper
  139. {
  140. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  141. {
  142. while (obj != null)
  143. {
  144. if (obj is T)
  145. return obj as T;
  146. obj = VisualTreeHelper.GetParent(obj);
  147. }
  148. return null;
  149. }
  150. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  151. where childItem : DependencyObject
  152. {
  153. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  154. {
  155. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  156. if (child != null && child is childItem)
  157. return (childItem)child;
  158. else
  159. {
  160. childItem childOfChild = FindVisualChild<childItem>(child);
  161. if (childOfChild != null)
  162. return childOfChild;
  163. }
  164. }
  165. return null;
  166. }
  167. }
  168. public static class ViewportHelper
  169. {
  170. public static CPDFDocument CopyDoc;
  171. public static bool IsInViewport(ScrollViewer sv, Control item)
  172. {
  173. if (item == null) return false;
  174. ItemsControl itemsControl = null;
  175. if (item is ListBoxItem)
  176. itemsControl = ItemsControl.ItemsControlFromItemContainer(item) as ListBox;
  177. else
  178. throw new NotSupportedException(item.GetType().Name);
  179. ScrollContentPresenter scrollContentPresenter = (ScrollContentPresenter)sv.Template.FindName("PART_ScrollContentPresenter", sv);
  180. MethodInfo isInViewportMethod = sv.GetType().GetMethod("IsInViewport", BindingFlags.NonPublic | BindingFlags.Instance);
  181. return (bool)isInViewportMethod.Invoke(sv, new object[] { scrollContentPresenter, item });
  182. }
  183. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  184. {
  185. while (obj != null)
  186. {
  187. if (obj is T)
  188. return obj as T;
  189. obj = VisualTreeHelper.GetParent(obj);
  190. }
  191. return null;
  192. }
  193. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  194. where childItem : DependencyObject
  195. {
  196. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  197. {
  198. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  199. if (child != null && child is childItem)
  200. return (childItem)child;
  201. else
  202. {
  203. childItem childOfChild = FindVisualChild<childItem>(child);
  204. if (childOfChild != null)
  205. return childOfChild;
  206. }
  207. }
  208. return null;
  209. }
  210. }
  211. public class ArrowHelper
  212. {
  213. public bool HasStartArrow
  214. {
  215. get
  216. {
  217. if (StartSharp != C_LINE_TYPE.LINETYPE_UNKNOWN && StartSharp != C_LINE_TYPE.LINETYPE_NONE)
  218. {
  219. return true;
  220. }
  221. return false;
  222. }
  223. }
  224. public bool IsStartClosed
  225. {
  226. get
  227. {
  228. if (StartSharp == C_LINE_TYPE.LINETYPE_CLOSEDARROW || StartSharp == C_LINE_TYPE.LINETYPE_RCLOSEDARROW || StartSharp == C_LINE_TYPE.LINETYPE_DIAMOND)
  229. {
  230. return true;
  231. }
  232. return false;
  233. }
  234. }
  235. public bool HasEndArrow
  236. {
  237. get
  238. {
  239. if (EndSharp != C_LINE_TYPE.LINETYPE_UNKNOWN && EndSharp != C_LINE_TYPE.LINETYPE_NONE)
  240. {
  241. return true;
  242. }
  243. return false;
  244. }
  245. }
  246. public bool IsEndClosed
  247. {
  248. get
  249. {
  250. if (EndSharp == C_LINE_TYPE.LINETYPE_CLOSEDARROW || EndSharp == C_LINE_TYPE.LINETYPE_RCLOSEDARROW || EndSharp == C_LINE_TYPE.LINETYPE_DIAMOND)
  251. {
  252. return true;
  253. }
  254. return false;
  255. }
  256. }
  257. public uint ArrowAngle { get; set; }
  258. public uint ArrowLength { get; set; }
  259. public Point? LineStart { get; set; }
  260. public Point? LineEnd { get; set; }
  261. public PathGeometry Body { get; set; }
  262. public C_LINE_TYPE StartSharp { get; set; }
  263. public C_LINE_TYPE EndSharp { get; set; }
  264. public ArrowHelper()
  265. {
  266. Body = new PathGeometry();
  267. ArrowLength = 12;
  268. ArrowAngle = 60;
  269. }
  270. protected PathFigure CreateLineBody()
  271. {
  272. if (LineStart != null && LineEnd != null)
  273. {
  274. PathFigure lineFigure = new PathFigure();
  275. lineFigure.StartPoint = (Point)LineStart;
  276. LineSegment linePath = new LineSegment();
  277. linePath.Point = (Point)LineEnd;
  278. lineFigure.Segments.Add(linePath);
  279. return lineFigure;
  280. }
  281. return null;
  282. }
  283. protected PathFigure CreateStartArrow()
  284. {
  285. switch (StartSharp)
  286. {
  287. case C_LINE_TYPE.LINETYPE_NONE:
  288. case C_LINE_TYPE.LINETYPE_UNKNOWN:
  289. break;
  290. case C_LINE_TYPE.LINETYPE_ARROW:
  291. case C_LINE_TYPE.LINETYPE_CLOSEDARROW:
  292. return CreateStartOpenArrow();
  293. case C_LINE_TYPE.LINETYPE_ROPENARROW:
  294. case C_LINE_TYPE.LINETYPE_RCLOSEDARROW:
  295. return CreateStartReverseArrow();
  296. case C_LINE_TYPE.LINETYPE_BUTT:
  297. return CreateStartButtArrow();
  298. case C_LINE_TYPE.LINETYPE_DIAMOND:
  299. return CreateStartDiamondArrow();
  300. case C_LINE_TYPE.LINETYPE_CIRCLE:
  301. return CreateStartRoundArrow();
  302. case C_LINE_TYPE.LINETYPE_SQUARE:
  303. return CreateStartSquareArrow();
  304. case C_LINE_TYPE.LINETYPE_SLASH:
  305. return CreateStartSlashArrow();
  306. default:
  307. break;
  308. }
  309. return null;
  310. }
  311. protected virtual PathFigure CreateEndArrow()
  312. {
  313. switch (EndSharp)
  314. {
  315. case C_LINE_TYPE.LINETYPE_NONE:
  316. case C_LINE_TYPE.LINETYPE_UNKNOWN:
  317. break;
  318. case C_LINE_TYPE.LINETYPE_ARROW:
  319. case C_LINE_TYPE.LINETYPE_CLOSEDARROW:
  320. return CreateEndOpenArrow();
  321. case C_LINE_TYPE.LINETYPE_ROPENARROW:
  322. case C_LINE_TYPE.LINETYPE_RCLOSEDARROW:
  323. return CreateEndReverseArrow();
  324. case C_LINE_TYPE.LINETYPE_BUTT:
  325. return CreateEndButtArrow();
  326. case C_LINE_TYPE.LINETYPE_DIAMOND:
  327. return CreateEndDiamondArrow();
  328. case C_LINE_TYPE.LINETYPE_CIRCLE:
  329. return CreateEndRoundArrow();
  330. case C_LINE_TYPE.LINETYPE_SQUARE:
  331. return CreateEndSquareArrow();
  332. case C_LINE_TYPE.LINETYPE_SLASH:
  333. return CreateEndSlashArrow();
  334. default:
  335. break;
  336. }
  337. return null;
  338. }
  339. public PathGeometry BuildArrowBody()
  340. {
  341. Body.Figures.Clear();
  342. PathFigure lineBody = CreateLineBody();
  343. if (lineBody != null)
  344. {
  345. Body.Figures.Add(lineBody);
  346. PathFigure arrowFigure = CreateStartArrow();
  347. if (arrowFigure != null)
  348. {
  349. Body.Figures.Add(arrowFigure);
  350. }
  351. arrowFigure = CreateEndArrow();
  352. if (arrowFigure != null)
  353. {
  354. Body.Figures.Add(arrowFigure);
  355. }
  356. }
  357. return Body;
  358. }
  359. private PathFigure CreateStartOpenArrow()
  360. {
  361. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  362. {
  363. return null;
  364. }
  365. PathFigure arrowFigure = new PathFigure();
  366. PolyLineSegment arrowSegment = new PolyLineSegment();
  367. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  368. lineVector.Normalize();
  369. lineVector *= ArrowLength;
  370. Matrix rotateMatrix = new Matrix();
  371. rotateMatrix.Rotate(ArrowAngle / 2);
  372. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  373. arrowSegment.Points.Add((Point)LineStart);
  374. rotateMatrix.Rotate(-ArrowAngle);
  375. arrowSegment.Points.Add((Point)LineStart + (lineVector * rotateMatrix));
  376. arrowFigure.Segments.Add(arrowSegment);
  377. arrowFigure.IsClosed = IsStartClosed;
  378. arrowFigure.IsFilled = IsStartClosed;
  379. return arrowFigure;
  380. }
  381. private PathFigure CreateEndOpenArrow()
  382. {
  383. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  384. {
  385. return null;
  386. }
  387. PathFigure arrowFigure = new PathFigure();
  388. PolyLineSegment arrowSegment = new PolyLineSegment();
  389. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  390. lineVector.Normalize();
  391. lineVector *= ArrowLength;
  392. Matrix rotateMatrix = new Matrix();
  393. rotateMatrix.Rotate(ArrowAngle / 2);
  394. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  395. arrowSegment.Points.Add((Point)LineEnd);
  396. rotateMatrix.Rotate(-ArrowAngle);
  397. arrowSegment.Points.Add((Point)LineEnd + (lineVector * rotateMatrix));
  398. arrowFigure.Segments.Add(arrowSegment);
  399. arrowFigure.IsClosed = IsEndClosed;
  400. arrowFigure.IsFilled = IsEndClosed;
  401. return arrowFigure;
  402. }
  403. private PathFigure CreateStartReverseArrow()
  404. {
  405. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  406. {
  407. return null;
  408. }
  409. PathFigure arrowFigure = new PathFigure();
  410. PolyLineSegment arrowSegment = new PolyLineSegment();
  411. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  412. lineVector.Normalize();
  413. lineVector *= ArrowLength;
  414. Matrix rotateMatrix = new Matrix();
  415. rotateMatrix.Rotate(ArrowAngle / 2);
  416. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  417. arrowSegment.Points.Add((Point)LineStart);
  418. rotateMatrix.Rotate(-ArrowAngle);
  419. arrowSegment.Points.Add((Point)LineStart + (lineVector * rotateMatrix));
  420. arrowFigure.Segments.Add(arrowSegment);
  421. arrowFigure.IsClosed = IsStartClosed;
  422. arrowFigure.IsFilled = IsStartClosed;
  423. return arrowFigure;
  424. }
  425. private PathFigure CreateEndReverseArrow()
  426. {
  427. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  428. {
  429. return null;
  430. }
  431. PathFigure arrowFigure = new PathFigure();
  432. PolyLineSegment arrowSegment = new PolyLineSegment();
  433. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  434. lineVector.Normalize();
  435. lineVector *= ArrowLength;
  436. Matrix rotateMatrix = new Matrix();
  437. rotateMatrix.Rotate(ArrowAngle / 2);
  438. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  439. arrowSegment.Points.Add((Point)LineEnd);
  440. rotateMatrix.Rotate(-ArrowAngle);
  441. arrowSegment.Points.Add((Point)LineEnd + (lineVector * rotateMatrix));
  442. arrowFigure.Segments.Add(arrowSegment);
  443. arrowFigure.IsClosed = IsEndClosed;
  444. arrowFigure.IsFilled = IsEndClosed;
  445. return arrowFigure;
  446. }
  447. private PathFigure CreateStartButtArrow()
  448. {
  449. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  450. {
  451. return null;
  452. }
  453. PathFigure arrowFigure = new PathFigure();
  454. LineSegment buttSegment = new LineSegment();
  455. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  456. lineVector.Normalize();
  457. lineVector *= ArrowLength;
  458. Matrix rotateMatrix = new Matrix();
  459. rotateMatrix.Rotate(90);
  460. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  461. rotateMatrix.Rotate(-180);
  462. buttSegment.Point = ((Point)LineStart + (lineVector * rotateMatrix));
  463. arrowFigure.Segments.Add(buttSegment);
  464. return arrowFigure;
  465. }
  466. private PathFigure CreateEndButtArrow()
  467. {
  468. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  469. {
  470. return null;
  471. }
  472. PathFigure arrowFigure = new PathFigure();
  473. LineSegment buttSegment = new LineSegment();
  474. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  475. lineVector.Normalize();
  476. lineVector *= ArrowLength;
  477. Matrix rotateMatrix = new Matrix();
  478. rotateMatrix.Rotate(90);
  479. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  480. rotateMatrix.Rotate(-180);
  481. buttSegment.Point = ((Point)LineEnd + (lineVector * rotateMatrix));
  482. arrowFigure.Segments.Add(buttSegment);
  483. return arrowFigure;
  484. }
  485. private PathFigure CreateStartDiamondArrow()
  486. {
  487. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  488. {
  489. return null;
  490. }
  491. PathFigure arrowFigure = new PathFigure();
  492. PolyLineSegment arrowSegment = new PolyLineSegment();
  493. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  494. lineVector.Normalize();
  495. lineVector *= ArrowLength;
  496. Matrix rotateMatrix = new Matrix();
  497. rotateMatrix.Rotate(45);
  498. Point cornerTop = (Point)LineStart + (lineVector * rotateMatrix);
  499. Vector turnVector = cornerTop - (Point)LineStart;
  500. turnVector.Normalize();
  501. turnVector *= ArrowLength;
  502. Matrix turnMatrix = new Matrix();
  503. turnMatrix.Rotate(-90);
  504. Point awayPoint = cornerTop + (turnVector * turnMatrix);
  505. rotateMatrix = new Matrix();
  506. rotateMatrix.Rotate(-45);
  507. Point cornerDown = (Point)LineStart + (lineVector * rotateMatrix);
  508. arrowFigure.StartPoint = (Point)LineStart;
  509. arrowSegment.Points.Add(cornerTop);
  510. arrowSegment.Points.Add(awayPoint);
  511. arrowSegment.Points.Add(cornerDown);
  512. arrowSegment.Points.Add((Point)LineStart);
  513. arrowFigure.Segments.Add(arrowSegment);
  514. arrowFigure.IsClosed = IsStartClosed;
  515. arrowFigure.IsFilled = IsStartClosed;
  516. return arrowFigure;
  517. }
  518. private PathFigure CreateEndDiamondArrow()
  519. {
  520. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  521. {
  522. return null;
  523. }
  524. PathFigure arrowFigure = new PathFigure();
  525. PolyLineSegment arrowSegment = new PolyLineSegment();
  526. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  527. lineVector.Normalize();
  528. lineVector *= ArrowLength;
  529. Matrix rotateMatrix = new Matrix();
  530. rotateMatrix.Rotate(45);
  531. Point cornerTop = (Point)LineEnd + (lineVector * rotateMatrix);
  532. Vector turnVector = cornerTop - (Point)LineEnd;
  533. turnVector.Normalize();
  534. turnVector *= ArrowLength;
  535. Matrix turnMatrix = new Matrix();
  536. turnMatrix.Rotate(-90);
  537. Point awayPoint = cornerTop + (turnVector * turnMatrix);
  538. rotateMatrix = new Matrix();
  539. rotateMatrix.Rotate(-45);
  540. Point cornerDown = (Point)LineEnd + (lineVector * rotateMatrix);
  541. arrowFigure.StartPoint = (Point)LineEnd;
  542. arrowSegment.Points.Add(cornerTop);
  543. arrowSegment.Points.Add(awayPoint);
  544. arrowSegment.Points.Add(cornerDown);
  545. arrowSegment.Points.Add((Point)LineEnd);
  546. arrowFigure.Segments.Add(arrowSegment);
  547. arrowFigure.IsClosed = IsEndClosed;
  548. arrowFigure.IsFilled = IsEndClosed;
  549. return arrowFigure;
  550. }
  551. private PathFigure CreateStartRoundArrow()
  552. {
  553. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  554. {
  555. return null;
  556. }
  557. PathFigure arrowFigure = new PathFigure();
  558. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  559. lineVector.Normalize();
  560. lineVector *= ArrowLength;
  561. Matrix rotateMatrix = new Matrix();
  562. rotateMatrix.Rotate(180);
  563. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  564. ArcSegment circleSegment = new ArcSegment();
  565. circleSegment.Point = (Point)LineStart;
  566. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  567. arrowFigure.Segments.Add(circleSegment);
  568. circleSegment = new ArcSegment();
  569. circleSegment.Point = (Point)arrowFigure.StartPoint;
  570. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  571. arrowFigure.Segments.Add(circleSegment);
  572. return arrowFigure;
  573. }
  574. private PathFigure CreateEndRoundArrow()
  575. {
  576. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  577. {
  578. return null;
  579. }
  580. PathFigure arrowFigure = new PathFigure();
  581. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  582. lineVector.Normalize();
  583. lineVector *= ArrowLength;
  584. Matrix rotateMatrix = new Matrix();
  585. rotateMatrix.Rotate(180);
  586. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  587. ArcSegment circleSegment = new ArcSegment();
  588. circleSegment.Point = (Point)LineEnd;
  589. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  590. arrowFigure.Segments.Add(circleSegment);
  591. circleSegment = new ArcSegment();
  592. circleSegment.Point = (Point)arrowFigure.StartPoint;
  593. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  594. arrowFigure.Segments.Add(circleSegment);
  595. return arrowFigure;
  596. }
  597. private PathFigure CreateStartSquareArrow()
  598. {
  599. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  600. {
  601. return null;
  602. }
  603. PathFigure arrowFigure = new PathFigure();
  604. PolyLineSegment squreSegment = new PolyLineSegment();
  605. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  606. lineVector.Normalize();
  607. lineVector *= (ArrowLength / 2);
  608. Matrix rotateMatrix = new Matrix();
  609. rotateMatrix.Rotate(90);
  610. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  611. rotateMatrix.Rotate(-180);
  612. Point pointCorner = (Point)LineStart + (lineVector * rotateMatrix);
  613. squreSegment.Points.Add(pointCorner);
  614. Vector moveVector = arrowFigure.StartPoint - pointCorner;
  615. moveVector.Normalize();
  616. moveVector *= (ArrowLength);
  617. rotateMatrix = new Matrix();
  618. rotateMatrix.Rotate(90);
  619. squreSegment.Points.Add(pointCorner + (moveVector * rotateMatrix));
  620. squreSegment.Points.Add(arrowFigure.StartPoint + (moveVector * rotateMatrix));
  621. squreSegment.Points.Add(arrowFigure.StartPoint);
  622. squreSegment.Points.Add((Point)LineStart);
  623. arrowFigure.Segments.Add(squreSegment);
  624. return arrowFigure;
  625. }
  626. private PathFigure CreateEndSquareArrow()
  627. {
  628. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  629. {
  630. return null;
  631. }
  632. PathFigure arrowFigure = new PathFigure();
  633. PolyLineSegment squreSegment = new PolyLineSegment();
  634. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  635. lineVector.Normalize();
  636. lineVector *= (ArrowLength / 2);
  637. Matrix rotateMatrix = new Matrix();
  638. rotateMatrix.Rotate(90);
  639. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  640. rotateMatrix.Rotate(-180);
  641. Point pointCorner = (Point)LineEnd + (lineVector * rotateMatrix);
  642. squreSegment.Points.Add(pointCorner);
  643. Vector moveVector = arrowFigure.StartPoint - pointCorner;
  644. moveVector.Normalize();
  645. moveVector *= (ArrowLength);
  646. rotateMatrix = new Matrix();
  647. rotateMatrix.Rotate(90);
  648. squreSegment.Points.Add(pointCorner + (moveVector * rotateMatrix));
  649. squreSegment.Points.Add(arrowFigure.StartPoint + (moveVector * rotateMatrix));
  650. squreSegment.Points.Add(arrowFigure.StartPoint);
  651. squreSegment.Points.Add((Point)LineEnd);
  652. arrowFigure.Segments.Add(squreSegment);
  653. return arrowFigure;
  654. }
  655. private PathFigure CreateStartSlashArrow()
  656. {
  657. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  658. {
  659. return null;
  660. }
  661. PathFigure arrowFigure = new PathFigure();
  662. LineSegment buttSegment = new LineSegment();
  663. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  664. lineVector.Normalize();
  665. lineVector *= ArrowLength;
  666. Matrix rotateMatrix = new Matrix();
  667. rotateMatrix.Rotate(45);
  668. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  669. rotateMatrix.Rotate(-180);
  670. buttSegment.Point = ((Point)LineStart + (lineVector * rotateMatrix));
  671. arrowFigure.Segments.Add(buttSegment);
  672. return arrowFigure;
  673. }
  674. private PathFigure CreateEndSlashArrow()
  675. {
  676. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  677. {
  678. return null;
  679. }
  680. PathFigure arrowFigure = new PathFigure();
  681. LineSegment buttSegment = new LineSegment();
  682. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  683. lineVector.Normalize();
  684. lineVector *= ArrowLength;
  685. Matrix rotateMatrix = new Matrix();
  686. rotateMatrix.Rotate(45);
  687. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  688. rotateMatrix.Rotate(-180);
  689. buttSegment.Point = ((Point)LineEnd + (lineVector * rotateMatrix));
  690. arrowFigure.Segments.Add(buttSegment);
  691. return arrowFigure;
  692. }
  693. }
  694. }
  695. public class PanelState
  696. {
  697. private static PanelState instance;
  698. public enum RightPanelState
  699. {
  700. None,
  701. PropertyPanel,
  702. ViewSettings
  703. }
  704. private bool _isLeftPanelExpand;
  705. public bool IsLeftPanelExpand
  706. {
  707. get { return _isLeftPanelExpand; }
  708. set
  709. {
  710. if (_isLeftPanelExpand != value)
  711. {
  712. _isLeftPanelExpand = value;
  713. OnPropertyChanged();
  714. }
  715. }
  716. }
  717. private RightPanelState _rightPanel;
  718. public RightPanelState RightPanel
  719. {
  720. get { return _rightPanel; }
  721. set
  722. {
  723. if (_rightPanel != value)
  724. {
  725. _rightPanel = value;
  726. OnPropertyChanged();
  727. }
  728. }
  729. }
  730. private PanelState() { }
  731. public static PanelState GetInstance()
  732. {
  733. if (instance == null)
  734. {
  735. instance = new PanelState();
  736. }
  737. return instance;
  738. }
  739. public event PropertyChangedEventHandler PropertyChanged;
  740. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  741. {
  742. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  743. }
  744. }
  745. public class SaveHelper
  746. {
  747. private static SaveHelper instance;
  748. private bool _canSave;
  749. public bool CanSave
  750. {
  751. get { return _canSave; }
  752. set
  753. {
  754. if (_canSave != value)
  755. {
  756. _canSave = value;
  757. OnPropertyChanged();
  758. }
  759. }
  760. }
  761. public event PropertyChangedEventHandler PropertyChanged;
  762. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  763. {
  764. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  765. }
  766. }
  767. }