CPDFShapeUI.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using compdfkit_tools.Common;
  2. using compdfkit_tools.Data;
  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.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace compdfkit_tools.PDFControlUI
  18. {
  19. /// <summary>
  20. /// CPDFRectUI.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class CPDFShapeUI : UserControl
  23. {
  24. private AnnotationType currentAnnotationType;
  25. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  26. public CPDFShapeUI()
  27. {
  28. InitializeComponent();
  29. BorderColorPickerControl.ColorChanged += BorderColorPickerControl_ColorChanged;
  30. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  31. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  32. }
  33. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  34. {
  35. PropertyChanged?.Invoke(this, GetShapeData());
  36. }
  37. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  38. {
  39. PropertyChanged?.Invoke(this, GetShapeData());
  40. }
  41. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  42. {
  43. PropertyChanged?.Invoke(this, GetShapeData());
  44. }
  45. private void FillColorPickerControl_ColorChanged(object sender, EventArgs e)
  46. {
  47. PropertyChanged?.Invoke(this, GetShapeData());
  48. }
  49. private void CPDFArrowControl_ArrowChanged(object sender, EventArgs e)
  50. {
  51. PropertyChanged?.Invoke(PropertyChanged, GetShapeData());
  52. }
  53. public DashStyle CalculateDashStyle(CPDFDash pdfDash, int Thickness )
  54. {
  55. DashStyle dashStyle = new DashStyle();
  56. if (pdfDash.IsSolid)
  57. {
  58. dashStyle = DashStyles.Solid;
  59. return dashStyle;
  60. }
  61. else
  62. {
  63. dashStyle.Dashes.Add(pdfDash.DashSpacing / Thickness);
  64. dashStyle.Dashes.Add(pdfDash.DashSpacing / Thickness);
  65. return dashStyle;
  66. }
  67. }
  68. public CPDFAnnotationData GetShapeData()
  69. {
  70. if (currentAnnotationType == AnnotationType.Circle || currentAnnotationType == AnnotationType.Square)
  71. {
  72. CPDFShapeData pdfShapeData = new CPDFShapeData();
  73. pdfShapeData.AnnotationType = currentAnnotationType;
  74. pdfShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  75. pdfShapeData.FillColor = ((SolidColorBrush)FillColorPickerControl.Brush).Color;
  76. pdfShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  77. pdfShapeData.Thickness = CPDFThicknessControl.Thickness;
  78. pdfShapeData.DashStyle = CalculateDashStyle(CPDFLineStyleControl.CPDFDash, CPDFThicknessControl.Thickness);
  79. pdfShapeData.LineType = CPDFArrowControl.LineType;
  80. pdfShapeData.Note = NoteTextBox.Text;
  81. return pdfShapeData;
  82. }
  83. else
  84. {
  85. CPDFLineShapeData pdfLineShapeData = new CPDFLineShapeData();
  86. pdfLineShapeData.AnnotationType = currentAnnotationType;
  87. pdfLineShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  88. pdfLineShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  89. pdfLineShapeData.LineType = CPDFArrowControl.LineType;
  90. pdfLineShapeData.Thickness = CPDFThicknessControl.Thickness;
  91. pdfLineShapeData.DashStyle = CalculateDashStyle(CPDFLineStyleControl.CPDFDash, CPDFThicknessControl.Thickness);
  92. pdfLineShapeData.Note = NoteTextBox.Text;
  93. return pdfLineShapeData;
  94. }
  95. }
  96. public void InitWhenRectAndRound()
  97. {
  98. FillColorStackPanel.Visibility = Visibility.Visible;
  99. ArrowStackPanel.Visibility = Visibility.Collapsed;
  100. FillColorPickerControl.ColorChanged += FillColorPickerControl_ColorChanged;
  101. CPDFArrowControl.ArrowChanged -= CPDFArrowControl_ArrowChanged;
  102. }
  103. public void InitWhenArrowAndLine()
  104. {
  105. FillColorStackPanel.Visibility = Visibility.Collapsed;
  106. ArrowStackPanel.Visibility = Visibility.Visible;
  107. CPDFArrowControl.ArrowChanged += CPDFArrowControl_ArrowChanged;
  108. FillColorPickerControl.ColorChanged -= FillColorPickerControl_ColorChanged;
  109. }
  110. public void InitWithAnnotationType(AnnotationType annotationType)
  111. {
  112. switch (annotationType)
  113. {
  114. case AnnotationType.Square:
  115. TitleTextBlock.Text = "Square";
  116. InitWhenRectAndRound();
  117. break;
  118. case AnnotationType.Circle:
  119. TitleTextBlock.Text = "Circle";
  120. InitWhenRectAndRound();
  121. break;
  122. case AnnotationType.Arrow:
  123. TitleTextBlock.Text = "Arrow";
  124. InitWhenArrowAndLine();
  125. break;
  126. case AnnotationType.Line:
  127. TitleTextBlock.Text = "Line";
  128. InitWhenArrowAndLine();
  129. break;
  130. default:
  131. throw new ArgumentException("Not Excepted Argument");
  132. }
  133. currentAnnotationType = annotationType;
  134. }
  135. }
  136. }