DataEntryPanel.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using Syncfusion.Data.Extensions;
  7. using Syncfusion.Pdf;
  8. using Syncfusion.Pdf.Graphics;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing.Imaging;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using System.Windows.Navigation;
  25. using Syncfusion.Pdf.Parsing;
  26. using Encoder = System.Drawing.Imaging.Encoder;
  27. using Image = System.Windows.Controls.Image;
  28. using System.Reflection;
  29. using System.Windows.Forms.VisualStyles;
  30. namespace PRSDesktop
  31. {
  32. /// <summary>
  33. /// Interaction logic for DataEntryPanel.xaml
  34. /// </summary>
  35. /// <remarks>
  36. /// This is a host because it has a singular code popup editor
  37. /// </remarks>
  38. public partial class DataEntryPanel : UserControl, IBasePanel, IDynamicEditorHost
  39. {
  40. IPopupEditorControl? Popup;
  41. public DataEntryPanel()
  42. {
  43. InitializeComponent();
  44. }
  45. public void Setup()
  46. {
  47. var types = CoreUtils.Entities
  48. .Where(x => x.IsAssignableTo(typeof(Entity))
  49. && x.HasInterface(typeof(IRemotable))
  50. && x.HasInterface(typeof(IPersistent))
  51. && x.GetCustomAttribute<AutoEntity>() is null
  52. && Security.CanEdit(x)
  53. && x.HasInterface<IScannable>())
  54. .Select(x => new Tuple<string, Type?>(x.Name, x)).OrderBy(x => x.Item1).ToList();
  55. types.Insert(0, new("", null));
  56. TypeBox.ItemsSource = types;
  57. TypeBox.DisplayMemberPath = "Item1";
  58. TypeBox.SelectedValuePath = "Item2";
  59. ScanPanel.Setup();
  60. }
  61. public void Refresh()
  62. {
  63. ScanPanel.Refresh();
  64. }
  65. public bool IsReady { get; set; }
  66. public string SectionName => "Data Entry";
  67. public DataModel DataModel(Selection selection)
  68. {
  69. return new EmptyDataModel();
  70. }
  71. public event DataModelUpdateEvent? OnUpdateDataModel;
  72. public void CreateToolbarButtons(IPanelHost host)
  73. {
  74. if (Security.IsAllowed<CanSetupScanTags>())
  75. {
  76. host.CreateSetupAction(new PanelAction
  77. {
  78. Caption = "Scan Tags",
  79. OnExecute = (action) =>
  80. {
  81. var list = new MasterList(typeof(ScanTag));
  82. list.ShowDialog();
  83. }
  84. });
  85. }
  86. }
  87. public void Heartbeat(TimeSpan time)
  88. {
  89. }
  90. public Dictionary<string, object[]> Selected()
  91. {
  92. return new Dictionary<string, object[]>();
  93. }
  94. public void Shutdown()
  95. {
  96. ScanPanel.Shutdown();
  97. }
  98. #region Host
  99. private HashSet<Type> _loadedTypes = new();
  100. public DynamicGridColumns Columns { get; set; } = new();
  101. IEnumerable<DynamicGridColumn> IDynamicEditorHost.Columns => Columns;
  102. public void LoadColumns(string column, Dictionary<string, string> columns)
  103. {
  104. var selectedType = TypeBox.SelectedValue as Type;
  105. if (selectedType is null) return;
  106. columns.Clear();
  107. foreach (var c in LookupFactory.DefineColumns(selectedType).ColumnNames().Where(x => x != "ID"))
  108. {
  109. columns.Add(c, c);
  110. }
  111. if (Popup?.EditorDefinition is CodePopupEditorControl codePopup && !columns.ContainsKey(codePopup.CodeColumn))
  112. {
  113. columns.Add(codePopup.CodeColumn, codePopup.CodeColumn);
  114. }
  115. }
  116. public IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(type);
  117. public void LoadLookups(ILookupEditorControl sender)
  118. {
  119. var editor = sender.EditorDefinition as ILookupEditor;
  120. var colname = sender.ColumnName;
  121. var values = editor.Values(colname, Editor.Items);
  122. sender.LoadLookups(values);
  123. }
  124. public Document? FindDocument(string filename) => null;
  125. public Document? GetDocument(Guid id) => null;
  126. public void SaveDocument(Document document) { }
  127. object?[] IDynamicEditorHost.GetItems() => Editor.Items;
  128. public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
  129. #endregion
  130. private void ClearEditor(Type TEntity)
  131. {
  132. UpdateEditor(TEntity, null);
  133. if(Popup is not null)
  134. {
  135. Popup.Value = Guid.Empty;
  136. }
  137. }
  138. private bool EditorChanged = false;
  139. private IDynamicDataGrid UpdateEditor(Type TEntity, BaseObject[]? items)
  140. {
  141. DetailBorder.Child = null;
  142. Editor = new EmbeddedDynamicEditorForm();
  143. Editor.SetLayoutType<VerticalDynamicEditorGridLayout>();
  144. Editor.SetValue(Grid.RowProperty, 1);
  145. Editor.SetValue(Grid.ColumnProperty, 0);
  146. Editor.SetValue(Grid.ColumnSpanProperty, 4);
  147. EditorChanged = false;
  148. Editor.OnAfterEditorValueChanged += (sender, column) =>
  149. {
  150. EditorChanged = true;
  151. return null;
  152. };
  153. Editor.OnOK += () =>
  154. {
  155. var cancel = new System.ComponentModel.CancelEventArgs();
  156. Editor.SaveItem(cancel);
  157. if(!cancel.Cancel)
  158. ClearEditor(TEntity);
  159. };
  160. Editor.OnCancel += () =>
  161. {
  162. ClearEditor(TEntity);
  163. };
  164. DetailBorder.Child = Editor;
  165. var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), TEntity);
  166. grid.InitialiseEditorForm(Editor, items ?? new object[] { Activator.CreateInstance(TEntity)! });
  167. return (grid as IDynamicDataGrid)!;
  168. }
  169. private void TypeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  170. {
  171. var selectedType = TypeBox.SelectedValue as Type;
  172. if (selectedType is not null)
  173. {
  174. var dataGrid = UpdateEditor(selectedType, null);
  175. var editorColumns = dataGrid.LoadEditorColumns();
  176. if (Popup is UIElement element)
  177. DetailHeader.Children.Remove(element);
  178. var code = DatabaseSchema.Properties(selectedType)
  179. .Where(x => x.Parent is null && (x.Editor is CodeEditor || x.Editor is UniqueCodeEditor)
  180. && x.Editor.Editable != Editable.Hidden)
  181. .FirstOrDefault();
  182. BaseEditor editor;
  183. if (code is not null)
  184. {
  185. editor = new CodePopupEditor(selectedType) { CodeColumn = code.Name };
  186. Popup = new CodePopupEditorControl { Margin = new Thickness(5), CodeColumn = code.Name };
  187. }
  188. else
  189. {
  190. editor = new PopupEditor(selectedType);
  191. Popup = new PopupEditorControl { Margin = new Thickness(5) };
  192. }
  193. if (Popup is IPopupEditorControl popupEditor)
  194. {
  195. Popup.ColumnName = "ID";
  196. popupEditor.Host = this;
  197. Columns = new DynamicGridColumns();
  198. Columns.ExtractColumns(selectedType);
  199. }
  200. var el = (Popup as UIElement)!;
  201. el.SetValue(DockPanel.DockProperty,Dock.Left);
  202. Popup.EditorDefinition = editor;
  203. Popup.OnEditorValueChanged += (s, v) =>
  204. {
  205. var entityID = (Guid)v["ID"];
  206. BaseObject[]? objs = null;
  207. if (entityID != Guid.Empty)
  208. {
  209. var obj = Client.Create(selectedType)
  210. .Query(
  211. Filter.Create<Entity>(selectedType, x => x.ID).IsEqualTo(entityID),
  212. editorColumns)
  213. .ToObjects(selectedType)
  214. .FirstOrDefault();
  215. if (obj is not null)
  216. {
  217. objs = new BaseObject[] { obj };
  218. }
  219. }
  220. UpdateEditor(selectedType, objs);
  221. };
  222. Popup.Configure();
  223. Popup.Loaded = true;
  224. DetailHeader.Children.Add(el);
  225. }
  226. else
  227. {
  228. DetailBorder.Child = null;
  229. if (Popup is UIElement el)
  230. DetailHeader.Children.Remove(el);
  231. }
  232. }
  233. private void ScanPanel_OnSelectAppliesTo(string appliesTo)
  234. {
  235. if(CoreUtils.TryGetEntity(appliesTo, out var entity))
  236. {
  237. TypeBox.SelectedValue = entity;
  238. }
  239. }
  240. }
  241. }