DimensionsEditorControl.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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;
  35. private ComboBox Combo;
  36. private DoubleTextBox Quantity;
  37. private DoubleTextBox Length;
  38. private DoubleTextBox Width;
  39. private DoubleTextBox Height;
  40. private DoubleTextBox Weight;
  41. private List<Tuple<string, TUnit>> Units;
  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. Quantity.Background = new SolidColorBrush(color);
  53. Length.Background = new SolidColorBrush(color);
  54. Width.Background = new SolidColorBrush(color);
  55. Height.Background = new SolidColorBrush(color);
  56. Weight.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. Quantity = AddDimension("Quantity: ", 1);
  96. Length = AddDimension("Length: ", 2);
  97. Width = AddDimension("Width: ", 3);
  98. Height = AddDimension("Height: ", 4);
  99. Weight = AddDimension("Weight: ", 5);
  100. return Grid;
  101. }
  102. private void UpdateColumn(int column, bool visible, DoubleTextBox box)
  103. {
  104. if (!visible)
  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 void Box_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  155. {
  156. CheckChanged();
  157. }
  158. private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  159. {
  160. if (Combo.SelectedValue is not Guid unitID) return;
  161. if (Combo.SelectedItem is not Tuple<string, TUnit> tuple) return;
  162. var unit = tuple.Item2;
  163. UpdateColumn(1, unit.HasQuantity, Quantity);
  164. UpdateColumn(2, unit.HasLength, Length);
  165. UpdateColumn(3, unit.HasWidth, Width);
  166. UpdateColumn(4, unit.HasHeight, Height);
  167. UpdateColumn(5, unit.HasWeight, Weight);
  168. foreach(var property in DatabaseSchema.Properties(typeof(TUnit)))
  169. {
  170. var value = property.Getter()(unit);
  171. OtherValues[$"{ColumnName}.Unit.{property.Name}"] = value;
  172. }
  173. CheckChanged();
  174. }
  175. public override object? GetValue(string property)
  176. {
  177. if (!property.StartsWith($"{ColumnName}.")) return null;
  178. property = property[(ColumnName.Length + 1)..];
  179. if (property == "Quantity") return Quantity.Value ?? 0.0;
  180. if (property == "Length") return Length.Value ?? 0.0;
  181. if (property == "Width") return Width.Value ?? 0.0;
  182. if (property == "Height") return Height.Value ?? 0.0;
  183. if (property == "Weight") return Weight.Value ?? 0.0;
  184. if (property == "Unit.ID") return Combo.SelectedValue is Guid guid ? guid : Guid.Empty;
  185. return null;
  186. }
  187. public override Dictionary<string, object?> GetValues()
  188. {
  189. var values = new Dictionary<string, object?>
  190. {
  191. {$"{ColumnName}.Quantity", Quantity.Value ?? 0.0},
  192. {$"{ColumnName}.Length", Length.Value ?? 0.0},
  193. {$"{ColumnName}.Width", Width.Value ?? 0.0},
  194. {$"{ColumnName}.Height", Height.Value ?? 0.0},
  195. {$"{ColumnName}.Weight", Weight.Value ?? 0.0},
  196. {$"{ColumnName}.Unit.ID", Combo.SelectedValue is Guid guid ? guid : Guid.Empty }
  197. };
  198. return values;
  199. }
  200. public override void SetValue(string property, object? value)
  201. {
  202. if (!property.StartsWith($"{ColumnName}.")) return;
  203. property = property[(ColumnName.Length + 1)..];
  204. if (value is null) return;
  205. if (property == "Unit.ID")
  206. {
  207. Combo.SelectedValue = value is Guid guid ? guid : Guid.Empty;
  208. return;
  209. }
  210. if (property == "Quantity") Quantity.Value = (double)value;
  211. if (property == "Length") Length.Value = (double)value;
  212. if (property == "Width") Width.Value = (double)value;
  213. if (property == "Height") Height.Value = (double)value;
  214. if (property == "Weight") Weight.Value = (double)value;
  215. }
  216. }
  217. public class ProductDimensionsEditorControl : DimensionsEditorControl<ProductDimensions, ProductDimensionUnitLink, ProductDimensionUnit> { }
  218. public class QuoteTakeOffDimensionsEditorControl : DimensionsEditorControl<QuoteTakeOffDimensions, QuoteTakeOffUnitLink, QuoteTakeOffUnit> { }
  219. public class StockDimensionsEditorControl : DimensionsEditorControl<StockDimensions, ProductDimensionUnitLink, ProductDimensionUnit> { }
  220. }