FreehandAnnotPropertyViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. //防止橡皮擦注释赋值给当前手绘注释
  235. if (PropertyPanel.annot is FreehandAnnotArgs)
  236. {
  237. CurrenFreeHandAnnot = PropertyPanel.annot as FreehandAnnotArgs;
  238. }
  239. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn);
  240. IsPen = false;
  241. }
  242. }
  243. private FreehandAnnotArgs CurrenFreeHandAnnot;//用来切换橡皮擦时,保存当前的手绘;
  244. //选择画笔
  245. private void Pen_Command(object obj)
  246. {
  247. var btn = obj as ToggleButton;
  248. if (btn.IsChecked == true)
  249. {
  250. PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, CurrenFreeHandAnnot);
  251. IsPen = true; ;
  252. if (CurrenFreeHandAnnot is FreehandAnnotArgs)
  253. {
  254. BasicVm.AnnotTypeTitle = FreehandTitle;
  255. }
  256. else
  257. {
  258. BasicVm.AnnotTypeTitle = EraseTitle;
  259. }
  260. }
  261. }
  262. public bool IsNavigationTarget(NavigationContext navigationContext)
  263. {
  264. return true;
  265. }
  266. public void OnNavigatedFrom(NavigationContext navigationContext)
  267. {
  268. BasicVm.IsMultiSelected = false;
  269. }
  270. public void OnNavigatedTo(NavigationContext navigationContext)
  271. {
  272. navigationContext.Parameters.TryGetValue<AnnotTransfer>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  273. if (PropertyPanel != null)
  274. {
  275. AnnotEvent = PropertyPanel.AnnotEvent;
  276. Annot = PropertyPanel.annot;
  277. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  278. if (Annot is FreehandAnnotArgs)
  279. {
  280. BasicVm.AnnotTypeTitle = FreehandTitle;
  281. }
  282. else
  283. {
  284. BasicVm.AnnotTypeTitle = EraseTitle;
  285. }
  286. if (BasicVm.IsMultiSelected)
  287. {
  288. IsAttributeEquals();
  289. }
  290. else
  291. {
  292. IsPen = true;
  293. BasicVm.IsFreeHandSelected = PropertyPanel.IsFreeHandSelected;
  294. GetAnnotProperty();
  295. }
  296. }
  297. }
  298. private List<FreehandAnnotArgs> ConvertLists()
  299. {
  300. List<FreehandAnnotArgs> Lists = new List<FreehandAnnotArgs>();
  301. foreach (var item in PropertyPanel.annotlists)
  302. {
  303. var selecteditem = item as FreehandAnnotArgs;
  304. if (selecteditem != null)
  305. {
  306. Lists.Add(selecteditem);
  307. }
  308. }
  309. if (Lists.Count != PropertyPanel.annotlists.Count)
  310. return null;
  311. else
  312. return Lists;
  313. }
  314. private void IsAttributeEquals()
  315. {
  316. var list = ConvertLists();
  317. if (list != null)
  318. {
  319. var temp = list[0];
  320. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  321. isNoEqualsDir.Add("Color", false);
  322. isNoEqualsDir.Add("Thickness", false);
  323. isNoEqualsDir.Add("FontStyleFontWeight", false);
  324. isNoEqualsDir.Add("ThickSolidDashStyle", false);
  325. foreach (var item in list)
  326. {
  327. if (item == list[0])
  328. continue;
  329. if (isNoEqualsDir["Color"] == false)
  330. {
  331. 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)
  332. {
  333. BasicVm.FontColor = new SolidColorBrush(Color.FromArgb(0x01, 0xff, 0xff, 0xff));
  334. isNoEqualsDir["Color"] = true;
  335. }
  336. }
  337. if (isNoEqualsDir["Thickness"] == false)
  338. {
  339. isNoEqualsDir["Thickness"] = true;
  340. if (temp.LineWidth > item.LineWidth)
  341. {
  342. BasicVm.AnnotThickness = temp.LineWidth;
  343. }
  344. else
  345. {
  346. BasicVm.AnnotThickness = item.LineWidth;
  347. }
  348. }
  349. if (isNoEqualsDir["ThickSolidDashStyle"] == false)
  350. {
  351. if (AnnotTransfer.IsSolidStyle(temp.LineDash) != AnnotTransfer.IsSolidStyle(item.LineDash))
  352. {
  353. isNoEqualsDir["ThickSolidDashStyle"] = true;
  354. }
  355. }
  356. }
  357. if (isNoEqualsDir["Color"] == false)
  358. {
  359. BasicVm.FontColor = new SolidColorBrush(temp.InkColor);
  360. }
  361. if (isNoEqualsDir["Thickness"] == false)
  362. {
  363. BasicVm.AnnotThickness = temp.LineWidth;
  364. }
  365. if (isNoEqualsDir["ThickSolidDashStyle"] == true)
  366. {
  367. BasicVm.IsSolidLine = BasicVm.IsDashLine = false;
  368. }
  369. else
  370. {
  371. var isSolid = AnnotTransfer.IsSolidStyle(temp.LineDash);
  372. BasicVm.IsSolidLine = isSolid;
  373. BasicVm.IsDashLine = !isSolid;
  374. }
  375. }
  376. }
  377. private void GetAnnotProperty()
  378. {
  379. if (Annot is FreehandAnnotArgs)
  380. {
  381. var annot = Annot as FreehandAnnotArgs;
  382. if (annot != null)
  383. {
  384. BasicVm.FillOpacity = annot.Transparency;
  385. BasicVm.FontColor = new SolidColorBrush(annot.InkColor);
  386. BasicVm.AnnotThickness = annot.LineWidth;
  387. BasicVm.Dash = annot.LineDash;
  388. var isSolid = AnnotTransfer.IsSolidStyle(annot.LineDash);
  389. BasicVm.IsSolidLine = isSolid;
  390. BasicVm.IsDashLine = !isSolid;
  391. IsPen = true;
  392. StrokeDashArray = new DoubleCollection();
  393. if (isSolid == false)
  394. {
  395. StrokeDashArray.Add(1);
  396. StrokeDashArray.Add(1);
  397. }
  398. }
  399. }
  400. if (Annot is EraseArgs)
  401. {
  402. var annot = Annot as EraseArgs;
  403. if (annot != null)
  404. {
  405. //进入橡皮擦更新一下缓存宽度
  406. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, EraseThicknessLine);
  407. AnnotEvent?.UpdateAnnot();
  408. //EraseThicknessLine = EraseThicknessLine;
  409. IsPen = false;
  410. }
  411. }
  412. }
  413. }
  414. }