FreehandAnnotPropertyViewModel.cs 14 KB

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