SharpsAnnotPropertyViewModel.cs 16 KB

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