DynamicFormLayoutGrid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.Threading;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media.Imaging;
  11. using InABox.Clients;
  12. using InABox.Core;
  13. using InABox.DynamicGrid;
  14. using InABox.Scripting;
  15. using InABox.Wpf;
  16. using InABox.WPF;
  17. using Microsoft.Win32;
  18. using Org.BouncyCastle.Asn1.Mozilla;
  19. using Syncfusion.Windows.Shared;
  20. using UnderlineType = InABox.Core.UnderlineType;
  21. namespace InABox.DynamicGrid;
  22. public class DynamicFormLayoutGrid : DynamicDataGrid<DigitalFormLayout>
  23. {
  24. private readonly BitmapImage design = Wpf.Resources.design.AsBitmapImage();
  25. private readonly BitmapImage tick = Wpf.Resources.tick.AsBitmapImage();
  26. public DigitalForm? Form { get; set; }
  27. public DynamicVariableGrid? VariableGrid { get; set; }
  28. private DigitalFormLayout[]? _layouts;
  29. public DigitalFormLayout[] Layouts
  30. {
  31. get
  32. {
  33. _layouts ??= Data.ToArray<DigitalFormLayout>();
  34. return _layouts;
  35. }
  36. }
  37. protected override void Init()
  38. {
  39. base.Init();
  40. //AddButton("Design", PRSDesktop.Resources.design.AsBitmapImage(), DesignClick);
  41. HiddenColumns.Add(x => x.Active);
  42. HiddenColumns.Add(x => x.Description);
  43. HiddenColumns.Add(x => x.Type);
  44. HiddenColumns.Add(x => x.Layout);
  45. HiddenColumns.Add(x => x.Form.Code);
  46. HiddenColumns.Add(x => x.Form.Description);
  47. if (Security.CanEdit<DigitalFormLayout>())
  48. {
  49. ActionColumns.Add(new DynamicTickColumn<DigitalFormLayout, bool>(x => x.Active, tick, tick, null,
  50. action: ActiveClick)
  51. {
  52. ToolTip = ActiveToolTip
  53. });
  54. }
  55. ActionColumns.Add(new DynamicImageColumn(DesignImage, DesignClick));
  56. AddEditButton("Auto Generate", null, AutoGenerate_Click);
  57. AddEditButton("Duplicate", null, Duplicate_Click);
  58. }
  59. private FrameworkElement? ActiveToolTip(DynamicActionColumn column, CoreRow? row)
  60. {
  61. if(row is null)
  62. {
  63. return column.TextToolTip("Active");
  64. }
  65. var active = row.Get<DigitalFormLayout, bool>(x => x.Active);
  66. var type = row.Get<DigitalFormLayout, DFLayoutType>(x => x.Type);
  67. if (active)
  68. {
  69. return column.TextToolTip($"This is the active layout for {type}.");
  70. }
  71. else
  72. {
  73. return column.TextToolTip($"This layout is inactive.");
  74. }
  75. }
  76. private bool ActiveClick(CoreRow? row)
  77. {
  78. if (row is null) return false;
  79. var item = row.ToObject<DigitalFormLayout>();
  80. var allItems = Data.ToArray<DigitalFormLayout>();
  81. var toSave = new List<DigitalFormLayout>();
  82. item.Active = !item.Active;
  83. toSave.Add(item);
  84. if (item.Active)
  85. {
  86. foreach(var otherItem in allItems)
  87. {
  88. if (otherItem.ID == item.ID) continue;
  89. if(otherItem.Type == item.Type)
  90. {
  91. otherItem.Active = false;
  92. toSave.Add(otherItem);
  93. }
  94. }
  95. }
  96. Client.Save(toSave, "Changed Active flag");
  97. return true;
  98. }
  99. protected override void DoReconfigure(DynamicGridOptions options)
  100. {
  101. base.DoReconfigure(options);
  102. options.RecordCount = true;
  103. options.ImportData = true;
  104. }
  105. private DFLayout LoadLayoutFromSpreadsheet(ISpreadsheet spreadsheet)
  106. {
  107. return DigitalFormUtils.LoadLayout(spreadsheet);
  108. }
  109. protected override void DoImport()
  110. {
  111. var dialog = new OpenFileDialog();
  112. dialog.Filter = "Excel Spreadsheet (.xlsx)|*.xlsx";
  113. if (dialog.ShowDialog() == true)
  114. {
  115. try
  116. {
  117. DFLayout layout;
  118. Dictionary<String, String> variablegroups = new Dictionary<string, string>();
  119. using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  120. {
  121. layout = LoadLayoutFromSpreadsheet(new Spreadsheet(fs));
  122. }
  123. var dfLayout = CreateItem();
  124. dfLayout.Description = $"Imported From {Path.GetFileName(dialog.FileName)}";
  125. dfLayout.Layout = layout.SaveLayout();
  126. if(EditItems(new DigitalFormLayout[] { dfLayout }))
  127. {
  128. var newVariables = new List<DigitalFormVariable>();
  129. String group = "";
  130. foreach (var element in layout.Elements)
  131. {
  132. if (element is DFLayoutHeader header)
  133. {
  134. group = header.Header;
  135. }
  136. else if (element is DFLayoutField field)
  137. {
  138. var variable = new DigitalFormVariable();
  139. variable.SetFieldType(field.GetType());
  140. variable.SaveProperties(field.GetProperties());
  141. variable.Group = group;
  142. variable.Code = field.Name;
  143. variable.Description = field.Name;
  144. newVariables.Add(variable);
  145. }
  146. }
  147. if(newVariables.Count > 0)
  148. {
  149. if (VariableGrid is not null)
  150. {
  151. var save = new List<DigitalFormVariable>();
  152. foreach(var newVariable in newVariables)
  153. {
  154. var variable = VariableGrid.GetVariable(newVariable.Code);
  155. if(variable is not null)
  156. {
  157. if(variable.FieldType() != newVariable.FieldType())
  158. {
  159. MessageBox.Show($"Variable [{newVariable.Code}] already exists with a different type!");
  160. }
  161. }
  162. else
  163. {
  164. save.Add(newVariable);
  165. }
  166. }
  167. VariableGrid.SaveItems(save.ToArray());
  168. VariableGrid.Refresh(false, true);
  169. }
  170. }
  171. Refresh(false, true);
  172. }
  173. }
  174. catch(Exception e)
  175. {
  176. MessageWindow.ShowError("Something went wrong", e);
  177. }
  178. }
  179. }
  180. private bool Duplicate_Click(Button btn, CoreRow[] rows)
  181. {
  182. if (rows.Length == 0) return false;
  183. SaveItems(rows.ToArray(x =>
  184. {
  185. var layout = x.ToObject<DigitalFormLayout>();
  186. var newLayout = CreateItem();
  187. newLayout.Description = layout.Description;
  188. newLayout.Type = layout.Type;
  189. newLayout.Layout = layout.Layout;
  190. return newLayout;
  191. }));
  192. return true;
  193. }
  194. private bool AutoGenerate_Click(Button btn, CoreRow[] rows)
  195. {
  196. var menu = new ContextMenu();
  197. menu.AddItem("Desktop Layout", null, AddDesktop_Click);
  198. menu.AddItem("Mobile Layout", null, AddMobile_Click);
  199. menu.IsOpen = true;
  200. return false;
  201. }
  202. private BitmapImage? DesignImage(CoreRow? row)
  203. {
  204. return row != null ? design : null;
  205. }
  206. private void AddMobile_Click()
  207. {
  208. var item = CreateItem();
  209. item.Layout = DFLayout.GenerateAutoMobileLayout(GetVariables()).SaveLayout();
  210. item.Type = DFLayoutType.Mobile;
  211. if (EditItems(new[] { item }))
  212. {
  213. SaveItem(item);
  214. Refresh(false, true);
  215. DoChanged();
  216. }
  217. }
  218. private void AddDesktop_Click()
  219. {
  220. var item = CreateItem();
  221. item.Layout = DFLayout.GenerateAutoDesktopLayout(GetVariables()).SaveLayout();
  222. item.Type = DFLayoutType.Desktop;
  223. if (EditItems(new[] { item }))
  224. {
  225. SaveItem(item);
  226. Refresh(false, true);
  227. DoChanged();
  228. }
  229. }
  230. private IList<DigitalFormVariable> GetVariables()
  231. => VariableGrid?.Variables ?? [];
  232. private void Design(DigitalFormLayout layout)
  233. {
  234. if (Form is null) return;
  235. var variables = GetVariables().ToList();
  236. var newVariables = new List<DigitalFormVariable>();
  237. var form = new DynamicFormDesignWindow
  238. {
  239. Type = layout.Type,
  240. ReadOnly = !Security.CanEdit<DigitalFormLayout>()
  241. };
  242. form.OnCreateVariable += (fieldType) =>
  243. {
  244. if (DynamicVariableUtils.CreateAndEdit(Form, variables, fieldType, out var variable))
  245. {
  246. newVariables.Add(variable);
  247. variables.Add(variable);
  248. return variable;
  249. }
  250. return null;
  251. };
  252. /*form.OnEditVariable += (variable) =>
  253. {
  254. var properties = variable.CreateProperties();
  255. if (DynamicVariableUtils.EditProperties(Item, GetVariables(), properties.GetType(), properties))
  256. {
  257. variable.SaveProperties(properties);
  258. return true;
  259. }
  260. return false;
  261. };*/
  262. form.LoadLayout(layout, variables);
  263. form.Initialize();
  264. if (form.ShowDialog() == true)
  265. {
  266. layout.Layout = form.SaveLayout();
  267. SaveItem(layout);
  268. if (VariableGrid is not null)
  269. {
  270. VariableGrid.SaveItems(newVariables);
  271. VariableGrid.Refresh(false, true);
  272. }
  273. Refresh(false, true);
  274. }
  275. }
  276. private bool DesignClick(CoreRow? row)
  277. {
  278. if (row == null)
  279. return false;
  280. Design(row.ToObject<DigitalFormLayout>());
  281. return false;
  282. }
  283. protected override bool CanCreateItems()
  284. {
  285. return base.CanCreateItems() && Form is not null;
  286. }
  287. public override DigitalFormLayout CreateItem()
  288. {
  289. var item = base.CreateItem();
  290. item.Form.ID = Form?.ID ?? Guid.Empty;
  291. return item;
  292. }
  293. protected override void DoDoubleClick(object sender, DynamicGridCellClickEventArgs args)
  294. {
  295. DesignClick(SelectedRows.FirstOrDefault());
  296. }
  297. protected override void OnAfterRefresh()
  298. {
  299. base.OnAfterRefresh();
  300. _layouts = null;
  301. }
  302. protected override void Reload(Filters<DigitalFormLayout> criteria, Columns<DigitalFormLayout> columns, ref SortOrder<DigitalFormLayout>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
  303. {
  304. if(Form is null)
  305. {
  306. criteria.Add(Filter.None<DigitalFormLayout>());
  307. }
  308. else
  309. {
  310. criteria.Add(Filter<DigitalFormLayout>.Where(x => x.Form.ID).IsEqualTo(Form.ID));
  311. }
  312. base.Reload(criteria, columns, ref sort, token, action);
  313. }
  314. }