DynamicFormLayoutGrid.cs 9.0 KB

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