FreehandAnnotPropertyViewModel.cs 15 KB

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