IDynamicGridGridUIComponent.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using ICSharpCode.AvalonEdit.Indentation;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using sun.tools.tree;
  5. using Syncfusion.Data;
  6. using Syncfusion.UI.Xaml.Grid;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics.CodeAnalysis;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Forms.VisualStyles;
  15. namespace InABox.DynamicGrid;
  16. public interface IDynamicGridGridUIComponent
  17. {
  18. IList<DynamicColumnBase> ColumnList { get; }
  19. int RowHeight { get; }
  20. }
  21. internal static class DynamicGridGridUIComponentExtension
  22. {
  23. public static Dictionary<int, double> CalculateColumnSizes(this IDynamicGridGridUIComponent component, double width)
  24. {
  25. var fAvailWidth = width - (SystemParameters.VerticalScrollBarWidth);
  26. //if (Data.Rows.Count * (DataGrid.RowHeight + 1) + DataGrid.HeaderRowHeight > height + 0.5F)
  27. //if (height < DataGrid.AutoScroller.VScrollBar.Maximum)
  28. // fAvailWidth -= (SystemParameters.VerticalScrollBarWidth + 0.75);
  29. double fCurWidth = 0.0F;
  30. var NumAutoCols = 0;
  31. var colWidths = new Dictionary<int, double>();
  32. for (var i = 0; i < component.ColumnList.Count; i++)
  33. {
  34. var col = component.ColumnList[i];
  35. if (col is DynamicImageColumn dic)
  36. {
  37. colWidths[i] = component.RowHeight;
  38. fCurWidth += colWidths[i];
  39. }
  40. else if (col is DynamicTextColumn dxc)
  41. {
  42. colWidths[i] = dxc.Width;
  43. if (dxc.Width != 0)
  44. fCurWidth += Math.Max(0.0F, dxc.Width);
  45. else
  46. NumAutoCols++;
  47. }
  48. else if (col is DynamicTemplateColumn dtc)
  49. {
  50. colWidths[i] = dtc.Width;
  51. if (dtc.Width != 0)
  52. fCurWidth += Math.Max(0.0F, dtc.Width);
  53. else
  54. NumAutoCols++;
  55. }
  56. else if (col is DynamicGridColumn dgc)
  57. {
  58. colWidths[i] = dgc.Width;
  59. if (dgc.Width != 0)
  60. fCurWidth += Math.Max(0.0F, dgc.Width);
  61. else
  62. NumAutoCols++;
  63. }
  64. }
  65. if (NumAutoCols > 0)
  66. {
  67. var fAutoWidth = (fAvailWidth - fCurWidth) / NumAutoCols;
  68. if (fAutoWidth < 100)
  69. fAutoWidth = 100;
  70. for (var i = 0; i < component.ColumnList.Count; i++)
  71. if (colWidths[i] == 0)
  72. colWidths[i] = fAutoWidth;
  73. }
  74. return colWidths;
  75. }
  76. public static int DesiredWidth(this IDynamicGridGridUIComponent component)
  77. {
  78. var result = 0;
  79. for (var i = 0; i < component.ColumnList.Count; i++)
  80. {
  81. var col = component.ColumnList[i];
  82. if (col is DynamicActionColumn)
  83. {
  84. result += (int)component.RowHeight;
  85. }
  86. else if (col is DynamicGridColumn)
  87. {
  88. var dgc = (DynamicGridColumn)col;
  89. result += dgc.Width > 0 ? dgc.Width : 300;
  90. }
  91. }
  92. return result;
  93. }
  94. private static Dictionary<Type, Type>? _editorColumnMap;
  95. private static Dictionary<Type, Type> EditorColumnMap
  96. {
  97. get
  98. {
  99. _editorColumnMap ??= CoreUtils.Entities.Where(x => x.IsClass && x.HasInterface<IDynamicGridEditorColumn>()).Select(x =>
  100. {
  101. var def = x.GetSuperclassDefinition(typeof(DynamicGridEditorColumn<,,>))!;
  102. return new KeyValuePair<Type, Type>(def.GenericTypeArguments[0], x);
  103. }).ToDictionary();
  104. return _editorColumnMap;
  105. }
  106. }
  107. public static bool CreateEditorColumn<T>(this IDynamicGridGridUIComponent component, DynamicGridColumn column,
  108. [NotNullWhen(true)] out IDynamicGridEditorColumn? newcol)
  109. {
  110. newcol = null;
  111. if (EditorColumnMap.TryGetValue(column.Editor.GetType(), out var colType))
  112. {
  113. if(colType.GetInterfaceDefinition(typeof(IDynamicGridEditorColumn<>)) is Type genericInterface)
  114. {
  115. newcol = Activator.CreateInstance(colType.MakeGenericType(typeof(T)), column) as IDynamicGridEditorColumn;
  116. }
  117. else
  118. {
  119. newcol = Activator.CreateInstance(colType, column) as IDynamicGridEditorColumn;
  120. }
  121. if(newcol is IDynamicGridEditorColumn gridCol)
  122. {
  123. gridCol.UpdateUIComponent(component);
  124. }
  125. return newcol is not null;
  126. }
  127. else
  128. {
  129. return false;
  130. }
  131. }
  132. public static bool CreateEditorColumn(this IDynamicGridGridUIComponent component, DynamicGridColumn column,
  133. [NotNullWhen(true)] out IDynamicGridEditorColumn? newcol)
  134. {
  135. newcol = null;
  136. if (EditorColumnMap.TryGetValue(column.Editor.GetType(), out var colType))
  137. {
  138. if(colType.GetInterfaceDefinition(typeof(IDynamicGridEditorColumn<>)) is not Type genericInterface)
  139. {
  140. newcol = Activator.CreateInstance(colType, column) as IDynamicGridEditorColumn;
  141. }
  142. if(newcol is IDynamicGridEditorColumn gridCol)
  143. {
  144. gridCol.UpdateUIComponent(component);
  145. }
  146. return newcol is not null;
  147. }
  148. else
  149. {
  150. return false;
  151. }
  152. }
  153. private static bool FilterByPredicate(CoreRow row, string column, FilterPredicate predicate)
  154. {
  155. var value = row[column];
  156. var vStr = value?.ToString()?.ToLower() ?? "";
  157. var pValue = predicate.FilterValue;
  158. var pStr = pValue?.ToString()?.ToLower() ?? "";
  159. return predicate.FilterType switch
  160. {
  161. FilterType.Contains => vStr.Contains(pStr),
  162. FilterType.EndsWith => vStr.EndsWith(pStr),
  163. FilterType.Equals => vStr.Equals(pStr),
  164. FilterType.GreaterThan => vStr.CompareTo(pStr) > 0,
  165. FilterType.GreaterThanOrEqual => vStr.CompareTo(pStr) >= 0,
  166. FilterType.LessThan => vStr.CompareTo(pStr) < 0,
  167. FilterType.LessThanOrEqual => vStr.CompareTo(pStr) <= 0,
  168. FilterType.NotContains => !vStr.Contains(pStr),
  169. FilterType.NotEndsWith => !vStr.EndsWith(pStr),
  170. FilterType.NotEquals => !vStr.Equals(pStr),
  171. FilterType.NotStartsWith => !vStr.StartsWith(pStr),
  172. FilterType.StartsWith => vStr.StartsWith(pStr),
  173. _ => true,
  174. };
  175. }
  176. public static Func<CoreRow, bool>? ConvertColumnPredicates(DynamicGridColumn gridColumn, IEnumerable<FilterPredicate> predicates)
  177. {
  178. Func<CoreRow, bool>? rowPredicate = null;
  179. foreach (var predicate in predicates)
  180. {
  181. var p = (CoreRow row) => FilterByPredicate(row, gridColumn.ColumnName, predicate);
  182. if(rowPredicate is null)
  183. {
  184. rowPredicate = p;
  185. }
  186. else
  187. {
  188. var prevP = rowPredicate;
  189. if(predicate.PredicateType == PredicateType.And)
  190. {
  191. rowPredicate = r => prevP(r) && p(r);
  192. }
  193. else
  194. {
  195. rowPredicate = r => prevP(r) || p(r);
  196. }
  197. }
  198. }
  199. return rowPredicate;
  200. }
  201. }