SharpsAnnotPropertyViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDF_Office.ViewModels.Tools;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Globalization;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Media;
  20. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  21. {
  22. public class DashStyleConverter : IValueConverter
  23. {
  24. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  25. {
  26. if (value is DashStyle)
  27. {
  28. var dash = value as DashStyle;
  29. if (dash.Dashes.Count == 0 || dash.Dashes[0] == 0)
  30. {
  31. return DashStyles.Solid.Dashes;
  32. }
  33. else
  34. {
  35. DashStyle dashx = new DashStyle();
  36. dashx.Dashes.Add(1);
  37. dashx.Dashes.Add(1);
  38. return dashx.Dashes;
  39. }
  40. }
  41. return value;
  42. }
  43. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  44. {
  45. throw new NotImplementedException();
  46. }
  47. }
  48. public class SharpsAnnotPropertyViewModel : BindableBase, INavigationAware
  49. {
  50. #region 属性
  51. private AnnotArgsType annotType;
  52. public AnnotArgsType AnnotType
  53. {
  54. get { return annotType; }
  55. set
  56. {
  57. SetProperty(ref annotType, value);
  58. SetAnnotType();
  59. }
  60. }
  61. private string annotTypeTitle;
  62. public string AnnotTypeTitle
  63. {
  64. get { return annotTypeTitle; }
  65. set
  66. {
  67. SetProperty(ref annotTypeTitle, value);
  68. }
  69. }
  70. private Brush selectColor = new SolidColorBrush(Colors.Transparent);
  71. public Brush SelectColor
  72. {
  73. get { return selectColor; }
  74. set
  75. {
  76. SetProperty(ref selectColor, value);
  77. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, (selectColor as SolidColorBrush).Color);
  78. AnnotEvent?.UpdateAnnot();
  79. }
  80. }
  81. private Brush fillColor = new SolidColorBrush(Colors.Transparent);
  82. public Brush FillColor
  83. {
  84. get { return fillColor; }
  85. set
  86. {
  87. SetProperty(ref fillColor, value);
  88. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, (fillColor as SolidColorBrush).Color);
  89. AnnotEvent?.UpdateAnnot();
  90. }
  91. }
  92. private double borderOpacity = 1;
  93. public double BorderOpacity
  94. {
  95. get { return borderOpacity; }
  96. set
  97. {
  98. SetProperty(ref borderOpacity, value);
  99. }
  100. }
  101. private double fillOpacity = 1;
  102. public double FillOpacity
  103. {
  104. get { return fillOpacity; }
  105. set
  106. {
  107. SetProperty(ref fillOpacity, value);
  108. }
  109. }
  110. private double lineWidth = 1;
  111. public double LineWidth
  112. {
  113. get { return lineWidth; }
  114. set
  115. {
  116. SetProperty(ref lineWidth, value);
  117. if (annotType == AnnotArgsType.AnnotLine)
  118. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, lineWidth);
  119. else
  120. AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, lineWidth);
  121. AnnotEvent?.UpdateAnnot();
  122. }
  123. }
  124. private Geometry dataPath = null;
  125. public Geometry DataPath
  126. {
  127. get { return dataPath; }
  128. set
  129. {
  130. SetProperty(ref dataPath, value);
  131. }
  132. }
  133. private DashStyle dash = new DashStyle();
  134. public DashStyle Dash
  135. {
  136. get { return dash; }
  137. set
  138. {
  139. SetProperty(ref dash, value);
  140. if(dash.Dashes[0] == 0)
  141. {
  142. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  143. }
  144. else
  145. {
  146. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, dash);
  147. }
  148. AnnotEvent?.UpdateAnnot();
  149. }
  150. }
  151. #endregion
  152. public DelegateCommand<object> SelectedThickCommand { get; set; }
  153. public DelegateCommand<object> SelectedColorCommand { get; set; }
  154. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  155. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  156. public DelegateCommand<object> LineStyleCommand { get; set; }
  157. public DelegateCommand<object> SharpsTypeCommand { get; set; }
  158. public DelegateCommand<object> ThicknessChangedCommand { get; set; }
  159. public event EventHandler<object> LoadPropertyHandler;
  160. public SharpsAnnotPropertyViewModel()
  161. {
  162. SelectedThickCommand = new DelegateCommand<object>(SelectedThick_Command);
  163. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  164. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity_Command);
  165. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  166. LineStyleCommand = new DelegateCommand<object>(LineStyle_Command);
  167. SharpsTypeCommand = new DelegateCommand<object>(SharpsType_Command);
  168. ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged_Command);
  169. }
  170. private void ThicknessChanged_Command(object obj)
  171. {
  172. if (obj != null)
  173. {
  174. var item = (ComboBoxItem)obj;
  175. var content = (string)item.Content;
  176. if (content != null)
  177. {
  178. var intData = int.Parse(content);
  179. LineWidth = intData;
  180. }
  181. }
  182. }
  183. private void SharpsType_Command(object obj)
  184. {
  185. if(obj != null)
  186. {
  187. var tag = (string)obj;
  188. SharpsType(tag);
  189. }
  190. }
  191. private void SharpsType(string tag,bool isFromToolsBtn = false)
  192. {
  193. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  194. switch (tag)
  195. {
  196. case "Rect":
  197. RectangleGeometry rectPath = new RectangleGeometry();
  198. rectPath.Rect = new Rect(0, 5, 28, 22);
  199. DataPath = rectPath;
  200. changeData[AnnotArgsType.AnnotSquare] = tag;
  201. break;
  202. case "Circle":
  203. EllipseGeometry circlePath = new EllipseGeometry();
  204. circlePath.RadiusX = 14;
  205. circlePath.RadiusY = 14;
  206. circlePath.Center = new Point(14, 14);
  207. DataPath = circlePath;
  208. changeData[AnnotArgsType.AnnotCircle] = tag;
  209. break;
  210. case "Arrow":
  211. {
  212. ArrowHelper arrowLine = new ArrowHelper();
  213. arrowLine.ArrowLength = 8;
  214. arrowLine.LineStart = new Point(8, 24);
  215. arrowLine.LineEnd = new Point(24, 8);
  216. arrowLine.StartSharp = C_LINE_TYPE.LINETYPE_NONE;
  217. arrowLine.EndSharp = C_LINE_TYPE.LINETYPE_ARROW;
  218. DataPath = arrowLine.BuildArrowBody();
  219. changeData[AnnotArgsType.AnnotLine] = tag;
  220. // changeData[AnnotArgsType.AnnotLine] = tag;
  221. }
  222. break;
  223. case "Line":
  224. {
  225. ArrowHelper arrowLine = new ArrowHelper();
  226. arrowLine.LineStart = new Point(0, 32);
  227. arrowLine.LineEnd = new Point(32, 0);
  228. DataPath = arrowLine.BuildArrowBody();
  229. changeData[AnnotArgsType.AnnotLine] = tag;
  230. }
  231. break;
  232. }
  233. if (isFromToolsBtn == false)
  234. PropertyPanel.AnnotTypeChangedInvoke(this, changeData);
  235. }
  236. private void LineStyle_Command(object obj)
  237. {
  238. if(obj!= null)
  239. {
  240. var tag = obj as string;
  241. if(tag == "Solid")
  242. {
  243. DashStyle dashAnnot = new DashStyle();
  244. dashAnnot.Dashes.Add(0);
  245. dashAnnot.Dashes.Add(0);
  246. Dash = dashAnnot;
  247. }
  248. else
  249. {
  250. DashStyle dashAnnot = new DashStyle();
  251. dashAnnot.Dashes.Add(1);
  252. dashAnnot.Dashes.Add(1);
  253. Dash = dashAnnot;
  254. }
  255. }
  256. }
  257. private void SelectedFillColor_Command(object obj)
  258. {
  259. if (obj != null)
  260. {
  261. var colorValue = (Color)obj;
  262. if (colorValue != null)
  263. {
  264. FillColor = new SolidColorBrush(colorValue);
  265. FillColor.Opacity = FillOpacity;
  266. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  267. changeData[AnnotArgsType.AnnotFreehand] = obj;
  268. PropertyPanel.DataChangedInvoke(this, changeData);
  269. }
  270. }
  271. }
  272. private void SelectedFillOpacity_Command(object obj)
  273. {
  274. if (obj != null)
  275. {
  276. FillOpacity = (double)obj;
  277. SelectColor.Opacity = FillOpacity;
  278. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  279. AnnotEvent?.UpdateAnnot();
  280. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  281. changeData[AnnotArgsType.AnnotFreehand] = FillOpacity;
  282. PropertyPanel.DataChangedInvoke(this, changeData);
  283. }
  284. }
  285. private void SelectedColor_Command(object obj)
  286. {
  287. if (obj != null)
  288. {
  289. var colorValue = (Color)obj;
  290. if (colorValue != null)
  291. {
  292. SelectColor = new SolidColorBrush(colorValue);
  293. SelectColor.Opacity = FillOpacity;
  294. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  295. changeData[AnnotArgsType.AnnotFreehand] = obj;
  296. PropertyPanel.DataChangedInvoke(this, changeData);
  297. }
  298. }
  299. }
  300. private void SelectedThick_Command(object obj)
  301. {
  302. if (obj is double)
  303. {
  304. var tran = (double)obj;
  305. BorderOpacity = tran;
  306. SelectColor.Opacity = tran;
  307. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, tran);
  308. AnnotEvent?.UpdateAnnot();
  309. }
  310. }
  311. public bool IsNavigationTarget(NavigationContext navigationContext)
  312. {
  313. return true;
  314. }
  315. public void OnNavigatedFrom(NavigationContext navigationContext)
  316. {
  317. }
  318. private void SetAnnotType()
  319. {
  320. switch (AnnotType)
  321. {
  322. case AnnotArgsType.AnnotCircle:
  323. AnnotTypeTitle = "圆";
  324. break;
  325. case AnnotArgsType.AnnotSquare:
  326. AnnotTypeTitle = "矩形";
  327. break;
  328. case AnnotArgsType.AnnotLine:
  329. var annotLine = Annot as LineAnnotArgs;
  330. if (annotLine != null)
  331. {
  332. if (annotLine.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && annotLine.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  333. AnnotTypeTitle = "箭头";
  334. else
  335. AnnotTypeTitle = "线";
  336. }
  337. break;
  338. }
  339. }
  340. public AnnotAttribEvent AnnotEvent { get; set; }
  341. private AnnotHandlerEventArgs Annot;
  342. private AnnotPropertyPanel PropertyPanel;
  343. public void OnNavigatedTo(NavigationContext navigationContext)
  344. {
  345. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  346. if (PropertyPanel != null)
  347. {
  348. AnnotEvent = PropertyPanel.AnnotEvent;
  349. AnnotType = PropertyPanel.annot.EventType;
  350. Annot = PropertyPanel.annot;
  351. GetAnnotProperty();
  352. LoadPropertyHandler?.Invoke(null, Annot);
  353. }
  354. }
  355. private void GetAnnotProperty()
  356. {
  357. if (Annot != null)
  358. {
  359. switch (Annot.EventType)
  360. {
  361. case AnnotArgsType.AnnotSquare:
  362. if (Annot is SquareAnnotArgs)
  363. {
  364. var Square = Annot as SquareAnnotArgs;
  365. SelectColor = new SolidColorBrush(Square.LineColor);
  366. FillColor = new SolidColorBrush(Square.BgColor);
  367. BorderOpacity = Square.Transparency;
  368. FillOpacity = Square.Transparency;
  369. LineWidth = Square.LineWidth;
  370. SharpsType("Rect",true);
  371. }
  372. break;
  373. case AnnotArgsType.AnnotCircle:
  374. if (Annot is CircleAnnotArgs)
  375. {
  376. var Circle = Annot as CircleAnnotArgs;
  377. SelectColor = new SolidColorBrush(Circle.LineColor);
  378. FillColor = new SolidColorBrush(Circle.BgColor);
  379. BorderOpacity = Circle.Transparency;
  380. FillOpacity = Circle.Transparency;
  381. LineWidth = Circle.LineWidth;
  382. SharpsType("Circle", true);
  383. }
  384. break;
  385. case AnnotArgsType.AnnotLine:
  386. if (Annot is LineAnnotArgs)
  387. {
  388. var line = Annot as LineAnnotArgs;
  389. SelectColor = new SolidColorBrush(line.LineColor);
  390. FillColor = new SolidColorBrush(line.LineColor);
  391. BorderOpacity = line.Transparency;
  392. FillOpacity = line.Transparency;
  393. LineWidth = line.LineWidth;
  394. if (line.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && line.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  395. SharpsType("Arrow", true);
  396. else
  397. SharpsType("Line", true);
  398. }
  399. break;
  400. }
  401. }
  402. }
  403. }
  404. }