FreehandAnnotPropertyViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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> SelectedValueChangedCommand { get; set; }
  94. public DelegateCommand<object> SelectPenThickChangedCommand { get; set; }
  95. public DelegateCommand<object> SetEraserThickCommand { get; set; }
  96. public FreehandAnnotPropertyViewModel()
  97. {
  98. EraseCommand = new DelegateCommand<object>(Erase_Command);
  99. PenCommand = new DelegateCommand<object>(Pen_Command);
  100. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
  101. SelectedValueChangedCommand = new DelegateCommand<object>(SelectedValueChanged_Command);
  102. SelectPenThickChangedCommand = new DelegateCommand<object>(SelectPenThickChanged_Command);
  103. SetEraserThickCommand = new DelegateCommand<object>(SelectEraserThickChanged_Command);
  104. InitVariable();
  105. }
  106. private void InitVariable()
  107. {
  108. }
  109. private void SelectEraserThickChanged_Command(object obj)
  110. {
  111. if (obj != null)
  112. {
  113. var item = (ComboBoxItem)obj;
  114. var content = (string)item.Content;
  115. if (content != null)
  116. {
  117. var intData = double.Parse(content);
  118. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
  119. AnnotEvent?.UpdateAnnot();
  120. EraseThicknessLine = intData;
  121. }
  122. }
  123. }
  124. private void SelectPenThickChanged_Command(object obj)
  125. {
  126. if (obj != null)
  127. {
  128. var item = (ComboBoxItem)obj;
  129. var content = (string)item.Content;
  130. if (content != null)
  131. {
  132. var intData = double.Parse(content);
  133. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, intData);
  134. AnnotEvent?.UpdateAnnot();
  135. ThicknessLine = intData;
  136. }
  137. }
  138. }
  139. private void SelectedValueChanged_Command(object obj)
  140. {
  141. if (obj != null)
  142. {
  143. annotOpacity = (double)obj;
  144. SelectColor.Opacity = annotOpacity;
  145. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, annotOpacity);
  146. AnnotEvent?.UpdateAnnot();
  147. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  148. changeData[AnnotArgsType.AnnotFreehand] = annotOpacity;
  149. PropertyPanel.DataChangedInvoke(this, changeData);
  150. }
  151. }
  152. private void SelectedColorChanged_Click(object obj)
  153. {
  154. if (obj != null)
  155. {
  156. var colorValue = (Color)obj;
  157. if (colorValue != null)
  158. {
  159. SelectColor = new SolidColorBrush(colorValue);
  160. SelectColor.Opacity = AnnotOpacity;
  161. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  162. changeData[AnnotArgsType.AnnotFreehand] = obj;
  163. PropertyPanel.DataChangedInvoke(this, changeData);
  164. }
  165. }
  166. }
  167. private void Erase_Command(object obj)
  168. {
  169. var btn = obj as ToggleButton;
  170. if(btn.IsChecked == true)
  171. {
  172. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  173. changeData[AnnotArgsType.AnnotErase] = btn;
  174. PropertyPanel.DataChangedInvoke(this, changeData);
  175. IsPen = false;
  176. }
  177. }
  178. private void Pen_Command(object obj)
  179. {
  180. var btn = obj as ToggleButton;
  181. if (btn.IsChecked == true)
  182. {
  183. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  184. changeData[AnnotArgsType.AnnotErase] = btn;
  185. PropertyPanel.DataChangedInvoke(this, changeData);
  186. IsPen = true;
  187. }
  188. }
  189. public bool IsNavigationTarget(NavigationContext navigationContext)
  190. {
  191. return true;
  192. }
  193. public void OnNavigatedFrom(NavigationContext navigationContext)
  194. {
  195. }
  196. public void OnNavigatedTo(NavigationContext navigationContext)
  197. {
  198. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  199. if (PropertyPanel != null)
  200. {
  201. AnnotEvent = PropertyPanel.AnnotEvent;
  202. Annot = PropertyPanel.annot;
  203. GetAnnotProperty();
  204. }
  205. }
  206. private void GetAnnotProperty()
  207. {
  208. if (Annot is FreehandAnnotArgs)
  209. {
  210. var annot = Annot as FreehandAnnotArgs;
  211. if (annot != null)
  212. {
  213. AnnotOpacity = annot.Transparency;
  214. SelectColor = new SolidColorBrush(annot.InkColor);
  215. ThicknessLine = annot.LineWidth;
  216. IsPen = true;
  217. }
  218. }
  219. if (Annot is EraseArgs)
  220. {
  221. var annot = Annot as EraseArgs;
  222. if (annot != null)
  223. {
  224. EraseThicknessLine = annot.Thickness;
  225. IsPen = false;
  226. }
  227. }
  228. }
  229. }
  230. }