FreehandAnnotPropertyViewModel.cs 8.3 KB

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