FreehandAnnotPropertyViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. }
  228. }
  229. public bool IsNavigationTarget(NavigationContext navigationContext)
  230. {
  231. return true;
  232. }
  233. public void OnNavigatedFrom(NavigationContext navigationContext)
  234. {
  235. BasicVm.IsMultiSelected = false;
  236. }
  237. public void OnNavigatedTo(NavigationContext navigationContext)
  238. {
  239. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  240. if (PropertyPanel != null)
  241. {
  242. AnnotEvent = PropertyPanel.AnnotEvent;
  243. Annot = PropertyPanel.annot;
  244. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  245. if(Annot is FreehandAnnotArgs)
  246. {
  247. BasicVm.AnnotTypeTitle = "画笔";
  248. }
  249. else
  250. {
  251. BasicVm.AnnotTypeTitle = "橡皮擦";
  252. }
  253. if (BasicVm.IsMultiSelected)
  254. {
  255. IsAttributeEquals();
  256. BasicVm.IsDashLine = BasicVm.IsSolidLine = false;
  257. }
  258. else
  259. {
  260. GetAnnotProperty();
  261. }
  262. }
  263. }
  264. private List<FreehandAnnotArgs> ConvertLists()
  265. {
  266. List<FreehandAnnotArgs> Lists = new List<FreehandAnnotArgs>();
  267. foreach (var item in PropertyPanel.annotlists)
  268. {
  269. var selecteditem = item as FreehandAnnotArgs;
  270. if (selecteditem != null)
  271. {
  272. Lists.Add(selecteditem);
  273. }
  274. }
  275. if (Lists.Count != PropertyPanel.annotlists.Count)
  276. return null;
  277. else
  278. return Lists;
  279. }
  280. private void IsAttributeEquals()
  281. {
  282. var list = ConvertLists();
  283. if (list != null)
  284. {
  285. var temp = list[0];
  286. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  287. isNoEqualsDir.Add("Color", false);
  288. isNoEqualsDir.Add("Thickness", false);
  289. isNoEqualsDir.Add("FontStyleFontWeight", false);
  290. isNoEqualsDir.Add("ThickSolidDashStyle", false);
  291. foreach (var item in list)
  292. {
  293. if (item == list[0])
  294. continue;
  295. if (isNoEqualsDir["Color"] == false)
  296. {
  297. 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)
  298. {
  299. BasicVm.FontColor = new SolidColorBrush(Colors.Transparent);
  300. isNoEqualsDir["Color"] = true;
  301. }
  302. }
  303. if (isNoEqualsDir["Thickness"] == false)
  304. {
  305. isNoEqualsDir["Thickness"] = true;
  306. if (temp.LineWidth > item.LineWidth)
  307. {
  308. BasicVm.AnnotThickness = temp.LineWidth;
  309. }
  310. else
  311. {
  312. BasicVm.AnnotThickness = item.LineWidth;
  313. }
  314. }
  315. if(isNoEqualsDir["ThickSolidDashStyle"] == false)
  316. {
  317. if(isSolidStyle(temp) != isSolidStyle(item))
  318. {
  319. isNoEqualsDir["ThickSolidDashStyle"] = true;
  320. }
  321. }
  322. }
  323. if (isNoEqualsDir["Color"] == false)
  324. {
  325. BasicVm.FontColor = new SolidColorBrush(temp.InkColor);
  326. }
  327. if (isNoEqualsDir["Thickness"] == false)
  328. {
  329. BasicVm.AnnotThickness = temp.LineWidth;
  330. }
  331. if(isNoEqualsDir["ThickSolidDashStyle"] == true)
  332. {
  333. var isSolid = isSolidStyle(temp);
  334. BasicVm.IsSolidLine = isSolid;
  335. BasicVm.IsDashLine = !isSolid;
  336. }
  337. else
  338. {
  339. BasicVm.IsSolidLine = BasicVm.IsDashLine = false;
  340. }
  341. }
  342. }
  343. //外部UI控件选中状态
  344. private bool isSolidStyle(FreehandAnnotArgs annot)
  345. {
  346. bool isSolid = true;
  347. if (annot.LineDash != null && annot.LineDash.Dashes.Count > 0)
  348. {
  349. foreach (var item in annot.LineDash.Dashes)
  350. {
  351. if (item > 0)
  352. {
  353. isSolid = false;
  354. break;
  355. }
  356. }
  357. }
  358. return isSolid;
  359. }
  360. private void GetAnnotProperty()
  361. {
  362. if (Annot is FreehandAnnotArgs)
  363. {
  364. var annot = Annot as FreehandAnnotArgs;
  365. if (annot != null)
  366. {
  367. BasicVm.FillOpacity = annot.Transparency;
  368. BasicVm.FontColor = new SolidColorBrush(annot.InkColor);
  369. BasicVm.AnnotThickness = annot.LineWidth;
  370. BasicVm.Dash = annot.LineDash;
  371. var isSolid = isSolidStyle(annot);
  372. BasicVm.IsSolidLine = isSolid;
  373. BasicVm.IsDashLine = !isSolid;
  374. IsPen = true;
  375. StrokeDashArray = new DoubleCollection();
  376. if (isSolid == false)
  377. {
  378. StrokeDashArray.Add(1);
  379. StrokeDashArray.Add(1);
  380. }
  381. }
  382. }
  383. if (Annot is EraseArgs)
  384. {
  385. var annot = Annot as EraseArgs;
  386. if (annot != null)
  387. {
  388. EraseThicknessLine = annot.Thickness;
  389. IsPen = false;
  390. }
  391. }
  392. }
  393. }
  394. }