DynamicFormLayoutGrid.cs 8.9 KB

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