DimensionsEditorControl.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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>
  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. protected override FrameworkElement CreateEditor()
  65. {
  66. Grid = new Grid
  67. {
  68. VerticalAlignment = VerticalAlignment.Stretch,
  69. HorizontalAlignment = HorizontalAlignment.Stretch
  70. };
  71. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(150, GridUnitType.Pixel) });
  72. Combo = new ComboBox
  73. {
  74. IsEnabled = Editor.AllowEditingUnit
  75. };
  76. Combo.SetValue(Grid.ColumnProperty, 0);
  77. Combo.SetValue(Grid.RowProperty, 0);
  78. Grid.Children.Add(Combo);
  79. var result = new Client<TUnit>()
  80. .Query(
  81. LookupFactory.DefineFilter<TUnit>(),
  82. LookupFactory.DefineColumns<TUnit>(),
  83. LookupFactory.DefineSort<TUnit>());
  84. Units = result.Rows.Select(row =>
  85. {
  86. var values = row.ToDictionary(new[] { "Display" });
  87. var display = LookupFactory.FormatLookup(typeof(TUnit), values, Array.Empty<string>());
  88. return new Tuple<string, TUnit>(display, row.ToObject<TUnit>());
  89. }).ToList();
  90. Combo.DisplayMemberPath = "Item1";
  91. Combo.SelectedValuePath = "Item2.ID";
  92. Combo.ItemsSource = Units;
  93. Combo.SelectionChanged += Combo_SelectionChanged;
  94. Quantity = AddDimension("Quantity: ", 1);
  95. Length = AddDimension("Length: ", 2);
  96. Width = AddDimension("Width: ", 3);
  97. Height = AddDimension("Height: ", 4);
  98. Weight = AddDimension("Weight: ", 5);
  99. return Grid;
  100. }
  101. private void UpdateColumn(int column, bool visible, DoubleTextBox box)
  102. {
  103. if (!visible)
  104. {
  105. box.Value = 0.0;
  106. }
  107. var columnDefinition = Grid.ColumnDefinitions[column * 2];
  108. if (visible)
  109. {
  110. columnDefinition.Width = new GridLength(1, GridUnitType.Star);
  111. }
  112. else
  113. {
  114. columnDefinition.Width = new GridLength(0);
  115. }
  116. columnDefinition = Grid.ColumnDefinitions[column * 2 - 1];
  117. if (visible)
  118. {
  119. columnDefinition.Width = GridLength.Auto;
  120. }
  121. else
  122. {
  123. columnDefinition.Width = new GridLength(0);
  124. }
  125. }
  126. private DoubleTextBox AddDimension(string name, int column)
  127. {
  128. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0) });
  129. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0) });
  130. var label = new Label
  131. {
  132. Content = name,
  133. VerticalAlignment = VerticalAlignment.Center,
  134. VerticalContentAlignment = VerticalAlignment.Center,
  135. Margin = new Thickness(5, 0, 0, 0)
  136. };
  137. label.SetValue(Grid.ColumnProperty, column * 2 - 1);
  138. var box = new DoubleTextBox
  139. {
  140. VerticalAlignment = VerticalAlignment.Stretch,
  141. VerticalContentAlignment = VerticalAlignment.Center,
  142. HorizontalAlignment = HorizontalAlignment.Stretch,
  143. HorizontalContentAlignment = HorizontalAlignment.Center,
  144. NumberDecimalDigits = 2,
  145. Margin = new Thickness(5, 0, 0, 0)
  146. };
  147. box.SetValue(Grid.ColumnProperty, column * 2);
  148. box.ValueChanged += Box_ValueChanged;
  149. Grid.Children.Add(label);
  150. Grid.Children.Add(box);
  151. return box;
  152. }
  153. private void Box_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  154. {
  155. CheckChanged();
  156. }
  157. private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  158. {
  159. if (Combo.SelectedValue is not Guid unitID) return;
  160. if (Combo.SelectedItem is not Tuple<string, TUnit> tuple) return;
  161. var unit = tuple.Item2;
  162. UpdateColumn(1, unit.HasQuantity, Quantity);
  163. UpdateColumn(2, unit.HasLength, Length);
  164. UpdateColumn(3, unit.HasWidth, Width);
  165. UpdateColumn(4, unit.HasHeight, Height);
  166. UpdateColumn(5, unit.HasWeight, Weight);
  167. foreach(var property in DatabaseSchema.Properties(typeof(TUnit)))
  168. {
  169. var value = property.Getter()(unit);
  170. OtherValues[$"{ColumnName}.Unit.{property.Name}"] = value;
  171. }
  172. CheckChanged();
  173. }
  174. public override object? GetValue(string property)
  175. {
  176. if (!property.StartsWith($"{ColumnName}.")) return null;
  177. property = property[(ColumnName.Length + 1)..];
  178. if (property == "Quantity") return Quantity.Value ?? 0.0;
  179. if (property == "Length") return Length.Value ?? 0.0;
  180. if (property == "Width") return Width.Value ?? 0.0;
  181. if (property == "Height") return Height.Value ?? 0.0;
  182. if (property == "Weight") return Weight.Value ?? 0.0;
  183. if (property == "Unit.ID") return Combo.SelectedValue is Guid guid ? guid : Guid.Empty;
  184. return null;
  185. }
  186. public override Dictionary<string, object?> GetValues()
  187. {
  188. var values = new Dictionary<string, object?>
  189. {
  190. {$"{ColumnName}.Quantity", Quantity.Value ?? 0.0},
  191. {$"{ColumnName}.Length", Length.Value ?? 0.0},
  192. {$"{ColumnName}.Width", Width.Value ?? 0.0},
  193. {$"{ColumnName}.Height", Height.Value ?? 0.0},
  194. {$"{ColumnName}.Weight", Weight.Value ?? 0.0},
  195. {$"{ColumnName}.Unit.ID", Combo.SelectedValue is Guid guid ? guid : Guid.Empty }
  196. };
  197. return values;
  198. }
  199. public override void SetValue(string property, object? value)
  200. {
  201. if (!property.StartsWith($"{ColumnName}.")) return;
  202. property = property[(ColumnName.Length + 1)..];
  203. if (value is null) return;
  204. if (property == "Unit.ID")
  205. {
  206. Combo.SelectedValue = value is Guid guid ? guid : Guid.Empty;
  207. return;
  208. }
  209. if (property == "Quantity") Quantity.Value = (double)value;
  210. if (property == "Length") Length.Value = (double)value;
  211. if (property == "Width") Width.Value = (double)value;
  212. if (property == "Height") Height.Value = (double)value;
  213. if (property == "Weight") Weight.Value = (double)value;
  214. }
  215. }
  216. }