DynamicFormLayoutGrid.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 && row.FirstColumn >= 0)
  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. var style = cell.InnerCell.GetStyle();
  153. if (string.IsNullOrWhiteSpace(cell.Content) && style.Foreground == Color.Empty) continue;
  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. if(EditItems(new DigitalFormLayout[] { dfLayout }))
  214. {
  215. Refresh(false, true);
  216. }
  217. }
  218. catch(Exception e)
  219. {
  220. Logger.Send(LogType.Error, "", CoreUtils.FormatException(e));
  221. MessageBox.Show($"Error: {e.Message}");
  222. }
  223. }
  224. }
  225. private bool Duplicate_Click(Button btn, CoreRow[] rows)
  226. {
  227. if (!rows.Any()) return false;
  228. SaveItems(rows.Select(x =>
  229. {
  230. var layout = x.ToObject<DigitalFormLayout>();
  231. layout.ID = Guid.Empty;
  232. return layout;
  233. }).ToArray());
  234. return true;
  235. }
  236. private bool AutoGenerate_Click(Button btn, CoreRow[] rows)
  237. {
  238. var menu = new ContextMenu();
  239. menu.AddItem("Desktop Layout", null, AddDesktop_Click);
  240. menu.AddItem("Mobile Layout", null, AddMobile_Click);
  241. menu.IsOpen = true;
  242. return false;
  243. }
  244. private BitmapImage? DesignImage(CoreRow? row)
  245. {
  246. return row != null ? design : null;
  247. }
  248. private void AddMobile_Click()
  249. {
  250. var item = CreateItem();
  251. item.Layout = DFLayout.GenerateAutoMobileLayout(GetVariables()).SaveLayout();
  252. item.Type = DFLayoutType.Mobile;
  253. if (EditItems(new[] { item }))
  254. {
  255. SaveItem(item);
  256. Refresh(false, true);
  257. OnChanged?.Invoke(this);
  258. }
  259. }
  260. private void AddDesktop_Click()
  261. {
  262. var item = CreateItem();
  263. item.Layout = DFLayout.GenerateAutoDesktopLayout(GetVariables()).SaveLayout();
  264. item.Type = DFLayoutType.Desktop;
  265. if (EditItems(new[] { item }))
  266. {
  267. SaveItem(item);
  268. Refresh(false, true);
  269. OnChanged?.Invoke(this);
  270. }
  271. }
  272. private DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>? GetVariableGrid()
  273. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>)
  274. as DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>;
  275. private List<DigitalFormVariable> GetVariables()
  276. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  277. private void Design(DigitalFormLayout layout)
  278. {
  279. var variables = GetVariables();
  280. var newVariables = new List<DigitalFormVariable>();
  281. var form = new DynamicFormDesignWindow
  282. {
  283. Type = layout.Type
  284. };
  285. form.OnCreateVariable += (fieldType) =>
  286. {
  287. if (DynamicVariableUtils.CreateAndEdit(Item, GetVariables(), fieldType, out var variable))
  288. {
  289. newVariables.Add(variable);
  290. return variable;
  291. }
  292. return null;
  293. };
  294. /*form.OnEditVariable += (variable) =>
  295. {
  296. var properties = variable.CreateProperties();
  297. if (DynamicVariableUtils.EditProperties(Item, GetVariables(), properties.GetType(), properties))
  298. {
  299. variable.SaveProperties(properties);
  300. return true;
  301. }
  302. return false;
  303. };*/
  304. form.LoadLayout(layout, variables);
  305. form.Initialize();
  306. if (form.ShowDialog() == true)
  307. {
  308. layout.Layout = form.SaveLayout();
  309. SaveItem(layout);
  310. var grid = GetVariableGrid();
  311. if (grid is not null)
  312. {
  313. grid.SaveItems(newVariables.ToArray());
  314. grid.Refresh(false, true);
  315. }
  316. }
  317. }
  318. private bool DesignClick(CoreRow? row)
  319. {
  320. if (row == null)
  321. return false;
  322. Design(LoadItem(row));
  323. return false;
  324. }
  325. //public override void SaveItem(DigitalFormLayout item)
  326. //{
  327. // bool bActive = item.Active;
  328. // foreach (var other in Items.Where(x=>(x != item) && (x.Type == item.Type)))
  329. // {
  330. // if (item.Active)
  331. // {
  332. // if (other.Active)
  333. // other.Active = false;
  334. // }
  335. // else
  336. // bActive = bActive || other.Active;
  337. // }
  338. // if (!bActive)
  339. // item.Active = true;
  340. // base.SaveItem(item);
  341. //}
  342. protected override void DoDoubleClick(object sender)
  343. {
  344. DesignClick(SelectedRows.FirstOrDefault());
  345. }
  346. }
  347. }