FreehandAnnotPropertyViewModel.cs 15 KB

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