FreetextAnnotPropertyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  5. using PDF_Office.ViewModels.Tools;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Media;
  17. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  18. {
  19. public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware
  20. {
  21. private double fillOpacity = 1;
  22. public double FillOpacity
  23. {
  24. get { return fillOpacity; }
  25. set
  26. {
  27. SetProperty(ref fillOpacity, value);
  28. }
  29. }
  30. private Brush selectColor = new SolidColorBrush(Colors.Black);
  31. public Brush SelectColor
  32. {
  33. get { return selectColor; }
  34. set
  35. {
  36. SetProperty(ref selectColor, value);
  37. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, (selectColor as SolidColorBrush).Color);
  38. AnnotEvent?.UpdateAnnot();
  39. }
  40. }
  41. private Brush fillColor = new SolidColorBrush(Colors.Transparent);
  42. public Brush FillColor
  43. {
  44. get { return fillColor; }
  45. set
  46. {
  47. SetProperty(ref fillColor, value);
  48. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, (fillColor as SolidColorBrush).Color);
  49. AnnotEvent?.UpdateAnnot();
  50. }
  51. }
  52. private FontFamily fontFamily = new FontFamily("Courier");
  53. public FontFamily TextFontFamily
  54. {
  55. get { return fontFamily; }
  56. set
  57. {
  58. SetProperty(ref fontFamily, value);
  59. }
  60. }
  61. private FontWeight fontWeights = FontWeights.Normal;
  62. public FontWeight TextFontWeights
  63. {
  64. get { return fontWeights; }
  65. set
  66. {
  67. SetProperty(ref fontWeights, value);
  68. }
  69. }
  70. private FontStyle fontStyle = FontStyles.Normal;
  71. public FontStyle TextFontStyle
  72. {
  73. get { return fontStyle; }
  74. set
  75. {
  76. SetProperty(ref fontStyle, value);
  77. }
  78. }
  79. private int fontSize = 24;
  80. public int TextFontSize
  81. {
  82. get { return fontSize; }
  83. set
  84. {
  85. SetProperty(ref fontSize, value);
  86. }
  87. }
  88. private List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
  89. public List<FontStyleItem> FontStyleList
  90. {
  91. get { return fontStyleList; }
  92. set
  93. {
  94. SetProperty(ref fontStyleList, value);
  95. }
  96. }
  97. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  98. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  99. public DelegateCommand<object> SelectedColorCommand { get; set; }
  100. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  101. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  102. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  103. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  104. public DelegateCommand<object> TextAlignChecked { get; set; }
  105. public event EventHandler<object> LoadPropertyHandler;
  106. public FreetextAnnotPropertyViewModel()
  107. {
  108. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
  109. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  110. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  111. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  112. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged_Command);
  113. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged_Command);
  114. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged_Command);
  115. TextAlignChecked = new DelegateCommand<object>(TextAlign_Checked);
  116. InitVariable();
  117. }
  118. private void InitVariable()
  119. {
  120. InitFontStyles();
  121. }
  122. private void InitFontStyles()
  123. {
  124. FontStyleList = LoadFontStyle.Load();
  125. }
  126. private void TextAlign_Checked(object obj)
  127. {
  128. if (obj != null && (string)obj != null)
  129. {
  130. var tag = (string)obj;
  131. switch (tag)
  132. {
  133. case "AlignLeft":
  134. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  135. break;
  136. case "AlignCenter":
  137. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  138. break;
  139. case "AlignRight":
  140. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  141. break;
  142. }
  143. AnnotEvent?.UpdateAnnot();
  144. }
  145. }
  146. private void SelectedFontStyle(object obj)
  147. {
  148. if (obj != null && (FontStyleItem)obj != null)
  149. {
  150. var item = (FontStyleItem)obj;
  151. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, item.mFontSize);
  152. AnnotEvent?.UpdateAnnot();
  153. }
  154. }
  155. private void SelectedFillOpacity(object obj)
  156. {
  157. if (obj != null)
  158. {
  159. FillOpacity = (double)obj;
  160. SelectColor.Opacity = FillOpacity;
  161. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  162. AnnotEvent?.UpdateAnnot();
  163. }
  164. }
  165. private void FontSizeChanged_Command(object obj)
  166. {
  167. if (obj != null)
  168. {
  169. var item = (ComboBoxItem)obj;
  170. var content = (string)item.Content;
  171. if (content != null)
  172. {
  173. var intData = int.Parse(content);
  174. TextFontSize = intData;
  175. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, intData);
  176. AnnotEvent?.UpdateAnnot();
  177. }
  178. }
  179. }
  180. private void FontStyleChanged_Command(object obj)
  181. {
  182. if (obj != null)
  183. {
  184. var item = (ComboBoxItem)obj;
  185. var content = (string)item.Content;
  186. if (content != null)
  187. {
  188. if (content == "Regular")
  189. {
  190. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  191. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  192. TextFontWeights = FontWeights.Normal;
  193. TextFontStyle = FontStyles.Normal;
  194. }
  195. if (content == "Bold")
  196. {
  197. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  198. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  199. TextFontWeights = FontWeights.Bold;
  200. TextFontStyle = FontStyles.Normal;
  201. }
  202. if (content == "Italic")
  203. {
  204. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  205. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  206. TextFontWeights = FontWeights.Normal;
  207. TextFontStyle = FontStyles.Italic;
  208. }
  209. if (content == "Bold Italic")
  210. {
  211. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  212. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  213. TextFontWeights = FontWeights.Bold;
  214. TextFontStyle = FontStyles.Italic;
  215. }
  216. AnnotEvent?.UpdateAnnot();
  217. }
  218. }
  219. }
  220. private void FontFamilyChanged_Command(object obj)
  221. {
  222. if (obj != null)
  223. {
  224. if ((int)obj > -1)
  225. {
  226. if ((int)obj == 0)
  227. {
  228. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Courier"));
  229. TextFontFamily = new FontFamily("Courier");
  230. }
  231. if ((int)obj == 1)
  232. {
  233. TextFontFamily = new FontFamily("Helvetica");
  234. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Helvetica"));
  235. }
  236. if ((int)obj == 2)
  237. {
  238. TextFontFamily = new FontFamily("Times");
  239. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Times"));
  240. }
  241. AnnotEvent?.UpdateAnnot();
  242. }
  243. }
  244. }
  245. private void SelectedFillColor_Command(object obj)
  246. {
  247. if (obj != null)
  248. {
  249. var colorValue = (Color)obj;
  250. if (colorValue != null)
  251. {
  252. FillColor = new SolidColorBrush(colorValue);
  253. FillColor.Opacity = FillOpacity;
  254. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  255. changeData[AnnotArgsType.AnnotFreehand] = obj;
  256. PropertyPanel.DataChangedInvoke(this, changeData);
  257. }
  258. }
  259. }
  260. private void SelectedColor_Command(object obj)
  261. {
  262. if (obj != null)
  263. {
  264. var colorValue = (Color)obj;
  265. if (colorValue != null)
  266. {
  267. SelectColor = new SolidColorBrush(colorValue);
  268. SelectColor.Opacity = FillOpacity;
  269. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  270. changeData[AnnotArgsType.AnnotFreehand] = obj;
  271. PropertyPanel.DataChangedInvoke(this, changeData);
  272. }
  273. }
  274. }
  275. public bool IsNavigationTarget(NavigationContext navigationContext)
  276. {
  277. return true;
  278. }
  279. public void OnNavigatedFrom(NavigationContext navigationContext)
  280. {
  281. }
  282. public AnnotAttribEvent AnnotEvent { get; set; }
  283. private FreeTextAnnotArgs Annot;
  284. private AnnotPropertyPanel PropertyPanel;
  285. public void OnNavigatedTo(NavigationContext navigationContext)
  286. {
  287. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  288. if (PropertyPanel != null)
  289. {
  290. AnnotEvent = PropertyPanel.AnnotEvent;
  291. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  292. LoadPropertyHandler?.Invoke(this, Annot);
  293. }
  294. }
  295. }
  296. }