DimensionsEditorControl.cs 11 KB

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