SharpsAnnotPropertyViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 == null || 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 != null && dash.Dashes.Count > 0)
  141. {
  142. if (dash.Dashes[0] == 0)
  143. {
  144. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  145. }
  146. else
  147. {
  148. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, dash);
  149. }
  150. }
  151. else
  152. {
  153. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  154. }
  155. AnnotEvent?.UpdateAnnot();
  156. }
  157. }
  158. #endregion
  159. public DelegateCommand<object> SelectedThickCommand { get; set; }
  160. public DelegateCommand<object> SelectedColorCommand { get; set; }
  161. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  162. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  163. public DelegateCommand<object> LineStyleCommand { get; set; }
  164. public DelegateCommand<object> SharpsTypeCommand { get; set; }
  165. public DelegateCommand<object> ThicknessChangedCommand { get; set; }
  166. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  167. public event EventHandler<object> LoadPropertyHandler;
  168. public SharpsAnnotPropertyViewModel()
  169. {
  170. SelectedThickCommand = new DelegateCommand<object>(SelectedThick_Command);
  171. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  172. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity_Command);
  173. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  174. LineStyleCommand = new DelegateCommand<object>(LineStyle_Command);
  175. SharpsTypeCommand = new DelegateCommand<object>(SharpsType_Command);
  176. ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged_Command);
  177. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  178. }
  179. private void ThicknessChanged_Command(object obj)
  180. {
  181. if (obj != null)
  182. {
  183. var item = (ComboBoxItem)obj;
  184. var content = (string)item.Content;
  185. if (content != null)
  186. {
  187. var intData = int.Parse(content);
  188. LineWidth = intData;
  189. }
  190. }
  191. }
  192. private void SharpsType_Command(object obj)
  193. {
  194. if(obj != null)
  195. {
  196. var tag = (string)obj;
  197. SharpsType(tag);
  198. }
  199. }
  200. private void SharpsType(string tag,bool isFromToolsBtn = false)
  201. {
  202. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  203. switch (tag)
  204. {
  205. case "Rect":
  206. RectangleGeometry rectPath = new RectangleGeometry();
  207. rectPath.Rect = new Rect(0, 5, 28, 22);
  208. DataPath = rectPath;
  209. changeData[AnnotArgsType.AnnotSquare] = tag;
  210. break;
  211. case "Circle":
  212. EllipseGeometry circlePath = new EllipseGeometry();
  213. circlePath.RadiusX = 14;
  214. circlePath.RadiusY = 14;
  215. circlePath.Center = new Point(14, 14);
  216. DataPath = circlePath;
  217. changeData[AnnotArgsType.AnnotCircle] = tag;
  218. break;
  219. case "Arrow":
  220. {
  221. ArrowHelper arrowLine = new ArrowHelper();
  222. arrowLine.ArrowLength = 8;
  223. arrowLine.LineStart = new Point(8, 24);
  224. arrowLine.LineEnd = new Point(24, 8);
  225. arrowLine.StartSharp = C_LINE_TYPE.LINETYPE_NONE;
  226. arrowLine.EndSharp = C_LINE_TYPE.LINETYPE_ARROW;
  227. DataPath = arrowLine.BuildArrowBody();
  228. changeData[AnnotArgsType.AnnotLine] = tag;
  229. // changeData[AnnotArgsType.AnnotLine] = tag;
  230. }
  231. break;
  232. case "Line":
  233. {
  234. ArrowHelper arrowLine = new ArrowHelper();
  235. arrowLine.LineStart = new Point(0, 32);
  236. arrowLine.LineEnd = new Point(32, 0);
  237. DataPath = arrowLine.BuildArrowBody();
  238. changeData[AnnotArgsType.AnnotLine] = tag;
  239. }
  240. break;
  241. }
  242. if (isFromToolsBtn == false)
  243. PropertyPanel.AnnotTypeChangedInvoke(this, changeData);
  244. }
  245. private void LineStyle_Command(object obj)
  246. {
  247. if(obj!= null)
  248. {
  249. var tag = obj as string;
  250. if(tag == "Solid")
  251. {
  252. DashStyle dashAnnot = new DashStyle();
  253. dashAnnot.Dashes.Add(0);
  254. dashAnnot.Dashes.Add(0);
  255. Dash = dashAnnot;
  256. }
  257. else
  258. {
  259. DashStyle dashAnnot = new DashStyle();
  260. dashAnnot.Dashes.Add(1);
  261. dashAnnot.Dashes.Add(1);
  262. Dash = dashAnnot;
  263. }
  264. }
  265. }
  266. private void SelectedFillColor_Command(object obj)
  267. {
  268. if (obj != null)
  269. {
  270. var colorValue = (Color)obj;
  271. if (colorValue != null)
  272. {
  273. FillColor = new SolidColorBrush(colorValue);
  274. FillColor.Opacity = FillOpacity;
  275. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  276. changeData[AnnotArgsType.AnnotFreehand] = obj;
  277. PropertyPanel.DataChangedInvoke(this, changeData);
  278. }
  279. }
  280. }
  281. private void SelectedFillOpacity_Command(object obj)
  282. {
  283. if (obj != null)
  284. {
  285. FillOpacity = (double)obj;
  286. SelectColor.Opacity = FillOpacity;
  287. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  288. AnnotEvent?.UpdateAnnot();
  289. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  290. changeData[AnnotArgsType.AnnotFreehand] = FillOpacity;
  291. PropertyPanel.DataChangedInvoke(this, changeData);
  292. }
  293. }
  294. private void SelectedOpacityValue(object obj)
  295. {
  296. if (obj != null)
  297. {
  298. FillOpacity = (double)obj;
  299. SelectColor.Opacity = FillOpacity;
  300. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  301. AnnotEvent?.UpdateAnnot();
  302. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  303. changeData[AnnotArgsType.AnnotFreehand] = FillOpacity;
  304. PropertyPanel.DataChangedInvoke(this, changeData);
  305. }
  306. }
  307. private void SelectedColor_Command(object obj)
  308. {
  309. if (obj != null)
  310. {
  311. var colorValue = (Color)obj;
  312. if (colorValue != null)
  313. {
  314. SelectColor = new SolidColorBrush(colorValue);
  315. SelectColor.Opacity = FillOpacity;
  316. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  317. changeData[AnnotArgsType.AnnotFreehand] = obj;
  318. PropertyPanel.DataChangedInvoke(this, changeData);
  319. }
  320. }
  321. }
  322. private void SelectedThick_Command(object obj)
  323. {
  324. if (obj is double)
  325. {
  326. var tran = (double)obj;
  327. BorderOpacity = tran;
  328. SelectColor.Opacity = tran;
  329. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, tran);
  330. AnnotEvent?.UpdateAnnot();
  331. }
  332. }
  333. public bool IsNavigationTarget(NavigationContext navigationContext)
  334. {
  335. return true;
  336. }
  337. public void OnNavigatedFrom(NavigationContext navigationContext)
  338. {
  339. }
  340. private void SetAnnotType()
  341. {
  342. switch (AnnotType)
  343. {
  344. case AnnotArgsType.AnnotCircle:
  345. AnnotTypeTitle = "圆";
  346. break;
  347. case AnnotArgsType.AnnotSquare:
  348. AnnotTypeTitle = "矩形";
  349. break;
  350. case AnnotArgsType.AnnotLine:
  351. var annotLine = Annot as LineAnnotArgs;
  352. if (annotLine != null)
  353. {
  354. if (annotLine.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && annotLine.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  355. AnnotTypeTitle = "箭头";
  356. else
  357. AnnotTypeTitle = "线";
  358. }
  359. break;
  360. }
  361. }
  362. public AnnotAttribEvent AnnotEvent { get; set; }
  363. private AnnotHandlerEventArgs Annot;
  364. private AnnotPropertyPanel PropertyPanel;
  365. public void OnNavigatedTo(NavigationContext navigationContext)
  366. {
  367. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  368. if (PropertyPanel != null)
  369. {
  370. AnnotEvent = PropertyPanel.AnnotEvent;
  371. AnnotType = PropertyPanel.annot.EventType;
  372. Annot = PropertyPanel.annot;
  373. GetAnnotProperty();
  374. LoadPropertyHandler?.Invoke(null, Annot);
  375. }
  376. }
  377. private void GetAnnotProperty()
  378. {
  379. if (Annot != null)
  380. {
  381. switch (Annot.EventType)
  382. {
  383. case AnnotArgsType.AnnotSquare:
  384. if (Annot is SquareAnnotArgs)
  385. {
  386. var Square = Annot as SquareAnnotArgs;
  387. SelectColor = new SolidColorBrush(Square.LineColor);
  388. FillColor = new SolidColorBrush(Square.BgColor);
  389. BorderOpacity = Square.Transparency;
  390. FillOpacity = Square.Transparency;
  391. LineWidth = Square.LineWidth;
  392. Dash = Square.LineDash;
  393. SharpsType("Rect",true);
  394. }
  395. break;
  396. case AnnotArgsType.AnnotCircle:
  397. if (Annot is CircleAnnotArgs)
  398. {
  399. var Circle = Annot as CircleAnnotArgs;
  400. SelectColor = new SolidColorBrush(Circle.LineColor);
  401. FillColor = new SolidColorBrush(Circle.BgColor);
  402. BorderOpacity = Circle.Transparency;
  403. FillOpacity = Circle.Transparency;
  404. LineWidth = Circle.LineWidth;
  405. Dash = Circle.LineDash;
  406. SharpsType("Circle", true);
  407. }
  408. break;
  409. case AnnotArgsType.AnnotLine:
  410. if (Annot is LineAnnotArgs)
  411. {
  412. var line = Annot as LineAnnotArgs;
  413. SelectColor = new SolidColorBrush(line.LineColor);
  414. FillColor = new SolidColorBrush(line.LineColor);
  415. BorderOpacity = line.Transparency;
  416. FillOpacity = line.Transparency;
  417. LineWidth = line.LineWidth;
  418. Dash = line.LineDash;
  419. if (line.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && line.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  420. SharpsType("Arrow", true);
  421. else
  422. SharpsType("Line", true);
  423. }
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. }