FreehandAnnotPropertyViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, newDash);
  134. break;
  135. case "Solid":
  136. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  137. break;
  138. }
  139. AnnotEvent?.UpdateAnnot();
  140. }
  141. }
  142. //设置不透明度
  143. private void SelectedOpacityValue(object obj)
  144. {
  145. if (obj != null)
  146. {
  147. BasicVm.FillOpacity = (double)obj;
  148. BasicVm.FontColor.Opacity = BasicVm.FillOpacity;
  149. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  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. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  189. if(Annot is FreehandAnnotArgs)
  190. {
  191. BasicVm.AnnotTypeTitle = "画笔";
  192. }
  193. else
  194. {
  195. BasicVm.AnnotTypeTitle = "橡皮擦";
  196. }
  197. if (BasicVm.IsMultiSelected)
  198. {
  199. IsAttributeEquals();
  200. BasicVm.SetStrDashStyle("None");
  201. }
  202. else
  203. {
  204. GetAnnotProperty();
  205. }
  206. }
  207. }
  208. private List<FreehandAnnotArgs> ConvertLists()
  209. {
  210. List<FreehandAnnotArgs> Lists = new List<FreehandAnnotArgs>();
  211. foreach (var item in PropertyPanel.annotlists)
  212. {
  213. var selecteditem = item as FreehandAnnotArgs;
  214. if (selecteditem != null)
  215. {
  216. Lists.Add(selecteditem);
  217. }
  218. }
  219. if (Lists.Count != PropertyPanel.annotlists.Count)
  220. return null;
  221. else
  222. return Lists;
  223. }
  224. private void IsAttributeEquals()
  225. {
  226. var list = ConvertLists();
  227. if (list != null)
  228. {
  229. var temp = list[0];
  230. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  231. isNoEqualsDir.Add("Color", false);
  232. isNoEqualsDir.Add("Thickness", false);
  233. isNoEqualsDir.Add("FontStyleFontWeight", false);
  234. isNoEqualsDir.Add("ThickSolidDashStyle", false);
  235. foreach (var item in list)
  236. {
  237. if (item == list[0])
  238. continue;
  239. if (isNoEqualsDir["Color"] == false)
  240. {
  241. 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)
  242. {
  243. BasicVm.FontColor = new SolidColorBrush(Colors.Transparent);
  244. isNoEqualsDir["Color"] = true;
  245. }
  246. }
  247. if (isNoEqualsDir["Thickness"] == false)
  248. {
  249. isNoEqualsDir["Thickness"] = true;
  250. if (temp.LineWidth > item.LineWidth)
  251. {
  252. BasicVm.AnnotThickness = temp.LineWidth;
  253. }
  254. else
  255. {
  256. BasicVm.AnnotThickness = item.LineWidth;
  257. }
  258. }
  259. if(isNoEqualsDir["ThickSolidDashStyle"] == false)
  260. {
  261. if(isSolidStyle(temp) != isSolidStyle(item))
  262. {
  263. isNoEqualsDir["ThickSolidDashStyle"] = true;
  264. }
  265. }
  266. }
  267. if (isNoEqualsDir["Color"] == false)
  268. {
  269. BasicVm.FontColor = new SolidColorBrush(temp.InkColor);
  270. }
  271. if (isNoEqualsDir["Thickness"] == false)
  272. {
  273. BasicVm.AnnotThickness = temp.LineWidth;
  274. }
  275. if(isNoEqualsDir["ThickSolidDashStyle"] == true)
  276. {
  277. var isSolid = isSolidStyle(temp);
  278. BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
  279. }
  280. else
  281. {
  282. BasicVm.SetStrDashStyle("None");
  283. }
  284. }
  285. }
  286. //外部UI控件选中状态
  287. private bool isSolidStyle(FreehandAnnotArgs annot)
  288. {
  289. bool isSolid = true;
  290. if (annot.LineDash != null && annot.LineDash.Dashes.Count > 0)
  291. {
  292. foreach (var item in annot.LineDash.Dashes)
  293. {
  294. if (item > 0)
  295. {
  296. isSolid = false;
  297. break;
  298. }
  299. }
  300. }
  301. return isSolid;
  302. }
  303. private void GetAnnotProperty()
  304. {
  305. if (Annot is FreehandAnnotArgs)
  306. {
  307. var annot = Annot as FreehandAnnotArgs;
  308. if (annot != null)
  309. {
  310. BasicVm.FillOpacity = annot.Transparency;
  311. BasicVm.FontColor = new SolidColorBrush(annot.InkColor);
  312. BasicVm.AnnotThickness = annot.LineWidth;
  313. BasicVm.Dash = annot.LineDash;
  314. var isSolid = isSolidStyle(annot);
  315. BasicVm.SetStrDashStyle(isSolid ? "Solid" : "Dash");
  316. IsPen = true;
  317. }
  318. }
  319. if (Annot is EraseArgs)
  320. {
  321. var annot = Annot as EraseArgs;
  322. if (annot != null)
  323. {
  324. EraseThicknessLine = annot.Thickness;
  325. IsPen = false;
  326. }
  327. }
  328. }
  329. }
  330. }