瀏覽代碼

Improved DFLayout Import functionality

Frank van den Bos 2 年之前
父節點
當前提交
733cd57a04

+ 9 - 5
InABox.Core/DigitalForms/Forms/DigitalFormVariable.cs

@@ -63,7 +63,11 @@ namespace InABox.Core
             }
         }
         
-        [EditorSequence(7)]
+        [EditorSequence(4)]
+        [TextBoxEditor]
+        public String Group { get; set; }
+        
+        [EditorSequence(5)]
         [MemoEditor(Visible = Visible.Hidden, Editable = Editable.Disabled)]
         public string Parameters
         {
@@ -76,19 +80,19 @@ namespace InABox.Core
         }
 
         [CheckBoxEditor(Visible = Visible.Default, Editable = Editable.Disabled)]
-        [EditorSequence(8)]
+        [EditorSequence(6)]
         public bool Required { get; set; }
 
         [CheckBoxEditor(Visible = Visible.Default, Editable = Editable.Disabled)]
-        [EditorSequence(9)]
+        [EditorSequence(7)]
         public bool Secure { get; set; }
 
         [CheckBoxEditor(Visible = Visible.Default, Editable = Editable.Disabled)]
-        [EditorSequence(10)]
+        [EditorSequence(8)]
         public bool Retain { get; set; }
 
         [CheckBoxEditor(Visible = Visible.Optional)]
-        [EditorSequence(11)]
+        [EditorSequence(9)]
         public bool Hidden { get; set; }
 
         [NullEditor]

+ 23 - 0
InABox.Core/DigitalForms/Layouts/DFLayout.cs

@@ -359,8 +359,20 @@ namespace InABox.Core
             layout.ColumnWidths.Add("*");
 
             int row = 1;
+            var group = "";
             foreach(var variable in variables)
             {
+                
+                if (!String.IsNullOrWhiteSpace(variable.Group) && !String.Equals(variable.Group, group))
+                {
+                    layout.RowHeights.Add("Auto");
+                    var header = new DFLayoutHeader()
+                        { Header = variable.Group, Row = row, Column = 1, ColumnSpan = 3, Collapsed = !String.IsNullOrWhiteSpace(group) };
+                    layout.Elements.Add(header);
+                    group = variable.Group;
+                    row++;                
+                }
+                
                 var rowHeight = "Auto";
 
                 var rowNum = new DFLayoutLabel { Caption = row.ToString(), Row = row, Column = 1 };
@@ -397,8 +409,19 @@ namespace InABox.Core
 
             var row = 1;
             var i = 0;
+            var group = "";
             foreach(var variable in variables)
             {
+                if (!String.IsNullOrWhiteSpace(variable.Group) && !String.Equals(variable.Group, group))
+                {
+                    layout.RowHeights.Add("Auto");
+                    var header = new DFLayoutHeader()
+                        { Header = variable.Group, Row = row, Column = 1, ColumnSpan = 2, Collapsed = !String.IsNullOrWhiteSpace(group) };
+                    layout.Elements.Add(header);
+                    group = variable.Group;
+                    row++;
+                }
+                
                 var rowHeight = "Auto";
                 layout.RowHeights.Add("Auto");
 

+ 3 - 0
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutField.cs

@@ -32,6 +32,8 @@ namespace InABox.Core
         public abstract TValue GetPropertyValue<TValue>(string name);
 
         public abstract object? ParseValue(object? value);
+
+        public abstract DFLayoutFieldProperties GetProperties();
     }
 
     public class DFLayoutField<T> : DFLayoutField where T : DFLayoutFieldProperties, new()
@@ -43,6 +45,7 @@ namespace InABox.Core
 
         public T Properties { get; }
 
+        public override DFLayoutFieldProperties GetProperties() => Properties;
 
         public override TValue GetPropertyValue<TValue>(string name)
         {

+ 85 - 43
InABox.DynamicGrid/DigitalForms/DynamicFormLayoutGrid.cs

@@ -120,7 +120,8 @@ namespace InABox.DynamicGrid
             return cells;
         }
 
-        private static Regex VariableRegex = new("^\\s*\\[(?<VAR>[^:\\]]+)(?::(?<TYPE>[^\\]]+))?\\]$");
+        private static Regex VariableRegex = new(@"^\[(?<VAR>[^:\]]+)(?::(?<TYPE>[^:\]]*))?(?::(?<PROPERTIES>[^\]]*))?\]$");
+        private static Regex HeaderRegex = new(@"^{(?<HEADER>[^:}]+)(?::(?<COLLAPSED>[^}]*))?}$");
 
         public static DFLayout LoadLayout(ISpreadsheet spreadsheet)
         {
@@ -163,18 +164,15 @@ namespace InABox.DynamicGrid
                 }
             }
 
