FreehandAnnotPropertyViewModel.cs 16 KB

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