Bläddra i källkod

Form Designer improvements

Kenric Nugteren 2 år sedan
förälder
incheckning
a99536bbba

+ 4 - 3
InABox.Core/DigitalForms/Layouts/Controls/DFLayoutHeader/DFLayoutHeader.cs

@@ -6,7 +6,7 @@ using System.Text;
 
 namespace InABox.Core
 {
-    public class DFLayoutHeader : DFLayoutControl
+    public class DFLayoutHeader : DFLayoutControl, IDFLayoutTextControl
     {
         [TextBoxEditor]
         [EditorSequence(-1)]
@@ -38,7 +38,7 @@ namespace InABox.Core
 
             Header = GetProperty("Header", "");
             Collapsed = GetProperty("Collapsed", false);
-            Style = Serialization.Deserialize<DFLayoutTextStyle>(GetProperty<string?>("Style", null)) ?? new DFLayoutTextStyle { IsBold = true };
+            Style.LoadProperties(this);
         }
 
         protected override void SaveProperties()
@@ -47,11 +47,12 @@ namespace InABox.Core
 
             SetProperty("Header", Header);
             SetProperty("Collapsed", Collapsed);
-            SetProperty("Style", Serialization.Serialize(Style));
+            Style.SaveProperties(this);
         }
 
         public T GetStyleProperty<T>(string name, T defaultValue)
         {
+            if (name == "IsBold") defaultValue = CoreUtils.ChangeType<T>(true) ?? defaultValue;
             return GetProperty($"Style.{name}", defaultValue);
         }
 

+ 3 - 4
InABox.Core/DigitalForms/Layouts/Controls/DFLayoutLabel/DFLayoutLabel.cs

