SharpsAnnotPropertyViewModel.cs 21 KB

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