SharpsAnnotPropertyViewModel.cs 20 KB

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