@@ -6,7 +6,7 @@ using System.Drawing;
 namespace InABox.Core
 {
 
-    public class DFLayoutLabel : DFLayoutControl
+    public class DFLayoutLabel : DFLayoutControl, IDFLayoutTextControl
     {
         [EditorSequence(0)]
         [MemoEditor]
@@ -31,15 +31,14 @@ namespace InABox.Core
         {
             base.LoadProperties();
             Caption = GetProperty("Caption", "");
-
-            Style = Serialization.Deserialize<DFLayoutTextStyle>(GetProperty<string?>("Style", null)) ?? new DFLayoutTextStyle();
+            Style.LoadProperties(this);
         }
 
         protected override void SaveProperties()
         {
             base.SaveProperties();
             SetProperty("Caption", Caption);
-            SetProperty("Style", Serialization.Serialize(Style));
+            Style.SaveProperties(this);
         }
 
         public T GetStyleProperty<T>(string name, T defaultValue)

+ 33 - 1
InABox.Core/DigitalForms/Layouts/Controls/DFLayoutLabel/DFLayoutTextStyle.cs

@@ -13,29 +13,42 @@ namespace InABox.Core
         Double
     }
 
+    public interface IDFLayoutTextControl
+    {
+        T GetStyleProperty<T>(string property, T defaultValue);
+
+        void SetStyleProperty(string property, object? value);
+    }
+
     public class DFLayoutTextStyle : EnclosedEntity
     {
         [EditorSequence(0)]
+        [Caption("Italic", IncludePath = false)]
         [CheckBoxEditor]
         public bool IsItalic { get; set; }
 
         [EditorSequence(1)]
         [CheckBoxEditor]
+        [Caption("Bold", IncludePath = false)]
         public bool IsBold { get; set; }
 
         [EditorSequence(2)]
         [EnumLookupEditor(typeof(UnderlineType))]
+        [Caption("Underline", IncludePath = false)]
         public UnderlineType Underline { get; set; }
 
         [EditorSequence(3)]
-        [DoubleEditor(Caption = "Font size in points. Set to 0 for the default size.")]
+        [Caption("Font Size", IncludePath = false)]
+        [DoubleEditor(ToolTip = "Font size in points. Set to 0 for the default size.")]
         public double FontSize { get; set; }
 
         [EditorSequence(4)]
+        [Caption("Text Colour", IncludePath = false)]
         [ColorEditor]
         public string Foreground { get; set; }
 
         [EditorSequence(5)]
+        [Caption("Background Colour", IncludePath = false)]
         [ColorEditor]
         public string Background { get; set; }
 
@@ -58,6 +71,25 @@ namespace InABox.Core
             FontSize = 0;
         }
 
+        public void LoadProperties(IDFLayoutTextControl control)
+        {
+            IsItalic = control.GetStyleProperty("IsItalic", false);
+            IsBold = control.GetStyleProperty("IsBold", false);
+            Underline = control.GetStyleProperty("Underline", UnderlineType.None);
+            Foreground = control.GetStyleProperty("Foreground", "");
+            Background = control.GetStyleProperty("Background", "");
+            FontSize = control.GetStyleProperty("FontSize", 0.0);
+        }
+        public void SaveProperties(IDFLayoutTextControl control)
+        {
+            control.SetStyleProperty("IsItalic", IsItalic);
+            control.SetStyleProperty("IsBold", IsBold);
+            control.SetStyleProperty("Underline", Underline);
+            control.SetStyleProperty("Foreground", Foreground);
+            control.SetStyleProperty("Background", Background);
+            control.SetStyleProperty("FontSize", FontSize);
+        }
+
         public Color GetForegroundColour() => ColourFromString(Foreground);
         public Color GetBackgroundColour() => ColourFromString(Background);
 

+ 1 - 1
InABox.DynamicGrid/DynamicGridCommon.cs

@@ -71,7 +71,7 @@ namespace InABox.DynamicGrid
 
     public delegate object? OnDefineFilter(object sender, Type type);
 
-    public delegate string[]? OnValidateData(object sender, BaseObject[] items);
+    public delegate IList<string>? OnValidateData(object sender, BaseObject[] items);
 
     public delegate void OnPrintData(object sender);
 

+ 29 - 3
InABox.DynamicGrid/Editors/ColorEditor.cs

@@ -1,4 +1,5 @@
 using System.Windows;
+using System.Windows.Controls;
 using System.Windows.Media;
 using Xceed.Wpf.Toolkit;
 
@@ -10,14 +11,39 @@ namespace InABox.DynamicGrid
 
         protected override FrameworkElement CreateEditor()
         {
+            var panel = new DockPanel
+            {
+                LastChildFill = false
+            };
+
             Editor = new ColorPicker
             {
                 HorizontalAlignment = HorizontalAlignment.Stretch,
                 VerticalAlignment = VerticalAlignment.Stretch,
-                VerticalContentAlignment = VerticalAlignment.Center
+                VerticalContentAlignment = VerticalAlignment.Center,
+                Width = 150
             };
             Editor.SelectedColorChanged += (o, e) => CheckChanged();
-            return Editor;
+            Editor.SetValue(DockPanel.DockProperty, Dock.Left);
+
+            var clearButton = new Button
+            {
+                Content = "Clear",
+                Margin = new Thickness(5, 0, 0, 0),
+                Width = 50
+            };
+            clearButton.Click += ClearButton_Click;
+            Editor.SetValue(DockPanel.DockProperty, Dock.Left);
+
+            panel.Children.Add(Editor);
+            panel.Children.Add(clearButton);
+
+            return panel;
+        }
+
+        private void ClearButton_Click(object sender, RoutedEventArgs e)
+        {
+            Editor.SelectedColor = null;
         }
 
         public override int DesiredHeight()
@@ -27,7 +53,7 @@ namespace InABox.DynamicGrid
 
         public override int DesiredWidth()
         {
-            return 150;
+            return int.MaxValue;
         }
 
         protected override string RetrieveValue()

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 873 - 35
InABox.DynamicGrid/FormDesigner/DynamicFormDesignGrid.cs


+ 2 - 1
InABox.DynamicGrid/FormDesigner/DynamicFormDesignWindow.xaml

@@ -5,7 +5,8 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:InABox.DynamicGrid"
         mc:Ignorable="d"
-        Title="Form Designer" Height="800" Width="1000" Padding="5">
+        Title="Form Designer" Height="800" Width="1000" Padding="5"
+                         KeyDown="DynamicFormWindow_KeyDown">
     <Grid Margin="5">
 
         <Grid.ColumnDefinitions>

+ 5 - 0
InABox.DynamicGrid/FormDesigner/DynamicFormDesignWindow.xaml.cs

@@ -75,5 +75,10 @@ namespace InABox.DynamicGrid
         {
             DialogResult = false;
         }
+
+        private void DynamicFormWindow_KeyDown(object sender, KeyEventArgs e)
+        {
+            Grid.HandleKeyDown(e);
+        }
     }
 }

+ 8 - 5
InABox.DynamicGrid/FormDesigner/DynamicFormLayoutGrid.cs

