| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 | 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<TDimensions, TLink, TUnit> : DynamicEnclosedEditorControl<TDimensions, DimensionsEditor>        where TDimensions : Dimensions<TLink, TUnit>, new()        where TLink : DimensionUnitLink<TUnit>, 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<Tuple<string, TUnit>> 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 = false //EditorDefinition.AllowEditingUnit            };            Combo.SetValue(Grid.ColumnProperty, 0);            Combo.SetValue(Grid.RowProperty, 0);            Grid.Children.Add(Combo);            var result = new Client<TUnit>()                .Query(                    LookupFactory.DefineFilter<TUnit>(),                    LookupFactory.DefineColumns<TUnit>(),                    LookupFactory.DefineSort<TUnit>());            Units = result.Rows.Select(row =>            {                var values = row.ToDictionary(new[] { "Display" });                var display = LookupFactory.FormatLookup(typeof(TUnit), values, Array.Empty<string>());                return new Tuple<string, TUnit>(display, row.ToObject<TUnit>());            }).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<string, TUnit> 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;            CheckChanged();        }        private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)        {            if (Combo.SelectedValue is not Guid unitID) return;            if (Combo.SelectedItem is not Tuple<string, TUnit> 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();        }        public override object? GetValue(string property)        {            if (!property.StartsWith($"{ColumnName}.")) return null;            property = property[(ColumnName.Length + 1)..];            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;        }        public override Dictionary<string, object?> GetValues()        {            var values = new Dictionary<string, object?>            {                {$"{ColumnName}.Quantity", QuantityBox.Value ?? 0.0},                {$"{ColumnName}.Length", LengthBox.Value ?? 0.0},                {$"{ColumnName}.Width", WidthBox.Value ?? 0.0},                {$"{ColumnName}.Height", HeightBox.Value ?? 0.0},                {$"{ColumnName}.Weight", WeightBox.Value ?? 0.0},                {$"{ColumnName}.Unit.ID", Combo.SelectedValue is Guid guid ? guid : Guid.Empty }            };            return values;        }        public override void SetValue(string property, object? value)        {            if (!property.StartsWith($"{ColumnName}.")) return;            property = property[(ColumnName.Length + 1)..];            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<ProductDimensions, ProductDimensionUnitLink, ProductDimensionUnit> { }    public class QuoteTakeOffDimensionsEditorControl : DimensionsEditorControl<QuoteTakeOffDimensions, QuoteTakeOffUnitLink, QuoteTakeOffUnit> { }    public class StockDimensionsEditorControl : DimensionsEditorControl<StockDimensions, ProductDimensionUnitLink,        ProductDimensionUnit>    {        public StockDimensionsEditorControl()        {                    }            }}
 |