FreehandAnnotPropertyViewModel.cs 16 KB

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