DynamicFormLayoutGrid.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media.Imaging;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.Scripting;
  12. using InABox.WPF;
  13. using Microsoft.Win32;
  14. using Org.BouncyCastle.Asn1.Mozilla;
  15. using Syncfusion.UI.Xaml.Spreadsheet;
  16. using Syncfusion.Windows.Shared;
  17. using UnderlineType = InABox.Core.UnderlineType;
  18. namespace InABox.DynamicGrid
  19. {
  20. public static class FormLayoutImporter
  21. {
  22. private class Cell
  23. {
  24. public string Content { get; set; }
  25. public int Row { get; set; }
  26. public int Column { get; set; }
  27. public int RowSpan { get; set; } = 1;
  28. public int ColumnSpan { get; set; } = 1;
  29. public ICell InnerCell { get; set; }
  30. public Cell(int row, int column, string content, ICell cell)
  31. {
  32. Row = row;
  33. Column = column;
  34. Content = content;
  35. InnerCell = cell;
  36. }
  37. }
  38. private static void DeleteColumn(List<Cell> cells, int column)
  39. {
  40. foreach(var cell in cells)
  41. {
  42. if(cell.Column <= column && cell.Column + cell.ColumnSpan - 1 >= column)
  43. {
  44. --cell.ColumnSpan;
  45. }
  46. else if(cell.Column > column)
  47. {
  48. --cell.Column;
  49. }
  50. }
  51. cells.RemoveAll(x => x.ColumnSpan < 0);
  52. }
  53. private static List<Cell> GetCells(ISheet sheet)
  54. {
  55. var grid = new Dictionary<int, Dictionary<int, Cell>>();
  56. for (int rowIdx = sheet.FirstRow; rowIdx <= sheet.LastRow; ++rowIdx)
  57. {
  58. var row = sheet.GetRow(rowIdx);
  59. if (row is not null)
  60. {
  61. var rowCells = new Dictionary<int, Cell>();
  62. for (int colIdx = row.FirstColumn; colIdx <= row.LastColumn; ++colIdx)
  63. {
  64. var cell = row.GetCell(colIdx);
  65. if (cell is not null)
  66. {
  67. rowCells.Add(colIdx, new Cell(rowIdx, colIdx, cell.GetValue(), cell));
  68. }
  69. }
  70. grid.Add(rowIdx, rowCells);
  71. }
  72. }
  73. foreach (var region in sheet.GetMergedCells())
  74. {
  75. for (int r = region.FirstRow; r <= region.LastRow; ++r)
  76. {
  77. if (!grid.TryGetValue(r, out var row)) continue;
  78. for (int c = region.FirstColumn; c <= region.LastColumn; ++c)
  79. {
  80. if ((r - region.FirstRow) + (c - region.FirstColumn) != 0)
  81. {
  82. row.Remove(c);
  83. }
  84. }
  85. if (row.Count == 0)
  86. {
  87. grid.Remove(r);
  88. }
  89. }
  90. if (grid.TryGetValue(region.FirstRow, out var cRow) && cRow.TryGetValue(region.FirstColumn, out var cCell))
  91. {
  92. cCell.RowSpan = region.LastRow - region.FirstRow + 1;
  93. cCell.ColumnSpan = region.LastColumn - region.FirstColumn + 1;
  94. }
  95. }
  96. var cells = new List<Cell>();
  97. foreach (var row in grid.Values)
  98. {
  99. foreach (var cell in row.Values)
  100. {
  101. cells.Add(cell);
  102. }
  103. }
  104. return cells;
  105. }
  106. public static DFLayout LoadLayout(ISpreadsheet spreadsheet)
  107. {
  108. var sheet = spreadsheet.GetSheet(0);
  109. var cells = GetCells(sheet);
  110. int firstRow = int.MaxValue;
  111. int lastRow = 0;
  112. int firstCol = int.MaxValue;
  113. int lastCol = 0;
  114. foreach (var cell in cells)
  115. {
  116. firstCol = Math.Min(cell.Column, firstCol);
  117. lastCol = Math.Max(cell.Column + cell.ColumnSpan - 1, lastCol);
  118. firstRow = Math.Min(cell.Row, firstRow);
  119. lastRow = Math.Max(cell.Row + cell.RowSpan - 1, lastRow);
  120. }
  121. var layout = new DFLayout();
  122. var columnWidths = new Dictionary<int, float>();
  123. var colOffset = 0;
  124. for (int col = firstCol; col <= lastCol; ++col)
  125. {
  126. var width = sheet.GetColumnWidth(col);
  127. if(width == float.MinValue)
  128. {
  129. layout.ColumnWidths.Add("10*");
  130. }
  131. else if(width <= 0f)
  132. {
  133. DeleteColumn(cells, col);
  134. }
  135. else
  136. {
  137. layout.ColumnWidths.Add($"{width}*");
  138. }
  139. }
  140. /*var rowHeights = new Dictionary<int, float>();
  141. for (int row = firstRow; row <= lastRow; ++row)
  142. {
  143. rowHeights[row] = sheet.GetRowHeight(row);
  144. }
  145. var totalHeight = rowHeights.Values.Sum();*/
  146. for(int row = firstRow; row <= lastRow; ++row)
  147. {
  148. layout.RowHeights.Add("Auto");
  149. }
  150. foreach(var cell in cells)
  151. {
  152. //if (string.IsNullOrWhiteSpace(cell.Content)) continue;
  153. var style = cell.InnerCell.GetStyle();
  154. var font = style.Font;
  155. layout.Elements.Add(new DFLayoutLabel
  156. {
  157. Caption = cell.Content,
  158. Row = cell.Row - firstRow + 1,
  159. Column = cell.Column - firstCol + 1 - colOffset,
  160. RowSpan = cell.RowSpan,
  161. ColumnSpan = cell.ColumnSpan,
  162. Style = new DFLayoutTextStyle
  163. {
  164. FontSize = font.FontSize,
  165. IsItalic = font.Italic,
  166. IsBold = font.Bold,
  167. Underline = font.Underline switch
  168. {
  169. Scripting.UnderlineType.None => UnderlineType.None,
  170. Scripting.UnderlineType.Single or Scripting.UnderlineType.SingleAccounting => UnderlineType.Single,
  171. Scripting.UnderlineType.Double or Scripting.UnderlineType.DoubleAccounting => UnderlineType.Double,
  172. _ => UnderlineType.None
  173. },
  174. BackgroundColour = style.Foreground,
  175. ForegroundColour = font.Colour
  176. }
  177. });
  178. }
  179. return layout;
  180. }
  181. }
  182. public abstract class DynamicFormLayoutGrid<T> : DynamicOneToManyGrid<DigitalForm, DigitalFormLayout> where T : Entity, IRemotable, IPersistent, new()
  183. {
  184. private readonly BitmapImage design = Properties.Resources.design.AsBitmapImage();
  185. public DynamicFormLayoutGrid()
  186. {
  187. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.ImportData);
  188. ActionColumns.Add(new DynamicImageColumn(DesignImage, DesignClick));
  189. //AddButton("Design", PRSDesktop.Resources.design.AsBitmapImage(), DesignClick);
  190. HiddenColumns.Add(x => x.Layout);
  191. AddButton("Auto Generate", null, AutoGenerate_Click);
  192. AddButton("Duplicate", null, Duplicate_Click);
  193. }
  194. private DFLayout LoadLayoutFromSpreadsheet(ISpreadsheet spreadsheet)
  195. {
  196. return FormLayoutImporter.LoadLayout(spreadsheet);
  197. }
  198. protected override void DoImport()
  199. {
  200. var dialog = new OpenFileDialog();
  201. dialog.Filter = "Excel Spreadsheet (.xlsx)|*.xlsx";
  202. if (dialog.ShowDialog() == true)
  203. {
  204. try
  205. {
  206. DFLayout layout;
  207. using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  208. {
  209. layout = LoadLayoutFromSpreadsheet(new Spreadsheet(fs));
  210. }
  211. var dfLayout = CreateItem();
  212. dfLayout.Layout = layout.SaveLayout();
  213. SaveItem(dfLayout);
  214. Refresh(false, true);
  215. }
  216. catch(Exception e)
  217. {
  218. Logger.Send(LogType.Error, "", CoreUtils.FormatException(e));
  219. MessageBox.Show($"Error: {e.Message}");
  220. }
  221. }
  222. }
  223. private bool Duplicate_Click(Button btn, CoreRow[] rows)
  224. {
  225. if (!rows.Any()) return false;
  226. SaveItems(rows.Select(x =>
  227. {
  228. var layout = x.ToObject<DigitalFormLayout>();
  229. layout.ID = Guid.Empty;
  230. return layout;
  231. }).ToArray());
  232. return true;
  233. }
  234. private bool AutoGenerate_Click(Button btn, CoreRow[] rows)
  235. {
  236. var menu = new ContextMenu();
  237. menu.AddItem("Desktop Layout", null, AddDesktop_Click);
  238. menu.AddItem("Mobile Layout", null, AddMobile_Click);
  239. menu.IsOpen = true;
  240. return false;
  241. }
  242. private BitmapImage? DesignImage(CoreRow? row)
  243. {
  244. return row != null ? design : null;
  245. }
  246. private void AddMobile_Click()
  247. {
  248. var item = CreateItem();
  249. item.Layout = DFLayout.GenerateAutoMobileLayout(GetVariables()).SaveLayout();
  250. item.Type = DFLayoutType.Mobile;
  251. if (EditItems(new[] { item }))
  252. {
  253. SaveItem(item);
  254. Refresh(false, true);
  255. OnChanged?.Invoke(this);
  256. }
  257. }
  258. private void AddDesktop_Click()
  259. {
  260. var item = CreateItem();
  261. item.Layout = DFLayout.GenerateAutoDesktopLayout(GetVariables()).SaveLayout();
  262. item.Type = DFLayoutType.Desktop;
  263. if (EditItems(new[] { item }))
  264. {
  265. SaveItem(item);
  266. Refresh(false, true);
  267. OnChanged?.Invoke(this);
  268. }
  269. }
  270. private DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>? GetVariableGrid()
  271. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>)
  272. as DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>;
  273. private List<DigitalFormVariable> GetVariables()
  274. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  275. private void Design(DigitalFormLayout layout)
  276. {
  277. var variables = GetVariables();
  278. var newVariables = new List<DigitalFormVariable>();
  279. var form = new DynamicFormDesignWindow
  280. {
  281. Type = layout.Type
  282. };
  283. form.OnCreateVariable += (fieldType) =>
  284. {
  285. if (DynamicVariableUtils.CreateAndEdit(Item, GetVariables(), fieldType, out var variable))
  286. {
  287. newVariables.Add(variable);
  288. return variable;
  289. }
  290. return null;
  291. };
  292. /*form.OnEditVariable += (variable) =>
  293. {
  294. var properties = variable.CreateProperties();
  295. if (DynamicVariableUtils.EditProperties(Item, GetVariables(), properties.GetType(), properties))
  296. {
  297. variable.SaveProperties(properties);
  298. return true;
  299. }
  300. return false;
  301. };*/
  302. form.LoadLayout(layout, variables);
  303. form.Initialize();
  304. if (form.ShowDialog() == true)
  305. {
  306. layout.Layout = form.SaveLayout();
  307. SaveItem(layout);
  308. var grid = GetVariableGrid();
  309. if (grid is not null)
  310. {
  311. grid.SaveItems(newVariables.ToArray());
  312. grid.Refresh(false, true);
  313. }
  314. }
  315. }
  316. private bool DesignClick(CoreRow? row)
  317. {
  318. if (row == null)
  319. return false;
  320. Design(LoadItem(row));
  321. return false;
  322. }
  323. //public override void SaveItem(DigitalFormLayout item)
  324. //{
  325. // bool bActive = item.Active;
  326. // foreach (var other in Items.Where(x=>(x != item) && (x.Type == item.Type)))
  327. // {
  328. // if (item.Active)
  329. // {
  330. // if (other.Active)
  331. // other.Active = false;
  332. // }
  333. // else
  334. // bActive = bActive || other.Active;
  335. // }
  336. // if (!bActive)
  337. // item.Active = true;
  338. // base.SaveItem(item);
  339. //}
  340. protected override void DoDoubleClick(object sender)
  341. {
  342. DesignClick(SelectedRows.FirstOrDefault());
  343. }
  344. }
  345. }