using Comal.Classes; using InABox.Clients; using InABox.Core; using PRSClasses; using Syncfusion.Windows.Shared; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using InABox.DynamicGrid; namespace PRS.Shared { /*public class DimensionsEditorControl { public static BaseDynamicEditorControl? Create(DimensionsEditor editor) { var dimensionsType = editor.DimensionsType; var dimensions = dimensionsType.GetSuperclassDefinition(typeof(Dimensions<,>)); if (dimensions == null) return null; var tLink = dimensions.GenericTypeArguments[0]; var tUnit = dimensions.GenericTypeArguments[1]; return Activator.CreateInstance(typeof(DimensionsEditorControl<,,>).MakeGenericType(dimensionsType, tLink, tUnit), editor) as BaseDynamicEditorControl; } }*/ public class DimensionsEditorControl : DynamicEnclosedEditorControl where TDimensions : Dimensions, new() where TLink : DimensionUnitLink, new() where TUnit : DimensionUnit, new() { private Grid Grid = null!; // Late-initialized in CreateEditor private ComboBox Combo = null!; // Late-initialized in CreateEditor private DoubleTextBox QuantityBox = null!; // Late-initialized in CreateEditor private DoubleTextBox LengthBox = null!; // Late-initialized in CreateEditor private DoubleTextBox WidthBox = null!; // Late-initialized in CreateEditor private DoubleTextBox HeightBox = null!; // Late-initialized in CreateEditor private DoubleTextBox WeightBox = null!; // Late-initialized in CreateEditor private List> Units = null!; // Late-initialized in CreateEditor public override int DesiredHeight() { return 25; } public override int DesiredWidth() { return int.MaxValue; } public override void SetColor(Color color) { QuantityBox.Background = new SolidColorBrush(color); LengthBox.Background = new SolidColorBrush(color); WidthBox.Background = new SolidColorBrush(color); HeightBox.Background = new SolidColorBrush(color); WeightBox.Background = new SolidColorBrush(color); } public override void SetFocus() { Combo.Focus(); } public override void Configure() { } protected override FrameworkElement CreateEditor() { Grid = new Grid { VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch }; Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(150, GridUnitType.Pixel) }); Combo = new ComboBox { IsEnabled = EditorDefinition.AllowEditingUnit }; Combo.SetValue(Grid.ColumnProperty, 0); Combo.SetValue(Grid.RowProperty, 0); Grid.Children.Add(Combo); var result = new Client() .Query( LookupFactory.DefineFilter(), LookupFactory.DefineColumns(), LookupFactory.DefineSort()); Units = result.Rows.Select(row => { var values = row.ToDictionary(new[] { "Display" }); var display = LookupFactory.FormatLookup(typeof(TUnit), values, Array.Empty()); return new Tuple(display, row.ToObject()); }).ToList(); Combo.DisplayMemberPath = "Item1"; Combo.SelectedValuePath = "Item2.ID"; Combo.ItemsSource = Units; Combo.SelectionChanged += Combo_SelectionChanged; QuantityBox = AddDimension("Quantity: ", 1); LengthBox = AddDimension("Length: ", 2); WidthBox = AddDimension("Width: ", 3); HeightBox = AddDimension("Height: ", 4); WeightBox = AddDimension("Weight: ", 5); return Grid; } public override void SetEnabled(bool enabled) { base.SetEnabled(enabled); } private void UpdateColumn(int column, bool visible, DoubleTextBox box) { if (!visible && box.Value != 0.0 && box.Value != null) { box.Value = 0.0; } var columnDefinition = Grid.ColumnDefinitions[column * 2]; if (visible) { columnDefinition.Width = new GridLength(1, GridUnitType.Star); } else { columnDefinition.Width = new GridLength(0); } columnDefinition = Grid.ColumnDefinitions[column * 2 - 1]; if (visible) { columnDefinition.Width = GridLength.Auto; } else { columnDefinition.Width = new GridLength(0); } } private DoubleTextBox AddDimension(string name, int column) { Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0) }); Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0) }); var label = new Label { Content = name, VerticalAlignment = VerticalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, Margin = new Thickness(5, 0, 0, 0) }; label.SetValue(Grid.ColumnProperty, column * 2 - 1); var box = new DoubleTextBox { VerticalAlignment = VerticalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, HorizontalContentAlignment = HorizontalAlignment.Center, NumberDecimalDigits = 2, Margin = new Thickness(5, 0, 0, 0) }; box.SetValue(Grid.ColumnProperty, column * 2); box.ValueChanged += Box_ValueChanged; Grid.Children.Add(label); Grid.Children.Add(box); return box; } private TUnit? GetSelectedUnit() { if (Combo.SelectedValue is not Guid unitID) return null; if (Combo.SelectedItem is not Tuple tuple) return null; return tuple.Item2; } private bool IsBoxVisible(DoubleTextBox box) { var unit = GetSelectedUnit(); if (unit is null) return false; if (box == QuantityBox) return unit.HasQuantity; if (box == LengthBox) return unit.HasLength; if (box == WidthBox) return unit.HasWidth; if (box == HeightBox) return unit.HasHeight; if (box == WeightBox) return unit.HasWeight; return false; } private void Box_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Don't trigger value changed if invisible, this will have been handled by the SelectionChanged event handler. if (d is not DoubleTextBox box || !IsBoxVisible(box)) return; if(((double?)e.OldValue ?? default(double)) == ((double?)e.NewValue ?? default(double))) { return; } CheckChanged(); } private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (Combo.SelectedValue is not Guid unitID) return; if (Combo.SelectedItem is not Tuple tuple) return; var unit = tuple.Item2; UpdateColumn(1, unit.HasQuantity, QuantityBox); UpdateColumn(2, unit.HasLength, LengthBox); UpdateColumn(3, unit.HasWidth, WidthBox); UpdateColumn(4, unit.HasHeight, HeightBox); UpdateColumn(5, unit.HasWeight, WeightBox); foreach(var property in DatabaseSchema.Properties(typeof(TUnit))) { var value = property.Getter()(unit); OtherValues[$"{ColumnName}.Unit.{property.Name}"] = value; } CheckChanged(); } protected override object? GetChildValue(string property) { if (property == "Quantity") return QuantityBox.Value ?? 0.0; if (property == "Length") return LengthBox.Value ?? 0.0; if (property == "Width") return WidthBox.Value ?? 0.0; if (property == "Height") return HeightBox.Value ?? 0.0; if (property == "Weight") return WeightBox.Value ?? 0.0; if (property == "Unit.ID") return Combo.SelectedValue is Guid guid ? guid : Guid.Empty; return null; } protected override IEnumerable> GetChildValues() { yield return new("Quantity", QuantityBox.Value ?? 0.0); yield return new("Length", LengthBox.Value ?? 0.0); yield return new("Width", WidthBox.Value ?? 0.0); yield return new("Height", HeightBox.Value ?? 0.0); yield return new("Weight", WeightBox.Value ?? 0.0); yield return new("Unit.ID", Combo.SelectedValue is Guid guid ? guid : Guid.Empty); } protected override void SetChildValue(string property, object? value) { if (property == "Unit.ID") { Combo.SelectedValue = value is Guid guid ? guid : Guid.Empty; return; } if (property == "Quantity") QuantityBox.Value = (double)(value ?? 0.0); if (property == "Length") LengthBox.Value = (double)(value ?? 0.0); if (property == "Width") WidthBox.Value = (double)(value ?? 0.0); if (property == "Height") HeightBox.Value = (double)(value ?? 0.0); if (property == "Weight") WeightBox.Value = (double)(value ?? 0.0); } } public class ProductDimensionsEditorControl : DimensionsEditorControl { } public class QuoteTakeOffDimensionsEditorControl : DimensionsEditorControl { } public class StockDimensionsEditorControl : DimensionsEditorControl { public StockDimensionsEditorControl() { } } }