SharpsAnnotPropertyViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using PDF_Office.CustomControl.CompositeControl;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.AnnotPanel;
  8. using PDF_Office.ViewModels.Tools;
  9. using PDF_Office.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_Office.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. public List<ComboDataItem> ThicknessItems { get; protected set; }
  56. private AnnotCommon _basicVm = new AnnotCommon();
  57. public AnnotCommon BasicVm
  58. {
  59. get { return _basicVm; }
  60. set => SetProperty(ref _basicVm, value);
  61. }
  62. private Geometry dataPath = null;
  63. public Geometry DataPath
  64. {
  65. get { return dataPath; }
  66. set
  67. {
  68. SetProperty(ref dataPath, value);
  69. }
  70. }
  71. private DashStyle dash = new DashStyle();
  72. public DashStyle Dash
  73. {
  74. get { return dash; }
  75. set
  76. {
  77. SetProperty(ref dash, value);
  78. }
  79. }
  80. private bool _isLineAnnot = false;
  81. public bool IsLineAnnot
  82. {
  83. get { return _isLineAnnot; }
  84. set
  85. {
  86. SetProperty(ref _isLineAnnot, value);
  87. }
  88. }
  89. #endregion
  90. public DelegateCommand<object> SelectedThickCommand { get; set; }
  91. public DelegateCommand<object> SelectedBorderColorCommand { get; set; }
  92. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  93. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  94. public DelegateCommand<object> LineStyleCommand { get; set; }
  95. public DelegateCommand<object> SharpsTypeCommand { get; set; }
  96. public DelegateCommand<object> ThicknessChangedCommand { get; set; }
  97. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  98. public event EventHandler<object> LoadPropertyHandler;
  99. public SharpsAnnotPropertyViewModel()
  100. {
  101. InitColorItems();
  102. InitFillColorItems();
  103. InitThicknessItems();
  104. SharpsTypeCommand = new DelegateCommand<object>(SharpsType_Command);
  105. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity_Command);
  106. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  107. SelectedThickCommand = new DelegateCommand<object>(SelectedThick_Command);
  108. SelectedBorderColorCommand = new DelegateCommand<object>(SelectedBorderColor);
  109. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  110. ThicknessChangedCommand = new DelegateCommand<object>(ThicknessChanged_Command);
  111. LineStyleCommand = new DelegateCommand<object>(LineStyle_Command);
  112. }
  113. private void InitThicknessItems()
  114. {
  115. ThicknessItems = new List<ComboDataItem>();
  116. ComboDataItem item = new ComboDataItem(1, "pt");
  117. ThicknessItems.Add(item);
  118. item = new ComboDataItem(2, "pt");
  119. ThicknessItems.Add(item);
  120. item = new ComboDataItem(4, "pt");
  121. ThicknessItems.Add(item);
  122. item = new ComboDataItem(6, "pt");
  123. ThicknessItems.Add(item);
  124. item = new ComboDataItem(8, "pt");
  125. ThicknessItems.Add(item);
  126. }
  127. private void InitColorItems()
  128. {
  129. BasicVm.ColorItems = AnnotColorList.GetColorList(ColorSelectorType.Border);
  130. }
  131. private void InitFillColorItems()
  132. {
  133. BasicVm.FillColorItems = AnnotColorList.GetColorList(ColorSelectorType.Fill);
  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. if (BasicVm.IsMultiSelected == false)
  228. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  229. }
  230. }
  231. private void SelectedFillColor_Command(object obj)
  232. {
  233. if (obj != null && PropertyPanel != null)
  234. {
  235. var colorValue = (Color)obj;
  236. if (colorValue != null)
  237. {
  238. BasicVm.FillColor = new SolidColorBrush(colorValue);
  239. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  240. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue);
  241. if (BasicVm.IsMultiSelected == false)
  242. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  243. }
  244. }
  245. }
  246. private void SelectedFillOpacity_Command(object obj)
  247. {
  248. if (obj != null)
  249. {
  250. BasicVm.FillOpacity = (double)obj;
  251. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  252. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  253. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, BasicVm.FillOpacity);
  254. }
  255. }
  256. private void SelectedOpacityValue(object obj)
  257. {
  258. if (obj != null)
  259. {
  260. BasicVm.FillOpacity = (double)obj;
  261. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  262. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  263. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, BasicVm.FillOpacity);
  264. }
  265. }
  266. private void SelectedBorderColor(object obj)
  267. {
  268. if (obj != null && PropertyPanel != null)
  269. {
  270. var colorValue = (Color)obj;
  271. if (colorValue != null)
  272. {
  273. BasicVm.BorderColor = new SolidColorBrush(colorValue);
  274. BasicVm.BorderColor.Opacity = BasicVm.BorderOpacity;
  275. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, colorValue);
  276. if (BasicVm.IsMultiSelected == false)
  277. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  278. }
  279. }
  280. }
  281. private void SelectedThick_Command(object obj)
  282. {
  283. if (obj is double)
  284. {
  285. var tran = (double)obj;
  286. BasicVm.AnnotThickness = tran;
  287. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, tran);
  288. if (BasicVm.IsMultiSelected == false)
  289. PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot);
  290. }
  291. }
  292. public bool IsNavigationTarget(NavigationContext navigationContext)
  293. {
  294. return true;
  295. }
  296. public void OnNavigatedFrom(NavigationContext navigationContext)
  297. {
  298. BasicVm.IsMultiSelected = false;
  299. }
  300. private void SetAnnotType()
  301. {
  302. switch (BasicVm.AnnotType)
  303. {
  304. case AnnotArgsType.AnnotCircle:
  305. BasicVm.AnnotTypeTitle = "圆";
  306. break;
  307. case AnnotArgsType.AnnotSquare:
  308. BasicVm.AnnotTypeTitle = "矩形";
  309. break;
  310. case AnnotArgsType.AnnotLine:
  311. var annotLine = Annot as LineAnnotArgs;
  312. if (annotLine != null)
  313. {
  314. if (annotLine.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && annotLine.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  315. BasicVm.AnnotTypeTitle = "箭头";
  316. else
  317. BasicVm.AnnotTypeTitle = "线";
  318. }
  319. break;
  320. }
  321. }
  322. public AnnotAttribEvent AnnotEvent { get; set; }
  323. private AnnotHandlerEventArgs Annot;
  324. private AnnotTransfer PropertyPanel;
  325. public void OnNavigatedTo(NavigationContext navigationContext)
  326. {
  327. navigationContext.Parameters.TryGetValue<AnnotTransfer>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  328. if (PropertyPanel != null)
  329. {
  330. AnnotEvent = PropertyPanel.AnnotEvent;
  331. BasicVm.AnnotType = PropertyPanel.annot.EventType;
  332. Annot = PropertyPanel.annot;
  333. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  334. if (BasicVm.IsMultiSelected)
  335. {
  336. IsAttributeEquals();
  337. }
  338. else
  339. {
  340. GetAnnotProperty();
  341. }
  342. LoadPropertyHandler?.Invoke(null, Annot);
  343. }
  344. }
  345. private List<SquareAnnotArgs> ConvertLists()
  346. {
  347. List<SquareAnnotArgs> FreeTextLists = new List<SquareAnnotArgs>();
  348. foreach (var item in PropertyPanel.annotlists)
  349. {
  350. var itemFreeText = item as SquareAnnotArgs;
  351. if (itemFreeText != null)
  352. {
  353. FreeTextLists.Add(itemFreeText);
  354. }
  355. }
  356. if (FreeTextLists.Count != PropertyPanel.annotlists.Count)
  357. return null;
  358. else
  359. return FreeTextLists;
  360. }
  361. private void IsAttributeEquals()
  362. {
  363. var list = ConvertLists();
  364. if (list != null)
  365. {
  366. var temp = list[0];
  367. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  368. isNoEqualsDir.Add("FillColor", false);
  369. isNoEqualsDir.Add("LineColor", false);
  370. isNoEqualsDir.Add("Thickness", false);
  371. isNoEqualsDir.Add("ThickSolidDashStyle", false);
  372. foreach (var item in list)
  373. {
  374. if (item == list[0])
  375. continue;
  376. if (isNoEqualsDir["FillColor"] == false)
  377. {
  378. 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)
  379. {
  380. BasicVm.FillColor = new SolidColorBrush(Color.FromArgb(0x01, 0xff, 0xff, 0xff));
  381. isNoEqualsDir["FillColor"] = true;
  382. }
  383. }
  384. if (isNoEqualsDir["LineColor"] == false)
  385. {
  386. 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)
  387. {
  388. BasicVm.BorderColor = new SolidColorBrush(Color.FromArgb(0x01,0xff,0xff,0xff));
  389. isNoEqualsDir["LineColor"] = true;
  390. }
  391. }
  392. if (isNoEqualsDir["Thickness"] == false)
  393. {
  394. isNoEqualsDir["Thickness"] = true;
  395. if (temp.LineWidth > item.LineWidth)
  396. {
  397. BasicVm.AnnotThickness = temp.LineWidth;
  398. }
  399. else
  400. {
  401. BasicVm.AnnotThickness = item.LineWidth;
  402. }
  403. }
  404. if (isNoEqualsDir["ThickSolidDashStyle"] == false)
  405. {
  406. if (AnnotTransfer.IsSolidStyle(temp.LineDash) != AnnotTransfer.IsSolidStyle(item.LineDash))
  407. {
  408. isNoEqualsDir["ThickSolidDashStyle"] = true;
  409. }
  410. }
  411. }
  412. ////以下是多选注释的属性相等时
  413. if (isNoEqualsDir["FillColor"] == false)
  414. {
  415. BasicVm.FillColor = new SolidColorBrush(temp.BgColor);
  416. }
  417. if (isNoEqualsDir["LineColor"] == false)
  418. {
  419. BasicVm.BorderColor = new SolidColorBrush(temp.LineColor);
  420. }
  421. if (isNoEqualsDir["Thickness"] == false)
  422. {
  423. BasicVm.AnnotThickness = temp.LineWidth;
  424. }
  425. if (isNoEqualsDir["ThickSolidDashStyle"] == true)
  426. {
  427. BasicVm.IsSolidLine = BasicVm.IsDashLine = false;
  428. }
  429. else
  430. {
  431. var isSolid = AnnotTransfer.IsSolidStyle(temp.LineDash);
  432. BasicVm.IsSolidLine = isSolid;
  433. BasicVm.IsDashLine = !isSolid;
  434. }
  435. }
  436. }
  437. private bool IsSolidStyle(AnnotHandlerEventArgs annot)
  438. {
  439. bool isSolid = true;
  440. if (annot is SquareAnnotArgs)
  441. {
  442. var Square = Annot as SquareAnnotArgs;
  443. isSolid = AnnotTransfer.IsSolidStyle(Square.LineDash);
  444. }
  445. else if (annot is CircleAnnotArgs)
  446. {
  447. var Circle = Annot as CircleAnnotArgs;
  448. isSolid = AnnotTransfer.IsSolidStyle(Circle.LineDash);
  449. }
  450. else if (annot is LineAnnotArgs)
  451. {
  452. var line = Annot as LineAnnotArgs;
  453. isSolid = AnnotTransfer.IsSolidStyle(line.LineDash);
  454. }
  455. return isSolid;
  456. }
  457. private void GetAnnotProperty()
  458. {
  459. if (Annot != null)
  460. {
  461. bool isSolid = true;
  462. switch (Annot.EventType)
  463. {
  464. case AnnotArgsType.AnnotSquare:
  465. if (Annot is SquareAnnotArgs)
  466. {
  467. var Square = Annot as SquareAnnotArgs;
  468. BasicVm.BorderColor = new SolidColorBrush(Square.LineColor);
  469. BasicVm.FillColor = new SolidColorBrush(Square.BgColor);
  470. BasicVm.BorderOpacity = Square.Transparency;
  471. BasicVm.FillOpacity = Square.Transparency;
  472. BasicVm.AnnotThickness = Square.LineWidth;
  473. Dash = Square.LineDash;
  474. SharpsType("Rect", true);
  475. BasicVm.AnnotTypeTitle = "矩形";
  476. IsLineAnnot = false;
  477. }
  478. break;
  479. case AnnotArgsType.AnnotCircle:
  480. if (Annot is CircleAnnotArgs)
  481. {
  482. var Circle = Annot as CircleAnnotArgs;
  483. BasicVm.BorderColor = new SolidColorBrush(Circle.LineColor);
  484. BasicVm.FillColor = new SolidColorBrush(Circle.BgColor);
  485. BasicVm.BorderOpacity = Circle.Transparency;
  486. BasicVm.FillOpacity = Circle.Transparency;
  487. BasicVm.AnnotThickness = Circle.LineWidth;
  488. Dash = Circle.LineDash;
  489. SharpsType("Circle", true);
  490. BasicVm.AnnotTypeTitle = "圆";
  491. IsLineAnnot = false;
  492. }
  493. break;
  494. case AnnotArgsType.AnnotLine:
  495. if (Annot is LineAnnotArgs)
  496. {
  497. var line = Annot as LineAnnotArgs;
  498. BasicVm.BorderColor = new SolidColorBrush(line.LineColor);
  499. BasicVm.FillColor = new SolidColorBrush(line.LineColor);
  500. BasicVm.BorderOpacity = line.Transparency;
  501. BasicVm.FillOpacity = line.Transparency;
  502. BasicVm.AnnotThickness = line.LineWidth;
  503. Dash = line.LineDash;
  504. if (line.TailLineType == C_LINE_TYPE.LINETYPE_ARROW && line.HeadLineType == C_LINE_TYPE.LINETYPE_NONE)
  505. {
  506. SharpsType("Arrow", true);
  507. BasicVm.AnnotTypeTitle = "箭头";
  508. }
  509. else
  510. {
  511. SharpsType("Line", true);
  512. BasicVm.AnnotTypeTitle = "线条";
  513. }
  514. IsLineAnnot = true;
  515. }
  516. break;
  517. }
  518. isSolid = IsSolidStyle(Annot);
  519. BasicVm.IsSolidLine = isSolid;
  520. BasicVm.IsDashLine = !isSolid;
  521. }
  522. }
  523. }
  524. }