FreehandAnnotPropertyViewModel.cs 14 KB

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