DynamicFormLayoutGrid.cs 9.5 KB

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