ソースを参照

dimensions eidotr unit size box

Kenric Nugteren 3 ヶ月 前
コミット
a1d29b9180
1 ファイル変更236 行追加226 行削除
  1. 236 226
      prs.shared/Editors/Dimensions/DimensionsEditorControl.cs

+ 236 - 226
prs.shared/Editors/Dimensions/DimensionsEditorControl.cs

@@ -12,281 +12,291 @@ using System.Windows.Controls;
 using System.Windows.Media;
 using InABox.DynamicGrid;
 
-namespace PRS.Shared
+namespace PRS.Shared;
+
+
+public class DimensionsEditorControl<TDimensions, TLink, TUnit> : DynamicEnclosedEditorControl<TDimensions, DimensionsEditor>
+    where TDimensions : Dimensions<TLink, TUnit>, new()
+    where TLink : DimensionUnitLink<TUnit>, new()
+    where TUnit : DimensionUnit, new()
 {
-    /*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];
+    private Grid Grid = null!; // Late-initialized in CreateEditor
+    private ComboBox Combo = null!; // Late-initialized in CreateEditor
 
-            return Activator.CreateInstance(typeof(DimensionsEditorControl<,,>).MakeGenericType(dimensionsType, tLink, tUnit), editor) as BaseDynamicEditorControl;
-        }
-    }*/
+    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 TextBox UnitSize = null!; // Late-initialised in CreateEditor
 
-    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 List<Tuple<string, TUnit>> Units = null!; // Late-initialized in CreateEditor
+
+    public override int DesiredHeight()
     {
+        return 25;
+    }
 
-        private Grid Grid = null!; // Late-initialized in CreateEditor
-        private ComboBox Combo = null!; // Late-initialized in CreateEditor
+    public override int DesiredWidth()
+    {
+        return int.MaxValue;
+    }
 
-        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
+    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);
+    }
 
-        private List<Tuple<string, TUnit>> Units = null!; // Late-initialized in CreateEditor
+    public override void SetFocus()
+    {
+        Combo.Focus();
+    }
 
-        public override int DesiredHeight()
-        {
-            return 25;
-        }
+    public override void Configure()
+    {
+    }
 
-        public override int DesiredWidth()
+    protected override FrameworkElement CreateEditor()
+    {
+        Grid = new Grid
         {
-            return int.MaxValue;
-        }
+            VerticalAlignment = VerticalAlignment.Stretch,
+            HorizontalAlignment = HorizontalAlignment.Stretch
+        };
+        Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(150, GridUnitType.Pixel) });
 
-        public override void SetColor(Color color)
+        Combo = new ComboBox
         {
-            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);
-        }
+            IsEnabled = EditorDefinition.AllowEditingUnit
+        };
+        Combo.SetValue(Grid.ColumnProperty, 0);
+        Combo.SetValue(Grid.RowProperty, 0);
+
+        Grid.Children.Add(Combo);
 
-        public override void SetFocus()
+        var result = new Client<TUnit>()
+            .Query(
+                LookupFactory.DefineFilter<TUnit>(),
+                LookupFactory.DefineColumns<TUnit>(),
+                LookupFactory.DefineSort<TUnit>());
+
+        Units = result.Rows.Select(row =>
         {
-            Combo.Focus();
-        }
+            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", "Quantity: ", 1);
+        LengthBox = AddDimension("Length", "Length: ", 2);
+        WidthBox = AddDimension("Width", "Width: ", 3);
+        HeightBox = AddDimension("Height", "Height: ", 4);
+        WeightBox = AddDimension("Weight", "Weight: ", 5);
+
+        Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
+        UnitSize = new TextBox
+        {
+            VerticalAlignment = VerticalAlignment.Stretch,
+            VerticalContentAlignment = VerticalAlignment.Center,
+            HorizontalAlignment = HorizontalAlignment.Stretch,
+            HorizontalContentAlignment = HorizontalAlignment.Center,
+            Margin = new Thickness(5, 0, 0, 0),
+            Padding = new(10, 0, 10, 0),
+            MinWidth = 100,
+            IsReadOnly = true,
+            Focusable = false,
+            Background = new SolidColorBrush(Colors.WhiteSmoke),
+            BorderBrush = new SolidColorBrush(Colors.Silver)
+        };
+        Grid.SetColumn(UnitSize, Grid.ColumnDefinitions.Count - 1);
+        Grid.Children.Add(UnitSize);
+
+        return Grid;
+    }
 
