DimensionsEditorControl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. NumberDecimalDigits = 2,
  155. Margin = new Thickness(5, 0, 0, 0)
  156. };
  157. box.Tag = property;
  158. box.SetValue(Grid.ColumnProperty, column * 2);
  159. box.ValueChanged += Box_ValueChanged;
  160. Grid.Children.Add(label);
  161. Grid.Children.Add(box);
  162. return box;
  163. }
  164. private TUnit? GetSelectedUnit()
  165. {
  166. if (Combo.SelectedValue is not Guid unitID) return null;
  167. if (Combo.SelectedItem is not Tuple<string, TUnit> tuple) return null;
  168. return tuple.Item2;
  169. }
  170. private bool IsBoxVisible(DoubleTextBox box)
  171. {
  172. var unit = GetSelectedUnit();
  173. if (unit is null) return false;
  174. if (box == QuantityBox) return unit.HasQuantity;
  175. if (box == LengthBox) return unit.HasLength;
  176. if (box == WidthBox) return unit.HasWidth;
  177. if (box == HeightBox) return unit.HasHeight;
  178. if (box == WeightBox) return unit.HasWeight;
  179. return false;
  180. }
  181. private void Box_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  182. {
  183. // Don't trigger value changed if invisible, this will have been handled by the SelectionChanged event handler.
  184. if (d is not DoubleTextBox box || !IsBoxVisible(box)) return;
  185. if(((double?)e.OldValue ?? default(double)) == ((double?)e.NewValue ?? default(double)))
  186. {
  187. return;
  188. }
  189. CheckChanged((box.Tag as string)!);
  190. }
  191. private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  192. {
  193. if (Combo.SelectedValue is not Guid unitID) return;
  194. if (Combo.SelectedItem is not Tuple<string, TUnit> tuple) return;
  195. var unit = tuple.Item2;
  196. UpdateColumn(1, unit.HasQuantity, QuantityBox);
  197. UpdateColumn(2, unit.HasLength, LengthBox);
  198. UpdateColumn(3, unit.HasWidth, WidthBox);
  199. UpdateColumn(4, unit.HasHeight, HeightBox);
  200. UpdateColumn(5, unit.HasWeight, WeightBox);
  201. foreach(var property in DatabaseSchema.Properties(typeof(TUnit)))
  202. {
  203. var value = property.Getter()(unit);
  204. OtherValues[$"{ColumnName}.Unit.{property.Name}"] = value;
  205. }
  206. CheckChanged("Unit.ID");
  207. }
  208. protected override object? GetChildValue(string property)
  209. {
  210. if (property == "Quantity") return QuantityBox.Value ?? 0.0;
  211. if (property == "Length") return LengthBox.Value ?? 0.0;
  212. if (property == "Width") return WidthBox.Value ?? 0.0;
  213. if (property == "Height") return HeightBox.Value ?? 0.0;
  214. if (property == "Weight") return WeightBox.Value ?? 0.0;
  215. if (property == "Unit.ID") return Combo.SelectedValue is Guid guid ? guid : Guid.Empty;
  216. if (property == "UnitSize") return UnitSize.Text;
  217. return null;
  218. }
  219. protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
  220. {
  221. yield return new("Quantity", QuantityBox.Value ?? 0.0);
  222. yield return new("Length", LengthBox.Value ?? 0.0);
  223. yield return new("Width", WidthBox.Value ?? 0.0);
  224. yield return new("Height", HeightBox.Value ?? 0.0);
  225. yield return new("Weight", WeightBox.Value ?? 0.0);
  226. yield return new("Unit.ID", Combo.SelectedValue is Guid guid ? guid : Guid.Empty);
  227. yield return new("UnitSize", UnitSize.Text);
  228. }
  229. protected override void SetChildValue(string property, object? value)
  230. {
  231. if (property == "Unit.ID")
  232. {
  233. Combo.SelectedValue = value is Guid guid ? guid : Guid.Empty;
  234. }
  235. else if(property == "UnitSize")
  236. {
  237. UnitSize.Text = value?.ToString();
  238. }
  239. else if (property == "Quantity") QuantityBox.Value = (double)(value ?? 0.0);
  240. else if (property == "Length") LengthBox.Value = (double)(value ?? 0.0);
  241. else if (property == "Width") WidthBox.Value = (double)(value ?? 0.0);
  242. else if (property == "Height") HeightBox.Value = (double)(value ?? 0.0);
  243. else if (property == "Weight") WeightBox.Value = (double)(value ?? 0.0);
  244. }
  245. }
  246. public class ProductDimensionsEditorControl : DimensionsEditorControl<ProductDimensions, ProductDimensionUnitLink, ProductDimensionUnit> { }
  247. public class QuoteTakeOffDimensionsEditorControl : DimensionsEditorControl<QuoteTakeOffDimensions, QuoteTakeOffUnitLink, QuoteTakeOffUnit> { }
  248. public class StockDimensionsEditorControl : DimensionsEditorControl<StockDimensions, ProductDimensionUnitLink,
  249. ProductDimensionUnit>
  250. {
  251. public StockDimensionsEditorControl()
  252. {
  253. }
  254. }