FreehandAnnotPropertyViewModel.cs 15 KB

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