IDynamicGridGridUIComponent.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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<T>
  17. {
  18. IList<DynamicColumnBase> ColumnList { get; }
  19. int RowHeight { get; }
  20. }
  21. internal static class DynamicGridGridUIComponentExtension
  22. {
  23. public static Dictionary<int, double> CalculateColumnSizes<T>(this IDynamicGridGridUIComponent<T> 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<T>(this IDynamicGridGridUIComponent<T> 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>() && x.IsGenericTypeDefinition && x.GetGenericArguments().Length == 1).Select(x =>
  100. {
  101. var def = x.GetSuperclassDefinition(typeof(DynamicGridEditorColumn<,,,>))!;
  102. return new KeyValuePair<Type, Type>(def.GenericTypeArguments[1], x);
  103. }).ToDictionary();
  104. return _editorColumnMap;
  105. }
  106. }
  107. public static bool CreateEditorColumn<T>(this IDynamicGridGridUIComponent<T> component, DynamicGridColumn column,
  108. [NotNullWhen(true)] out IDynamicGridEditorColumn? newcol,
  109. [NotNullWhen(true)] out IProperty? prop)
  110. where T : BaseObject
  111. {
  112. try
  113. {
  114. prop = DatabaseSchema.Property(typeof(T), column.ColumnName);
  115. }
  116. catch (Exception e)
  117. {
  118. Logger.Send(LogType.Error, ClientFactory.UserID,
  119. string.Format("Error constructing Column [{0}] : {1}\n{2}", column.ColumnName, e.Message, e.StackTrace));
  120. prop = null;
  121. }
  122. newcol = null;
  123. if (prop != null && EditorColumnMap.TryGetValue(prop.Editor.GetType(), out var colType))
  124. {
  125. newcol = Activator.CreateInstance(colType.MakeGenericType(typeof(T)), column) as IDynamicGridEditorColumn;
  126. if(newcol is IDynamicGridEditorColumn<T> gridCol)
  127. {
  128. gridCol.UpdateUIComponent(component);
  129. }
  130. return newcol is not null;
  131. }
  132. else
  133. {
  134. return false;
  135. }
  136. }
  137. private static bool FilterByPredicate(CoreRow row, string column, FilterPredicate predicate)
  138. {
  139. var value = row[column];
  140. var vStr = value?.ToString()?.ToLower() ?? "";
  141. var pValue = predicate.FilterValue;
  142. var pStr = pValue?.ToString()?.ToLower() ?? "";
  143. return predicate.FilterType switch
  144. {
  145. FilterType.Contains => vStr.Contains(pStr),
  146. FilterType.EndsWith => vStr.EndsWith(pStr),
  147. FilterType.Equals => vStr.Equals(pStr),
  148. FilterType.GreaterThan => vStr.CompareTo(pStr) > 0,
  149. FilterType.GreaterThanOrEqual => vStr.CompareTo(pStr) >= 0,
  150. FilterType.LessThan => vStr.CompareTo(pStr) < 0,
  151. FilterType.LessThanOrEqual => vStr.CompareTo(pStr) <= 0,
  152. FilterType.NotContains => !vStr.Contains(pStr),
  153. FilterType.NotEndsWith => !vStr.EndsWith(pStr),
  154. FilterType.NotEquals => !vStr.Equals(pStr),
  155. FilterType.NotStartsWith => !vStr.StartsWith(pStr),
  156. FilterType.StartsWith => vStr.StartsWith(pStr),
  157. _ => true,
  158. };
  159. }
  160. public static Func<CoreRow, bool>? ConvertColumnPredicates(DynamicGridColumn gridColumn, IEnumerable<FilterPredicate> predicates)
  161. {
  162. Func<CoreRow, bool>? rowPredicate = null;
  163. foreach (var predicate in predicates)
  164. {
  165. var p = (CoreRow row) => FilterByPredicate(row, gridColumn.ColumnName, predicate);
  166. if(rowPredicate is null)
  167. {
  168. rowPredicate = p;
  169. }
  170. else
  171. {
  172. var prevP = rowPredicate;
  173. if(predicate.PredicateType == PredicateType.And)
  174. {
  175. rowPredicate = r => prevP(r) && p(r);
  176. }
  177. else
  178. {
  179. rowPredicate = r => prevP(r) || p(r);
  180. }
  181. }
  182. }
  183. return rowPredicate;
  184. }
  185. }