MatrixHeaderDescriptor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using FastReport.Table;
  5. using FastReport.Utils;
  6. namespace FastReport.Matrix
  7. {
  8. /// <summary>
  9. /// The descriptor that is used to describe one element of the matrix header.
  10. /// </summary>
  11. /// <remarks>
  12. /// The <see cref="MatrixHeaderDescriptor"/> class is used to define one header element of the matrix
  13. /// (either the column element or row element). The key properties are
  14. /// <see cref="MatrixDescriptor.Expression"/>, <see cref="Sort"/> and <see cref="Totals"/>.
  15. /// <para/>To set visual appearance of the element, use the <see cref="MatrixDescriptor.TemplateCell"/>
  16. /// property. To set visual appearance of the "total" element, use the <see cref="TemplateTotalCell"/>
  17. /// property.
  18. /// <para/>The collection of descriptors used to represent the matrix header is stored
  19. /// in the <b>MatrixObject.Data.Columns</b> and <b>MatrixObject.Data.Rows</b> properties.
  20. /// </remarks>
  21. public class MatrixHeaderDescriptor : MatrixDescriptor
  22. {
  23. #region Fields
  24. private SortOrder sort;
  25. private bool totals;
  26. private bool totalsFirst;
  27. private bool pageBreak;
  28. private bool suppressTotals;
  29. private TableColumn templateTotalColumn;
  30. private TableRow templateTotalRow;
  31. private TableCell templateTotalCell;
  32. #endregion
  33. #region Properties
  34. /// <summary>
  35. /// Gets or sets the sort order of header values.
  36. /// </summary>
  37. /// <remarks>
  38. /// This property determines how the values displayed in this element are sorted. The default sort
  39. /// is ascending.
  40. /// </remarks>
  41. public SortOrder Sort
  42. {
  43. get { return sort; }
  44. set { sort = value; }
  45. }
  46. /// <summary>
  47. /// Gets or sets a value indicating that this element has associated "total" element.
  48. /// </summary>
  49. /// <remarks>
  50. /// To access the matrix cell that is bound to the "Total" element, use the
  51. /// <see cref="TemplateTotalCell"/> property. It may be useful to change the
  52. /// "Total" text by something else.
  53. /// </remarks>
  54. /// <example>This example shows how to change the "Total" text of the total element.
  55. /// <code>
  56. /// MatrixObject matrix;
  57. /// matrix.Data.Rows[0].TemplateTotalCell.Text = "Grand Total";
  58. /// </code>
  59. /// </example>
  60. public bool Totals
  61. {
  62. get { return totals; }
  63. set { totals = value; }
  64. }
  65. /// <summary>
  66. /// Gets or sets the value indicating whether the total values must be printed before the data.
  67. /// </summary>
  68. public bool TotalsFirst
  69. {
  70. get { return totalsFirst; }
  71. set { totalsFirst = value; }
  72. }
  73. /// <summary>
  74. /// Gets or sets a value indicating that the page break must be printed before this element.
  75. /// </summary>
  76. /// <remarks>
  77. /// Page break is not printed before the very first element.
  78. /// </remarks>
  79. public bool PageBreak
  80. {
  81. get { return pageBreak; }
  82. set { pageBreak = value; }
  83. }
  84. /// <summary>
  85. /// Gets or sets a value that determines whether it is necessary to suppress totals
  86. /// if there is only one value in a group.
  87. /// </summary>
  88. public bool SuppressTotals
  89. {
  90. get { return suppressTotals; }
  91. set { suppressTotals = value; }
  92. }
  93. /// <summary>
  94. /// Gets or sets the template column bound to the "total" element of this descriptor.
  95. /// </summary>
  96. /// <remarks>
  97. /// This property is for internal use; usually you don't need to use it.
  98. /// </remarks>
  99. public TableColumn TemplateTotalColumn
  100. {
  101. get { return templateTotalColumn; }
  102. set { templateTotalColumn = value; }
  103. }
  104. /// <summary>
  105. /// Gets or sets the template row bound to the "total" element of this descriptor.
  106. /// </summary>
  107. /// <remarks>
  108. /// This property is for internal use; usually you don't need to use it.
  109. /// </remarks>
  110. public TableRow TemplateTotalRow
  111. {
  112. get { return templateTotalRow; }
  113. set { templateTotalRow = value; }
  114. }
  115. /// <summary>
  116. /// Gets or sets the template cell bound to the "total" element of this descriptor.
  117. /// </summary>
  118. /// <remarks>
  119. /// This property may be useful to change the "Total" text by something else.
  120. /// <note>
  121. /// Before using this property, you must initialize the matrix descriptors by
  122. /// calling the <see cref="MatrixObject.BuildTemplate"/> method.
  123. /// </note>
  124. /// </remarks>
  125. /// <example>This example shows how to change the "Total" element.
  126. /// <code>
  127. /// MatrixObject matrix;
  128. /// matrix.Data.Rows[0].TemplateTotalCell.Text = "Grand Total";
  129. /// matrix.Data.Rows[0].TemplateTotalCell.Fill = new SolidFill(Color.Green);
  130. /// </code>
  131. /// </example>
  132. public TableCell TemplateTotalCell
  133. {
  134. get { return templateTotalCell; }
  135. set { templateTotalCell = value; }
  136. }
  137. #endregion
  138. #region Public Methods
  139. /// <inheritdoc/>
  140. public override void Assign(MatrixDescriptor source)
  141. {
  142. base.Assign(source);
  143. MatrixHeaderDescriptor src = source as MatrixHeaderDescriptor;
  144. if (src != null)
  145. {
  146. Sort = src.Sort;
  147. Totals = src.Totals;
  148. TotalsFirst = src.TotalsFirst;
  149. PageBreak = src.PageBreak;
  150. SuppressTotals = src.SuppressTotals;
  151. TemplateTotalCell = src.TemplateTotalCell;
  152. }
  153. }
  154. /// <inheritdoc/>
  155. public override void Serialize(FRWriter writer)
  156. {
  157. MatrixHeaderDescriptor c = writer.DiffObject as MatrixHeaderDescriptor;
  158. base.Serialize(writer);
  159. writer.ItemName = "Header";
  160. if (Sort != c.Sort)
  161. writer.WriteValue("Sort", Sort);
  162. if (Totals != c.Totals)
  163. writer.WriteBool("Totals", Totals);
  164. if (TotalsFirst != c.TotalsFirst)
  165. writer.WriteBool("TotalsFirst", TotalsFirst);
  166. if (PageBreak != c.PageBreak)
  167. writer.WriteBool("PageBreak", PageBreak);
  168. if (SuppressTotals != c.SuppressTotals)
  169. writer.WriteBool("SuppressTotals", SuppressTotals);
  170. }
  171. #endregion
  172. /// <summary>
  173. /// Initializes a new instance of the <see cref="MatrixHeaderDescriptor"/> class with
  174. /// default settings.
  175. /// </summary>
  176. public MatrixHeaderDescriptor()
  177. : this("", SortOrder.Ascending, true)
  178. {
  179. }
  180. /// <summary>
  181. /// Initializes a new instance of the <see cref="MatrixHeaderDescriptor"/> class with
  182. /// specified expression.
  183. /// </summary>
  184. /// <param name="expression">The descriptor's expression.</param>
  185. public MatrixHeaderDescriptor(string expression)
  186. : this(expression, SortOrder.Ascending, true)
  187. {
  188. }
  189. /// <summary>
  190. /// Initializes a new instance of the <see cref="MatrixHeaderDescriptor"/> class with
  191. /// specified expression and totals.
  192. /// </summary>
  193. /// <param name="expression">The descriptor's expression.</param>
  194. /// <param name="totals">Indicates whether to show the "total" element.</param>
  195. public MatrixHeaderDescriptor(string expression, bool totals)
  196. : this(expression, SortOrder.Ascending, totals)
  197. {
  198. }
  199. /// <summary>
  200. /// Initializes a new instance of the <see cref="MatrixHeaderDescriptor"/> class with
  201. /// specified expression, sort order and totals.
  202. /// </summary>
  203. /// <param name="expression">The descriptor's expression.</param>
  204. /// <param name="sort">Sort order used to sort header values.</param>
  205. /// <param name="totals">Indicates whether to show the "total" element.</param>
  206. public MatrixHeaderDescriptor(string expression, SortOrder sort, bool totals)
  207. {
  208. Expression = expression;
  209. this.sort = sort;
  210. this.totals = totals;
  211. }
  212. }
  213. }