FreehandAnnotPropertyViewModel.cs 15 KB

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