DynamicFormLayoutGrid.cs 8.8 KB

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