FreetextAnnotPropertyViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.CustomControl.CompositeControl;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  6. using PDF_Office.ViewModels.Tools;
  7. using PDFSettings;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using System;
  12. using System.Collections.Generic;
  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.Media;
  19. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  20. {
  21. public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware
  22. {
  23. private FontBoardVm _fontVm = new FontBoardVm(true);
  24. public FontBoardVm FontVm
  25. {
  26. get { return _fontVm; }
  27. set { SetProperty(ref _fontVm, value); }
  28. }
  29. private AnnotBasePropertyVM _basicVm = new AnnotBasePropertyVM();
  30. public AnnotBasePropertyVM BasicVm
  31. {
  32. get { return _basicVm; }
  33. set => SetProperty(ref _basicVm, value);
  34. }
  35. public AnnotAttribEvent AnnotEvent { get; set; }
  36. private FreeTextAnnotArgs Annot;
  37. private AnnotPropertyPanel PropertyPanel;
  38. public DelegateCommand SelectedPresetFontCommand { get; set; }
  39. public DelegateCommand FontFamilyChangedCommand { get; set; }
  40. public DelegateCommand FontStyleWeightChangedCommand { get; set; }
  41. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  42. public DelegateCommand<object> SelectedColorCommand { get; set; }
  43. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  44. public DelegateCommand FontSizeChangedCommand { get; set; }
  45. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  46. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  47. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  48. public DelegateCommand CustomFontStyleCommand { get; set; }
  49. public DelegateCommand ReDefineFontStyleCommand { get; set; }
  50. public DelegateCommand RestoreDefaultStyleCommand { get; set; }
  51. public event EventHandler<object> LoadPropertyHandler;
  52. public FreetextAnnotPropertyViewModel()
  53. {
  54. //文本设置
  55. //预设文本样式
  56. SelectedPresetFontCommand = new DelegateCommand(SelectedPresetFont);
  57. //文本字重、字体
  58. FontStyleWeightChangedCommand = new DelegateCommand(FontStyleWeightChanged);
  59. //字体
  60. FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged);
  61. //自定义
  62. CustomFontStyleCommand = new DelegateCommand(CustomFontStyle);
  63. //重新定义
  64. ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
  65. //恢复默认
  66. RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
  67. //颜色
  68. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  69. //填充颜色
  70. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColorChange);
  71. //填充颜色不透明度
  72. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
  73. //大小
  74. FontSizeChangedCommand = new DelegateCommand(FontSizeChanged);
  75. //内容对齐
  76. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  77. //MVP不上
  78. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  79. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  80. InitVariable();
  81. }
  82. private void InitVariable()
  83. {
  84. }
  85. private void TextAlignChecked(object obj)
  86. {
  87. if ((string)obj != null)
  88. {
  89. switch ((string)obj)
  90. {
  91. case "AlignLeft":
  92. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  93. break;
  94. case "AlignCenter":
  95. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  96. break;
  97. case "AlignRight":
  98. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  99. break;
  100. case "Justify":
  101. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Justify);
  102. break;
  103. }
  104. }
  105. }
  106. private void LineMode_Checked(object obj)
  107. {
  108. if(obj != null)
  109. {
  110. var tag = ((string)obj);
  111. switch (tag)
  112. {
  113. case "Dashed":
  114. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  115. break;
  116. case "Solid":
  117. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  118. break;
  119. }
  120. AnnotEvent?.UpdateAnnot();
  121. }
  122. }
  123. //从预设样式设置文本
  124. private void SelectedPresetFont()
  125. {
  126. if (FontVm.CurrentPresetFont != null)
  127. {
  128. var itemComboxItem = FontVm.CurrentPresetFont;
  129. var item = FontVm.PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  130. if (item != null)
  131. {
  132. FontVm.GetCurrentFontSize(item.mFontSize);
  133. FontVm.GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  134. FontVm.GetFontWeights_Style(item.mFontStyle, item.mFontWeight);
  135. Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
  136. AnnotAttribDir.Add(AnnotAttrib.FontSize, item.mFontSize);
  137. AnnotAttribDir.Add(AnnotAttrib.FontFamily, item.mFontFamily);
  138. AnnotAttribDir.Add(AnnotAttrib.FontStyle, item.mFontStyle);
  139. AnnotAttribDir.Add(AnnotAttrib.FontWeight, item.mFontWeight);
  140. PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
  141. }
  142. }
  143. }
  144. //设置文本字重、样式
  145. private void FontStyleWeightChanged()
  146. {
  147. if(FontVm.CurrrentFontWeightStyle != null)
  148. {
  149. FontVm.UpdateFontWeight_Style();
  150. Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
  151. AnnotAttribDir.Add(AnnotAttrib.FontStyle, FontVm.FontStyleItem);
  152. AnnotAttribDir.Add(AnnotAttrib.FontWeight, FontVm.FontWeightItem);
  153. PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
  154. }
  155. }
  156. private void SelectedFillOpacity(object obj)
  157. {
  158. if (obj != null)
  159. {
  160. BasicVm.FillOpacity = (double)obj;
  161. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  162. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  163. }
  164. }
  165. private void SelectedOpacityValue(object obj)
  166. {
  167. if (obj != null && obj is double)
  168. {
  169. BasicVm.FillOpacity = (double)obj;
  170. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  171. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  172. }
  173. }
  174. //设置字体大小
  175. private void FontSizeChanged()
  176. {
  177. if (FontVm.CurrentFontSize != null)
  178. {
  179. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontSize, FontVm.CurrentFontSize.Value);
  180. }
  181. }
  182. //设置字体样式
  183. private void FontFamilyChanged()
  184. {
  185. if (string.IsNullOrEmpty(FontVm.CurrentFontFamily.ValueStr) == false)
  186. {
  187. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontFamily, new FontFamily(FontVm.CurrentFontFamily.ValueStr));
  188. }
  189. }
  190. private void SelectedFillColorChange(object obj)
  191. {
  192. if (obj != null)
  193. {
  194. var colorValue = (Color)obj;
  195. if (colorValue != null)
  196. {
  197. BasicVm.FillColor = new SolidColorBrush(colorValue);
  198. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue);
  199. // BasicVm.FillColor.Opacity = BasicVmFillOpacity;
  200. }
  201. }
  202. }
  203. private void SelectedColor_Command(object obj)
  204. {
  205. if (obj != null)
  206. {
  207. var colorValue = (Color)obj;
  208. if (colorValue != null)
  209. {
  210. FontVm.FontColor = new SolidColorBrush(colorValue);
  211. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontColor, colorValue);
  212. }
  213. }
  214. }
  215. private void CustomFontStyle()
  216. {
  217. if (FontVm.CurrentPresetFont != null)
  218. {
  219. ContextMenu menu;
  220. if (FontVm.CurrentPresetFont.ValueStr == "custom")
  221. {
  222. menu = SelectAnnotContextMenu(false);
  223. }
  224. else
  225. {
  226. menu = SelectAnnotContextMenu(true);
  227. }
  228. if (menu != null)
  229. {
  230. menu.IsOpen = true;
  231. }
  232. }
  233. }
  234. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  235. {
  236. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  237. if (popMenu != null && popMenu.Items.Count == 2)
  238. {
  239. //用所选部分重新定义
  240. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  241. menuItem.IsEnabled = isEnable;
  242. menuItem.Command = ReDefineFontStyleCommand;
  243. //恢复默认预设样式
  244. menuItem = popMenu.Items[1] as MenuItem;
  245. menuItem.IsEnabled = isEnable;
  246. menuItem.Command = RestoreDefaultStyleCommand;
  247. }
  248. return popMenu;
  249. }
  250. public List<PresetFontItem> FontStyleList = new List<PresetFontItem>();
  251. private void ReDefineFontStyle()
  252. {
  253. var item = FontStyleList.FirstOrDefault(temp => temp.mTag == FontVm.CurrentPresetFont.ValueStr);
  254. if (FontVm.CurrentFontFamily.ValueStr == "Bold")
  255. {
  256. item.mFontStyle = FontStyles.Normal;
  257. item.mFontWeight = FontWeights.Bold;
  258. }
  259. else if (FontVm.CurrentFontFamily.ValueStr == "Regular")
  260. {
  261. item.mFontStyle = FontStyles.Normal;
  262. item.mFontWeight = FontWeights.Normal;
  263. }
  264. else
  265. {
  266. item.mFontStyle = FontStyles.Italic;
  267. item.mFontWeight = FontWeights.Bold;
  268. }
  269. item.mFontSize = (int)FontVm.CurrentFontSize.Value;
  270. }
  271. private void RestoreDefaultStyle()
  272. {
  273. // var item = FontStyleList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  274. }
  275. public bool IsNavigationTarget(NavigationContext navigationContext)
  276. {
  277. return true;
  278. }
  279. public void OnNavigatedFrom(NavigationContext navigationContext)
  280. {
  281. BasicVm.IsMultiSelected = false;
  282. }
  283. public void OnNavigatedTo(NavigationContext navigationContext)
  284. {
  285. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  286. if (PropertyPanel != null)
  287. {
  288. AnnotEvent = PropertyPanel.AnnotEvent;
  289. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  290. LoadPropertyHandler?.Invoke(this, Annot);
  291. BasicVm.AnnotTypeTitle = "文字";
  292. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  293. if (BasicVm.IsMultiSelected)
  294. {
  295. IsAttributeEquals();
  296. BasicVm.SetStrDashStyle("None");
  297. }
  298. else
  299. {
  300. GetAnnotProperty();
  301. }
  302. }
  303. }
  304. private List<FreeTextAnnotArgs> ConvertLists()
  305. {
  306. List<FreeTextAnnotArgs> FreeTextLists = new List<FreeTextAnnotArgs>();
  307. foreach (var item in PropertyPanel.annotlists)
  308. {
  309. var itemFreeText = item as FreeTextAnnotArgs;
  310. if (itemFreeText != null)
  311. {
  312. FreeTextLists.Add(itemFreeText);
  313. }
  314. }
  315. if (FreeTextLists.Count != PropertyPanel.annotlists.Count)
  316. return null;
  317. else
  318. return FreeTextLists;
  319. }
  320. private void IsAttributeEquals()
  321. {
  322. var list = ConvertLists();
  323. if (list != null)
  324. {
  325. var temp = list[0];
  326. Dictionary<string, bool> isNoEqualsDir = new Dictionary<string, bool>();
  327. isNoEqualsDir.Add("FontFamily", false);
  328. isNoEqualsDir.Add("FontSize", false);
  329. isNoEqualsDir.Add("FontStyleFontWeight", false);
  330. isNoEqualsDir.Add("FontColor", false);
  331. isNoEqualsDir.Add("FillColor", false);
  332. foreach (var item in list)
  333. {
  334. if (item == list[0])
  335. continue;
  336. if (temp.FontFamily != item.FontFamily)
  337. {
  338. isNoEqualsDir["FontFamily"] = true;
  339. FontVm.CurrentFontFamily = null;
  340. }
  341. if (temp.FontSize != item.FontSize)
  342. {
  343. isNoEqualsDir["FontSize"] = true;
  344. FontVm.CurrentFontSize = null;
  345. }
  346. if (temp.FontStyle != item.FontStyle || temp.FontWeight != item.FontWeight)
  347. {
  348. isNoEqualsDir["FontStyleFontWeight"] = true;
  349. FontVm.CurrrentFontWeightStyle = null;
  350. }
  351. if (isNoEqualsDir["FontFamily"] == true || isNoEqualsDir["FontSize"] == true || isNoEqualsDir["FontStyleFontWeight"] == true)
  352. {
  353. FontVm.CurrentPresetFont = null;
  354. }
  355. if (isNoEqualsDir["FontColor"] == false)
  356. {
  357. if (temp.FontColor.A != item.FontColor.A || temp.FontColor.R != item.FontColor.R || temp.FontColor.G != item.FontColor.G || temp.FontColor.B != item.FontColor.B)
  358. {
  359. FontVm.FontColor = new SolidColorBrush(Colors.Transparent);
  360. isNoEqualsDir["FontColor"] = true;
  361. }
  362. }
  363. if (isNoEqualsDir["FillColor"] == false)
  364. {
  365. 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)
  366. {
  367. BasicVm.FillColor = new SolidColorBrush(Colors.Transparent);
  368. isNoEqualsDir["FillColor"] = true;
  369. }
  370. }
  371. }
  372. ////以下是多选注释的属性相等时
  373. if (isNoEqualsDir["FontFamily"] == false)
  374. {
  375. FontVm.GetCurrentFontFamily(temp.FontFamily.ToString(), temp.FontFamily.ToString());
  376. }
  377. if (isNoEqualsDir["FontSize"] == false)
  378. {
  379. FontVm.GetCurrentFontSize((int)temp.FontSize);
  380. }
  381. if (isNoEqualsDir["FontStyleFontWeight"] == false)
  382. {
  383. FontVm.GetFontWeights_Style(temp.FontStyle, temp.FontWeight);
  384. }
  385. if (isNoEqualsDir["FontColor"] == false)
  386. {
  387. FontVm.FontColor = new SolidColorBrush(temp.FontColor);
  388. }
  389. if (isNoEqualsDir["FillColor"] == false)
  390. {
  391. FontVm.FontColor = new SolidColorBrush(temp.BgColor);
  392. }
  393. }
  394. }
  395. private void GetAnnotProperty()
  396. {
  397. BasicVm.FillColor = new SolidColorBrush(Annot.BgColor);
  398. BasicVm.FillOpacity = Annot.Transparency;
  399. FontVm.FontColor = new SolidColorBrush(Annot.FontColor);
  400. FontVm.GetCurrentFontSize((int)Annot.FontSize);
  401. FontVm.GetCurrentFontFamily(Annot.FontFamily.ToString(), Annot.FontFamily.ToString());
  402. FontVm.GetFontWeights_Style(Annot.FontStyle, Annot.FontWeight);
  403. string str = "";
  404. if (Annot.Align == TextAlignment.Left)
  405. str = "AlignLeft";
  406. else if (Annot.Align == TextAlignment.Center)
  407. str = "AlignCenter";
  408. else if (Annot.Align == TextAlignment.Right)
  409. str = "AlignRight";
  410. else if(Annot.Align == TextAlignment.Justify)
  411. str = "AlignJustify";
  412. else
  413. str = "None";
  414. BasicVm.SetStrDashStyle(str);
  415. }
  416. }
  417. }