AnnotationCollectionEditor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Design-time UI editor for Annotations.
  6. //
  7. #if DESIGNER
  8. using System.ComponentModel;
  9. using System.Drawing.Design;
  10. using System.Globalization;
  11. using FastReport.DataVisualization.Charting;
  12. namespace FastReport.Design.DataVisualization.Charting
  13. {
  14. /// <summary>
  15. /// Designer editor for the Annotation Collection.
  16. /// </summary>
  17. internal class AnnotationCollectionEditor : ChartCollectionEditor
  18. {
  19. #region Methods
  20. /// <summary>
  21. /// Object constructor.
  22. /// </summary>
  23. public AnnotationCollectionEditor()
  24. : base(typeof(AnnotationCollection))
  25. {
  26. }
  27. /// <summary>
  28. /// Gets the data types that this collection editor can contain.
  29. /// </summary>
  30. /// <returns>An array of data types that this collection can contain.</returns>
  31. protected override Type[] CreateNewItemTypes()
  32. {
  33. return new Type[] {
  34. typeof(LineAnnotation),
  35. typeof(VerticalLineAnnotation),
  36. typeof(HorizontalLineAnnotation),
  37. typeof(TextAnnotation),
  38. typeof(RectangleAnnotation),
  39. typeof(EllipseAnnotation),
  40. typeof(ArrowAnnotation),
  41. typeof(Border3DAnnotation),
  42. typeof(CalloutAnnotation),
  43. typeof(PolylineAnnotation),
  44. typeof(PolygonAnnotation),
  45. typeof(ImageAnnotation),
  46. typeof(AnnotationGroup)
  47. };
  48. }
  49. /// <summary>
  50. /// Create annotation instance in the editor
  51. /// </summary>
  52. /// <param name="itemType">Item type.</param>
  53. /// <returns>Newly created item.</returns>
  54. protected override object CreateInstance(Type itemType)
  55. {
  56. Chart control = (Chart)GetChartReference(Context.Instance);
  57. // Call base class
  58. Annotation annotation = base.CreateInstance(itemType) as Annotation;
  59. // Generate unique name
  60. if (control != null)
  61. {
  62. annotation.Name = NextUniqueName(control, itemType);
  63. }
  64. return annotation;
  65. }
  66. /// <summary>
  67. /// Finds the unique name for a new annotation being added to the collection
  68. /// </summary>
  69. /// <param name="control">Chart control reference.</param>
  70. /// <param name="type">Type of the annotation added.</param>
  71. /// <returns>Next unique chart annotation name</returns>
  72. private static string NextUniqueName(Chart control, Type type)
  73. {
  74. // Find unique name
  75. string result = string.Empty;
  76. string prefix = type.Name;
  77. for (int i = 1; i < System.Int32.MaxValue; i++)
  78. {
  79. result = prefix + i.ToString(CultureInfo.InvariantCulture);
  80. // Check whether the name is unique
  81. if (control.Annotations.IsUniqueName(result))
  82. {
  83. break;
  84. }
  85. }
  86. return result;
  87. }
  88. #endregion // Methods
  89. }
  90. /// <summary>
  91. /// UI type editor for the annotation anchor point
  92. /// </summary>
  93. internal class AnchorPointUITypeEditor : System.Drawing.Design.UITypeEditor
  94. {
  95. #region Editor methods and properties
  96. /// <summary>
  97. /// Display a drop down list with check boxes.
  98. /// </summary>
  99. /// <param name="context">Editing context.</param>
  100. /// <param name="provider">Provider.</param>
  101. /// <param name="value">Value to edit.</param>
  102. /// <returns>Result</returns>
  103. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  104. {
  105. if (context != null && context.Instance != null && provider != null)
  106. {
  107. IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  108. if (edSvc != null &&
  109. context.Instance is Annotation)
  110. {
  111. // Create control for editing
  112. AnchorPointNameTreeView control = new AnchorPointNameTreeView(
  113. edSvc,
  114. (Annotation)context.Instance,
  115. value as DataPoint);
  116. // Show drop down control
  117. edSvc.DropDownControl(control);
  118. // Get new enumeration value
  119. value = control.GetNewValue();
  120. // Dispose control
  121. control.Dispose();
  122. }
  123. }
  124. return value;
  125. }
  126. /// <summary>
  127. /// Gets editing style.
  128. /// </summary>
  129. /// <param name="context">Editing context.</param>
  130. /// <returns>Editor style.</returns>
  131. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  132. {
  133. if (context != null && context.Instance != null)
  134. {
  135. return UITypeEditorEditStyle.DropDown;
  136. }
  137. return base.GetEditStyle(context);
  138. }
  139. #endregion
  140. }
  141. /// <summary>
  142. /// Anchor data points name tree view, which is used for the UI type editing.
  143. /// </summary>
  144. internal class AnchorPointNameTreeView : TreeView
  145. {
  146. #region Control fields
  147. // Annotation object to edit
  148. private Annotation _annotation = null;
  149. private DataPoint _dataPoint = null;
  150. IWindowsFormsEditorService _edSvc = null;
  151. #endregion
  152. #region Control constructor
  153. /// <summary>
  154. /// Public constructor.
  155. /// </summary>
  156. /// <param name="edSvc">Editor service.</param>
  157. /// <param name="annotation">Annotation to edit.</param>
  158. /// <param name="dataPoint">Annotation anchor data point to edit.</param>
  159. public AnchorPointNameTreeView(
  160. IWindowsFormsEditorService edSvc,
  161. Annotation annotation,
  162. DataPoint dataPoint)
  163. {
  164. // Set editable value
  165. this._annotation = annotation;
  166. this._dataPoint = dataPoint;
  167. this._edSvc = edSvc;
  168. // Set control border style
  169. this.BorderStyle = BorderStyle.None;
  170. // Fill tree with data point names
  171. this.FillTree();
  172. }
  173. #endregion
  174. #region Control methods
  175. /// <summary>
  176. /// Fills data points name tree.
  177. /// </summary>
  178. private void FillTree()
  179. {
  180. bool nodeSelected = false;
  181. this.BeginUpdate();
  182. // Add "None" option
  183. TreeNode noPoint = this.Nodes.Add("NotSet");
  184. // Get chart object
  185. if (this._annotation != null &&
  186. _annotation.AnnotationGroup == null &&
  187. this._annotation.Chart != null)
  188. {
  189. Chart chart = this._annotation.Chart;
  190. // Loop through all series
  191. foreach (Series series in chart.Series)
  192. {
  193. TreeNode seriesNode = this.Nodes.Add(series.Name);
  194. seriesNode.Tag = series;
  195. // Indicate that there are no points in series
  196. if (series.Points.Count == 0)
  197. {
  198. seriesNode.Nodes.Add("(empty)");
  199. }
  200. // Loop through all points
  201. int index = 1;
  202. foreach (DataPoint point in series.Points)
  203. {
  204. TreeNode dataPointNode = seriesNode.Nodes.Add("DataPoint" + index.ToString(System.Globalization.CultureInfo.InvariantCulture));
  205. dataPointNode.Tag = point;
  206. ++index;
  207. // Check if this node should be selected
  208. if (point == _dataPoint)
  209. {
  210. seriesNode.Expand();
  211. this.SelectedNode = dataPointNode;
  212. nodeSelected = true;
  213. }
  214. }
  215. }
  216. }
  217. // Select default node
  218. if (!nodeSelected)
  219. {
  220. this.SelectedNode = noPoint;
  221. }
  222. this.EndUpdate();
  223. }
  224. /// <summary>
  225. /// Gets new data point.
  226. /// </summary>
  227. /// <returns>New enum value.</returns>
  228. public DataPoint GetNewValue()
  229. {
  230. if (this.SelectedNode != null &&
  231. this.SelectedNode.Tag != null &&
  232. this.SelectedNode.Tag is DataPoint)
  233. {
  234. return (DataPoint)this.SelectedNode.Tag;
  235. }
  236. return null;
  237. }
  238. /// <summary>
  239. /// Mouse double clicked.
  240. /// </summary>
  241. protected override void OnDoubleClick(EventArgs e)
  242. {
  243. base.OnDoubleClick(e);
  244. if (this._edSvc != null)
  245. {
  246. if (GetNewValue() != null)
  247. {
  248. this._edSvc.CloseDropDown();
  249. }
  250. else if (this.SelectedNode != null &&
  251. this.SelectedNode.Text == "NotSet")
  252. {
  253. this._edSvc.CloseDropDown();
  254. }
  255. }
  256. }
  257. #endregion
  258. }
  259. /// <summary>
  260. /// UI type editor for the annotation axes.
  261. /// </summary>
  262. internal class AnnotationAxisUITypeEditor : System.Drawing.Design.UITypeEditor
  263. {
  264. #region Editor methods and properties
  265. /// <summary>
  266. /// Display a drop down list with check boxes.
  267. /// </summary>
  268. /// <param name="context">Editing context.</param>
  269. /// <param name="provider">Provider.</param>
  270. /// <param name="value">Value to edit.</param>
  271. /// <returns>Result</returns>
  272. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  273. {
  274. if (context != null && context.Instance != null && provider != null)
  275. {
  276. IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  277. if (edSvc != null &&
  278. context.Instance is Annotation)
  279. {
  280. // Check if we dealing with X or Y axis
  281. bool showXAxes = true;
  282. if (context.PropertyDescriptor != null &&
  283. context.PropertyDescriptor.Name == "AxisY")
  284. {
  285. showXAxes = false;
  286. }
  287. // Create control for editing
  288. AnnotationAxisNameTreeView control = new AnnotationAxisNameTreeView(
  289. edSvc,
  290. (Annotation)context.Instance,
  291. value as Axis,
  292. showXAxes);
  293. // Show drop down control
  294. edSvc.DropDownControl(control);
  295. // Get new enumeration value
  296. value = control.GetNewValue();
  297. // Dispose control
  298. control.Dispose();
  299. }
  300. }
  301. return value;
  302. }
  303. /// <summary>
  304. /// Gets editing style.
  305. /// </summary>
  306. /// <param name="context">Editing context.</param>
  307. /// <returns>Editor style.</returns>
  308. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  309. {
  310. if (context != null && context.Instance != null)
  311. {
  312. return UITypeEditorEditStyle.DropDown;
  313. }
  314. return base.GetEditStyle(context);
  315. }
  316. #endregion
  317. }
  318. /// <summary>
  319. /// Annotation axes names tree view, which is used for the UI type editing.
  320. /// </summary>
  321. internal class AnnotationAxisNameTreeView : TreeView
  322. {
  323. #region Control fields
  324. // Annotation object to edit
  325. private Annotation _annotation = null;
  326. private Axis _axis = null;
  327. IWindowsFormsEditorService _edSvc = null;
  328. private bool _showXAxes = true;
  329. #endregion
  330. #region Control constructor
  331. /// <summary>
  332. /// Public constructor.
  333. /// </summary>
  334. /// <param name="edSvc">Editor service.</param>
  335. /// <param name="annotation">Annotation to edit.</param>
  336. /// <param name="axis">Axis object.</param>
  337. /// <param name="showXAxes">Indicates if X or Y axis should be shown.</param>
  338. public AnnotationAxisNameTreeView(
  339. IWindowsFormsEditorService edSvc,
  340. Annotation annotation,
  341. Axis axis,
  342. bool showXAxes)
  343. {
  344. // Set editable value
  345. this._annotation = annotation;
  346. this._axis = axis;
  347. this._edSvc = edSvc;
  348. this._showXAxes = showXAxes;
  349. // Set control border style
  350. this.BorderStyle = BorderStyle.None;
  351. // Fill tree with data point names
  352. this.FillTree();
  353. }
  354. #endregion
  355. #region Control methods
  356. /// <summary>
  357. /// Fills data points name tree.
  358. /// </summary>
  359. private void FillTree()
  360. {
  361. bool nodeSelected = false;
  362. this.BeginUpdate();
  363. // Add "None" option
  364. TreeNode noPoint = this.Nodes.Add("NotSet");
  365. // Get chart object
  366. if (this._annotation != null &&
  367. _annotation.AnnotationGroup == null &&
  368. this._annotation.Chart != null)
  369. {
  370. Chart chart = this._annotation.Chart;
  371. // Loop through all chart areas
  372. foreach (ChartArea chartArea in chart.ChartAreas)
  373. {
  374. TreeNode areaNode = this.Nodes.Add(chartArea.Name);
  375. areaNode.Tag = chartArea;
  376. // Loop through all axes
  377. foreach (Axis curAxis in chartArea.Axes)
  378. {
  379. // Hide X or Y axes
  380. if (curAxis.AxisName == AxisName.Y || curAxis.AxisName == AxisName.Y2)
  381. {
  382. if (_showXAxes)
  383. {
  384. continue;
  385. }
  386. }
  387. if (curAxis.AxisName == AxisName.X || curAxis.AxisName == AxisName.X2)
  388. {
  389. if (!_showXAxes)
  390. {
  391. continue;
  392. }
  393. }
  394. // Create child node
  395. TreeNode axisNode = areaNode.Nodes.Add(curAxis.Name);
  396. axisNode.Tag = curAxis;
  397. // Check if this node should be selected
  398. if (_axis == curAxis)
  399. {
  400. areaNode.Expand();
  401. this.SelectedNode = axisNode;
  402. nodeSelected = true;
  403. }
  404. }
  405. }
  406. }
  407. // Select default node
  408. if (!nodeSelected)
  409. {
  410. this.SelectedNode = noPoint;
  411. }
  412. this.EndUpdate();
  413. }
  414. /// <summary>
  415. /// Gets new data point.
  416. /// </summary>
  417. /// <returns>New enum value.</returns>
  418. public Axis GetNewValue()
  419. {
  420. if (this.SelectedNode != null &&
  421. this.SelectedNode.Tag != null &&
  422. this.SelectedNode.Tag is Axis)
  423. {
  424. return (Axis)this.SelectedNode.Tag;
  425. }
  426. return null;
  427. }
  428. /// <summary>
  429. /// Mouse double clicked.
  430. /// </summary>
  431. protected override void OnDoubleClick(EventArgs e)
  432. {
  433. base.OnDoubleClick(e);
  434. if (this._edSvc != null)
  435. {
  436. if (GetNewValue() != null)
  437. {
  438. this._edSvc.CloseDropDown();
  439. }
  440. else if (this.SelectedNode != null &&
  441. this.SelectedNode.Text == "NotSet")
  442. {
  443. this._edSvc.CloseDropDown();
  444. }
  445. }
  446. }
  447. #endregion
  448. }
  449. }
  450. #endif