-            /*var rowHeights = new Dictionary<int, float>();
             for (int row = firstRow; row <= lastRow; ++row)
             {
-                rowHeights[row] = sheet.GetRowHeight(row);
+                float height = sheet.GetRowHeight(row);
+                float defaultheight = sheet.GetDefaultRowHeight(); 
+                float multiplier = defaultheight == 0 ? 1.0F : height/defaultheight;
+                var final = Math.Round(multiplier * 10,0) * 5;
+                layout.RowHeights.Add($"{final}");
             }
-            var totalHeight = rowHeights.Values.Sum();*/
-
-            for(int row = firstRow; row <= lastRow; ++row)
-            {
-                layout.RowHeights.Add("Auto");
-            }
-
+            
             foreach(var cell in cells)
             {
                 var style = cell.InnerCell.GetStyle();
@@ -182,50 +180,60 @@ namespace InABox.DynamicGrid
                 if (string.IsNullOrWhiteSpace(cell.Content) && style.Foreground == Color.Empty) continue;
 
                 DFLayoutControl? control;
+                String content = cell.Content?.Trim() ?? "";
 
-                var match = VariableRegex.Match(cell.Content);
-                if (match.Success)
-                {
-                    var variableName = match.Groups["VAR"];
-                    var variableType = match.Groups["TYPE"];
+                var headermatch = HeaderRegex.Match(content);
+                var variablematch = VariableRegex.Match(content);
 
+                if (headermatch.Success)
+                {
+                    var text = headermatch.Groups["HEADER"];
+                    var collapsed = headermatch.Groups["COLLAPSED"];
+                    var header = new DFLayoutHeader()
+                    {
+                        Header = text.Value, 
+                        Collapsed = collapsed.Success ? String.Equals(collapsed.Value.ToUpper(),"COLLAPSED") : false,
+                        Style = CreateStyle(style)
+                    };
+                    
+                    control = header;
+                }
+                else if (variablematch.Success)
+                {
+                    var variableName = variablematch.Groups["VAR"];
+                    var variableType = variablematch.Groups["TYPE"];
+                    var variableProps = variablematch.Groups["PROPERTIES"];
+                    
                     Type? fieldType = null;
                     if (variableType.Success)
-                    {
                         fieldType = DFUtils.GetFieldType(variableType.Value);
-                    }
                     fieldType ??= typeof(DFLayoutStringField);
-
+                    
                     var field = (Activator.CreateInstance(fieldType) as DFLayoutField)!;
                     field.Name = variableName.Value;
+                    
+                    
+                    if (variableProps.Success)
+                    {
+                        if (field is DFLayoutOptionField option)
+                            option.Properties.Options = variableProps.Value;
+                        
+                        // need to populate other variable types here
+                        
+                    }
+                    
+                    
                     control = field;
                 }
                 else
                 {
-                    var font = style.Font;
-
                     control = new DFLayoutLabel
                     {
                         Caption = cell.Content,
-
-                        Style = new DFLayoutTextStyle
-                        {
-                            FontSize = font.FontSize,
-
-                            IsItalic = font.Italic,
-                            IsBold = font.Bold,
-                            Underline = font.Underline switch
-                            {
-                                Scripting.UnderlineType.None => UnderlineType.None,
-                                Scripting.UnderlineType.Single or Scripting.UnderlineType.SingleAccounting => UnderlineType.Single,
-                                Scripting.UnderlineType.Double or Scripting.UnderlineType.DoubleAccounting => UnderlineType.Double,
-                                _ => UnderlineType.None
-                            },
-                            BackgroundColour = style.Foreground,
-                            ForegroundColour = font.Colour
-                        }
+                        Style = CreateStyle(style)
                     };
                 }
+                
                 if(control is not null)
                 {
                     control.Row = cell.Row - firstRow + 1;
@@ -238,7 +246,33 @@ namespace InABox.DynamicGrid
 
             return layout;
         }
+
+        private static DFLayoutTextStyle CreateStyle(ICellStyle style)
+        {
+            if (style == null)
+                return new DFLayoutTextStyle();
+
+            var result = new DFLayoutTextStyle
+            {
+                FontSize = style.Font.FontSize,
+
+                IsItalic = style.Font.Italic,
+                IsBold = style.Font.Bold,
+                Underline = style.Font.Underline switch
+                {
+                    Scripting.UnderlineType.None => UnderlineType.None,
+                    Scripting.UnderlineType.Single or Scripting.UnderlineType.SingleAccounting => UnderlineType.Single,
+                    Scripting.UnderlineType.Double or Scripting.UnderlineType.DoubleAccounting => UnderlineType.Double,
+                    _ => UnderlineType.None
+                },
+                BackgroundColour = style.Background,
+                ForegroundColour = style.Font.Colour
+            };
+            return result;
+        }
     }
+    
+
 
     public abstract class DynamicFormLayoutGrid<T> : DynamicOneToManyGrid<DigitalForm, DigitalFormLayout> where T : Entity, IRemotable, IPersistent, new()
     {
@@ -269,26 +303,34 @@ namespace InABox.DynamicGrid
                 try
                 {
                     DFLayout layout;
+                    Dictionary<String, String> variablegroups = new Dictionary<string, string>();
                     using (var fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                     {
                         layout = LoadLayoutFromSpreadsheet(new Spreadsheet(fs));
                     }
 
                     var dfLayout = CreateItem();
+                    dfLayout.Code = Path.GetFileNameWithoutExtension(dialog.FileName).ToUpper();
+                    dfLayout.Description = $"Imported From {Path.GetFileName(dialog.FileName)}";
                     dfLayout.Layout = layout.SaveLayout();
                     if(EditItems(new DigitalFormLayout[] { dfLayout }))
                     {
                         var newVariables = new List<DigitalFormVariable>();
+                        String group = "";
                         foreach (var element in layout.Elements)
                         {
-                            if(element is DFLayoutField field)
+                            if (element is DFLayoutHeader header)
                             {
-                                var variable = new DigitalFormVariable
-                                {
-                                    Code = field.Name,
-                                    Description = field.Name
-                                };
+                                group = header.Header;
+                            }
+                            else if (element is DFLayoutField field)
+                            {
+                                var variable = new DigitalFormVariable();
                                 variable.SetFieldType(field.GetType());
+                                variable.SaveProperties(field.GetProperties());
+                                variable.Group = group;
+                                variable.Code = field.Name;
+                                variable.Description = field.Name;
                                 newVariables.Add(variable);
                             }
                         }

+ 2 - 0
inabox.scripting/Spreadsheet/ISheet.cs

@@ -101,6 +101,8 @@ namespace InABox.Scripting
         /// <returns></returns>
         float GetRowHeight(int row);
 
+        float GetDefaultRowHeight();
+
         ISheet MergeCells(int firstRow, int lastRow, int firstColumn, int lastColumn);
         IEnumerable<CellRange> GetMergedCells();
 

+ 6 - 4
inabox.scripting/Spreadsheet/NPOISpreadsheet.cs

@@ -108,6 +108,8 @@ namespace InABox.Scripting
             return _sheet.GetRow(row)?.HeightInPoints ?? _sheet.DefaultRowHeightInPoints;
         }
 
+        public float GetDefaultRowHeight() => _sheet.DefaultRowHeightInPoints;
+
         public float GetColumnWidth(int column)
         {
             if (_sheet.IsColumnHidden(column)) return 0f;
@@ -508,9 +510,9 @@ namespace InABox.Scripting
 
         public Spreadsheet Spreadsheet { get; }
 
-        public Color Background => ConvertColour(_style.FillBackgroundColorColor);
+        public Color Background => ConvertColour(_style.FillForegroundColorColor);
 
-        public Color Foreground => ConvertColour(_style.FillForegroundColorColor);
+        public Color Foreground => ColourFromIndex(_style.GetFont(Spreadsheet.Workbook).Color);
 
         public IFont Font => new Font(_style.GetFont(Spreadsheet.Workbook), Spreadsheet);
 
@@ -552,8 +554,8 @@ namespace InABox.Scripting
                 }
                 else
                 {
-                    var argb = extendedColour.ARGB;
-                    return Color.FromArgb(argb[0], argb[1], argb[2], argb[3]);
+                    var rgb = extendedColour.RGBWithTint;
+                    return Color.FromArgb(255, rgb[0], rgb[1], rgb[2]);
                 }
             }
             else if(colour is HSSFColor hssfColour)