TextAnnotPropertyViewModel.cs 11 KB

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