IDynamicGridGridUIComponent.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using sun.tools.tree;
  4. using Syncfusion.Data;
  5. using Syncfusion.UI.Xaml.Grid;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics.CodeAnalysis;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Forms.VisualStyles;
  14. namespace InABox.DynamicGrid;
  15. internal interface IDynamicGridGridUIComponent<T>
  16. {
  17. IList<DynamicColumnBase> ColumnList { get; }
  18. int RowHeight { get; }
  19. }
  20. internal static class DynamicGridGridUIComponentExtension
  21. {
  22. public static Dictionary<int, double> CalculateColumnSizes<T>(this IDynamicGridGridUIComponent<T> component, double width)
  23. {
  24. var fAvailWidth = width - (SystemParameters.VerticalScrollBarWidth);
  25. //if (Data.Rows.Count * (DataGrid.RowHeight + 1) + DataGrid.HeaderRowHeight > height + 0.5F)
  26. //if (height < DataGrid.AutoScroller.VScrollBar.Maximum)
  27. // fAvailWidth -= (SystemParameters.VerticalScrollBarWidth + 0.75);
  28. double fCurWidth = 0.0F;
  29. var NumAutoCols = 0;
  30. var colWidths = new Dictionary<int, double>();
  31. for (var i = 0; i < component.ColumnList.Count; i++)
  32. {
  33. var col = component.ColumnList[i];
  34. if (col is DynamicImageColumn dic)
  35. {
  36. colWidths[i] = component.RowHeight;
  37. fCurWidth += colWidths[i];
  38. }
  39. else if (col is DynamicTextColumn dxc)
  40. {
  41. colWidths[i] = dxc.Width;
  42. if (dxc.Width != 0)
  43. fCurWidth += Math.Max(0.0F, dxc.Width);
  44. else
  45. NumAutoCols++;
  46. }
  47. else if (col is DynamicTemplateColumn dtc)
  48. {
  49. colWidths[i] = dtc.Width;
  50. if (dtc.Width != 0)
  51. fCurWidth += Math.Max(0.0F, dtc.Width);
  52. else
  53. NumAutoCols++;
  54. }
  55. else if (col is DynamicGridColumn dgc)
  56. {
  57. colWidths[i] = dgc.Width;
  58. if (dgc.Width != 0)
  59. fCurWidth += Math.Max(0.0F, dgc.Width);
  60. else
  61. NumAutoCols++;
  62. }
  63. }
  64. if (NumAutoCols > 0)
  65. {
  66. var fAutoWidth = (fAvailWidth - fCurWidth) / NumAutoCols;
  67. if (fAutoWidth < 100)
  68. fAutoWidth = 100;
  69. for (var i = 0; i < component.ColumnList.Count; i++)
  70. if (colWidths[i] == 0)
  71. colWidths[i] = fAutoWidth;
  72. }
  73. return colWidths;
  74. }
  75. public static int DesiredWidth<T>(this IDynamicGridGridUIComponent<T> component)
  76. {
  77. var result = 0;
  78. for (var i = 0; i < component.ColumnList.Count; i++)
  79. {
  80. var col = component.ColumnList[i];
  81. if (col is DynamicActionColumn)
  82. {
  83. result += (int)component.RowHeight;
  84. }
  85. else if (col is DynamicGridColumn)
  86. {
  87. var dgc = (DynamicGridColumn)col;
  88. result += dgc.Width > 0 ? dgc.Width : 300;
  89. }
  90. }
  91. return result;
  92. }
  93. public static bool CreateEditorColumn<T>(this IDynamicGridGridUIComponent<T> component, DynamicGridColumn column,
  94. [NotNullWhen(true)] out IDynamicGridEditorColumn? newcol,
  95. [NotNullWhen(true)] out IProperty? prop)
  96. where T : BaseObject
  97. {
  98. try
  99. {
  100. prop = DatabaseSchema.Property(typeof(T), column.ColumnName);
  101. }
  102. catch (Exception e)
  103. {
  104. Logger.Send(LogType.Error, ClientFactory.UserID,
  105. string.Format("Error constructing Column [{0}] : {1}\n{2}", column.ColumnName, e.Message, e.StackTrace));
  106. prop = null;
  107. }
  108. newcol = null;
  109. if (prop != null)
  110. {
  111. if (prop.Editor is IntegerEditor)
  112. newcol = new DynamicGridIntegerColumn<T>(column);
  113. else if (prop.Editor is CurrencyEditor)
  114. newcol = new DynamicGridCurrencyColumn<T>(column);
  115. else if (prop.Editor is DoubleEditor)
  116. newcol = new DynamicGridDoubleColumn<T>(column);
  117. else if (prop.Editor is DateTimeEditor)
  118. newcol = new DynamicGridDateTimeColumn<T>(column);
  119. else if (prop.Editor is DateEditor)
  120. newcol = new DynamicGridDateColumn<T>(column);
  121. else if (prop.Editor is TimeOfDayEditor)
  122. newcol = new DynamicGridTimeOfDayColumn<T>(column);
  123. else if (prop.Editor is TimestampEditor)
  124. newcol = new DynamicGridTimeStampColumn<T>(column);
  125. else if (prop.Editor is DurationEditor)
  126. newcol = new DynamicGridDurationColumn<T>(column);
  127. else if (prop.Editor is CheckBoxEditor)
  128. newcol = new DynamicGridCheckBoxColumn<T>(column);
  129. else if (prop.Editor is ColorEditor)
  130. newcol = new DynamicGridColorColumn<T>(column, column.Width, component.RowHeight);
  131. else if (prop.Editor is PopupEditor)
  132. newcol = new DynamicGridPopupColumn<T>(column);
  133. else if (prop.Editor is CodePopupEditor)
  134. newcol = new DynamicGridCodePopupColumn<T>(column);
  135. else if (prop.Editor is EnumLookupEditor)
  136. newcol = new DynamicGridEnumLookupColumn<T>(column);
  137. else if (prop.Editor is ComboLookupEditor)
  138. newcol = new DynamicGridComboLookupColumn<T>(column);
  139. else if (prop.Editor is LookupEditor)
  140. newcol = new DynamicGridLookupColumn<T>(column);
  141. else if (prop.Editor is MemoEditor)
  142. newcol = new DynamicGridMemoColumn<T>(column);
  143. else if (prop.Editor is CodeEditor)
  144. newcol = new DynamicGridCodeColumn<T>(column);
  145. else if (prop.Editor is UniqueCodeEditor)
  146. newcol = new DynamicGridUniqueCodeColumn<T>(column);
  147. else if (prop.Editor is TextBoxEditor)
  148. newcol = new DynamicGridTextBoxColumn<T>(column);
  149. return newcol is not null;
  150. }
  151. else
  152. {
  153. return false;
  154. }
  155. }
  156. private static bool FilterByPredicate(CoreRow row, string column, FilterPredicate predicate)
  157. {
  158. var value = row[column];
  159. var vStr = value?.ToString()?.ToLower() ?? "";
  160. var pValue = predicate.FilterValue;
  161. var pStr = pValue?.ToString()?.ToLower() ?? "";
  162. return predicate.FilterType switch
  163. {
  164. FilterType.Contains => vStr.Contains(pStr),
  165. FilterType.EndsWith => vStr.EndsWith(pStr),
  166. FilterType.Equals => vStr.Equals(pStr),
  167. FilterType.GreaterThan => vStr.CompareTo(pStr) > 0,
  168. FilterType.GreaterThanOrEqual => vStr.CompareTo(pStr) >= 0,
  169. FilterType.LessThan => vStr.CompareTo(pStr) < 0,
  170. FilterType.LessThanOrEqual => vStr.CompareTo(pStr) <= 0,
  171. FilterType.NotContains => !vStr.Contains(pStr),
  172. FilterType.NotEndsWith => !vStr.EndsWith(pStr),
  173. FilterType.NotEquals => !vStr.Equals(pStr),
  174. FilterType.NotStartsWith => !vStr.StartsWith(pStr),
  175. FilterType.StartsWith => vStr.StartsWith(pStr),
  176. _ => true,
  177. };
  178. }
  179. public static Func<CoreRow, bool>? ConvertColumnPredicates(DynamicGridColumn gridColumn, IEnumerable<FilterPredicate> predicates)
  180. {
  181. Func<CoreRow, bool>? rowPredicate = null;
  182. foreach (var predicate in predicates)
  183. {
  184. var p = (CoreRow row) => FilterByPredicate(row, gridColumn.ColumnName, predicate);
  185. if(rowPredicate is null)
  186. {
  187. rowPredicate = p;
  188. }
  189. else
  190. {
  191. var prevP = rowPredicate;
  192. if(predicate.PredicateType == PredicateType.And)
  193. {
  194. rowPredicate = r => prevP(r) && p(r);
  195. }
  196. else
  197. {
  198. rowPredicate = r => prevP(r) || p(r);
  199. }
  200. }
  201. }
  202. return rowPredicate;
  203. }
  204. }