FreehandAnnotPropertyViewModel.cs 16 KB

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