DynamicFormLayoutGrid.cs 9.2 KB

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