TextAnnotPropertyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using ComPDFKitViewer.AnnotEvent;
  2. using PDF_Office.Model;
  3. using PDF_Office.ViewModels.Tools;
  4. using PDFSettings;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Media;
  14. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  15. {
  16. public class TextAnnotPropertyViewModel:BindableBase, INavigationAware
  17. {
  18. #region 属性
  19. private AnnotArgsType annotType;
  20. public AnnotArgsType AnnotType
  21. {
  22. get { return annotType; }
  23. set
  24. {
  25. SetProperty(ref annotType, value);
  26. SetAnnotType();
  27. }
  28. }
  29. private string annotTypeTitle;
  30. public string AnnotTypeTitle
  31. {
  32. get { return annotTypeTitle; }
  33. set
  34. {
  35. SetProperty(ref annotTypeTitle, value);
  36. }
  37. }
  38. private double annotOpacity;
  39. public double AnnotOpacity
  40. {
  41. get {return annotOpacity;}
  42. set
  43. {
  44. SetProperty(ref annotOpacity, value);
  45. }
  46. }
  47. /// <summary>
  48. /// 示例背景
  49. /// </summary>
  50. private Brush sampleTextBg = new SolidColorBrush(Colors.Transparent);
  51. public Brush SampleTextBg
  52. {
  53. get { return sampleTextBg; }
  54. set { SetProperty(ref sampleTextBg, value); SetAnnotProperty(); }
  55. }
  56. private Brush selectColor = new SolidColorBrush(Colors.Transparent);
  57. public Brush SelectColor
  58. {
  59. get { return selectColor; }
  60. set { SetProperty(ref selectColor, value); SetAnnotProperty(); }
  61. }
  62. #endregion
  63. private AnnotHandlerEventArgs Annot;
  64. private AnnotPropertyPanel PropertyPanel;
  65. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  66. public DelegateCommand<object> OpacityItemCommand { get; set; }
  67. public DelegateCommand<object> OpacityValueChangedCommand { get; set; }
  68. public TextAnnotPropertyViewModel()
  69. {
  70. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
  71. OpacityItemCommand = new DelegateCommand<object>(OpacityItemCommand_Click);
  72. OpacityValueChangedCommand = new DelegateCommand<object>(OpacityValueChanged_Command);
  73. }
  74. private void OpacityItemCommand_Click(object obj)
  75. {
  76. if(obj!= null)
  77. {
  78. int opacity = -1;
  79. if(int.TryParse((string)obj, out opacity))
  80. {
  81. AnnotOpacity = opacity/100D;
  82. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  83. changeData[AnnotType] = AnnotOpacity;
  84. PropertyPanel.DataChangedInvoke(this, changeData);
  85. }
  86. }
  87. }
  88. private void OpacityValueChanged_Command(object obj)
  89. {
  90. if (obj != null)
  91. {
  92. if (AnnotType == AnnotArgsType.AnnotHighlight)
  93. SampleTextBg.Opacity = annotOpacity;
  94. else
  95. SelectColor.Opacity = annotOpacity;
  96. SetAnnotProperty();
  97. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  98. changeData[AnnotType] = annotOpacity;
  99. PropertyPanel.DataChangedInvoke(this, changeData);
  100. }
  101. }
  102. public void SelectedColorChanged_Click(object color)
  103. {
  104. if (color != null)
  105. {
  106. var colorValue = (Color)color;
  107. if (colorValue != null)
  108. {
  109. switch (AnnotType)
  110. {
  111. case AnnotArgsType.AnnotHighlight:
  112. SampleTextBg = new SolidColorBrush(colorValue);
  113. SampleTextBg.Opacity = AnnotOpacity;
  114. break;
  115. case AnnotArgsType.AnnotUnderline:
  116. case AnnotArgsType.AnnotStrikeout:
  117. case AnnotArgsType.AnnotSquiggly:
  118. case AnnotArgsType.AnnotSticky:
  119. SelectColor = new SolidColorBrush(colorValue);
  120. SelectColor.Opacity = AnnotOpacity;
  121. break;
  122. }
  123. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  124. changeData[AnnotType] = color;
  125. PropertyPanel.DataChangedInvoke(this, changeData);
  126. }
  127. }
  128. }
  129. private void SetAnnotType()
  130. {
  131. switch (AnnotType)
  132. {
  133. case AnnotArgsType.AnnotHighlight:
  134. AnnotTypeTitle = "高亮";
  135. break;
  136. case AnnotArgsType.AnnotUnderline:
  137. AnnotTypeTitle = "下划线";
  138. break;
  139. case AnnotArgsType.AnnotStrikeout:
  140. AnnotTypeTitle = "删除线";
  141. break;
  142. case AnnotArgsType.AnnotSquiggly:
  143. AnnotTypeTitle = "波浪线";
  144. break;
  145. case AnnotArgsType.AnnotSticky:
  146. AnnotTypeTitle = "??";
  147. break;
  148. }
  149. }
  150. public bool IsNavigationTarget(NavigationContext navigationContext)
  151. {
  152. return true;
  153. }
  154. public void OnNavigatedFrom(NavigationContext navigationContext)
  155. {
  156. }
  157. public void OnNavigatedTo(NavigationContext navigationContext)
  158. {
  159. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  160. // navigationContext.Parameters.TryGetValue<ComPDFKitViewer.PdfViewer.CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  161. if(PropertyPanel != null)
  162. {
  163. Annot = PropertyPanel.annot;
  164. AnnotType = Annot.EventType;
  165. GetAnnotProperty();
  166. }
  167. }
  168. private void GetAnnotProperty()
  169. {
  170. if (Annot != null)
  171. {
  172. switch (Annot.EventType)
  173. {
  174. case AnnotArgsType.AnnotHighlight:
  175. if (Annot is TextHighlightAnnotArgs)
  176. {
  177. var Hightlight = Annot as TextHighlightAnnotArgs;
  178. AnnotOpacity = Hightlight.Transparency;
  179. SampleTextBg = new SolidColorBrush(Hightlight.Color);
  180. }
  181. break;
  182. case AnnotArgsType.AnnotUnderline:
  183. {
  184. var Underline = Annot as TextUnderlineAnnotArgs;
  185. AnnotOpacity = Underline.Transparency;
  186. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  187. SelectColor = new SolidColorBrush(Underline.Color);
  188. }
  189. break;
  190. case AnnotArgsType.AnnotSquiggly:
  191. {
  192. var Squiggly = Annot as TextSquigglyAnnotArgs;
  193. AnnotOpacity = Squiggly.Transparency;
  194. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  195. SelectColor = new SolidColorBrush(Squiggly.Color);
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. private void SetAnnotProperty()
  202. {
  203. if (Annot != null)
  204. {
  205. switch (Annot.EventType)
  206. {
  207. case AnnotArgsType.AnnotHighlight:
  208. if (Annot is TextHighlightAnnotArgs)
  209. {
  210. var Hightlight = Annot as TextHighlightAnnotArgs;
  211. if (Hightlight.Transparency != AnnotOpacity)
  212. Hightlight.Transparency = AnnotOpacity;
  213. var c = (SampleTextBg as SolidColorBrush).Color;
  214. if (Hightlight.Color.A != c.A || Hightlight.Color.B != c.B || Hightlight.Color.G != c.G || Hightlight.Color.R != c.R)
  215. Hightlight.Color = (SampleTextBg as SolidColorBrush).Color;
  216. }
  217. break;
  218. case AnnotArgsType.AnnotUnderline:
  219. {
  220. var Underline = Annot as TextUnderlineAnnotArgs;
  221. if (Underline.Transparency != AnnotOpacity)
  222. Underline.Transparency = AnnotOpacity;
  223. var c = (SelectColor as SolidColorBrush).Color;
  224. if (Underline.Color.A != c.A || Underline.Color.B != c.B || Underline.Color.G != c.G || Underline.Color.R != c.R)
  225. Underline.Color = (SelectColor as SolidColorBrush).Color;
  226. }
  227. break;
  228. case AnnotArgsType.AnnotSquiggly:
  229. {
  230. var Squiggly = Annot as TextSquigglyAnnotArgs;
  231. if (Squiggly.Transparency != AnnotOpacity)
  232. AnnotOpacity = Squiggly.Transparency;
  233. var c = (SelectColor as SolidColorBrush).Color;
  234. if (Squiggly.Color.A != c.A || Squiggly.Color.B != c.B || Squiggly.Color.G != c.G || Squiggly.Color.R != c.R)
  235. Squiggly.Color = (SelectColor as SolidColorBrush).Color;
  236. }
  237. break;
  238. case AnnotArgsType.AnnotStrikeout:
  239. {
  240. var Strikeout = Annot as TextStrikeoutAnnotArgs;
  241. if (Strikeout.Transparency != AnnotOpacity)
  242. Strikeout.Transparency = AnnotOpacity;
  243. var c = (SelectColor as SolidColorBrush).Color;
  244. if (Strikeout.Color.A != c.A || Strikeout.Color.B != c.B || Strikeout.Color.G != c.G || Strikeout.Color.R != c.R)
  245. Strikeout.Color = (SelectColor as SolidColorBrush).Color;
  246. }
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. }