TextAnnotPropertyViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. if(Annot.EventType == AnnotArgsType.AnnotHighlight)
  98. {
  99. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (sampleTextBg as SolidColorBrush).Color);
  100. AnnotEvent?.UpdateAnnot();
  101. }
  102. }
  103. }
  104. private Brush selectColor = new SolidColorBrush(Colors.Transparent);
  105. public Brush SelectColor
  106. {
  107. get { return selectColor; }
  108. set {
  109. SetProperty(ref selectColor, value);
  110. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (selectColor as SolidColorBrush).Color);
  111. AnnotEvent?.UpdateAnnot();
  112. // SetAnnotProperty();
  113. }
  114. }
  115. #endregion
  116. public AnnotAttribEvent AnnotEvent { get; set; }
  117. private AnnotHandlerEventArgs Annot;
  118. private AnnotPropertyPanel PropertyPanel;
  119. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  120. public DelegateCommand<object> OpacityItemCommand { get; set; }
  121. public DelegateCommand<object> OpacityValueChangedCommand { get; set; }
  122. public DelegateCommand<object> SelectedValueChangedCommand { get; set; }
  123. public event EventHandler<object> LoadPropertyHandler;
  124. public TextAnnotPropertyViewModel()
  125. {
  126. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
  127. OpacityItemCommand = new DelegateCommand<object>(OpacityItemCommand_Click);
  128. OpacityValueChangedCommand = new DelegateCommand<object>(OpacityValueChanged_Command);
  129. SelectedValueChangedCommand = new DelegateCommand<object>(SelectedValueChanged_Command);
  130. }
  131. private void SelectedValueChanged_Command(object obj)
  132. {
  133. if (obj != null)
  134. {
  135. annotOpacity = (double)obj;
  136. if (AnnotType == AnnotArgsType.AnnotHighlight)
  137. SampleTextBg.Opacity = annotOpacity;
  138. else
  139. SelectColor.Opacity = annotOpacity;
  140. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  141. AnnotEvent?.UpdateAnnot();
  142. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  143. changeData[AnnotType] = annotOpacity;
  144. PropertyPanel.DataChangedInvoke(this, changeData);
  145. }
  146. }
  147. private void OpacityItemCommand_Click(object obj)
  148. {
  149. if (obj != null)
  150. {
  151. if (AnnotType == AnnotArgsType.AnnotHighlight)
  152. SampleTextBg.Opacity = annotOpacity;
  153. else
  154. SelectColor.Opacity = annotOpacity;
  155. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  156. AnnotEvent?.UpdateAnnot();
  157. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  158. changeData[AnnotType] = annotOpacity;
  159. PropertyPanel.DataChangedInvoke(this, changeData);
  160. }
  161. }
  162. private void OpacityValueChanged_Command(object obj)
  163. {
  164. if (obj != null)
  165. {
  166. if (AnnotType == AnnotArgsType.AnnotHighlight)
  167. SampleTextBg.Opacity = annotOpacity;
  168. else
  169. SelectColor.Opacity = annotOpacity;
  170. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  171. AnnotEvent?.UpdateAnnot();
  172. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  173. changeData[AnnotType] = annotOpacity;
  174. PropertyPanel.DataChangedInvoke(this, changeData);
  175. }
  176. }
  177. public void SelectedColorChanged_Click(object color)
  178. {
  179. if (color != null && PropertyPanel != null)
  180. {
  181. var colorValue = (Color)color;
  182. if (colorValue != null)
  183. {
  184. switch (AnnotType)
  185. {
  186. case AnnotArgsType.AnnotHighlight:
  187. SampleTextBg = new SolidColorBrush(colorValue);
  188. SampleTextBg.Opacity = AnnotOpacity;
  189. break;
  190. case AnnotArgsType.AnnotUnderline:
  191. case AnnotArgsType.AnnotStrikeout:
  192. case AnnotArgsType.AnnotSquiggly:
  193. case AnnotArgsType.AnnotSticky:
  194. SelectColor = new SolidColorBrush(colorValue);
  195. SelectColor.Opacity = AnnotOpacity;
  196. break;
  197. }
  198. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  199. changeData[AnnotType] = color;
  200. PropertyPanel.DataChangedInvoke(this, changeData);
  201. }
  202. }
  203. }
  204. private void SetAnnotType()
  205. {
  206. switch (AnnotType)
  207. {
  208. case AnnotArgsType.AnnotHighlight:
  209. AnnotTypeTitle = "高亮";
  210. break;
  211. case AnnotArgsType.AnnotUnderline:
  212. AnnotTypeTitle = "下划线";
  213. break;
  214. case AnnotArgsType.AnnotStrikeout:
  215. AnnotTypeTitle = "删除线";
  216. break;
  217. case AnnotArgsType.AnnotSquiggly:
  218. AnnotTypeTitle = "波浪线";
  219. break;
  220. case AnnotArgsType.AnnotSticky:
  221. AnnotTypeTitle = "??";
  222. break;
  223. }
  224. }
  225. public bool IsNavigationTarget(NavigationContext navigationContext)
  226. {
  227. return true;
  228. }
  229. public void OnNavigatedFrom(NavigationContext navigationContext)
  230. {
  231. }
  232. public void OnNavigatedTo(NavigationContext navigationContext)
  233. {
  234. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  235. if(PropertyPanel != null)
  236. {
  237. AnnotEvent = PropertyPanel.AnnotEvent;
  238. Annot = PropertyPanel.annot;
  239. AnnotType = Annot.EventType;
  240. GetAnnotProperty();
  241. LoadPropertyHandler?.Invoke(null, Annot);
  242. }
  243. }
  244. private void GetAnnotProperty()
  245. {
  246. if (Annot != null)
  247. {
  248. switch (Annot.EventType)
  249. {
  250. case AnnotArgsType.AnnotHighlight:
  251. if (Annot is TextHighlightAnnotArgs)
  252. {
  253. var Hightlight = Annot as TextHighlightAnnotArgs;
  254. AnnotOpacity = Hightlight.Transparency;
  255. SampleTextBg = new SolidColorBrush(Hightlight.Color);
  256. }
  257. break;
  258. case AnnotArgsType.AnnotUnderline:
  259. {
  260. var Underline = Annot as TextUnderlineAnnotArgs;
  261. AnnotOpacity = Underline.Transparency;
  262. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  263. SelectColor = new SolidColorBrush(Underline.Color);
  264. }
  265. break;
  266. case AnnotArgsType.AnnotStrikeout:
  267. {
  268. var Strikeout = Annot as TextStrikeoutAnnotArgs;
  269. AnnotOpacity = Strikeout.Transparency;
  270. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  271. SelectColor = new SolidColorBrush(Strikeout.Color);
  272. }
  273. break;
  274. case AnnotArgsType.AnnotSquiggly:
  275. {
  276. var Squiggly = Annot as TextSquigglyAnnotArgs;
  277. AnnotOpacity = Squiggly.Transparency;
  278. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  279. SelectColor = new SolidColorBrush(Squiggly.Color);
  280. }
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. }