TextAnnotPropertyViewModel.cs 12 KB

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