FreehandAnnotPropertyViewModel.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.Globalization;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Data;
  18. using System.Windows.Media;
  19. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  20. {
  21. public class EraseThicknessConverter : IValueConverter
  22. {
  23. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  24. {
  25. if (value is double)
  26. {
  27. return (double)value * 6;
  28. }
  29. return value;
  30. }
  31. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  32. {
  33. throw new NotImplementedException();
  34. }
  35. }
  36. public class FreehandAnnotPropertyViewModel : BindableBase, INavigationAware
  37. {
  38. private bool isPen = true;
  39. public bool IsPen
  40. {
  41. get { return isPen; }
  42. set
  43. {
  44. SetProperty(ref isPen, value);
  45. }
  46. }
  47. private Brush selectColor = new SolidColorBrush(Colors.Transparent);
  48. public Brush SelectColor
  49. {
  50. get { return selectColor; }
  51. set { SetProperty(ref selectColor, value);
  52. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (SelectColor as SolidColorBrush).Color);
  53. AnnotEvent?.UpdateAnnot();
  54. }
  55. }
  56. private double annotOpacity = 1;
  57. public double AnnotOpacity
  58. {
  59. get { return annotOpacity; }
  60. set
  61. {
  62. SetProperty(ref annotOpacity, value);
  63. }
  64. }
  65. private double thicknessLine = 1;
  66. public double ThicknessLine
  67. {
  68. get { return thicknessLine; }
  69. set
  70. {
  71. SetProperty(ref thicknessLine, value);
  72. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, thicknessLine);
  73. AnnotEvent?.UpdateAnnot();
  74. }
  75. }
  76. private double erasethicknessLine = 1;
  77. public double EraseThicknessLine
  78. {
  79. get { return erasethicknessLine; }
  80. set
  81. {
  82. SetProperty(ref erasethicknessLine, value);
  83. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, erasethicknessLine);
  84. AnnotEvent?.UpdateAnnot();
  85. }
  86. }
  87. public AnnotAttribEvent AnnotEvent { get; set; }
  88. private AnnotHandlerEventArgs Annot;
  89. private AnnotPropertyPanel PropertyPanel;
  90. public DelegateCommand<object> EraseCommand { get; set; }
  91. public DelegateCommand<object> PenCommand { get; set; }
  92. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  93. public DelegateCommand<object> SelectPenThickChangedCommand { get; set; }
  94. public DelegateCommand<object> SetEraserThickCommand { get; set; }
  95. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  96. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  97. public FreehandAnnotPropertyViewModel()
  98. {
  99. EraseCommand = new DelegateCommand<object>(Erase_Command);
  100. PenCommand = new DelegateCommand<object>(Pen_Command);
  101. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
  102. SelectPenThickChangedCommand = new DelegateCommand<object>(SelectPenThickChanged_Command);
  103. SetEraserThickCommand = new DelegateCommand<object>(SelectEraserThickChanged_Command);
  104. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  105. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  106. InitVariable();
  107. }
  108. private void InitVariable()
  109. {
  110. }
  111. private void SelectEraserThickChanged_Command(object obj)
  112. {
  113. if (obj != null)
  114. {
  115. var item = (ComboBoxItem)obj;
  116. var content = (string)item.Content;
  117. if (content != null)
  118. {
  119. var intData = double.Parse(content);
  120. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
  121. AnnotEvent?.UpdateAnnot();
  122. EraseThicknessLine = intData;
  123. }
  124. }
  125. }
  126. private void LineMode_Checked(object obj)
  127. {
  128. if (obj != null)
  129. {
  130. var tag = ((string)obj);
  131. switch (tag)
  132. {
  133. case "Dashed":
  134. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  135. break;
  136. case "Solid":
  137. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  138. break;
  139. }
  140. AnnotEvent?.UpdateAnnot();
  141. }
  142. }
  143. private void SelectPenThickChanged_Command(object obj)
  144. {
  145. if (obj != null)
  146. {
  147. var item = (ComboBoxItem)obj;
  148. var content = (string)item.Content;
  149. if (content != null)
  150. {
  151. var intData = double.Parse(content);
  152. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
  153. AnnotEvent?.UpdateAnnot();
  154. ThicknessLine = intData;
  155. }
  156. }
  157. }
  158. private void SelectedOpacityValue(object obj)
  159. {
  160. if (obj != null)
  161. {
  162. annotOpacity = (double)obj;
  163. SelectColor.Opacity = annotOpacity;
  164. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  165. AnnotEvent?.UpdateAnnot();
  166. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  167. changeData[AnnotArgsType.AnnotFreehand] = annotOpacity;
  168. PropertyPanel.DataChangedInvoke(this, changeData);
  169. }
  170. }
  171. private void SelectedColorChanged_Click(object obj)
  172. {
  173. if (obj != null)
  174. {
  175. var colorValue = (Color)obj;
  176. if (colorValue != null)
  177. {
  178. SelectColor = new SolidColorBrush(colorValue);
  179. SelectColor.Opacity = AnnotOpacity;
  180. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  181. changeData[AnnotArgsType.AnnotFreehand] = obj;
  182. PropertyPanel.DataChangedInvoke(this, changeData);
  183. }
  184. }
  185. }
  186. private void Erase_Command(object obj)
  187. {
  188. var btn = obj as ToggleButton;
  189. if(btn.IsChecked == true)
  190. {
  191. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  192. changeData[AnnotArgsType.AnnotErase] = btn;
  193. PropertyPanel.DataChangedInvoke(this, changeData);
  194. IsPen = false;
  195. }
  196. }
  197. private void Pen_Command(object obj)
  198. {
  199. var btn = obj as ToggleButton;
  200. if (btn.IsChecked == true)
  201. {
  202. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  203. changeData[AnnotArgsType.AnnotErase] = btn;
  204. PropertyPanel.DataChangedInvoke(this, changeData);
  205. IsPen = true;
  206. }
  207. }
  208. public bool IsNavigationTarget(NavigationContext navigationContext)
  209. {
  210. return true;
  211. }
  212. public void OnNavigatedFrom(NavigationContext navigationContext)
  213. {
  214. }
  215. public void OnNavigatedTo(NavigationContext navigationContext)
  216. {
  217. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  218. if (PropertyPanel != null)
  219. {
  220. AnnotEvent = PropertyPanel.AnnotEvent;
  221. Annot = PropertyPanel.annot;
  222. GetAnnotProperty();
  223. }
  224. }
  225. private void GetAnnotProperty()
  226. {
  227. if (Annot is FreehandAnnotArgs)
  228. {
  229. var annot = Annot as FreehandAnnotArgs;
  230. if (annot != null)
  231. {
  232. AnnotOpacity = annot.Transparency;
  233. SelectColor = new SolidColorBrush(annot.InkColor);
  234. ThicknessLine = annot.LineWidth;
  235. IsPen = true;
  236. }
  237. }
  238. if (Annot is EraseArgs)
  239. {
  240. var annot = Annot as EraseArgs;
  241. if (annot != null)
  242. {
  243. EraseThicknessLine = annot.Thickness;
  244. IsPen = false;
  245. }
  246. }
  247. }
  248. }
  249. }