SharpsAnnotPropertyViewModel.cs 21 KB

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