FreehandAnnotPropertyViewModel.cs 9.3 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. #region 属性
  39. private AnnotBasePropertyVM _basicVm = new AnnotBasePropertyVM();
  40. public AnnotBasePropertyVM BasicVm
  41. {
  42. get { return _basicVm; }
  43. set => SetProperty(ref _basicVm, value);
  44. }
  45. private bool _isPen = true;
  46. public bool IsPen
  47. {
  48. get { return _isPen; }
  49. set => SetProperty(ref _isPen, value);
  50. }
  51. private double _erasethicknessLine = 1;
  52. public double EraseThicknessLine
  53. {
  54. get { return _erasethicknessLine; }
  55. set => SetProperty(ref _erasethicknessLine, value);
  56. }
  57. #endregion 属性
  58. public AnnotAttribEvent AnnotEvent { get; set; }
  59. private AnnotHandlerEventArgs Annot;
  60. private AnnotPropertyPanel PropertyPanel;
  61. public DelegateCommand<object> EraseCommand { get; set; }
  62. public DelegateCommand<object> PenCommand { get; set; }
  63. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  64. public DelegateCommand<object> SelectPenThickChangedCommand { get; set; }
  65. public DelegateCommand<object> SetEraserThickCommand { get; set; }
  66. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  67. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  68. public FreehandAnnotPropertyViewModel()
  69. {
  70. EraseCommand = new DelegateCommand<object>(Erase_Command);
  71. PenCommand = new DelegateCommand<object>(Pen_Command);
  72. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged);
  73. SelectPenThickChangedCommand = new DelegateCommand<object>(SelectPenThickChanged);
  74. SetEraserThickCommand = new DelegateCommand<object>(SelectEraserThickChanged);
  75. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  76. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  77. InitVariable();
  78. }
  79. private void InitVariable()
  80. {
  81. }
  82. //设置颜色
  83. private void SelectedColorChanged(object obj)
  84. {
  85. if (obj != null)
  86. {
  87. var colorValue = (Color)obj;
  88. if (colorValue != null)
  89. {
  90. BasicVm.FontColor = new SolidColorBrush(colorValue);
  91. BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
  92. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, colorValue);
  93. if (BasicVm.IsMultiSelected == false)
  94. {
  95. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, colorValue);
  96. }
  97. }
  98. }
  99. }
  100. //设置线条大小
  101. private void SelectPenThickChanged(object obj)
  102. {
  103. if (obj != null && obj is double)
  104. {
  105. var thick = (double)obj;
  106. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, thick);
  107. }
  108. }
  109. //设置橡皮擦大小
  110. private void SelectEraserThickChanged(object obj)
  111. {
  112. if (obj != null && obj is double)
  113. {
  114. var eraser = (double)obj;
  115. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, eraser);
  116. AnnotEvent?.UpdateAnnot();
  117. }
  118. }
  119. //设置线条样式
  120. private void LineMode_Checked(object obj)
  121. {
  122. if (obj != null)
  123. {
  124. var tag = ((string)obj);
  125. DashStyle newDash = new DashStyle();
  126. switch (tag)
  127. {
  128. case "Dashed":
  129. newDash.Dashes.Add(2);
  130. newDash.Dashes.Add(2);
  131. // AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  132. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, newDash);
  133. break;
  134. case "Solid":
  135. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  136. break;
  137. }
  138. AnnotEvent?.UpdateAnnot();
  139. }
  140. }
  141. //设置不透明度
  142. private void SelectedOpacityValue(object obj)
  143. {
  144. if (obj != null)
  145. {
  146. BasicVm.FillOpacity = (double)obj;
  147. BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
  148. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  149. AnnotEvent?.UpdateAnnot();
  150. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, BasicVm.FillOpacity);
  151. }
  152. }
  153. //选择橡皮擦
  154. private void Erase_Command(object obj)
  155. {
  156. var btn = obj as ToggleButton;
  157. if(btn.IsChecked == true)
  158. {
  159. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  160. IsPen = false;
  161. }
  162. }
  163. //选择画笔
  164. private void Pen_Command(object obj)
  165. {
  166. var btn = obj as ToggleButton;
  167. if (btn.IsChecked == true)
  168. {
  169. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  170. IsPen = true;
  171. }
  172. }
  173. public bool IsNavigationTarget(NavigationContext navigationContext)
  174. {
  175. return true;
  176. }
  177. public void OnNavigatedFrom(NavigationContext navigationContext)
  178. {
  179. BasicVm.IsMultiSelected = false;
  180. }
  181. public void OnNavigatedTo(NavigationContext navigationContext)
  182. {
  183. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  184. if (PropertyPanel != null)
  185. {
  186. AnnotEvent = PropertyPanel.AnnotEvent;
  187. Annot = PropertyPanel.annot;
  188. if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count > 1)
  189. {
  190. BasicVm.IsMultiSelected = true;
  191. }
  192. else
  193. {
  194. BasicVm.IsMultiSelected = false;
  195. }
  196. if (BasicVm.IsMultiSelected)
  197. {
  198. double thickness = 0;
  199. foreach(var item in PropertyPanel.annotlists)
  200. {
  201. if ((item as FreehandAnnotArgs).LineWidth >= thickness)
  202. thickness = (item as FreehandAnnotArgs).LineWidth;
  203. }
  204. BasicVm.AnnotThickness = thickness;
  205. BasicVm.SetStrDashStyle("None");
  206. }
  207. else
  208. {
  209. GetAnnotProperty();
  210. }
  211. }
  212. }
  213. private void GetAnnotProperty()
  214. {
  215. if (Annot is FreehandAnnotArgs)
  216. {
  217. var annot = Annot as FreehandAnnotArgs;
  218. if (annot != null)
  219. {
  220. BasicVm.FillOpacity = annot.Transparency;
  221. BasicVm.FontColor = new SolidColorBrush(annot.InkColor);
  222. BasicVm.AnnotThickness = annot.LineWidth;
  223. BasicVm.Dash = annot.LineDash;
  224. bool isSolid = true;
  225. if(annot.LineDash != null && annot.LineDash.Dashes.Count > 0)
  226. {
  227. foreach(var item in annot.LineDash.Dashes)
  228. {
  229. if(item > 0)
  230. {
  231. isSolid = false;
  232. break;
  233. }
  234. }
  235. }
  236. BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
  237. IsPen = true;
  238. }
  239. }
  240. if (Annot is EraseArgs)
  241. {
  242. var annot = Annot as EraseArgs;
  243. if (annot != null)
  244. {
  245. EraseThicknessLine = annot.Thickness;
  246. IsPen = false;
  247. }
  248. }
  249. }
  250. }
  251. }