DynamicFormLayoutGrid.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media.Imaging;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.Scripting;
  13. using InABox.Wpf;
  14. using InABox.WPF;
  15. using Microsoft.Win32;
  16. using Org.BouncyCastle.Asn1.Mozilla;
  17. using Syncfusion.Windows.Shared;
  18. using UnderlineType = InABox.Core.UnderlineType;
  19. namespace InABox.DynamicGrid;
  20. public abstract class DynamicFormLayoutGrid : DynamicOneToManyGrid<DigitalForm, DigitalFormLayout>
  21. {
  22. private readonly BitmapImage design = Wpf.Resources.design.AsBitmapImage();
  23. protected override void Init()
  24. {
  25. base.Init();
  26. ActionColumns.Add(new DynamicImageColumn(DesignImage, DesignClick));
  27. //AddButton("Design", PRSDesktop.Resources.design.AsBitmapImage(), DesignClick);
  28. HiddenColumns.Add(x => x.Code);
  29. HiddenColumns.Add(x => x.Description);
  30. HiddenColumns.Add(x => x.Type);
  31. HiddenColumns.Add(x => x.Layout);
  32. HiddenColumns.Add(x => x.Form.Code);
  33. HiddenColumns.Add(x => x.Form.Description);
  34. AddButton("Auto Generate", null, AutoGenerate_Click);
  35. AddButton("Duplicate", null, Duplicate_Click);
  36. }
  37. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  38. {
  39. base.DoReconfigure(options);
  40. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.ImportData);
  41. }
  42. private DFLayout LoadLayoutFromSpreadsheet(ISpreadsheet spreadsheet)
  43. {
  44. return DigitalFormUtils.LoadLayout(spreadsheet);
  45. }
  46. protected override void DoImport()
  47. {
  48. var dialog = new OpenFileDialog();
  49. dialog.Filter = "Excel Spreadsheet (.xlsx)|*.xlsx";
  50. if (dialog.ShowDialog() == true)
  51. {
  52. try
  53. {
  54. DFLayout layout;
  55. Dictionary<String, String> variablegroups = new Dictionary<string, string>();
  56. using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  57. {
  58. layout = LoadLayoutFromSpreadsheet(new Spreadsheet(fs));
  59. }
  60. var dfLayout = CreateItem();
  61. dfLayout.Code = Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper();
  62. dfLayout.Description = $"Imported From {Path.GetFileName(dialog.FileName)}";
  63. dfLayout.Layout = layout.SaveLayout();
  64. if(EditItems(new DigitalFormLayout[] { dfLayout }))
  65. {
  66. var newVariables = new List<DigitalFormVariable>();
  67. String group = "";
  68. foreach (var element in layout.Elements)
  69. {
  70. if (element is DFLayoutHeader header)
  71. {
  72. group = header.Header;
  73. }
  74. else if (element is DFLayoutField field)
  75. {
  76. var variable = new DigitalFormVariable();
  77. variable.SetFieldType(field.GetType());
  78. variable.SaveProperties(field.GetProperties());
  79. variable.Group = group;
  80. variable.Code = field.Name;
  81. variable.Description = field.Name;
  82. newVariables.Add(variable);
  83. }
  84. }
  85. if(newVariables.Count > 0)
  86. {
  87. var variables = GetVariableGrid();
  88. if (variables is not null)
  89. {
  90. var save = new List<DigitalFormVariable>();
  91. foreach(var newVariable in newVariables)
  92. {
  93. var variable = variables.GetVariable(newVariable.Code);
  94. if(variable is not null)
  95. {
  96. if(variable.FieldType() != newVariable.FieldType())
  97. {
  98. MessageBox.Show($"Variable [{newVariable.Code}] already exists with a different type!");
  99. }
  100. }
  101. else
  102. {
  103. save.Add(newVariable);
  104. }
  105. }
  106. variables.SaveItems(save.ToArray());
  107. variables.Refresh(false, true);
  108. }
  109. }
  110. Refresh(false, true);
  111. }
  112. }
  113. catch(Exception e)
  114. {
  115. MessageWindow.ShowError("Something went wrong", e);
  116. }
  117. }
  118. }
  119. private bool Duplicate_Click(Button btn, CoreRow[] rows)
  120. {
  121. if (!rows.Any()) return false;
  122. SaveItems(rows.Select(x =>
  123. {
  124. var layout = x.ToObject<DigitalFormLayout>();
  125. var newLayout = CreateItem();
  126. newLayout.Code = layout.Code;
  127. newLayout.Description = layout.Description;
  128. newLayout.Type = layout.Type;
  129. newLayout.Layout = layout.Layout;
  130. return newLayout;
  131. }).ToArray());
  132. return true;
  133. }
  134. private bool AutoGenerate_Click(Button btn, CoreRow[] rows)
  135. {
  136. var menu = new ContextMenu();
  137. menu.AddItem("Desktop Layout", null, AddDesktop_Click);
  138. menu.AddItem("Mobile Layout", null, AddMobile_Click);
  139. menu.IsOpen = true;
  140. return false;
  141. }
  142. private BitmapImage? DesignImage(CoreRow? row)
  143. {
  144. return row != null ? design : null;
  145. }
  146. private void AddMobile_Click()
  147. {
  148. var item = CreateItem();
  149. item.Layout = DFLayout.GenerateAutoMobileLayout(GetVariables()).SaveLayout();
  150. item.Type = DFLayoutType.Mobile;
  151. if (EditItems(new[] { item }))
  152. {
  153. SaveItem(item);
  154. Refresh(false, true);
  155. DoChanged();
  156. }
  157. }
  158. private void AddDesktop_Click()
  159. {
  160. var item = CreateItem();
  161. item.Layout = DFLayout.GenerateAutoDesktopLayout(GetVariables()).SaveLayout();
  162. item.Type = DFLayoutType.Desktop;
  163. if (EditItems(new[] { item }))
  164. {
  165. SaveItem(item);
  166. Refresh(false, true);
  167. DoChanged();
  168. }
  169. }
  170. private DynamicVariableGrid? GetVariableGrid()
  171. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  172. as DynamicVariableGrid;
  173. private List<DigitalFormVariable> GetVariables()
  174. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  175. private void Design(DigitalFormLayout layout)
  176. {
  177. var variables = GetVariables();
  178. var newVariables = new List<DigitalFormVariable>();
  179. var form = new DynamicFormDesignWindow
  180. {
  181. Type = layout.Type
  182. };
  183. form.OnCreateVariable += (fieldType) =>
  184. {
  185. if (DynamicVariableUtils.CreateAndEdit(Item, GetVariables(), fieldType, out var variable))
  186. {
  187. newVariables.Add(variable);
  188. return variable;
  189. }
  190. return null;
  191. };
  192. /*form.OnEditVariable += (variable) =>
  193. {
  194. var properties = variable.CreateProperties();
  195. if (DynamicVariableUtils.EditProperties(Item, GetVariables(), properties.GetType(), properties))
  196. {
  197. variable.SaveProperties(properties);
  198. return true;
  199. }
  200. return false;
  201. };*/
  202. form.LoadLayout(layout, variables);
  203. form.Initialize();
  204. if (form.ShowDialog() == true)
  205. {
  206. layout.Layout = form.SaveLayout();
  207. SaveItem(layout);
  208. var grid = GetVariableGrid();
  209. if (grid is not null)
  210. {
  211. grid.SaveItems(newVariables.ToArray());
  212. grid.Refresh(false, true);
  213. }
  214. }
  215. }
  216. private bool DesignClick(CoreRow? row)
  217. {
  218. if (row == null)
  219. return false;
  220. Design(LoadItem(row));
  221. return false;
  222. }
  223. //public override void SaveItem(DigitalFormLayout item)
  224. //{
  225. // bool bActive = item.Active;
  226. // foreach (var other in Items.Where(x=>(x != item) && (x.Type == item.Type)))
  227. // {
  228. // if (item.Active)
  229. // {
  230. // if (other.Active)
  231. // other.Active = false;
  232. // }
  233. // else
  234. // bActive = bActive || other.Active;
  235. // }
  236. // if (!bActive)
  237. // item.Active = true;
  238. // base.SaveItem(item);
  239. //}
  240. protected override void DoDoubleClick(object sender)
  241. {
  242. DesignClick(SelectedRows.FirstOrDefault());
  243. }
  244. }