SharpsAnnotPropertyViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using PDF_Master.CustomControl.CompositeControl;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using PDF_Master.Model.AnnotPanel;
  8. using PDF_Master.ViewModels.Tools;
  9. using PDF_Master.ViewModels.Tools.AnnotManager;
  10. using PDFSettings;
  11. using Prism.Commands;
  12. using Prism.Mvvm;
  13. using Prism.Regions;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Globalization;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using System.Windows.Media;
  24. namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
  25. {
  26. public class DashStyleConverter : IValueConverter
  27. {
  28. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  29. {
  30. if (value is DashStyle)
  31. {
  32. var dash = value as DashStyle;
  33. if (dash.Dashes == null || dash.Dashes.Count == 0 || dash.Dashes[0] == 0)
  34. {
  35. return DashStyles.Solid.Dashes;
  36. }
  37. else
  38. {
  39. DashStyle dashx = new DashStyle();
  40. dashx.Dashes.Add(1);
  41. dashx.Dashes.Add(1);
  42. return dashx.Dashes;
  43. }
  44. }
  45. return value;
  46. }
  47. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  48. {
  49. throw new NotImplementedException();
  50. }
  51. }
  52. public class SharpsAnnotPropertyViewModel : BindableBase, INavigationAware
  53. {
  54. #region 属性
  55. private bool isRect = false;
  56. public bool IsRect
  57. {
  58. get { return isRect; }
  59. set => SetProperty(ref isRect, value);
  60. }
  61. private bool isCircle = false;
  62. public bool IsCircle
  63. {
  64. get { return isCircle; }
  65. set => SetProperty(ref isCircle, value);
  66. }
  67. private bool isArrow = false;
  68. public bool IsArrow
  69. {
  70. get { return isArrow; }
  71. set => SetProperty(ref isArrow, value);
  72. }
  73. private bool isLine = false;
  74. public bool IsLine
  75. {
  76. get { return isLine; }
  77. set => SetProperty(ref isLine, value);
  78. }
  79. private string _strShapeChecked = "";
  80. public string StrShapeChecked
  81. {
  82. get { return _strShapeChecked; }
  83. set
  84. {
  85. SetProperty(ref _strShapeChecked, value);
  86. }
  87. }
  88. public List<ComboDataItem> ThicknessItems { get; protected set; }
  89. private AnnotCommon _basicVm = new AnnotCommon();
  90. public AnnotCommon BasicVm
  91. {
  92. get { return _basicVm; }
  93. set => SetProperty(ref _basicVm, value);
  94. }
  95. private Geometry dataPath = null;
  96. public Geometry DataPath
  97. {
  98. get { return dataPath; }
  99. set
  100. {
  101. SetProperty(ref dataPath, value);
  102. }
  103. }
  104. private DashStyle dash = new DashStyle();
  105. public DashStyle Dash
  106. {
  107. get { return dash; }
  108. set
  109. {
  110. SetProperty(ref dash, value);
  111. }
  112. }
  113. private bool _isLineAnnot = false;
  114. public bool IsLineAnnot
  115. {
  116. get { return _isLineAnnot; }
  117. set
  118. {
  119. SetProperty(ref _isLineAnnot, value);
  120. }
  121. }
  122. #endregion
  123. public DelegateCommand<object> SelectedThickCommand { get; set; }
  124. public DelegateCommand<object> SelectedBorderColorCommand { get; set; }
  125. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  126. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  127. public DelegateCommand<object> LineStyleCommand { get; set; }
  128. public DelegateCommand<object> SharpsTypeCommand { get; set; }
  129. public DelegateCommand<object> ThicknessChangedCommand { get; set; }
  130. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  131. public event EventHandler<object> LoadPropertyHandler;
  132. public SharpsAnnotPropertyViewModel()
  133. {
  134. InitColorItems();
  135. InitFillColorItems();
  136. InitThicknessItems();
  137. SharpsTypeCommand = new DelegateCommand<object>(SharpsType_Command);
  138. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity_Command);
  139. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  140. SelectedThickCommand = new DelegateCommand<object>(SelectedThick_Command);
  141. SelectedBorderColorCommand = new DelegateCommand<object>(SelectedBorderColor);
  142. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  143. ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged_Command);
  144. LineStyleCommand = new DelegateCommand<object>(LineStyle_Command);
  145. }
  146. private void InitThicknessItems()
  147. {
  148. ThicknessItems = new List<ComboDataItem>();
  149. ComboDataItem item = new ComboDataItem(1, "pt");
  150. ThicknessItems.Add(item);
  151. item = new ComboDataItem(2, "pt");
  152. ThicknessItems.Add(item);
  153. item = new ComboDataItem(4, "pt");
  154. ThicknessItems.Add(item);
  155. item = new ComboDataItem(6, "pt");
  156. ThicknessItems.Add(item);
  157. item = new ComboDataItem(8, "pt");
  158. ThicknessItems.Add(item);
  159. }
  160. private void InitColorItems()
  161. {
  162. BasicVm.ColorItems = AnnotColorList.GetColorList(ColorSelectorType.Border);
  163. }
  164. private void InitFillColorItems()
  165. {
  166. BasicVm.FillColorItems = AnnotColorList.GetColorList(ColorSelectorType.Fill);
  167. }
  168. private void ThicknessChanged_Command(object obj)
  169. {
  170. if (obj != null)
  171. {
  172. var item = (ComboBoxItem)obj;
  173. var content = (string)item.Content;
  174. if (content != null)
  175. {
  176. var intData = int.Parse(content);
  177. BasicVm.AnnotThickness = intData;
  178. }
  179. }
  180. }
  181. private void SharpsType_Command(object obj)
  182. {
  183. if (obj != null)
  184. {
  185. var tag = (string)obj;
  186. SharpsType(tag);
  187. }
  188. }
  189. private void SharpsType(string tag, bool isFromToolsBtn = false)
  190. {
  191. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  192. switch (tag)
  193. {
  194. case "Rect":
  195. RectangleGeometry rectPath = new RectangleGeometry();
  196. rectPath.Rect = new Rect(0, 5, 28, 22);
  197. DataPath = rectPath;
  198. changeData[AnnotArgsType.AnnotSquare] = tag;
  199. IsLineAnnot = false;
  200. break;
  201. case "Circle":
  202. EllipseGeometry circlePath = new EllipseGeometry();
  203. circlePath.RadiusX = 14;
  204. circlePath.RadiusY = 14;
  205. circlePath.Center = new Point(14, 14);
  206. DataPath = circlePath;
  207. changeData[AnnotArgsType.AnnotCircle] = tag;
  208. IsLineAnnot = false;
  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. IsLineAnnot = true;
  221. // changeData[AnnotArgsType.AnnotLine] = tag;
  222. }
  223. break;
  224. case "Line":
  225. {
  226. ArrowHelper arrowLine = new ArrowHelper();
  227. arrowLine.LineStart = new Point(0, 32);
  228. arrowLine.LineEnd = new Point(32, 0);
  229. DataPath = arrowLine.BuildArrowBody();
  230. changeData[AnnotArgsType.AnnotLine] = tag;
  231. IsLineAnnot = true;
  232. }
  233. break;
  234. }
  235. if (isFromToolsBtn == false)
  236. PropertyPanel.AnnotTypeChangedInvoke(this, changeData);
  237. StrShapeChecked = tag;
  238. BasicVm.SetOtherTag(tag);
  239. }
  240. private void LineStyle_Command(object obj)
  241. {
  242. if (obj != null)
  243. {
  244. var tag = obj as string;
  245. if (tag == "Solid")
  246. {
  247. DashStyle dashAnnot = new DashStyle();
  248. dashAnnot.Dashes.Add(0);
  249. dashAnnot.Dashes.Add(0);
  250. Dash = dashAnnot;
  251. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  252. }
  253. else
  254. {
  255. DashStyle dashAnnot = new DashStyle();
  256. dashAnnot.Dashes.Add(1);
  257. dashAnnot.Dashes.Add(1);
  258. Dash = dashAnnot;
  259. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, dash);
  260. }
  261. if (BasicVm.IsMultiSelected == false)
  262. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  263. }
  264. }
  265. private void SelectedFillColor_Command(object obj)
  266. {
  267. if (obj != null && PropertyPanel != null)
  268. {
  269. var colorValue = (Color)obj;
  270. if (colorValue != null)
  271. {
  272. BasicVm.FillColor = new SolidColorBrush(colorValue);
  273. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  274. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue);
  275. if (BasicVm.IsMultiSelected == false)
  276. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  277. }
  278. }
  279. }
  280. private void SelectedFillOpacity_Command(object obj)
  281. {
  282. if (obj != null)
  283. {
  284. BasicVm.FillOpacity = (double)obj;
  285. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  286. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  287. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, BasicVm.FillOpacity);
  288. }
  289. }
  290. private void SelectedOpacityValue(object obj)
  291. {
  292. if (obj != null)
  293. {
  294. BasicVm.FillOpacity = (double)obj;
  295. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  296. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  297. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, BasicVm.FillOpacity);
  298. }
  299. }
  300. private void SelectedBorderColor(object obj)
  301. {
  302. if (obj != null && PropertyPanel != null)
  303. {
  304. var colorValue = (Color)obj;
  305. if (colorValue != null)
  306. {
  307. BasicVm.BorderColor = new SolidColorBrush(colorValue);
  308. BasicVm.BorderColor.Opacity = BasicVm.BorderOpacity;
  309. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, colorValue);
  310. if (BasicVm.IsMultiSelected == false)
  311. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  312. }
  313. }
  314. }
  315. private void SelectedThick_Command(object obj)
  316. {
  317. if (obj is double)
  318. {
  319. var tran = (double)obj;
  320. BasicVm.AnnotThickness = tran;
  321. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, tran);
  322. if (BasicVm.IsMultiSelected == false)
  323. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  324. }
  325. }
  326. public bool IsNavigationTarget(NavigationContext navigationContext)
  327. {
  328. return true;
  329. }
  330. public void OnNavigatedFrom(NavigationContext navigationContext)
  331. {
  332. BasicVm.IsMultiSelected = false;
  333. }
  334. private void SetAnnotType()
  335. {
  336. switch (BasicVm.AnnotType)
  337. {
  338. case AnnotArgsType.AnnotCircle:
  339. BasicVm.AnnotTypeTitle = "圆";
  340. break;
  341. case AnnotArgsType.AnnotSquare:
  342. BasicVm.AnnotTypeTitle = "矩形";
  343. break;
  344. case AnnotArgsType.AnnotLine:
  345. var annotLine = Annot as LineAnnotArgs;
  346. if (annotLine != null)
  347. {
  348. if (annotLine.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && annotLine.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  349. BasicVm.AnnotTypeTitle = "箭头";
  350. else
  351. BasicVm.AnnotTypeTitle = "线";
  352. }
  353. break;
  354. }
  355. }
  356. public AnnotAttribEvent AnnotEvent { get; set; }
  357. private AnnotHandlerEventArgs Annot;
  358. private AnnotTransfer PropertyPanel;
  359. public void OnNavigatedTo(NavigationContext navigationContext)
  360. {
  361. navigationContext.Parameters.TryGetValue<AnnotTransfer>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  362. if (PropertyPanel != null)
  363. {
  364. AnnotEvent = PropertyPanel.AnnotEvent;
  365. BasicVm.AnnotType = PropertyPanel.annot.EventType;
  366. Annot = PropertyPanel.annot;
  367. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  368. if (BasicVm.IsMultiSelected)
  369. {
  370. IsAttributeEquals();
  371. }
  372. else
  373. {
  374. GetAnnotProperty();
  375. }
  376. LoadPropertyHandler?.Invoke(null, Annot);
  377. }
  378. }
  379. private List<SquareAnnotArgs> ConvertLists()
  380. {
  381. List<SquareAnnotArgs> FreeTextLists = new List<SquareAnnotArgs>();
  382. foreach (var item in PropertyPanel.annotlists)
  383. {
  384. var itemFreeText = item as SquareAnnotArgs;
  385. if (itemFreeText != null)
  386. {
  387. FreeTextLists.Add(itemFreeText);
  388. }
  389. }
  390. if (FreeTextLists.Count != PropertyPanel.annotlists.Count)
  391. return null;
  392. else
  393. return FreeTextLists;
  394. }
  395. private void IsAttributeEquals()
  396. {
  397. var list = ConvertLists();
  398. if (list != null)
  399. {
  400. var temp = list[0];
  401. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  402. isNoEqualsDir.Add("FillColor", false);
  403. isNoEqualsDir.Add("LineColor", false);
  404. isNoEqualsDir.Add("Thickness", false);
  405. isNoEqualsDir.Add("ThickSolidDashStyle", false);
  406. foreach (var item in list)
  407. {
  408. if (item == list[0])
  409. continue;
  410. if (isNoEqualsDir["FillColor"] == false)
  411. {
  412. if (temp.BgColor.A != item.BgColor.A || temp.BgColor.R != item.BgColor.R || temp.BgColor.G != item.BgColor.G || temp.BgColor.B != item.BgColor.B)
  413. {
  414. BasicVm.FillColor = new SolidColorBrush(Color.FromArgb(0x01, 0xff, 0xff, 0xff));
  415. isNoEqualsDir["FillColor"] = true;
  416. }
  417. }
  418. if (isNoEqualsDir["LineColor"] == false)
  419. {
  420. if (temp.LineColor.A != item.LineColor.A || temp.LineColor.R != item.LineColor.R || temp.LineColor.G != item.LineColor.G || temp.LineColor.B != item.LineColor.B)
  421. {
  422. BasicVm.BorderColor = new SolidColorBrush(Color.FromArgb(0x01,0xff,0xff,0xff));
  423. isNoEqualsDir["LineColor"] = true;
  424. }
  425. }
  426. if (isNoEqualsDir["Thickness"] == false)
  427. {
  428. isNoEqualsDir["Thickness"] = true;
  429. if (temp.LineWidth > item.LineWidth)
  430. {
  431. BasicVm.AnnotThickness = temp.LineWidth;
  432. }
  433. else
  434. {
  435. BasicVm.AnnotThickness = item.LineWidth;
  436. }
  437. }
  438. if (isNoEqualsDir["ThickSolidDashStyle"] == false)
  439. {
  440. if (AnnotTransfer.IsSolidStyle(temp.LineDash) != AnnotTransfer.IsSolidStyle(item.LineDash))
  441. {
  442. isNoEqualsDir["ThickSolidDashStyle"] = true;
  443. }
  444. }
  445. }
  446. ////以下是多选注释的属性相等时
  447. if (isNoEqualsDir["FillColor"] == false)
  448. {
  449. BasicVm.FillColor = new SolidColorBrush(temp.BgColor);
  450. }
  451. if (isNoEqualsDir["LineColor"] == false)
  452. {
  453. BasicVm.BorderColor = new SolidColorBrush(temp.LineColor);
  454. }
  455. if (isNoEqualsDir["Thickness"] == false)
  456. {
  457. BasicVm.AnnotThickness = temp.LineWidth;
  458. }
  459. if (isNoEqualsDir["ThickSolidDashStyle"] == true)
  460. {
  461. BasicVm.IsSolidLine = BasicVm.IsDashLine = false;
  462. }
  463. else
  464. {
  465. var isSolid = AnnotTransfer.IsSolidStyle(temp.LineDash);
  466. BasicVm.IsSolidLine = isSolid;
  467. BasicVm.IsDashLine = !isSolid;
  468. }
  469. }
  470. }
  471. private bool IsSolidStyle(AnnotHandlerEventArgs annot)
  472. {
  473. bool isSolid = true;
  474. if (annot is SquareAnnotArgs)
  475. {
  476. var Square = Annot as SquareAnnotArgs;
  477. isSolid = AnnotTransfer.IsSolidStyle(Square.LineDash);
  478. }
  479. else if (annot is CircleAnnotArgs)
  480. {
  481. var Circle = Annot as CircleAnnotArgs;
  482. isSolid = AnnotTransfer.IsSolidStyle(Circle.LineDash);
  483. }
  484. else if (annot is LineAnnotArgs)
  485. {
  486. var line = Annot as LineAnnotArgs;
  487. isSolid = AnnotTransfer.IsSolidStyle(line.LineDash);
  488. }
  489. return isSolid;
  490. }
  491. private void GetAnnotProperty()
  492. {
  493. if (Annot != null)
  494. {
  495. bool isSolid = true;
  496. switch (Annot.EventType)
  497. {
  498. case AnnotArgsType.AnnotSquare:
  499. if (Annot is SquareAnnotArgs)
  500. {
  501. var Square = Annot as SquareAnnotArgs;
  502. BasicVm.BorderColor = new SolidColorBrush(Square.LineColor);
  503. BasicVm.FillColor = new SolidColorBrush(Square.BgColor);
  504. BasicVm.BorderOpacity = Square.Transparency;
  505. BasicVm.FillOpacity = Square.Transparency;
  506. BasicVm.AnnotThickness = Square.LineWidth;
  507. Dash = Square.LineDash;
  508. SharpsType("Rect", true);
  509. BasicVm.AnnotTypeTitle = "矩形";
  510. IsLineAnnot = false;
  511. IsRect = true;
  512. }
  513. break;
  514. case AnnotArgsType.AnnotCircle:
  515. if (Annot is CircleAnnotArgs)
  516. {
  517. var Circle = Annot as CircleAnnotArgs;
  518. BasicVm.BorderColor = new SolidColorBrush(Circle.LineColor);
  519. BasicVm.FillColor = new SolidColorBrush(Circle.BgColor);
  520. BasicVm.BorderOpacity = Circle.Transparency;
  521. BasicVm.FillOpacity = Circle.Transparency;
  522. BasicVm.AnnotThickness = Circle.LineWidth;
  523. Dash = Circle.LineDash;
  524. SharpsType("Circle", true);
  525. BasicVm.AnnotTypeTitle = "圆";
  526. IsLineAnnot = false;
  527. IsCircle = true;
  528. }
  529. break;
  530. case AnnotArgsType.AnnotLine:
  531. if (Annot is LineAnnotArgs)
  532. {
  533. var line = Annot as LineAnnotArgs;
  534. BasicVm.BorderColor = new SolidColorBrush(line.LineColor);
  535. BasicVm.FillColor = new SolidColorBrush(line.LineColor);
  536. BasicVm.BorderOpacity = line.Transparency;
  537. BasicVm.FillOpacity = line.Transparency;
  538. BasicVm.AnnotThickness = line.LineWidth;
  539. Dash = line.LineDash;
  540. if (line.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && line.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  541. {
  542. SharpsType("Arrow", true);
  543. BasicVm.AnnotTypeTitle = "箭头";
  544. IsArrow = true;
  545. }
  546. else
  547. {
  548. SharpsType("Line", true);
  549. BasicVm.AnnotTypeTitle = "线条";
  550. IsLine = true;
  551. }
  552. IsLineAnnot = true;
  553. }
  554. break;
  555. }
  556. isSolid = IsSolidStyle(Annot);
  557. BasicVm.IsSolidLine = isSolid;
  558. BasicVm.IsDashLine = !isSolid;
  559. }
  560. }
  561. }
  562. }