MapObject.DesignExt.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. using FastReport.Utils;
  6. using FastReport.Map.Forms;
  7. namespace FastReport.Map
  8. {
  9. partial class MapObject : IHasEditor
  10. {
  11. #region Fields
  12. private bool needDesignerModify;
  13. private int doubleClickTickCount;
  14. private PointF doubleClickPos;
  15. #endregion // Fields
  16. #region Properties
  17. /// <summary>
  18. /// This property is not relevant to this class.
  19. /// </summary>
  20. [Browsable(false)]
  21. public new bool CanGrow
  22. {
  23. get { return base.CanGrow; }
  24. set { base.CanGrow = value; }
  25. }
  26. /// <summary>
  27. /// This property is not relevant to this class.
  28. /// </summary>
  29. [Browsable(false)]
  30. public new bool CanShrink
  31. {
  32. get { return base.CanShrink; }
  33. set { base.CanShrink = value; }
  34. }
  35. /// <summary>
  36. /// This property is not relevant to this class.
  37. /// </summary>
  38. [Browsable(false)]
  39. public new string Style
  40. {
  41. get { return base.Style; }
  42. set { base.Style = value; }
  43. }
  44. /// <summary>
  45. /// This property is not relevant to this class.
  46. /// </summary>
  47. [Browsable(false)]
  48. public new string EvenStyle
  49. {
  50. get { return base.EvenStyle; }
  51. set { base.EvenStyle = value; }
  52. }
  53. /// <summary>
  54. /// This property is not relevant to this class.
  55. /// </summary>
  56. [Browsable(false)]
  57. public new StylePriority EvenStylePriority
  58. {
  59. get { return base.EvenStylePriority; }
  60. set { base.EvenStylePriority = value; }
  61. }
  62. /// <summary>
  63. /// This property is not relevant to this class.
  64. /// </summary>
  65. [Browsable(false)]
  66. public new string HoverStyle
  67. {
  68. get { return base.HoverStyle; }
  69. set { base.HoverStyle = value; }
  70. }
  71. /// <inheritdoc/>
  72. public override bool IsSelected
  73. {
  74. get
  75. {
  76. if (Report == null)
  77. return false;
  78. return Report.Designer.SelectedObjects.IndexOf(this) != -1 || IsInternalSelected;
  79. }
  80. }
  81. private bool IsInternalSelected
  82. {
  83. get
  84. {
  85. if (Report == null)
  86. return false;
  87. SelectedObjectCollection selection = Report.Designer.SelectedObjects;
  88. return selection.Count > 0 && (
  89. (selection[0] is MapLayer && (selection[0] as MapLayer).Map == this) ||
  90. (selection[0] is ShapeBase && (selection[0] as ShapeBase).Map == this));
  91. }
  92. }
  93. #endregion // Properties
  94. #region Private Methods
  95. internal void GenerateRandomData()
  96. {
  97. if (IsEmpty)
  98. return;
  99. foreach (MapLayer layer in Layers)
  100. {
  101. layer.InitializeData();
  102. if (!String.IsNullOrEmpty(layer.SpatialColumn))
  103. {
  104. double value = 0;
  105. foreach (ShapeBase shape in layer.Shapes)
  106. {
  107. layer.AddValue(shape.SpatialValue, value);
  108. value += 50;
  109. }
  110. }
  111. else
  112. {
  113. layer.AddValue("1", 0);
  114. layer.AddValue("2", 1000);
  115. }
  116. layer.FinalizeData();
  117. }
  118. }
  119. private Base HitTest(PointF point)
  120. {
  121. for (int i = Layers.Count - 1; i >= 0; i--)
  122. {
  123. ShapeBase shape = Layers[i].HitTest(point);
  124. if (shape != null)
  125. return shape;
  126. }
  127. return null;
  128. }
  129. #endregion // Private Methods
  130. #region Public Methods
  131. /// <inheritdoc/>
  132. public override void Draw(FRPaintEventArgs e)
  133. {
  134. base.Draw(e);
  135. if (IsDesigning)
  136. {
  137. if (IsEmpty)
  138. {
  139. string s = Res.Get("ComponentsMisc,Map,Hint");
  140. Font font = new Font(DrawUtils.DefaultReportFont.Name, DrawUtils.DefaultFont.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, DrawUtils.DefaultFont.Style);
  141. e.Graphics.DrawString(s, font, Brushes.Black,
  142. new RectangleF(AbsLeft * e.ScaleX, AbsTop * e.ScaleY, Width * e.ScaleX, Height * e.ScaleY),
  143. e.Cache.GetStringFormat(StringAlignment.Center, StringAlignment.Center, StringTrimming.None, StringFormatFlags.NoClip, 0, 0));
  144. }
  145. else
  146. {
  147. try
  148. {
  149. SaveState();
  150. GenerateRandomData();
  151. DrawMap(e);
  152. }
  153. finally
  154. {
  155. RestoreState();
  156. }
  157. }
  158. }
  159. else
  160. try
  161. {
  162. SaveState();
  163. DrawMap(e);
  164. }
  165. finally
  166. {
  167. RestoreState();
  168. }
  169. DrawMarkers(e);
  170. Border.Draw(e, new RectangleF(AbsLeft, AbsTop, Width, Height));
  171. if (IsDesigning && IsSelected)
  172. {
  173. float m = Report.Designer.DpiMultiplier();
  174. e.Graphics.DrawImage(Report.Designer.GetImage(75), (int)(AbsLeft * e.ScaleX + 8 * m), (int)(AbsTop * e.ScaleY - 8 * m));
  175. }
  176. }
  177. /// <inheritdoc/>
  178. public override SizeF GetPreferredSize()
  179. {
  180. if ((Page as ReportPage).IsImperialUnitsUsed)
  181. return new SizeF(Units.Inches * 4, Units.Inches * 4f);
  182. return new SizeF(Units.Millimeters * 80, Units.Millimeters * 80);
  183. }
  184. /// <inheritdoc/>
  185. public bool InvokeEditor()
  186. {
  187. using (MapEditorForm form = new MapEditorForm())
  188. {
  189. form.Map = this;
  190. return form.ShowDialog() == DialogResult.OK;
  191. }
  192. }
  193. /// <inheritdoc/>
  194. public override void OnBeforeInsert(int flags)
  195. {
  196. base.OnBeforeInsert(flags);
  197. // fill is reset by the designer's default formatting tool. Set it back.
  198. Fill = new SolidFill(Color.Gainsboro);
  199. }
  200. #endregion // Public Methods
  201. #region Designer mouse support
  202. /// <inheritdoc/>
  203. public override void HandleMouseHover(FRMouseEventArgs e)
  204. {
  205. if (IsSelected)
  206. {
  207. float m = 1 / Report.Designer.Zoom;
  208. if (new RectangleF(AbsLeft + 8 * m, AbsTop - 8 * m, 16 * m, 16 * m).Contains(new PointF(e.x, e.y)))
  209. {
  210. e.handled = true;
  211. e.cursor = Cursors.SizeAll;
  212. }
  213. }
  214. }
  215. /// <inheritdoc/>
  216. public override void HandleMouseDown(FRMouseEventArgs e)
  217. {
  218. // allow doubleclick when polygon is selected
  219. bool doubleClick = Environment.TickCount - doubleClickTickCount < SystemInformation.DoubleClickTime &&
  220. new PointF(e.x, e.y).Equals(doubleClickPos);
  221. doubleClickTickCount = Environment.TickCount;
  222. doubleClickPos = new PointF(e.x, e.y);
  223. if (e.mode != WorkspaceMode2.None)
  224. return;
  225. // check move handle
  226. HandleMouseHover(e);
  227. if (e.handled)
  228. {
  229. // do base logic such as selecting/deselecting
  230. // and return with e.Mode = WorkspaceMode2.Move
  231. base.HandleMouseDown(e);
  232. e.handled = true;
  233. e.mode = WorkspaceMode2.Move;
  234. }
  235. else if (PointInObject(new PointF(e.x, e.y)))
  236. {
  237. e.handled = true;
  238. // hit test polygons
  239. Base obj = HitTest(new PointF(e.x, e.y));
  240. // pass rightclick and doubleclick to the map object
  241. if (obj == null || doubleClick || e.button == MouseButtons.Right)
  242. obj = this;
  243. SelectedObjectCollection selection = Report.Designer.SelectedObjects;
  244. if (e.modifierKeys == Keys.Shift)
  245. {
  246. // toggle selection
  247. if (selection.IndexOf(obj) != -1)
  248. {
  249. if (selection.Count > 1)
  250. selection.Remove(obj);
  251. }
  252. else
  253. selection.Add(obj);
  254. }
  255. else
  256. {
  257. // select the object if not selected yet
  258. if (selection.IndexOf(obj) == -1)
  259. {
  260. selection.Clear();
  261. selection.Add(obj);
  262. }
  263. }
  264. e.mode = WorkspaceMode2.Custom;
  265. e.activeObject = this;
  266. isPanning = true;
  267. panned = false;
  268. e.delta = new PointF(0, 0);
  269. }
  270. }
  271. /// <inheritdoc/>
  272. public override void HandleMouseMove(FRMouseEventArgs e)
  273. {
  274. base.HandleMouseMove(e);
  275. if (!e.handled && e.button == MouseButtons.None)
  276. {
  277. // don't process if mouse is over move area
  278. HandleMouseHover(e);
  279. if (e.handled)
  280. {
  281. e.handled = false;
  282. return;
  283. }
  284. if (PointInObject(new PointF(e.x, e.y)))
  285. {
  286. e.handled = true;
  287. }
  288. else
  289. {
  290. // mouse leave, save changes if any
  291. if (needDesignerModify)
  292. {
  293. Report.Designer.SetModified(this, "Change", Name);
  294. needDesignerModify = false;
  295. }
  296. }
  297. }
  298. if (isPanning && !IsEmpty)
  299. {
  300. OffsetX += e.delta.X / Zoom;
  301. OffsetY += e.delta.Y / Zoom;
  302. panned = true;
  303. }
  304. }
  305. /// <inheritdoc/>
  306. public override void HandleMouseUp(FRMouseEventArgs e)
  307. {
  308. base.HandleMouseUp(e);
  309. if (isPanning)
  310. {
  311. if (panned)
  312. needDesignerModify = true;
  313. }
  314. isPanning = false;
  315. panned = false;
  316. }
  317. /// <inheritdoc/>
  318. public override void HandleMouseWheel(FRMouseEventArgs e)
  319. {
  320. if (IsSelected && !IsEmpty)
  321. {
  322. if (e.wheelDelta < 0)
  323. ZoomOut();
  324. else
  325. ZoomIn();
  326. needDesignerModify = true;
  327. e.handled = true;
  328. }
  329. }
  330. #endregion
  331. }
  332. }