FreehandAnnotPropertyViewModel.cs 15 KB

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