SharpsAnnotPropertyViewModel.cs 20 KB

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