DimensionsEditorControl.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using Syncfusion.Windows.Shared;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. namespace InABox.DynamicGrid
  13. {
  14. public class DimensionsEditorControl
  15. {
  16. public static BaseDynamicEditorControl? Create(DimensionsEditor editor)
  17. {
  18. var dimensionsType = editor.DimensionsType;
  19. var dimensions = dimensionsType.GetSuperclassDefinition(typeof(Dimensions<,>));
  20. if (dimensions == null) return null;
  21. var tLink = dimensions.GenericTypeArguments[0];
  22. var tUnit = dimensions.GenericTypeArguments[1];
  23. return Activator.CreateInstance(typeof(DimensionsEditorControl<,,>).MakeGenericType(dimensionsType, tLink, tUnit), editor) as BaseDynamicEditorControl;
  24. }
  25. }
  26. public class DimensionsEditorControl<TDimensions, TLink, TUnit> : DynamicEnclosedEditorControl<TDimensions, DimensionsEditor>
  27. where TDimensions : Dimensions<TLink, TUnit>, new()
  28. where TLink : DimensionUnitLink<TUnit>, new()
  29. where TUnit : DimensionUnit, new()
  30. {
  31. private Grid Grid;
  32. private ComboBox Combo;
  33. private DoubleTextBox Quantity;
  34. private DoubleTextBox Length;
  35. private DoubleTextBox Width;
  36. private DoubleTextBox Height;
  37. private DoubleTextBox Weight;
  38. private List<Tuple<string, TUnit>> Units;
  39. private DimensionsEditor Editor;
  40. public DimensionsEditorControl(DimensionsEditor editor)
  41. {
  42. Editor = editor;
  43. }
  44. public override int DesiredHeight()
  45. {
  46. return 25;
  47. }
  48. public override int DesiredWidth()
  49. {
  50. return int.MaxValue;
  51. }
  52. public override void SetColor(Color color)
  53. {
  54. Quantity.Background = new SolidColorBrush(color);
  55. Length.Background = new SolidColorBrush(color);
  56. Width.Background = new SolidColorBrush(color);
  57. Height.Background = new SolidColorBrush(color);
  58. Weight.Background = new SolidColorBrush(color);
  59. }
  60. public override void SetFocus()
  61. {
  62. Combo.Focus();
  63. }
  64. public override void Configure()
  65. {
  66. }
  67. protected override FrameworkElement CreateEditor()
  68. {
  69. Grid = new Grid
  70. {
  71. VerticalAlignment = VerticalAlignment.Stretch,
  72. HorizontalAlignment = HorizontalAlignment.Stretch
  73. };
  74. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(150, GridUnitType.Pixel) });
  75. Combo = new ComboBox
  76. {
  77. IsEnabled = Editor.AllowEditingUnit
  78. };
  79. Combo.SetValue(Grid.ColumnProperty, 0);
  80. Combo.SetValue(Grid.RowProperty, 0);
  81. Grid.Children.Add(Combo);
  82. var result = new Client<TUnit>()
  83. .Query(
  84. LookupFactory.DefineFilter<TUnit>(),
  85. LookupFactory.DefineColumns<TUnit>(),
  86. LookupFactory.DefineSort<TUnit>());
  87. Units = result.Rows.Select(row =>
  88. {
  89. var values = row.ToDictionary(new[] { "Display" });
  90. var display = LookupFactory.FormatLookup(typeof(TUnit), values, Array.Empty<string>());
  91. return new Tuple<string, TUnit>(display, row.ToObject<TUnit>());
  92. }).ToList();
  93. Combo.DisplayMemberPath = "Item1";
  94. Combo.SelectedValuePath = "Item2.ID";
  95. Combo.ItemsSource = Units;
  96. Combo.SelectionChanged += Combo_SelectionChanged;
  97. Quantity = AddDimension("Quantity: ", 1);
  98. Length = AddDimension("Length: ", 2);
  99. Width = AddDimension("Width: ", 3);
  100. Height = AddDimension("Height: ", 4);
  101. Weight = AddDimension("Weight: ", 5);
  102. return Grid;
  103. }
  104. private void UpdateColumn(int column, bool visible, DoubleTextBox box)
  105. {
  106. if (!visible)
  107. {
  108. box.Value = 0.0;
  109. }
  110. var columnDefinition = Grid.ColumnDefinitions[column * 2];
  111. if (visible)
  112. {
  113. columnDefinition.Width = new GridLength(1, GridUnitType.Star);
  114. }
  115. else
  116. {
  117. columnDefinition.Width = new GridLength(0);
  118. }
  119. columnDefinition = Grid.ColumnDefinitions[column * 2 - 1];
  120. if (visible)
  121. {
  122. columnDefinition.Width = GridLength.Auto;
  123. }
  124. else
  125. {
  126. columnDefinition.Width = new GridLength(0);
  127. }
  128. }
  129. private DoubleTextBox AddDimension(string name, int column)
  130. {
  131. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0) });
  132. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0) });
  133. var label = new Label
  134. {
  135. Content = name,
  136. VerticalAlignment = VerticalAlignment.Center,
  137. VerticalContentAlignment = VerticalAlignment.Center,
  138. Margin = new Thickness(5, 0, 0, 0)
  139. };
  140. label.SetValue(Grid.ColumnProperty, column * 2 - 1);
  141. var box = new DoubleTextBox
  142. {
  143. VerticalAlignment = VerticalAlignment.Stretch,
  144. VerticalContentAlignment = VerticalAlignment.Center,
  145. HorizontalAlignment = HorizontalAlignment.Stretch,
  146. HorizontalContentAlignment = HorizontalAlignment.Center,
  147. NumberDecimalDigits = 2,
  148. Margin = new Thickness(5, 0, 0, 0)
  149. };
  150. box.SetValue(Grid.ColumnProperty, column * 2);
  151. box.ValueChanged += Box_ValueChanged;
  152. Grid.Children.Add(label);
  153. Grid.Children.Add(box);
  154. return box;
  155. }
  156. private void Box_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  157. {
  158. CheckChanged();
  159. }
  160. private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  161. {
  162. if (Combo.SelectedValue is not Guid unitID) return;
  163. if (Combo.SelectedItem is not Tuple<string, TUnit> tuple) return;
  164. var unit = tuple.Item2;
  165. UpdateColumn(1, unit.HasQuantity, Quantity);
  166. UpdateColumn(2, unit.HasLength, Length);
  167. UpdateColumn(3, unit.HasWidth, Width);
  168. UpdateColumn(4, unit.HasHeight, Height);
  169. UpdateColumn(5, unit.HasWeight, Weight);
  170. foreach(var property in DatabaseSchema.Properties(typeof(TUnit)))
  171. {
  172. var value = property.Getter()(unit);
  173. OtherValues[$"{ColumnName}.Unit.{property.Name}"] = value;
  174. }
  175. CheckChanged();
  176. }
  177. public override object? GetValue(string property)
  178. {
  179. if (!property.StartsWith($"{ColumnName}.")) return null;
  180. property = property[(ColumnName.Length + 1)..];
  181. if (property == "Quantity") return Quantity.Value ?? 0.0;
  182. if (property == "Length") return Length.Value ?? 0.0;
  183. if (property == "Width") return Width.Value ?? 0.0;
  184. if (property == "Height") return Height.Value ?? 0.0;
  185. if (property == "Weight") return Weight.Value ?? 0.0;
  186. if (property == "Unit.ID") return Combo.SelectedValue is Guid guid ? guid : Guid.Empty;
  187. return null;
  188. }
  189. public override Dictionary<string, object?> GetValues()
  190. {
  191. var values = new Dictionary<string, object?>
  192. {
  193. {$"{ColumnName}.Quantity", Quantity.Value ?? 0.0},
  194. {$"{ColumnName}.Length", Length.Value ?? 0.0},
  195. {$"{ColumnName}.Width", Width.Value ?? 0.0},
  196. {$"{ColumnName}.Height", Height.Value ?? 0.0},
  197. {$"{ColumnName}.Weight", Weight.Value ?? 0.0},
  198. {$"{ColumnName}.Unit.ID", Combo.SelectedValue is Guid guid ? guid : Guid.Empty }
  199. };
  200. return values;
  201. }
  202. public override void SetValue(string property, object? value)
  203. {
  204. if (!property.StartsWith($"{ColumnName}.")) return;
  205. property = property[(ColumnName.Length + 1)..];
  206. if (value is null) return;
  207. if (property == "Unit.ID")
  208. {
  209. Combo.SelectedValue = value is Guid guid ? guid : Guid.Empty;
  210. return;
  211. }
  212. if (property == "Quantity") Quantity.Value = (double)value;
  213. if (property == "Length") Length.Value = (double)value;
  214. if (property == "Width") Width.Value = (double)value;
  215. if (property == "Height") Height.Value = (double)value;
  216. if (property == "Weight") Weight.Value = (double)value;
  217. }
  218. }
  219. }