FreehandAnnotPropertyViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. GetAnnotProperty();
  286. }
  287. }
  288. }
  289. private List<FreehandAnnotArgs> ConvertLists()
  290. {
  291. List<FreehandAnnotArgs> Lists = new List<FreehandAnnotArgs>();
  292. foreach (var item in PropertyPanel.annotlists)
  293. {
  294. var selecteditem = item as FreehandAnnotArgs;
  295. if (selecteditem != null)
  296. {
  297. Lists.Add(selecteditem);
  298. }
  299. }
  300. if (Lists.Count != PropertyPanel.annotlists.Count)
  301. return null;
  302. else
  303. return Lists;
  304. }
  305. private void IsAttributeEquals()
  306. {
  307. var list = ConvertLists();
  308. if (list != null)
  309. {
  310. var temp = list[0];
  311. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  312. isNoEqualsDir.Add("Color", false);
  313. isNoEqualsDir.Add("Thickness", false);
  314. isNoEqualsDir.Add("FontStyleFontWeight", false);
  315. isNoEqualsDir.Add("ThickSolidDashStyle", false);
  316. foreach (var item in list)
  317. {
  318. if (item == list[0])
  319. continue;
  320. if (isNoEqualsDir["Color"] == false)
  321. {
  322. 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)
  323. {
  324. BasicVm.FontColor = new SolidColorBrush(Color.FromArgb(0x01, 0xff, 0xff, 0xff));
  325. isNoEqualsDir["Color"] = true;
  326. }
  327. }
  328. if (isNoEqualsDir["Thickness"] == false)
  329. {
  330. isNoEqualsDir["Thickness"] = true;
  331. if (temp.LineWidth > item.LineWidth)
  332. {
  333. BasicVm.AnnotThickness = temp.LineWidth;
  334. }
  335. else
  336. {
  337. BasicVm.AnnotThickness = item.LineWidth;
  338. }
  339. }
  340. if (isNoEqualsDir["ThickSolidDashStyle"] == false)
  341. {
  342. if (AnnotTransfer.IsSolidStyle(temp.LineDash) != AnnotTransfer.IsSolidStyle(item.LineDash))
  343. {
  344. isNoEqualsDir["ThickSolidDashStyle"] = true;
  345. }
  346. }
  347. }
  348. if (isNoEqualsDir["Color"] == false)
  349. {
  350. BasicVm.FontColor = new SolidColorBrush(temp.InkColor);
  351. }
  352. if (isNoEqualsDir["Thickness"] == false)
  353. {
  354. BasicVm.AnnotThickness = temp.LineWidth;
  355. }
  356. if (isNoEqualsDir["ThickSolidDashStyle"] == true)
  357. {
  358. BasicVm.IsSolidLine = BasicVm.IsDashLine = false;
  359. }
  360. else
  361. {
  362. var isSolid = AnnotTransfer.IsSolidStyle(temp.LineDash);
  363. BasicVm.IsSolidLine = isSolid;
  364. BasicVm.IsDashLine = !isSolid;
  365. }
  366. }
  367. }
  368. private void GetAnnotProperty()
  369. {
  370. if (Annot is FreehandAnnotArgs)
  371. {
  372. var annot = Annot as FreehandAnnotArgs;
  373. if (annot != null)
  374. {
  375. BasicVm.FillOpacity = annot.Transparency;
  376. BasicVm.FontColor = new SolidColorBrush(annot.InkColor);
  377. BasicVm.AnnotThickness = annot.LineWidth;
  378. BasicVm.Dash = annot.LineDash;
  379. var isSolid = AnnotTransfer.IsSolidStyle(annot.LineDash);
  380. BasicVm.IsSolidLine = isSolid;
  381. BasicVm.IsDashLine = !isSolid;
  382. IsPen = true;
  383. StrokeDashArray = new DoubleCollection();
  384. if (isSolid == false)
  385. {
  386. StrokeDashArray.Add(1);
  387. StrokeDashArray.Add(1);
  388. }
  389. }
  390. }
  391. if (Annot is EraseArgs)
  392. {
  393. var annot = Annot as EraseArgs;
  394. if (annot != null)
  395. {
  396. EraseThicknessLine = annot.Thickness;
  397. IsPen = false;
  398. }
  399. }
  400. }
  401. }
  402. }