ShapeBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using System.ComponentModel;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Diagnostics;
  6. using System.Text;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using FastReport.Utils;
  10. namespace FastReport.Map
  11. {
  12. /// <summary>
  13. /// The base class for shape objects such as <see cref="ShapePoint"/>, <see cref="ShapePolyLine"/> and <see cref="ShapePolygon"/>.
  14. /// </summary>
  15. public class ShapeBase : Base, ICustomTypeDescriptor
  16. {
  17. #region Fields
  18. private bool visible;
  19. private bool useCustomStyle;
  20. private ShapeStyle customStyle;
  21. private float centerOffsetX;
  22. private float centerOffsetY;
  23. private float shapeOffsetX;
  24. private float shapeOffsetY;
  25. private float shapeScale;
  26. private ShapeSpatialData spatialData;
  27. private double value;
  28. private double saveValue;
  29. private int shapeIndex;
  30. #endregion // Fields
  31. #region Properties
  32. /// <summary>
  33. /// Gets or sets the shape visibility.
  34. /// </summary>
  35. [DefaultValue(true)]
  36. [Category("Behavior")]
  37. public bool Visible
  38. {
  39. get { return visible; }
  40. set { visible = value; }
  41. }
  42. /// <summary>
  43. /// Gets or sets a value indicating that custom shape style is used.
  44. /// </summary>
  45. /// <remarks>
  46. /// If this property is <b>false</b>, the layer's DefaultShapeStyle is used.
  47. /// </remarks>
  48. [DefaultValue(false)]
  49. [Category("Appearance")]
  50. public bool UseCustomStyle
  51. {
  52. get { return useCustomStyle; }
  53. set
  54. {
  55. useCustomStyle = value;
  56. if (value)
  57. customStyle = new ShapeStyle();
  58. else
  59. customStyle = null;
  60. }
  61. }
  62. /// <summary>
  63. /// Gets a custom shape style.
  64. /// </summary>
  65. /// <remarks>
  66. /// To use this property, first set the <see cref="UseCustomStyle"/> property to <b>true</b>.
  67. /// </remarks>
  68. [Category("Appearance")]
  69. public ShapeStyle CustomStyle
  70. {
  71. get { return customStyle; }
  72. }
  73. /// <summary>
  74. /// Gets or sets the center point X offset.
  75. /// </summary>
  76. /// <remarks>
  77. /// Use this property to adjust the label's position.
  78. /// </remarks>
  79. [Category("Appearance")]
  80. [DefaultValue(0f)]
  81. public float CenterOffsetX
  82. {
  83. get { return centerOffsetX; }
  84. set { centerOffsetX = value; }
  85. }
  86. /// <summary>
  87. /// Gets or sets the center point Y offset.
  88. /// </summary>
  89. /// <remarks>
  90. /// Use this property to adjust the label's position.
  91. /// </remarks>
  92. [Category("Appearance")]
  93. [DefaultValue(0f)]
  94. public float CenterOffsetY
  95. {
  96. get { return centerOffsetY; }
  97. set { centerOffsetY = value; }
  98. }
  99. /// <summary>
  100. /// Gets or sets the shape X offset.
  101. /// </summary>
  102. /// <remarks>
  103. /// Use this property to adjust the shape position.
  104. /// </remarks>
  105. [Category("Appearance")]
  106. [DefaultValue(0f)]
  107. public float ShapeOffsetX
  108. {
  109. get { return shapeOffsetX; }
  110. set { shapeOffsetX = value; }
  111. }
  112. /// <summary>
  113. /// Gets or sets the shape Y offset.
  114. /// </summary>
  115. /// <remarks>
  116. /// Use this property to adjust the shape position.
  117. /// </remarks>
  118. [Category("Appearance")]
  119. [DefaultValue(0f)]
  120. public float ShapeOffsetY
  121. {
  122. get { return shapeOffsetY; }
  123. set { shapeOffsetY = value; }
  124. }
  125. /// <summary>
  126. /// Gets or sets the scale factor for this shape.
  127. /// </summary>
  128. /// <remarks>
  129. /// Use this property to adjust the shape size.
  130. /// </remarks>
  131. [Category("Appearance")]
  132. [DefaultValue(1f)]
  133. public float ShapeScale
  134. {
  135. get { return shapeScale; }
  136. set { shapeScale = value; }
  137. }
  138. /// <summary>
  139. /// Gets or sets the spatial data associated with this shape.
  140. /// </summary>
  141. [Browsable(false)]
  142. public ShapeSpatialData SpatialData
  143. {
  144. get { return spatialData; }
  145. set { spatialData = value; }
  146. }
  147. /// <summary>
  148. /// Gets or sets the value.
  149. /// </summary>
  150. [DefaultValue(double.NaN)]
  151. [Category("Data")]
  152. public double Value
  153. {
  154. get { return value; }
  155. set { this.value = value; }
  156. }
  157. /// <summary>
  158. /// Gets a reference to the parent Map object.
  159. /// </summary>
  160. [Browsable(false)]
  161. public MapObject Map
  162. {
  163. get { return Layer.Map; }
  164. }
  165. /// <summary>
  166. /// Gets a reference to the parent Layer object.
  167. /// </summary>
  168. [Browsable(false)]
  169. public MapLayer Layer
  170. {
  171. get { return Parent as MapLayer; }
  172. }
  173. internal int ShapeIndex
  174. {
  175. get { return shapeIndex; }
  176. set { shapeIndex = value; }
  177. }
  178. internal bool IsValueEmpty
  179. {
  180. get { return double.IsNaN(Value); }
  181. }
  182. internal string SpatialValue
  183. {
  184. get { return SpatialData.GetValue(Layer.SpatialColumn); }
  185. }
  186. #endregion // Properties
  187. #region Public Methods
  188. /// <inheritdoc/>
  189. public override void Assign(Base source)
  190. {
  191. base.Assign(source);
  192. ShapeBase src = source as ShapeBase;
  193. Visible = src.Visible;
  194. SpatialData.Assign(src.SpatialData);
  195. Value = src.Value;
  196. CenterOffsetX = src.CenterOffsetX;
  197. CenterOffsetY = src.CenterOffsetY;
  198. ShapeOffsetX = src.ShapeOffsetX;
  199. ShapeOffsetY = src.ShapeOffsetY;
  200. ShapeScale = src.ShapeScale;
  201. UseCustomStyle = src.UseCustomStyle;
  202. if (UseCustomStyle)
  203. CustomStyle.Assign(src.CustomStyle);
  204. }
  205. /// <summary>
  206. /// Draws the shape.
  207. /// </summary>
  208. /// <param name="e">Object that provides a data for paint event.</param>
  209. public virtual void Draw(FRPaintEventArgs e)
  210. {
  211. }
  212. /// <summary>
  213. /// Draws the label.
  214. /// </summary>
  215. /// <param name="e">Object that provides a data for paint event.</param>
  216. public virtual void DrawLabel(FRPaintEventArgs e)
  217. {
  218. }
  219. /// <summary>
  220. /// Checks if the shape is under cursor.
  221. /// </summary>
  222. /// <param name="point">The cursor coordinates.</param>
  223. /// <returns><b>true</b> if the cursor is over the shape.</returns>
  224. public virtual bool HitTest(PointF point)
  225. {
  226. return false;
  227. }
  228. /// <summary>
  229. /// Reduces the number of points in the shape.
  230. /// </summary>
  231. /// <param name="accuracy">The accuracy value.</param>
  232. public virtual void Simplify(double accuracy)
  233. {
  234. }
  235. /// <inheritdoc/>
  236. public override void Serialize(FRWriter writer)
  237. {
  238. ShapeBase c = writer.DiffObject as ShapeBase;
  239. base.Serialize(writer);
  240. if (Visible != c.Visible)
  241. writer.WriteBool("Visible", Visible);
  242. if (!SpatialData.IsEqual(c.SpatialData))
  243. writer.WriteValue("SpatialData", SpatialData);
  244. if (Value != c.Value)
  245. writer.WriteDouble("Value", Value);
  246. if (CenterOffsetX != c.CenterOffsetX)
  247. writer.WriteFloat("CenterOffsetX", CenterOffsetX);
  248. if (CenterOffsetY != c.CenterOffsetY)
  249. writer.WriteFloat("CenterOffsetY", CenterOffsetY);
  250. if (ShapeOffsetX != c.ShapeOffsetX)
  251. writer.WriteFloat("ShapeOffsetX", ShapeOffsetX);
  252. if (ShapeOffsetY != c.ShapeOffsetY)
  253. writer.WriteFloat("ShapeOffsetY", ShapeOffsetY);
  254. if (ShapeScale != c.ShapeScale)
  255. writer.WriteFloat("ShapeScale", ShapeScale);
  256. if (UseCustomStyle != c.UseCustomStyle)
  257. writer.WriteBool("UseCustomStyle", UseCustomStyle);
  258. if (UseCustomStyle)
  259. CustomStyle.Serialize(writer, "CustomStyle", c.CustomStyle != null ? c.CustomStyle : new ShapeStyle());
  260. }
  261. /// <summary>
  262. /// Initializes a component before running a report.
  263. /// </summary>
  264. public virtual void InitializeComponent()
  265. {
  266. }
  267. /// <summary>
  268. /// Finalizes a component before running a report.
  269. /// </summary>
  270. public virtual void FinalizeComponent()
  271. {
  272. }
  273. /// <summary>
  274. /// Saves the state of this component.
  275. /// </summary>
  276. public virtual void SaveState()
  277. {
  278. saveValue = Value;
  279. }
  280. /// <summary>
  281. /// Restores the state of this component.
  282. /// </summary>
  283. public virtual void RestoreState()
  284. {
  285. Value = saveValue;
  286. }
  287. #endregion // Public Methods
  288. #region ICustomTypeDescriptor Members
  289. /// <inheritdoc/>
  290. public PropertyDescriptorCollection GetProperties()
  291. {
  292. return GetProperties(null);
  293. }
  294. /// <inheritdoc/>
  295. public PropertyDescriptorCollection GetProperties(Attribute[] attr)
  296. {
  297. PropertyDescriptorCollection typeProps = TypeDescriptor.GetProperties(this.GetType(), attr);
  298. PropertyDescriptorCollection properties = new PropertyDescriptorCollection(null);
  299. foreach (PropertyDescriptor desc in typeProps)
  300. {
  301. properties.Add(desc);
  302. }
  303. foreach (string key in SpatialData.GetKeys())
  304. {
  305. properties.Add(new MetadataPropertyDescriptor(SpatialData, key, attr));
  306. }
  307. return properties;
  308. }
  309. /// <inheritdoc/>
  310. public String GetClassName()
  311. {
  312. return TypeDescriptor.GetClassName(this, true);
  313. }
  314. /// <inheritdoc/>
  315. public AttributeCollection GetAttributes()
  316. {
  317. return TypeDescriptor.GetAttributes(this, true);
  318. }
  319. /// <inheritdoc/>
  320. public String GetComponentName()
  321. {
  322. return TypeDescriptor.GetComponentName(this, true);
  323. }
  324. /// <inheritdoc/>
  325. public TypeConverter GetConverter()
  326. {
  327. return TypeDescriptor.GetConverter(this, true);
  328. }
  329. /// <inheritdoc/>
  330. public EventDescriptor GetDefaultEvent()
  331. {
  332. return TypeDescriptor.GetDefaultEvent(this, true);
  333. }
  334. /// <inheritdoc/>
  335. public PropertyDescriptor GetDefaultProperty()
  336. {
  337. return TypeDescriptor.GetDefaultProperty(this, true);
  338. }
  339. /// <inheritdoc/>
  340. public object GetEditor(Type editorBaseType)
  341. {
  342. return TypeDescriptor.GetEditor(this, editorBaseType, true);
  343. }
  344. /// <inheritdoc/>
  345. public EventDescriptorCollection GetEvents(Attribute[] attributes)
  346. {
  347. return TypeDescriptor.GetEvents(this, attributes, true);
  348. }
  349. /// <inheritdoc/>
  350. public EventDescriptorCollection GetEvents()
  351. {
  352. return TypeDescriptor.GetEvents(this, true);
  353. }
  354. /// <inheritdoc/>
  355. public object GetPropertyOwner(PropertyDescriptor pd)
  356. {
  357. return this;
  358. }
  359. #endregion
  360. /// <summary>
  361. /// Initializes a new instance of the <see cref="ShapeBase"/> class.
  362. /// </summary>
  363. public ShapeBase()
  364. {
  365. visible = true;
  366. spatialData = new ShapeSpatialData();
  367. value = double.NaN;
  368. shapeScale = 1;
  369. }
  370. }
  371. }