SharpsAnnotPropertyViewModel.cs 17 KB

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