FreetextAnnotPropertyViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.Model;
  4. using PDF_Office.ViewModels.Tools;
  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;
  14. using System.Windows.Controls;
  15. using System.Windows.Media;
  16. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  17. {
  18. public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware
  19. {
  20. private double fillOpacity = 1;
  21. public double FillOpacity
  22. {
  23. get { return fillOpacity; }
  24. set
  25. {
  26. SetProperty(ref fillOpacity, value);
  27. }
  28. }
  29. private Brush selectColor = new SolidColorBrush(Colors.Transparent);
  30. public Brush SelectColor
  31. {
  32. get { return selectColor; }
  33. set
  34. {
  35. SetProperty(ref selectColor, value);
  36. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, (selectColor as SolidColorBrush).Color);
  37. AnnotEvent?.UpdateAnnot();
  38. }
  39. }
  40. private Brush fillColor = new SolidColorBrush(Colors.Transparent);
  41. public Brush FillColor
  42. {
  43. get { return fillColor; }
  44. set
  45. {
  46. SetProperty(ref fillColor, value);
  47. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, (fillColor as SolidColorBrush).Color);
  48. AnnotEvent?.UpdateAnnot();
  49. }
  50. }
  51. private FontFamily fontFamily = new FontFamily("Courier");
  52. public FontFamily TextFontFamily
  53. {
  54. get { return fontFamily; }
  55. set
  56. {
  57. SetProperty(ref fontFamily, value);
  58. }
  59. }
  60. private FontWeight fontWeights = FontWeights.Normal;
  61. public FontWeight TextFontWeights
  62. {
  63. get { return fontWeights; }
  64. set
  65. {
  66. SetProperty(ref fontWeights, value);
  67. }
  68. }
  69. private FontStyle fontStyle = FontStyles.Normal;
  70. public FontStyle TextFontStyle
  71. {
  72. get { return fontStyle; }
  73. set
  74. {
  75. SetProperty(ref fontStyle, value);
  76. }
  77. }
  78. private int fontSize = 24;
  79. public int TextFontSize
  80. {
  81. get { return fontSize; }
  82. set
  83. {
  84. SetProperty(ref fontSize, value);
  85. }
  86. }
  87. public DelegateCommand<object> SelectedColorCommand { get; set; }
  88. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  89. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  90. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  91. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  92. public DelegateCommand<object> TextAlignChecked { get; set; }
  93. public event EventHandler<object> LoadPropertyHandler;
  94. public FreetextAnnotPropertyViewModel()
  95. {
  96. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  97. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  98. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged_Command);
  99. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged_Command);
  100. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged_Command);
  101. TextAlignChecked = new DelegateCommand<object>(TextAlign_Checked);
  102. }
  103. private void TextAlign_Checked(object obj)
  104. {
  105. if (obj != null && (string)obj != null)
  106. {
  107. var tag = (string)obj;
  108. switch (tag)
  109. {
  110. case "AlignLeft":
  111. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  112. break;
  113. case "AlignCenter":
  114. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  115. break;
  116. case "AlignRight":
  117. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  118. break;
  119. }
  120. AnnotEvent?.UpdateAnnot();
  121. }
  122. }
  123. private void FontSizeChanged_Command(object obj)
  124. {
  125. if (obj != null)
  126. {
  127. var item = (ComboBoxItem)obj;
  128. var content = (string)item.Content;
  129. if (content != null)
  130. {
  131. var intData = int.Parse(content);
  132. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, intData);
  133. AnnotEvent?.UpdateAnnot();
  134. }
  135. }
  136. }
  137. private void FontStyleChanged_Command(object obj)
  138. {
  139. if (obj != null)
  140. {
  141. var item = (ComboBoxItem)obj;
  142. var content = (string)item.Content;
  143. if (content != null)
  144. {
  145. if (content == "Regular")
  146. {
  147. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  148. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  149. TextFontWeights = FontWeights.Normal;
  150. TextFontStyle = FontStyles.Normal;
  151. }
  152. if (content == "Bold")
  153. {
  154. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  155. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  156. TextFontWeights = FontWeights.Bold;
  157. TextFontStyle = FontStyles.Normal;
  158. }
  159. if (content == "Italic")
  160. {
  161. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  162. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  163. TextFontWeights = FontWeights.Normal;
  164. TextFontStyle = FontStyles.Italic;
  165. }
  166. if (content == "Bold Italic")
  167. {
  168. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  169. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  170. TextFontWeights = FontWeights.Bold;
  171. TextFontStyle = FontStyles.Italic;
  172. }
  173. AnnotEvent?.UpdateAnnot();
  174. }
  175. }
  176. }
  177. private void FontFamilyChanged_Command(object obj)
  178. {
  179. if (obj != null)
  180. {
  181. if ((int)obj > -1)
  182. {
  183. if ((int)obj == 0)
  184. {
  185. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Courier"));
  186. TextFontFamily = new FontFamily("Courier");
  187. }
  188. if ((int)obj == 1)
  189. {
  190. TextFontFamily = new FontFamily("Helvetica");
  191. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Helvetica"));
  192. }
  193. if ((int)obj == 2)
  194. {
  195. TextFontFamily = new FontFamily("Times");
  196. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Times"));
  197. }
  198. AnnotEvent?.UpdateAnnot();
  199. }
  200. }
  201. }
  202. private void SelectedFillColor_Command(object obj)
  203. {
  204. if (obj != null)
  205. {
  206. var colorValue = (Color)obj;
  207. if (colorValue != null)
  208. {
  209. FillColor = new SolidColorBrush(colorValue);
  210. FillColor.Opacity = FillOpacity;
  211. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  212. changeData[AnnotArgsType.AnnotFreehand] = obj;
  213. PropertyPanel.DataChangedInvoke(this, changeData);
  214. }
  215. }
  216. }
  217. private void SelectedColor_Command(object obj)
  218. {
  219. if (obj != null)
  220. {
  221. var colorValue = (Color)obj;
  222. if (colorValue != null)
  223. {
  224. SelectColor = new SolidColorBrush(colorValue);
  225. SelectColor.Opacity = FillOpacity;
  226. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  227. changeData[AnnotArgsType.AnnotFreehand] = obj;
  228. PropertyPanel.DataChangedInvoke(this, changeData);
  229. }
  230. }
  231. }
  232. public bool IsNavigationTarget(NavigationContext navigationContext)
  233. {
  234. return true;
  235. }
  236. public void OnNavigatedFrom(NavigationContext navigationContext)
  237. {
  238. }
  239. public AnnotAttribEvent AnnotEvent { get; set; }
  240. private FreeTextAnnotArgs Annot;
  241. private AnnotPropertyPanel PropertyPanel;
  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 as FreeTextAnnotArgs;
  249. LoadPropertyHandler?.Invoke(this, Annot);
  250. }
  251. }
  252. }
  253. }