FreehandAnnotPropertyViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  6. using PDF_Office.ViewModels.Tools;
  7. using PDFSettings;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Controls;
  18. using System.Windows.Controls.Primitives;
  19. using System.Windows.Data;
  20. using System.Windows.Media;
  21. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  22. {
  23. public class EraseThicknessConverter : IValueConverter
  24. {
  25. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  26. {
  27. if (value is double)
  28. {
  29. return (double)value * 6;
  30. }
  31. return value;
  32. }
  33. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. }
  38. public class FreehandAnnotPropertyViewModel : BindableBase, INavigationAware
  39. {
  40. #region 属性
  41. private AnnotBasePropertyVM _basicVm = new AnnotBasePropertyVM();
  42. public AnnotBasePropertyVM BasicVm
  43. {
  44. get { return _basicVm; }
  45. set => SetProperty(ref _basicVm, value);
  46. }
  47. private bool _isPen = true;
  48. public bool IsPen
  49. {
  50. get { return _isPen; }
  51. set => SetProperty(ref _isPen, value);
  52. }
  53. private double _erasethicknessLine = 1;
  54. public double EraseThicknessLine
  55. {
  56. get { return _erasethicknessLine; }
  57. set => SetProperty(ref _erasethicknessLine, value);
  58. }
  59. #endregion 属性
  60. public AnnotAttribEvent AnnotEvent { get; set; }
  61. private AnnotHandlerEventArgs Annot;
  62. private AnnotPropertyPanel PropertyPanel;
  63. public DelegateCommand<object> EraseCommand { get; set; }
  64. public DelegateCommand<object> PenCommand { get; set; }
  65. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  66. public DelegateCommand<object> SelectPenThickChangedCommand { get; set; }
  67. public DelegateCommand<object> SetEraserThickCommand { get; set; }
  68. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  69. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  70. public FreehandAnnotPropertyViewModel()
  71. {
  72. EraseCommand = new DelegateCommand<object>(Erase_Command);
  73. PenCommand = new DelegateCommand<object>(Pen_Command);
  74. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged);
  75. SelectPenThickChangedCommand = new DelegateCommand<object>(SelectPenThickChanged);
  76. SetEraserThickCommand = new DelegateCommand<object>(SelectEraserThickChanged);
  77. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  78. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  79. InitVariable();
  80. }
  81. private void InitVariable()
  82. {
  83. }
  84. //设置颜色
  85. private void SelectedColorChanged(object obj)
  86. {
  87. if (obj != null)
  88. {
  89. var colorValue = (Color)obj;
  90. if (colorValue != null)
  91. {
  92. BasicVm.FontColor = new SolidColorBrush(colorValue);
  93. BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
  94. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, colorValue);
  95. if (BasicVm.IsMultiSelected == false)
  96. {
  97. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, colorValue);
  98. }
  99. }
  100. }
  101. }
  102. //设置线条大小
  103. private void SelectPenThickChanged(object obj)
  104. {
  105. if (obj != null && obj is double)
  106. {
  107. var thick = (double)obj;
  108. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, thick);
  109. }
  110. }
  111. //设置橡皮擦大小
  112. private void SelectEraserThickChanged(object obj)
  113. {
  114. if (obj != null && obj is double)
  115. {
  116. var eraser = (double)obj;
  117. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, eraser);
  118. AnnotEvent?.UpdateAnnot();
  119. }
  120. }
  121. //设置线条样式
  122. private void LineMode_Checked(object obj)
  123. {
  124. if (obj != null)
  125. {
  126. var tag = ((string)obj);
  127. DashStyle newDash = new DashStyle();
  128. switch (tag)
  129. {
  130. case "Dashed":
  131. newDash.Dashes.Add(2);
  132. newDash.Dashes.Add(2);
  133. // AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  134. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, newDash);
  135. break;
  136. case "Solid":
  137. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  138. break;
  139. }
  140. AnnotEvent?.UpdateAnnot();
  141. }
  142. }
  143. //设置不透明度
  144. private void SelectedOpacityValue(object obj)
  145. {
  146. if (obj != null)
  147. {
  148. BasicVm.FillOpacity = (double)obj;
  149. BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
  150. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  151. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, BasicVm.FillOpacity);
  152. }
  153. }
  154. //选择橡皮擦
  155. private void Erase_Command(object obj)
  156. {
  157. var btn = obj as ToggleButton;
  158. if(btn.IsChecked == true)
  159. {
  160. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  161. IsPen = false;
  162. }
  163. }
  164. //选择画笔
  165. private void Pen_Command(object obj)
  166. {
  167. var btn = obj as ToggleButton;
  168. if (btn.IsChecked == true)
  169. {
  170. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  171. IsPen = true;
  172. }
  173. }
  174. public bool IsNavigationTarget(NavigationContext navigationContext)
  175. {
  176. return true;
  177. }
  178. public void OnNavigatedFrom(NavigationContext navigationContext)
  179. {
  180. BasicVm.IsMultiSelected = false;
  181. }
  182. public void OnNavigatedTo(NavigationContext navigationContext)
  183. {
  184. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  185. if (PropertyPanel != null)
  186. {
  187. AnnotEvent = PropertyPanel.AnnotEvent;
  188. Annot = PropertyPanel.annot;
  189. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  190. if(Annot is FreehandAnnotArgs)
  191. {
  192. BasicVm.AnnotTypeTitle = "画笔";
  193. }
  194. else
  195. {
  196. BasicVm.AnnotTypeTitle = "橡皮擦";
  197. }
  198. if (BasicVm.IsMultiSelected)
  199. {
  200. IsAttributeEquals();
  201. BasicVm.SetStrDashStyle("None");
  202. }
  203. else
  204. {
  205. GetAnnotProperty();
  206. }
  207. }
  208. }
  209. private List<FreehandAnnotArgs> ConvertLists()
  210. {
  211. List<FreehandAnnotArgs> Lists = new List<FreehandAnnotArgs>();
  212. foreach (var item in PropertyPanel.annotlists)
  213. {
  214. var selecteditem = item as FreehandAnnotArgs;
  215. if (selecteditem != null)
  216. {
  217. Lists.Add(selecteditem);
  218. }
  219. }
  220. if (Lists.Count != PropertyPanel.annotlists.Count)
  221. return null;
  222. else
  223. return Lists;
  224. }
  225. private void IsAttributeEquals()
  226. {
  227. var list = ConvertLists();
  228. if (list != null)
  229. {
  230. var temp = list[0];
  231. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  232. isNoEqualsDir.Add("Color", false);
  233. isNoEqualsDir.Add("Thickness", false);
  234. isNoEqualsDir.Add("FontStyleFontWeight", false);
  235. isNoEqualsDir.Add("ThickSolidDashStyle", false);
  236. foreach (var item in list)
  237. {
  238. if (item == list[0])
  239. continue;
  240. if (isNoEqualsDir["Color"] == false)
  241. {
  242. if (temp.InkColor.A != item.InkColor.A || temp.InkColor.R != item.InkColor.R || temp.InkColor.G != item.InkColor.G || temp.InkColor.B != item.InkColor.B)
  243. {
  244. BasicVm.FontColor = new SolidColorBrush(Colors.Transparent);
  245. isNoEqualsDir["Color"] = true;
  246. }
  247. }
  248. if (isNoEqualsDir["Thickness"] == false)
  249. {
  250. isNoEqualsDir["Thickness"] = true;
  251. if (temp.LineWidth > item.LineWidth)
  252. {
  253. BasicVm.AnnotThickness = temp.LineWidth;
  254. }
  255. else
  256. {
  257. BasicVm.AnnotThickness = item.LineWidth;
  258. }
  259. }
  260. if(isNoEqualsDir["ThickSolidDashStyle"] == false)
  261. {
  262. if(isSolidStyle(temp) != isSolidStyle(item))
  263. {
  264. isNoEqualsDir["ThickSolidDashStyle"] = true;
  265. }
  266. }
  267. }
  268. if (isNoEqualsDir["Color"] == false)
  269. {
  270. BasicVm.FontColor = new SolidColorBrush(temp.InkColor);
  271. }
  272. if (isNoEqualsDir["Thickness"] == false)
  273. {
  274. BasicVm.AnnotThickness = temp.LineWidth;
  275. }
  276. if(isNoEqualsDir["ThickSolidDashStyle"] == true)
  277. {
  278. var isSolid = isSolidStyle(temp);
  279. BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
  280. }
  281. else
  282. {
  283. BasicVm.SetStrDashStyle("None");
  284. }
  285. }
  286. }
  287. //外部UI控件选中状态
  288. private bool isSolidStyle(FreehandAnnotArgs annot)
  289. {
  290. bool isSolid = true;
  291. if (annot.LineDash != null && annot.LineDash.Dashes.Count > 0)
  292. {
  293. foreach (var item in annot.LineDash.Dashes)
  294. {
  295. if (item > 0)
  296. {
  297. isSolid = false;
  298. break;
  299. }
  300. }
  301. }
  302. return isSolid;
  303. }
  304. private void GetAnnotProperty()
  305. {
  306. if (Annot is FreehandAnnotArgs)
  307. {
  308. var annot = Annot as FreehandAnnotArgs;
  309. if (annot != null)
  310. {
  311. BasicVm.FillOpacity = annot.Transparency;
  312. BasicVm.FontColor = new SolidColorBrush(annot.InkColor);
  313. BasicVm.AnnotThickness = annot.LineWidth;
  314. BasicVm.Dash = annot.LineDash;
  315. var isSolid = isSolidStyle(annot);
  316. BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
  317. IsPen = true;
  318. }
  319. }
  320. if (Annot is EraseArgs)
  321. {
  322. var annot = Annot as EraseArgs;
  323. if (annot != null)
  324. {
  325. EraseThicknessLine = annot.Thickness;
  326. IsPen = false;
  327. }
  328. }
  329. }
  330. }
  331. }