123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using InABox.Core;
- using InABox.WPF;
- using NPOI.Util.Collections;
- namespace InABox.DynamicGrid
- {
- internal class DynamicVariableGrid : DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>
- {
- private bool ShowHidden = false;
- private Button ShowHiddenButton;
- private Button HideButton;
- public DynamicVariableGrid()
- {
- Options.Add(DynamicGridOption.MultiSelect);
- ShowHiddenButton = AddButton("Show Hidden", null, ToggleHidden_Click);
- HideButton = AddButton("Hide Variable", null, Hide_Click);
- HideButton.IsEnabled = false;
- HiddenColumns.Add(x => x.Hidden);
- }
- public DigitalFormVariable? GetVariable(string code)
- {
- return Items.Where(x => x.Code == code).FirstOrDefault();
- }
- private bool ShouldHide(CoreRow[] rows)
- {
- return rows.Any(x => !x.Get<DigitalFormVariable, bool>(x => x.Hidden));
- }
- private bool Hide_Click(Button btn, CoreRow[] rows)
- {
- if(rows.Length == 0)
- {
- MessageBox.Show("No rows selected");
- return false;
- }
- var hide = ShouldHide(rows);
- var items = LoadItems(rows);
- foreach (var item in items)
- {
- item.Hidden = hide;
- }
- if(items.Length > 0)
- {
- SaveItems(items);
- return true;
- }
- return false;
- }
- protected override void SelectItems(CoreRow[]? rows)
- {
- base.SelectItems(rows);
- if(rows is null)
- {
- HideButton.IsEnabled = false;
- }
- else
- {
- HideButton.IsEnabled = true;
- HideButton.Content = (ShouldHide(rows) ? "Hide Variable" : "Un-hide Variable") + (rows.Length > 1 ? "s" : "");
- }
- }
- private bool ToggleHidden_Click(Button btn, CoreRow[] rows)
- {
- ShowHidden = !ShowHidden;
- ShowHiddenButton.Content = ShowHidden ? "Hide Hidden" : "Show Hidden";
- return true;
- }
- private void CreateMenu(ContextMenu parent, string header, Type type)
- {
- parent.AddItem(header, null, type, (itemtype) =>
- {
- if(DynamicVariableUtils.CreateAndEdit(Item, Items, itemtype, out var variable))
- {
- SaveItem(variable);
- Refresh(false, true);
- }
- });
- }
- protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
- {
- var menu = new ContextMenu();
- foreach(var fieldType in DFUtils.GetFieldTypes())
- {
- var caption = fieldType.GetCaption();
- if (string.IsNullOrWhiteSpace(caption))
- {
- caption = CoreUtils.Neatify(fieldType.Name);
- }
- CreateMenu(menu, caption, fieldType);
- }
- menu.IsOpen = true;
- }
- /*protected override DigitalFormVariable LoadItem(CoreRow row)
- {
- return Items.FirstOrDefault(r => r.ID.Equals(row.Get<DigitalFormVariable, Guid>(c => c.ID)));
- }*/
- protected override void DoEdit()
- {
- if (!SelectedRows.Any())
- return;
- var variable = LoadItem(SelectedRows.First());
- var properties = variable.CreateProperties();
- if (DynamicVariableUtils.EditProperties(Item, Items, properties.GetType(), properties))
- {
- variable.SaveProperties(properties);
- SaveItem(variable);
- Refresh(false, true);
- }
- }
- protected override void DoDelete()
- {
- var rows = SelectedRows.ToArray();
- if (rows.Any())
- if (CanDeleteItems(rows))
- if (MessageBox.Show("Are you sure you want to delete this variable? This will all cause data associated with this variable to be lost.\n(If you want to just hide the variable, set it to 'Hidden' instead.)", "Confirm Deletion", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
- {
- DeleteItems(rows);
- SelectedRows = Array.Empty<CoreRow>();
- DoChanged();
- Refresh(false, true);
- SelectItems(null);
- }
- }
- protected override bool FilterRecord(CoreRow row)
- {
- return ShowHidden || !row.Get<DigitalFormVariable, bool>(x => x.Hidden);
- }
- }
- public static class DynamicVariableUtils
- {
- public static bool CreateAndEdit(
- DigitalForm form, IList<DigitalFormVariable> variables,
- Type fieldType,
- [NotNullWhen(true)] out DigitalFormVariable? variable)
- {
- var fieldBaseType = fieldType.GetSuperclassDefinition(typeof(DFLayoutField<>));
- if (fieldBaseType != null)
- {
- var propertiesType = fieldBaseType.GetGenericArguments()[0];
- var properties = (Activator.CreateInstance(propertiesType) as DFLayoutFieldProperties)!;
- if (DynamicVariableUtils.EditProperties(form, variables, propertiesType, properties))
- {
- variable = new DigitalFormVariable();
- variable.SaveProperties(fieldType, properties);
- return true;
- }
- }
- variable = null;
- return false;
- }
- public static bool EditProperties(DigitalForm form, IList<DigitalFormVariable> variables, Type type, DFLayoutFieldProperties item)
- {
- var editor = new DynamicEditorForm(type);
- if (item is DFLayoutLookupFieldProperties)
- {
- editor.OnFormCustomiseEditor += (sender, items, column, editor) => LookupEditor_OnFormCustomiseEditor(sender, variables, items, column, editor);
- editor.OnEditorValueChanged += (sender, name, value) =>
- {
- var result = DynamicGridUtils.UpdateEditorValue(new[] { item }, name, value);
- if (name == "LookupType")
- {
- var grid = (sender as DynamicEditorGrid)!;
- var edit = grid.FindEditor("Filter");
- if (edit is FilterEditorControl filter)
- {
- filter.FilterType = value is string str ?
- CoreUtils.GetEntityOrNull(str) :
- null;
- }
- var propertiesEditor = grid.FindEditor(nameof(DFLayoutLookupFieldProperties.AdditionalProperties));
- if (propertiesEditor is MultiLookupEditorControl multi && multi.EditorDefinition is MultiLookupEditor combo)
- {
- combo.Clear();
- multi.Configure();
- }
- }
- return new();
- };
- }
- else
- {
- editor.OnFormCustomiseEditor += (sender, items, column, editor) => Editor_OnFormCustomiseEditor(sender, variables, column, editor);
- }
- editor.OnDefineLookups += o =>
- {
- var def = (o.EditorDefinition as ILookupEditor)!;
- var colname = o.ColumnName;
- // Nope, there is nothing dodgy about this at all
- // I am not breaking any rules by passing in the QA Form instance, rather than the Field instance
- // so that I can get access to the "AppliesTo" property, and thus the list of properties that can be updated
- // Nothing to see here, I promise!
- CoreTable? values;
- if (o.ColumnName == "Property")
- {
- values = def.Values(colname, new object[] { form });
- }
- else
- {
- values = def.Values(colname, new object[] { item });
- }
- 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;
- };
- editor.Items = new BaseObject[] { item };
- return editor.ShowDialog() == true;
- }
- private static void Editor_OnFormCustomiseEditor(IDynamicEditorForm sender, IList<DigitalFormVariable> vars, DynamicGridColumn column, BaseEditor editor)
- {
- if ((column.ColumnName == "Expression" || column.ColumnName == "ColourExpression") && editor is ExpressionEditor exp)
- {
- var variables = new List<string>();
- foreach (var variable in vars)
- {
- //variables.Add(variable.Code);
- foreach (var col in variable.GetVariableColumns())
- {
- variables.Add(col.ColumnName);
- }
- }
- variables.Sort();
- exp.VariableNames = variables;
- }
- }
- private static void LookupEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, IList<DigitalFormVariable> vars, object[] items, DynamicGridColumn column, BaseEditor editor)
- {
- if (column.ColumnName == "Filter" && editor is FilterEditor fe)
- {
- var properties = (items[0] as DFLayoutLookupFieldProperties)!;
- var lookupType = properties.LookupType;
- var entityType = CoreUtils.GetEntityOrNull(lookupType);
- fe.Type = entityType;
- }
- Editor_OnFormCustomiseEditor(sender, vars, column, editor);
- }
- }
- }
|