SharpsAnnotPropertyViewModel.cs 21 KB

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