DimensionsEditorControl.cs 11 KB

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