FreehandAnnotPropertyViewModel.cs 15 KB

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