DynamicFormLayoutGrid.cs 9.4 KB

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