|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|
|
using System.Drawing;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using System.Windows.Media.Imaging;
|
|
@@ -119,6 +120,8 @@ namespace InABox.DynamicGrid
|
|
|
return cells;
|
|
|
}
|
|
|
|
|
|
+ private static Regex VariableRegex = new("^\\s*\\[(?<VAR>[^:\\]]+)(?::(?<TYPE>[^\\]]+))?\\]$");
|
|
|
+
|
|
|
public static DFLayout LoadLayout(ISpreadsheet spreadsheet)
|
|
|
{
|
|
|
var sheet = spreadsheet.GetSheet(0);
|
|
@@ -178,33 +181,59 @@ namespace InABox.DynamicGrid
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(cell.Content) && style.Foreground == Color.Empty) continue;
|
|
|
|
|
|
- var font = style.Font;
|
|
|
+ DFLayoutControl? control;
|
|
|
|
|
|
- layout.Elements.Add(new DFLayoutLabel
|
|
|
+ var match = VariableRegex.Match(cell.Content);
|
|
|
+ if (match.Success)
|
|
|
{
|
|
|
- Caption = cell.Content,
|
|
|
- Row = cell.Row - firstRow + 1,
|
|
|
- Column = cell.Column - firstCol + 1 - colOffset,
|
|
|
- RowSpan = cell.RowSpan,
|
|
|
- ColumnSpan = cell.ColumnSpan,
|
|
|
+ var variableName = match.Groups["VAR"];
|
|
|
+ var variableType = match.Groups["TYPE"];
|
|
|
|
|
|
- Style = new DFLayoutTextStyle
|
|
|
+ Type? fieldType = null;
|
|
|
+ if (variableType.Success)
|
|
|
{
|
|
|
- FontSize = font.FontSize,
|
|
|
+ fieldType = DFUtils.GetFieldType(variableType.Value);
|
|
|
+ }
|
|
|
+ fieldType ??= typeof(DFLayoutStringField);
|
|
|
|
|
|
- IsItalic = font.Italic,
|
|
|
- IsBold = font.Bold,
|
|
|
- Underline = font.Underline switch
|
|
|
+ var field = (Activator.CreateInstance(fieldType) as DFLayoutField)!;
|
|
|
+ field.Name = variableName.Value;
|
|
|
+ control = field;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var font = style.Font;
|
|
|
+
|
|
|
+ control = new DFLayoutLabel
|
|
|
+ {
|
|
|
+ Caption = cell.Content,
|
|
|
+
|
|
|
+ Style = new DFLayoutTextStyle
|
|
|
{
|
|
|
- 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
|
|
|
- }
|
|
|
- });
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+ if(control is not null)
|
|
|
+ {
|
|
|
+ control.Row = cell.Row - firstRow + 1;
|
|
|
+ control.Column = cell.Column - firstCol + 1 - colOffset;
|
|
|
+ control.RowSpan = cell.RowSpan;
|
|
|
+ control.ColumnSpan = cell.ColumnSpan;
|
|
|
+ layout.Elements.Add(control);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return layout;
|
|
@@ -249,6 +278,47 @@ namespace InABox.DynamicGrid
|
|
|
dfLayout.Layout = layout.SaveLayout();
|
|
|
if(EditItems(new DigitalFormLayout[] { dfLayout }))
|
|
|
{
|
|
|
+ var newVariables = new List<DigitalFormVariable>();
|
|
|
+ foreach (var element in layout.Elements)
|
|
|
+ {
|
|
|
+ if(element is DFLayoutField field)
|
|
|
+ {
|
|
|
+ var variable = new DigitalFormVariable
|
|
|
+ {
|
|
|
+ Code = field.Name,
|
|
|
+ Description = field.Name
|
|
|
+ };
|
|
|
+ variable.SetFieldType(field.GetType());
|
|
|
+ newVariables.Add(variable);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(newVariables.Count > 0)
|
|
|
+ {
|
|
|
+ var variables = GetVariableGrid();
|
|
|
+ if (variables is not null)
|
|
|
+ {
|
|
|
+ var save = new List<DigitalFormVariable>();
|
|
|
+ foreach(var newVariable in newVariables)
|
|
|
+ {
|
|
|
+ var variable = variables.GetVariable(newVariable.Code);
|
|
|
+ if(variable is not null)
|
|
|
+ {
|
|
|
+ if(variable.FieldType() != newVariable.FieldType())
|
|
|
+ {
|
|
|
+ MessageBox.Show($"Variable [{newVariable.Code}] already exists with a different type!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ save.Add(newVariable);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ variables.SaveItems(save.ToArray());
|
|
|
+ variables.Refresh(false, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
Refresh(false, true);
|
|
|
}
|
|
|
}
|
|
@@ -320,9 +390,9 @@ namespace InABox.DynamicGrid
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>? GetVariableGrid()
|
|
|
- => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>)
|
|
|
- as DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>;
|
|
|
+ private DynamicVariableGrid? GetVariableGrid()
|
|
|
+ => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
|
|
|
+ as DynamicVariableGrid;
|
|
|
|
|
|
private List<DigitalFormVariable> GetVariables()
|
|
|
=> GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
|