@@ -67,7 +67,7 @@ namespace InABox.DynamicGrid
             for (int rowIdx = sheet.FirstRow; rowIdx <= sheet.LastRow; ++rowIdx)
             {
                 var row = sheet.GetRow(rowIdx);
-                if (row is not null)
+                if (row is not null && row.FirstColumn >= 0)
                 {
                     var rowCells = new Dictionary<int, Cell>();
                     for (int colIdx = row.FirstColumn; colIdx <= row.LastColumn; ++colIdx)
@@ -174,9 +174,10 @@ namespace InABox.DynamicGrid
 
             foreach(var cell in cells)
             {
-                //if (string.IsNullOrWhiteSpace(cell.Content)) continue;
-
                 var style = cell.InnerCell.GetStyle();
+
+                if (string.IsNullOrWhiteSpace(cell.Content) && style.Foreground == Color.Empty) continue;
+
                 var font = style.Font;
 
                 layout.Elements.Add(new DFLayoutLabel
@@ -246,8 +247,10 @@ namespace InABox.DynamicGrid
 
                     var dfLayout = CreateItem();
                     dfLayout.Layout = layout.SaveLayout();
-                    SaveItem(dfLayout);
-                    Refresh(false, true);
+                    if(EditItems(new DigitalFormLayout[] { dfLayout }))
+                    {
+                        Refresh(false, true);
+                    }
                 }
                 catch(Exception e)
                 {

+ 32 - 1
InABox.DynamicGrid/FormDesigner/DynamicVariableGrid.cs

@@ -157,7 +157,7 @@ namespace InABox.DynamicGrid
             if (fieldBaseType != null)
             {
                 var propertiesType = fieldBaseType.GetGenericArguments()[0];
-                var properties = Activator.CreateInstance(propertiesType) as DFLayoutFieldProperties;
+                var properties = (Activator.CreateInstance(propertiesType) as DFLayoutFieldProperties)!;
                 if (DynamicVariableUtils.EditProperties(form, variables, propertiesType, properties))
                 {
                     variable = new DigitalFormVariable();
@@ -203,6 +203,7 @@ namespace InABox.DynamicGrid
             {
                 editor.OnFormCustomiseEditor += (sender, items, column, editor) => Editor_OnFormCustomiseEditor(sender, variables, column, editor);
             }
+
             editor.Items = new BaseObject[] { item };
             editor.OnDefineLookups += o =>
             {
@@ -224,6 +225,36 @@ namespace InABox.DynamicGrid
 
                 o.LoadLookups(values);
             };
+
+            var thisVariable = variables.Where(x => x.Code == item.Code).ToList();
+
+            editor.OnValidateData += (sender, items) =>
+            {
+                var errors = new List<string>();
+                foreach(var item in items.Cast<DFLayoutFieldProperties>())
+                {
+                    if (string.IsNullOrWhiteSpace(item.Code))
+                    {
+                        errors.Add("[Code] may not be blank!");
+                    }
+                    else
+                    {
+                        var codeVars = variables.Where(x => x.Code == item.Code).ToList();
+                        if(codeVars.Count > 1)
+                        {
+                            errors.Add($"Duplicate code [{item.Code}]");
+                        }
+                        else if(codeVars.Count == 1)
+                        {
+                            if (!thisVariable.Contains(codeVars.First()))
+                            {
+                                errors.Add($"There is already a variable with code [{item.Code}]");
+                            }
+                        }
+                    }
+                }
+                return errors;
+            };
             return editor.ShowDialog() == true;
         }
 

+ 48 - 2
inabox.wpf/WPFUtils.cs

@@ -44,6 +44,33 @@ namespace InABox.WPF
                 dependencyObject = parent;
             }
         }
+       
+        public static int GetColumn(this Grid grid, DependencyObject dependencyObject)
+        {
+            while (true)
+            {
+                var parent = LogicalTreeHelper.GetParent(dependencyObject);
+                if (parent == null)
+                    return -1;
+                if (parent == grid)
+                    return Grid.GetColumn(dependencyObject as UIElement);
+                dependencyObject = parent;
+            }
+        }
+
+        public static int GetColumnSpan(this Grid grid, DependencyObject dependencyObject)
+        {
+            while (true)
+            {
+                var parent = LogicalTreeHelper.GetParent(dependencyObject);
+                if (parent == null)
+                    return -1;
+                if (parent == grid)
+                    return Grid.GetColumnSpan(dependencyObject as UIElement);
+                dependencyObject = parent;
+            }
+        }
+       
         public static void SetGridPosition(this FrameworkElement element, int row, int column, int rowspan, int colspan)
         {
             element.SetValue(Grid.ColumnProperty, column);
@@ -147,7 +174,7 @@ namespace InABox.WPF
             return item;
         }
 
-        public static Separator AddSeparator(this ContextMenu menu, int index = -1)
+        private static Separator DoAddSeparator(ItemsControl menu, int index)
         {
             var separator = new Separator();
 
@@ -155,7 +182,7 @@ namespace InABox.WPF
 
             return separator;
         }
-        public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1)
+        private static Separator DoAddSeparatorIfNeeded(ItemsControl menu, int index)
         {
             if (menu.Items.Count == 0) return null;
 
@@ -172,6 +199,25 @@ namespace InABox.WPF
             return separator;
         }
 
+        private static void DoRemoveUnnecessarySeparators(ItemsControl menu)
+        {
+            while(menu.Items.Count > 0 && menu.Items[0] is Separator)
+            {
+                menu.Items.RemoveAt(0);
+            }
+            while(menu.Items.Count > 0 && menu.Items[^1] is Separator)
+            {
+                menu.Items.RemoveAt(menu.Items.Count - 1);
+            }
+        }
+
+        public static Separator AddSeparator(this ContextMenu menu, int index = -1) => DoAddSeparator(menu, index);
+        public static Separator AddSeparator(this MenuItem menu, int index = -1) => DoAddSeparator(menu, index);
+        public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
+        public static Separator? AddSeparatorIfNeeded(this MenuItem menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
+        public static void RemoveUnnecessarySeparators(this ContextMenu menu) => DoRemoveUnnecessarySeparators(menu);
+        public static void RemoveUnnecessarySeparators(this MenuItem menu) => DoRemoveUnnecessarySeparators(menu);
+
         public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
             => DoAddMenuItem(menu, caption, image, click, enabled, index);
         public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)

Vissa filer visades inte eftersom för många filer har ändrats