FreehandAnnotPropertyViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. if (BasicVm.IsMultiSelected)
  91. {
  92. foreach (var item in PropertyPanel.AnnotEvents)
  93. {
  94. item?.UpdateAttrib(AnnotAttrib.Color, colorValue);
  95. item?.UpdateAnnot();
  96. }
  97. }
  98. else
  99. {
  100. BasicVm.FontColor = new SolidColorBrush(colorValue);
  101. BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
  102. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, colorValue);
  103. AnnotEvent?.UpdateAnnot();
  104. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, colorValue);
  105. }
  106. }
  107. }
  108. }
  109. //设置线条大小
  110. private void SelectPenThickChanged(object obj)
  111. {
  112. if (obj != null && obj is double)
  113. {
  114. var thick = (double)obj;
  115. if (BasicVm.IsMultiSelected)
  116. {
  117. foreach (var itemEvent in PropertyPanel.AnnotEvents)
  118. {
  119. itemEvent?.UpdateAttrib(AnnotAttrib.Thickness, thick);
  120. itemEvent?.UpdateAnnot();
  121. }
  122. }
  123. else
  124. {
  125. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, thick);
  126. AnnotEvent?.UpdateAnnot();
  127. }
  128. }
  129. }
  130. //设置橡皮擦大小
  131. private void SelectEraserThickChanged(object obj)
  132. {
  133. if (obj != null && obj is double)
  134. {
  135. var eraser = (double)obj;
  136. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, eraser);
  137. AnnotEvent?.UpdateAnnot();
  138. }
  139. }
  140. //设置线条样式
  141. private void LineMode_Checked(object obj)
  142. {
  143. if (obj != null)
  144. {
  145. var tag = ((string)obj);
  146. DashStyle newDash = new DashStyle();
  147. switch (tag)
  148. {
  149. case "Dashed":
  150. newDash.Dashes.Add(2);
  151. newDash.Dashes.Add(2);
  152. // AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  153. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, newDash);
  154. break;
  155. case "Solid":
  156. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  157. break;
  158. }
  159. AnnotEvent?.UpdateAnnot();
  160. }
  161. }
  162. //设置不透明度
  163. private void SelectedOpacityValue(object obj)
  164. {
  165. if (obj != null)
  166. {
  167. BasicVm.FillOpacity = (double)obj;
  168. BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
  169. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  170. AnnotEvent?.UpdateAnnot();
  171. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, BasicVm.FillOpacity);
  172. }
  173. }
  174. //选择橡皮擦
  175. private void Erase_Command(object obj)
  176. {
  177. var btn = obj as ToggleButton;
  178. if(btn.IsChecked == true)
  179. {
  180. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  181. IsPen = false;
  182. }
  183. }
  184. //选择画笔
  185. private void Pen_Command(object obj)
  186. {
  187. var btn = obj as ToggleButton;
  188. if (btn.IsChecked == true)
  189. {
  190. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  191. IsPen = true;
  192. }
  193. }
  194. public bool IsNavigationTarget(NavigationContext navigationContext)
  195. {
  196. return true;
  197. }
  198. public void OnNavigatedFrom(NavigationContext navigationContext)
  199. {
  200. BasicVm.IsMultiSelected = false;
  201. }
  202. public void OnNavigatedTo(NavigationContext navigationContext)
  203. {
  204. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  205. if (PropertyPanel != null)
  206. {
  207. AnnotEvent = PropertyPanel.AnnotEvent;
  208. Annot = PropertyPanel.annot;
  209. if (PropertyPanel.annotlists != null && PropertyPanel.annotlists.Count > 1)
  210. {
  211. BasicVm.IsMultiSelected = true;
  212. }
  213. else
  214. {
  215. BasicVm.IsMultiSelected = false;
  216. }
  217. if (BasicVm.IsMultiSelected)
  218. {
  219. double thickness = 0;
  220. foreach(var item in PropertyPanel.annotlists)
  221. {
  222. if ((item as FreehandAnnotArgs).LineWidth >= thickness)
  223. thickness = (item as FreehandAnnotArgs).LineWidth;
  224. }
  225. BasicVm.AnnotThickness = thickness;
  226. BasicVm.SetStrDashStyle("None");
  227. }
  228. else
  229. {
  230. GetAnnotProperty();
  231. }
  232. }
  233. }
  234. private void GetAnnotProperty()
  235. {
  236. if (Annot is FreehandAnnotArgs)
  237. {
  238. var annot = Annot as FreehandAnnotArgs;
  239. if (annot != null)
  240. {
  241. BasicVm.FillOpacity = annot.Transparency;
  242. BasicVm.FontColor = new SolidColorBrush(annot.InkColor);
  243. BasicVm.AnnotThickness = annot.LineWidth;
  244. BasicVm.Dash = annot.LineDash;
  245. bool isSolid = true;
  246. if(annot.LineDash != null && annot.LineDash.Dashes.Count > 0)
  247. {
  248. foreach(var item in annot.LineDash.Dashes)
  249. {
  250. if(item > 0)
  251. {
  252. isSolid = false;
  253. break;
  254. }
  255. }
  256. }
  257. BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
  258. IsPen = true;
  259. }
  260. }
  261. if (Annot is EraseArgs)
  262. {
  263. var annot = Annot as EraseArgs;
  264. if (annot != null)
  265. {
  266. EraseThicknessLine = annot.Thickness;
  267. IsPen = false;
  268. }
  269. }
  270. }
  271. }
  272. }