DFSignatureControl.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media.Imaging;
  11. namespace InABox.DynamicGrid
  12. {
  13. public class DFSignatureControl : DynamicFormFieldControl<DFLayoutSignaturePad, DFLayoutSignaturePadProperties, byte[]>
  14. {
  15. private DFLayoutEmbeddedMediaValue _value = null!;
  16. private Grid Grid = null!; // Late-initialised
  17. private Image Image = null!; // Late-initialised
  18. private bool _isEmpty = true;
  19. protected override FrameworkElement Create()
  20. {
  21. Grid = new Grid();
  22. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  23. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  24. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  25. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  26. Grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  27. Image = new Image();
  28. Image.StretchDirection = StretchDirection.DownOnly;
  29. Image.Source = EmbeddedImageUtilities.CreateEmptySignature();
  30. var clearButton = new Button
  31. {
  32. Content = "Clear",
  33. Margin = new Thickness(0, 5, 5, 0),
  34. Width = 60,
  35. Height = 35
  36. };
  37. clearButton.Tag = Image;
  38. clearButton.Click += SignatureClear_Click;
  39. var editButton = new Button
  40. {
  41. Content = "Edit",
  42. Margin = new Thickness(0, 5, 0, 0),
  43. Width = 60,
  44. Height = 35
  45. };
  46. editButton.Tag = Image;
  47. editButton.Click += SignatureEdit_Click;
  48. Image.SetGridPosition(0, 0, 1, 3);
  49. clearButton.SetGridPosition(1, 1, 1, 1);
  50. editButton.SetGridPosition(1, 2, 1, 1);
  51. Grid.Children.Add(Image);
  52. Grid.Children.Add(clearButton);
  53. Grid.Children.Add(editButton);
  54. return Grid;
  55. }
  56. public override void Deserialize(string serialized)
  57. {
  58. if (!String.IsNullOrWhiteSpace(serialized) && serialized.IsBase64String())
  59. SetValue(Convert.FromBase64String(serialized));
  60. else
  61. SetValue(null);
  62. }
  63. public override string Serialize()
  64. {
  65. var data = GetValue();
  66. return data.Any() ? Convert.ToBase64String(data) : "";
  67. }
  68. private void SignatureClear_Click(object sender, RoutedEventArgs e)
  69. {
  70. if (!_isEmpty)
  71. {
  72. Image.Source = EmbeddedImageUtilities.CreateEmptySignature();
  73. _isEmpty = true;
  74. ChangeField();
  75. }
  76. }
  77. private void SignatureEdit_Click(object sender, RoutedEventArgs e)
  78. {
  79. var window = new SignaturePadWindow(_isEmpty ? null : Image.Source);
  80. if (window.ShowDialog() == true)
  81. {
  82. Image.Source = window.Image ?? EmbeddedImageUtilities.CreateEmptySignature();
  83. _isEmpty = window.Image == null;
  84. ChangeField();
  85. }
  86. }
  87. public override byte[] GetValue()
  88. {
  89. return EmbeddedImageUtilities.SaveImageToBytes(Image, _isEmpty, new PngBitmapEncoder()) ?? Array.Empty<byte>();
  90. }
  91. public override void SetValue(byte[]? value)
  92. {
  93. _isEmpty = !EmbeddedImageUtilities.LoadImageFromData(Image, value, () => EmbeddedImageUtilities.CreateEmptySignature());
  94. }
  95. protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  96. {
  97. base.OnPropertyChanged(e);
  98. if (e.Property == IsEnabledProperty)
  99. {
  100. Grid.RowDefinitions[1].Height = (bool)e.NewValue
  101. ? GridLength.Auto
  102. : new GridLength(0);
  103. }
  104. }
  105. protected override bool IsEmpty() => _isEmpty;
  106. }
  107. }