DFMultiSignatureControl.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. using System.Windows.Media.Effects;
  9. using System.Windows.Media.Imaging;
  10. using System.Windows.Media;
  11. using System.Windows;
  12. using InABox.WPF;
  13. namespace InABox.DynamicGrid
  14. {
  15. public class DFMultiSignatureControl : DynamicFormFieldControl<DFLayoutMultiSignaturePad, DFLayoutMultiSignaturePadProperties, Dictionary<string, byte[]>, Dictionary<string, byte[]>?>
  16. {
  17. private Grid Grid = null!; // Late-initialised
  18. private Grid Images = null!; // Late-initialised
  19. private bool Enabled = true;
  20. static DFMultiSignatureControl()
  21. {
  22. IsEnabledProperty.OverrideMetadata(
  23. typeof(DFMultiSignatureControl),
  24. new UIPropertyMetadata(
  25. true,
  26. ControlIsEnabledChanged));
  27. }
  28. private static void ControlIsEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  29. {
  30. var control = (DFMultiSignatureControl)obj;
  31. control.IsEnabled = true;
  32. control.Enabled = (bool)e.NewValue;
  33. }
  34. protected override FrameworkElement Create()
  35. {
  36. Grid = new Grid();
  37. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  38. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  39. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  40. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  41. Grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  42. var imagesScroll = new ScrollViewer
  43. {
  44. HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
  45. VerticalScrollBarVisibility = ScrollBarVisibility.Disabled,
  46. Background = new SolidColorBrush(Colors.Gray)
  47. };
  48. Images = new Grid { Height = 150 };
  49. Images.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  50. Images.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  51. imagesScroll.Content = Images;
  52. var clearButton = new Button
  53. {
  54. Content = "Clear",
  55. Margin = new Thickness(0, 5, 0, 0),
  56. Width = 60,
  57. Height = 35
  58. };
  59. clearButton.Click += MultiSignatureClear_Click;
  60. var addButton = new Button
  61. {
  62. Content = "Add",
  63. Margin = new Thickness(0, 5, 0, 0),
  64. Width = 60,
  65. Height = 35
  66. };
  67. addButton.Click += MultiSignatureAdd_Click;
  68. imagesScroll.SetGridPosition(0, 0, 1, 3);
  69. clearButton.SetGridPosition(1, 0, 1, 1);
  70. addButton.SetGridPosition(1, 2, 1, 1);
  71. Grid.Children.Add(imagesScroll);
  72. Grid.Children.Add(clearButton);
  73. Grid.Children.Add(addButton);
  74. return Grid;
  75. }
  76. private void AddMultiSignature(BitmapSource? source, string name)
  77. {
  78. var image = new Image();
  79. if (source != null)
  80. {
  81. image.Source = source;
  82. var label = new Label
  83. {
  84. Content = name.ToUpper(),
  85. VerticalContentAlignment = VerticalAlignment.Center,
  86. VerticalAlignment = VerticalAlignment.Stretch,
  87. HorizontalContentAlignment = HorizontalAlignment.Center
  88. };
  89. var column = Images.ColumnDefinitions.Count;
  90. var menu = new ContextMenu();
  91. menu.AddItem("Remove Signature", null, image, MultiSignatureRemove_Click);
  92. image.ContextMenuOpening += (sender, args) =>
  93. {
  94. if (!Enabled) args.Handled = true;
  95. };
  96. Images.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  97. image.ContextMenu = menu;
  98. label.ContextMenu = menu;
  99. image.SetGridPosition(0, column, 1, 1);
  100. label.SetGridPosition(1, column, 1, 1);
  101. Images.Children.Add(image);
  102. Images.Children.Add(label);
  103. }
  104. }
  105. private void MultiSignatureAdd_Click(object sender, RoutedEventArgs e)
  106. {
  107. var window = new SignaturePadWindow(null, true);
  108. if (window.ShowDialog() == true)
  109. {
  110. var sigName = window.SignatureName.ToUpper().Trim();
  111. if (string.IsNullOrWhiteSpace(sigName))
  112. {
  113. MessageBox.Show("Name cannot be empty!");
  114. }
  115. else if (Images.Children.Cast<UIElement>().Any(x => x is Label lbl && (string)lbl.Content == sigName))
  116. {
  117. MessageBox.Show("A signature with that name already exists!");
  118. }
  119. else
  120. {
  121. AddMultiSignature(window.Image, window.SignatureName);
  122. ChangeField();
  123. }
  124. }
  125. }
  126. private void MultiSignatureRemove_Click(Image image)
  127. {
  128. var imageColumn = Grid.GetColumn(image);
  129. var removes = new List<UIElement>();
  130. foreach (var child in Images.Children)
  131. {
  132. if (child is UIElement el)
  133. {
  134. var column = Grid.GetColumn(el);
  135. if (column == imageColumn)
  136. removes.Add(el);
  137. else if (column > imageColumn)
  138. Grid.SetColumn(el, column - 1);
  139. }
  140. }
  141. foreach (var element in removes)
  142. {
  143. Images.Children.Remove(element);
  144. }
  145. ChangeField();
  146. }
  147. private void MultiSignatureClear_Click(object sender, RoutedEventArgs e)
  148. {
  149. if (Images.Children.Count > 0)
  150. {
  151. Images.Children.Clear();
  152. ChangeField();
  153. }
  154. }
  155. public override Dictionary<string, byte[]>? GetSerializedValue()
  156. {
  157. return GetValue();
  158. }
  159. public override void SetSerializedValue(Dictionary<string, byte[]>? value)
  160. {
  161. SetValue(value);
  162. }
  163. public override Dictionary<string, byte[]> GetValue()
  164. {
  165. var columns = Images.Children.Cast<UIElement>().GroupBy(x => Grid.GetColumn(x));
  166. var data = new Dictionary<string, byte[]>();
  167. foreach (var column in columns)
  168. {
  169. if (column.FirstOrDefault(x => x is Image) is Image image
  170. && column.FirstOrDefault(x => x is Label) is Label label)
  171. {
  172. var imgData = EmbeddedImageUtilities.SaveImageToBytes(image, false, new PngBitmapEncoder());
  173. if (imgData != null && label.Content is string str)
  174. {
  175. data.Add(str, imgData);
  176. }
  177. }
  178. }
  179. return data;
  180. }
  181. public override void SetValue(Dictionary<string, byte[]>? value)
  182. {
  183. Images.Children.Clear();
  184. if (value is IDictionary<string, byte[]> data)
  185. {
  186. foreach (var (name, imgData) in data)
  187. {
  188. AddMultiSignature(ImageUtils.BitmapImageFromBytes(imgData), name);
  189. }
  190. }
  191. }
  192. protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  193. {
  194. base.OnPropertyChanged(e);
  195. if (e.Property == IsEnabledProperty)
  196. {
  197. Grid.RowDefinitions[1].Height = (bool)e.NewValue
  198. ? GridLength.Auto
  199. : new GridLength(0);
  200. }
  201. }
  202. protected override bool IsEmpty() => Images.Children.Count == 0;
  203. }
  204. }