VirtualGuides.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using FastReport.Utils;
  8. namespace FastReport.Design.PageDesigners
  9. {
  10. internal class VirtualGuides
  11. {
  12. private DesignWorkspaceBase workspace;
  13. private SortedList<float, ComponentBase> vGuides;
  14. private SortedList<float, ComponentBase> hGuides;
  15. private List<RectangleF> guides;
  16. public DesignWorkspaceBase Workspace
  17. {
  18. get { return workspace; }
  19. }
  20. public Designer Designer
  21. {
  22. get { return workspace.Designer; }
  23. }
  24. private void CheckVGuide(float x, ComponentBase c)
  25. {
  26. x = Converter.DecreasePrecision(x, 2);
  27. int i = vGuides.IndexOfKey(x);
  28. if (i != -1)
  29. {
  30. float scale = Workspace.GetScale();
  31. guides.Add(new RectangleF(x * scale, c.AbsTop * scale,
  32. x * scale, vGuides.Values[i].AbsTop * scale));
  33. }
  34. }
  35. private void CheckHGuide(float y, ComponentBase c)
  36. {
  37. y = Converter.DecreasePrecision(y, 2);
  38. int i = hGuides.IndexOfKey(y);
  39. if (i != -1)
  40. {
  41. float scale = Workspace.GetScale();
  42. guides.Add(new RectangleF(c.AbsLeft * scale, y * scale,
  43. hGuides.Values[i].AbsLeft * scale, y * scale));
  44. }
  45. }
  46. private void AddVGuide(float x, ComponentBase c)
  47. {
  48. x = Converter.DecreasePrecision(x, 2);
  49. if (!vGuides.ContainsKey(x))
  50. vGuides.Add(x, c);
  51. }
  52. private void AddHGuide(float y, ComponentBase c)
  53. {
  54. y = Converter.DecreasePrecision(y, 2);
  55. if (!hGuides.ContainsKey(y))
  56. hGuides.Add(y, c);
  57. }
  58. public void Draw(Graphics g)
  59. {
  60. using (Pen pen = new Pen(Color.CornflowerBlue))
  61. {
  62. pen.DashStyle = DashStyle.Dot;
  63. foreach (RectangleF rect in guides)
  64. {
  65. g.DrawLine(pen, rect.Left, rect.Top, rect.Width, rect.Height);
  66. }
  67. }
  68. }
  69. public void Create()
  70. {
  71. vGuides.Clear();
  72. hGuides.Clear();
  73. foreach (Base obj in Designer.Objects)
  74. {
  75. if (obj is ComponentBase && !(obj is BandBase) && !Designer.SelectedObjects.Contains(obj))
  76. {
  77. ComponentBase c = obj as ComponentBase;
  78. AddVGuide(c.AbsLeft, c);
  79. AddVGuide(c.AbsRight, c);
  80. AddHGuide(c.AbsTop, c);
  81. AddHGuide(c.AbsBottom, c);
  82. }
  83. }
  84. }
  85. public void Check()
  86. {
  87. guides.Clear();
  88. foreach (Base obj in Designer.SelectedObjects)
  89. {
  90. if (obj is ComponentBase && !(obj is BandBase))
  91. {
  92. ComponentBase c = obj as ComponentBase;
  93. CheckVGuide(c.AbsLeft, c);
  94. CheckVGuide(c.AbsRight, c);
  95. CheckHGuide(c.AbsTop, c);
  96. CheckHGuide(c.AbsBottom, c);
  97. }
  98. }
  99. }
  100. public void Clear()
  101. {
  102. guides.Clear();
  103. }
  104. public VirtualGuides(DesignWorkspaceBase w)
  105. {
  106. workspace = w;
  107. vGuides = new SortedList<float, ComponentBase>();
  108. hGuides = new SortedList<float, ComponentBase>();
  109. guides = new List<RectangleF>();
  110. }
  111. }
  112. }