-        public override void Configure()
+    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;
         }
 
-        protected override FrameworkElement CreateEditor()
+        var columnDefinition = Grid.ColumnDefinitions[column * 2];
+        if (visible)
         {
-            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<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;
+            columnDefinition.Width = new GridLength(1, GridUnitType.Star);
+            columnDefinition.MinWidth = 50;
         }
-
-        public override void SetEnabled(bool enabled)
+        else
         {
-            base.SetEnabled(enabled);
+            columnDefinition.Width = new GridLength(0);
         }
-
-        private void UpdateColumn(int column, bool visible, DoubleTextBox box)
+        columnDefinition = Grid.ColumnDefinitions[column * 2 - 1];
+        if (visible)
         {
-            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);
-            }
+            columnDefinition.Width = GridLength.Auto;
         }
-
-        private DoubleTextBox AddDimension(string name, int column)
+        else
         {
-            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;
+            columnDefinition.Width = new GridLength(0);
         }
+    }
 
-        private TUnit? GetSelectedUnit()
+    private DoubleTextBox AddDimension(string property, 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
         {
-            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)
+            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
         {
-            var unit = GetSelectedUnit();
-            if (unit is null) return false;
+            VerticalAlignment = VerticalAlignment.Stretch,
+            VerticalContentAlignment = VerticalAlignment.Center,
+            HorizontalAlignment = HorizontalAlignment.Stretch,
+            HorizontalContentAlignment = HorizontalAlignment.Center,
+            NumberDecimalDigits = 2,
+            Margin = new Thickness(5, 0, 0, 0)
+        };
+        box.Tag = property;
+        box.SetValue(Grid.ColumnProperty, column * 2);
+        box.ValueChanged += Box_ValueChanged;
+
+        Grid.Children.Add(label);
+        Grid.Children.Add(box);
+
+        return box;
+    }
 
-            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;
+    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;
 
-            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;
 
-        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;
-            }
+        return false;
+    }
 
-            CheckChanged();
-        }
+    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;
 
-        private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        if(((double?)e.OldValue ?? default(double)) == ((double?)e.NewValue ?? default(double)))
         {
-            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();
+            return;
         }
 
-        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;
-        }
+        CheckChanged((box.Tag as string)!);
+    }
 
-        protected override IEnumerable<KeyValuePair<string, object?>> 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);
-        }
+    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);
 
-        protected override void SetChildValue(string property, object? value)
+        foreach(var property in DatabaseSchema.Properties(typeof(TUnit)))
         {
-            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);
+            var value = property.Getter()(unit);
+            OtherValues[$"{ColumnName}.Unit.{property.Name}"] = value;
         }
 
+        CheckChanged("Unit.ID");
     }
 
-    public class ProductDimensionsEditorControl : DimensionsEditorControl<ProductDimensions, ProductDimensionUnitLink, ProductDimensionUnit> { }
-    public class QuoteTakeOffDimensionsEditorControl : DimensionsEditorControl<QuoteTakeOffDimensions, QuoteTakeOffUnitLink, QuoteTakeOffUnit> { }
+    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;
+        if (property == "UnitSize") return UnitSize.Text;
+
+        return null;
+    }
 
-    public class StockDimensionsEditorControl : DimensionsEditorControl<StockDimensions, ProductDimensionUnitLink,
-        ProductDimensionUnit>
+    protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
     {
-        public StockDimensionsEditorControl()
+        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);
+        yield return new("UnitSize", UnitSize.Text);
+    }
+
+    protected override void SetChildValue(string property, object? value)
+    {
+        if (property == "Unit.ID")
         {
-            
+            Combo.SelectedValue = value is Guid guid ? guid : Guid.Empty;
         }
+        else if(property == "UnitSize")
+        {
+            UnitSize.Text = value?.ToString();
+        }
+        else if (property == "Quantity") QuantityBox.Value = (double)(value ?? 0.0);
+        else if (property == "Length") LengthBox.Value = (double)(value ?? 0.0);
+        else if (property == "Width") WidthBox.Value = (double)(value ?? 0.0);
+        else if (property == "Height") HeightBox.Value = (double)(value ?? 0.0);
+        else 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()
+    {
         
     }
+    
 }