Selaa lähdekoodia

Finished ChangeType button for DigitalForm variables; the layouts are now also updated.

Kenric Nugteren 5 päivää sitten
vanhempi
commit
290004d266

+ 1 - 1
InABox.Core/DigitalForms/Layouts/DFLayout.cs

@@ -277,7 +277,7 @@ namespace InABox.Core
 
         public void LoadVariables(IEnumerable<DigitalFormVariable> variables)
         {
-            foreach (var field in Elements.Where(x => x is DFLayoutField).Cast<DFLayoutField>())
+            foreach (var field in Elements.OfType<DFLayoutField>())
             {
                 var variable = variables.FirstOrDefault(x => string.Equals(x.Code, field.Name));
                 if (variable != null)

+ 13 - 0
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutDoubleField/DFLayoutDoubleField.cs

@@ -4,4 +4,17 @@
     public class DFLayoutDoubleField : DFLayoutField<DFLayoutDoubleFieldProperties>, IDFLayoutFormField
     {
     }
+
+    public class DFLayoutDoubleFieldConverters :
+        IDFLayoutFieldConverter<DFLayoutIntegerFieldProperties, int, DFLayoutDoubleFieldProperties, double>
+    {
+        public double Convert(DFLayoutIntegerFieldProperties properties, int value, DFLayoutDoubleFieldProperties toProperties)
+        {
+            return value;
+        }
+
+        public void ConvertProperties(DFLayoutIntegerFieldProperties properties, DFLayoutDoubleFieldProperties toProperties)
+        {
+        }
+    }
 }

+ 9 - 0
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutFieldProperties.cs

@@ -102,6 +102,7 @@ namespace InABox.Core
         }
 
         public abstract object? GetDefaultValue();
+        public abstract void SetDefaultValue(object? value);
 
         private class PropertyLookupGenerator : LookupGenerator<object>
         {
@@ -250,5 +251,13 @@ namespace InABox.Core
         {
             return Default;
         }
+
+        public sealed override void SetDefaultValue(object? value)
+        {
+            if(value is TValue tValue)
+            {
+                Default = tValue;
+            }
+        }
     }
 }

+ 43 - 0
inabox.wpf/DigitalForms/DynamicVariableGrid.cs

@@ -31,6 +31,8 @@ public class DynamicVariableGrid : DynamicDataGrid<DigitalFormVariable>
         }
     }
 
+    public DynamicFormLayoutGrid? LayoutGrid { get; set; }
+
     protected override void Init()
     {
         base.Init();
@@ -109,9 +111,18 @@ public class DynamicVariableGrid : DynamicDataGrid<DigitalFormVariable>
             }
         }
         converter.ConvertProperties(oldProperties, newProperties);
+        newProperties.SetDefaultValue(converter.Convert(oldProperties, oldProperties.GetDefaultValue(), newProperties));
 
         if (DynamicVariableUtils.EditProperties(Form, Variables, newProperties.GetType(), newProperties, false))
         {
+            if(!MessageWindow.ShowYesNo(
+                $"This will convert field '{variable.Code}' from {oldFieldType.GetCaption()} to {fieldType.GetCaption()}. Note that this will " +
+                $"update all instances of this form '{Form.Code}' that have been filled in, converting the field data. Do you wish to proceed?",
+                "Proceed?"))
+            {
+                return;
+            }
+
             variable.SaveProperties(fieldType, newProperties);
 
             if (DFUtils.GetFormInstanceType(Form.AppliesTo) is not Type instanceType)
@@ -145,6 +156,38 @@ public class DynamicVariableGrid : DynamicDataGrid<DigitalFormVariable>
 
             Client.Create(instanceType).Save(forms, $"Changed {variable.Code} from {oldFieldType.GetCaption()} to {fieldType.GetCaption()}");
 
+            if(LayoutGrid is not null)
+            {
+                foreach(var layout in LayoutGrid.Layouts)
+                {
+                    var dfLayout = DFLayout.FromLayoutString(layout.Layout);
+
+                    void UpdateList(List<DFLayoutControl> elements)
+                    {
+                        var updates = new List<(int, DFLayoutField)>();
+                        foreach(var (i, element) in elements.WithIndex())
+                        {
+                            if (element is not DFLayoutField field
+                                || field.Name != variable.Code) continue;
+                            updates.Add((i, field));
+                        }
+                        foreach(var (i, field) in updates)
+                        {
+                            var newField = (Activator.CreateInstance(fieldType) as DFLayoutField)!;
+                            newField.LoadFromString(field.SaveToString());
+                            elements[i] = newField;
+                        }
+                    }
+
+                    UpdateList(dfLayout.Elements);
+                    UpdateList(dfLayout.HiddenElements);
+
+                    layout.Layout = dfLayout.SaveLayout();
+                }
+                LayoutGrid.SaveItems(LayoutGrid.Layouts);
+                LayoutGrid.Refresh(false, true);
+            }
+
             SaveItem(variable);
             Refresh(false, true);
         }