ExportsOptions.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. namespace FastReport.Utils
  4. {
  5. #if !COMMUNITY
  6. /// <summary>
  7. /// Class for handling Exports visibility in the Preview control.
  8. /// </summary>
  9. public partial class ExportsOptions
  10. {
  11. private static ExportsOptions instance = null;
  12. private List<ExportsTreeNode> menuNodes;
  13. /// <summary>
  14. /// All exports available in the Preview control.
  15. /// </summary>
  16. public List<ExportsTreeNode> ExportsMenu
  17. {
  18. get
  19. {
  20. RemoveCloudsAndMessengersDuplicatesInMenuNodes();
  21. return menuNodes;
  22. }
  23. }
  24. private ExportsOptions()
  25. {
  26. menuNodes = new List<ExportsTreeNode>();
  27. }
  28. private void RemoveCloudsAndMessengersDuplicatesInMenuNodes()
  29. {
  30. int last = menuNodes.Count - 1;
  31. for (int i = last; i >= 0; i--)
  32. {
  33. ExportsTreeNode node = menuNodes[i];
  34. if (node.Name == "Cloud" || node.Name == "Messengers")
  35. {
  36. menuNodes.Remove(node);
  37. }
  38. }
  39. }
  40. /// <summary>
  41. /// Gets an instance of ExportOptions.
  42. /// </summary>
  43. /// <returns></returns>
  44. public static ExportsOptions GetInstance()
  45. {
  46. if (instance == null)
  47. instance = new ExportsOptions();
  48. return instance;
  49. }
  50. /// <summary>
  51. /// Exports menu node.
  52. /// </summary>
  53. public partial class ExportsTreeNode
  54. {
  55. private const string EXPORT_ITEM_PREFIX = "Export,";
  56. private const string ITEM_POSTFIX = ",Name";
  57. private const string EXPORT_ITEM_POSTFIX = ",File";
  58. private const string CATEGORY_PREFIX = "Export,ExportGroups,";
  59. private readonly string name;
  60. private readonly List<ExportsTreeNode> nodes = new List<ExportsTreeNode>();
  61. private readonly Type exportType = null;
  62. private readonly int imageIndex = -1;
  63. private ObjectInfo tag = null;
  64. private bool enabled = true;
  65. private readonly bool isExport;
  66. /// <summary>
  67. /// Gets the name.
  68. /// </summary>
  69. public string Name
  70. {
  71. get { return name; }
  72. }
  73. /// <summary>
  74. /// Gets nodes.
  75. /// </summary>
  76. public List<ExportsTreeNode> Nodes
  77. {
  78. get { return nodes; }
  79. }
  80. /// <summary>
  81. /// Gets type of the export.
  82. /// </summary>
  83. public Type ExportType
  84. {
  85. get { return exportType; }
  86. }
  87. /// <summary>
  88. /// Gets index of the image.
  89. /// </summary>
  90. public int ImageIndex
  91. {
  92. get { return imageIndex; }
  93. }
  94. /// <summary>
  95. /// Gets or sets the tag.
  96. /// </summary>
  97. public ObjectInfo Tag
  98. {
  99. get { return tag; }
  100. set { tag = value; }
  101. }
  102. /// <summary>
  103. /// Gets or sets a value that indicates is node enabled.
  104. /// </summary>
  105. public bool Enabled
  106. {
  107. get { return enabled; }
  108. set { enabled = value; }
  109. }
  110. /// <summary>
  111. /// Gets true if node is export, otherwise false.
  112. /// </summary>
  113. public bool IsExport
  114. {
  115. get { return isExport; }
  116. }
  117. public ExportsTreeNode(string name, bool isExport)
  118. {
  119. this.name = name;
  120. this.isExport = isExport;
  121. }
  122. public ExportsTreeNode(string name, Type exportType, bool isExport)
  123. : this(name, isExport)
  124. {
  125. this.exportType = exportType;
  126. }
  127. public ExportsTreeNode(string name, Type exportType, bool isExport, ObjectInfo tag)
  128. : this(name, exportType, isExport)
  129. {
  130. this.tag = tag;
  131. }
  132. public ExportsTreeNode(string name, int imageIndex, bool isExport)
  133. : this(name, isExport)
  134. {
  135. this.imageIndex = imageIndex;
  136. }
  137. public ExportsTreeNode(string name, Type exportType, int imageIndex, bool isExport)
  138. : this(name, exportType, isExport)
  139. {
  140. this.imageIndex = imageIndex;
  141. }
  142. public ExportsTreeNode(string name, Type exportType, int imageIndex, bool isExport, ObjectInfo tag)
  143. : this(name, exportType, imageIndex, isExport)
  144. {
  145. this.tag = tag;
  146. }
  147. public ExportsTreeNode(string name, Type exportType, int imageIndex, bool enabled, bool isExport)
  148. : this(name, exportType, imageIndex, isExport)
  149. {
  150. this.enabled = enabled;
  151. }
  152. ///<inheritdoc/>
  153. public override bool Equals(object obj)
  154. {
  155. if (obj is ExportsTreeNode)
  156. {
  157. ExportsTreeNode Obj = obj as ExportsTreeNode;
  158. bool equalNodes = true;
  159. foreach (var node in Nodes)
  160. equalNodes &= node.ContainsIn(Obj.Nodes);
  161. return equalNodes && Obj.Name == Name && Obj.ImageIndex == ImageIndex && Obj.IsExport == isExport && Obj.ExportType == ExportType && Obj.Enabled == Enabled;
  162. }
  163. return base.Equals(obj);
  164. }
  165. /// <summary>
  166. /// If it is equal node or its contained in node childnodes, it returns true, otherwise false.
  167. /// </summary>
  168. /// <param name="node"></param>
  169. /// <returns></returns>
  170. public bool ContainsIn(ExportsTreeNode node)
  171. {
  172. return this.Equals(node) || this.ContainsIn(node.Nodes);
  173. }
  174. /// <summary>
  175. /// If it is contained in the list or in its elements, it returns true, otherwise false.
  176. /// </summary>
  177. /// <param name="nodes"></param>
  178. /// <returns></returns>
  179. public bool ContainsIn(List<ExportsTreeNode> nodes)
  180. {
  181. foreach (ExportsTreeNode n in nodes)
  182. if (this.Equals(n) || this.ContainsIn(n))
  183. return true;
  184. return false;
  185. }
  186. }
  187. /// <summary>
  188. /// Saves current visible exports in config file.
  189. /// </summary>
  190. public void SaveExportOptions()
  191. {
  192. SaveOptions();
  193. }
  194. /// <summary>
  195. /// Restores visible exports from config file.
  196. /// </summary>
  197. public void RestoreExportOptions()
  198. {
  199. RestoreOptions();
  200. }
  201. /// <summary>
  202. ///
  203. /// </summary>
  204. public void RegisterExports()
  205. {
  206. Queue<ExportsTreeNode> queue = new Queue<ExportsTreeNode>(menuNodes);
  207. while (queue.Count != 0)
  208. {
  209. ExportsTreeNode node = queue.Dequeue();
  210. ObjectInfo tag = null;
  211. if (node.ExportType != null)
  212. {
  213. tag = RegisteredObjects.AddExport(node.ExportType, node.ToString(), node.ImageIndex);
  214. }
  215. node.Tag = tag;
  216. foreach (ExportsTreeNode nextNode in node.Nodes)
  217. {
  218. queue.Enqueue(nextNode);
  219. }
  220. }
  221. }
  222. private ExportsTreeNode FindItem(string name, Type exportType)
  223. {
  224. Queue<ExportsTreeNode> queue = new Queue<ExportsTreeNode>(menuNodes);
  225. while (queue.Count != 0)
  226. {
  227. ExportsTreeNode node = queue.Dequeue();
  228. if (exportType != null && node.ExportType == exportType ||
  229. !string.IsNullOrEmpty(name) && node.Name == name)
  230. {
  231. return node;
  232. }
  233. foreach (ExportsTreeNode nextNode in node.Nodes)
  234. {
  235. queue.Enqueue(nextNode);
  236. }
  237. }
  238. return null;
  239. }
  240. /// <summary>
  241. /// Sets Export category visibility.
  242. /// </summary>
  243. /// <param name="name">Export category name.</param>
  244. /// <param name="enabled">Visibility state.</param>
  245. public void SetExportCategoryEnabled(string name, bool enabled)
  246. {
  247. FindItem(name, null).Enabled = enabled;
  248. }
  249. /// <summary>
  250. /// Sets Export visibility.
  251. /// </summary>
  252. /// <param name="exportType">Export type.</param>
  253. /// <param name="enabled">Visibility state.</param>
  254. public void SetExportEnabled(Type exportType, bool enabled)
  255. {
  256. ExportsTreeNode node = FindItem(null, exportType);
  257. if (node != null)
  258. {
  259. node.Enabled = enabled;
  260. }
  261. }
  262. }
  263. #endif
  264. }