FreetextAnnotPropertyViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  163. AnnotEvent?.UpdateAnnot();
  164. }
  165. }
  166. private void SelectedOpacityValue(object obj)
  167. {
  168. if (obj != null && obj is double)
  169. {
  170. BasicVm.FillOpacity = (double)obj;
  171. BasicVm.FillColor.Opacity = BasicVm.FillOpacity;
  172. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity);
  173. AnnotEvent?.UpdateAnnot();
  174. }
  175. }
  176. //设置字体大小
  177. private void FontSizeChanged()
  178. {
  179. if (FontVm.CurrentFontSize != null)
  180. {
  181. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontSize, FontVm.CurrentFontSize.Value);
  182. }
  183. }
  184. //设置字体样式
  185. private void FontFamilyChanged()
  186. {
  187. if (string.IsNullOrEmpty(FontVm.CurrentFontFamily.ValueStr) == false)
  188. {
  189. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontFamily, new FontFamily(FontVm.CurrentFontFamily.ValueStr));
  190. }
  191. }
  192. private void SelectedFillColorChange(object obj)
  193. {
  194. if (obj != null)
  195. {
  196. var colorValue = (Color)obj;
  197. if (colorValue != null)
  198. {
  199. BasicVm.FillColor = new SolidColorBrush(colorValue);
  200. PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue);
  201. // BasicVm.FillColor.Opacity = BasicVmFillOpacity;
  202. }
  203. }
  204. }
  205. private void SelectedColor_Command(object obj)
  206. {
  207. if (obj != null)
  208. {
  209. var colorValue = (Color)obj;
  210. if (colorValue != null)
  211. {
  212. FontVm.FontColor = new SolidColorBrush(colorValue);
  213. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, colorValue);
  214. AnnotEvent?.UpdateAnnot();
  215. }
  216. }
  217. }
  218. private void CustomFontStyle()
  219. {
  220. if (FontVm.CurrentPresetFont != null)
  221. {
  222. ContextMenu menu;
  223. if (FontVm.CurrentPresetFont.ValueStr == "custom")
  224. {
  225. menu = SelectAnnotContextMenu(false);
  226. }
  227. else
  228. {
  229. menu = SelectAnnotContextMenu(true);
  230. }
  231. if (menu != null)
  232. {
  233. menu.IsOpen = true;
  234. }
  235. }
  236. }
  237. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  238. {
  239. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  240. if (popMenu != null && popMenu.Items.Count == 2)
  241. {
  242. //用所选部分重新定义
  243. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  244. menuItem.IsEnabled = isEnable;
  245. menuItem.Command = ReDefineFontStyleCommand;
  246. //恢复默认预设样式
  247. menuItem = popMenu.Items[1] as MenuItem;
  248. menuItem.IsEnabled = isEnable;
  249. menuItem.Command = RestoreDefaultStyleCommand;
  250. }
  251. return popMenu;
  252. }
  253. public List<PresetFontItem> FontStyleList = new List<PresetFontItem>();
  254. private void ReDefineFontStyle()
  255. {
  256. var item = FontStyleList.FirstOrDefault(temp => temp.mTag == FontVm.CurrentPresetFont.ValueStr);
  257. if (FontVm.CurrentFontFamily.ValueStr == "Bold")
  258. {
  259. item.mFontStyle = FontStyles.Normal;
  260. item.mFontWeight = FontWeights.Bold;
  261. }
  262. else if (FontVm.CurrentFontFamily.ValueStr == "Regular")
  263. {
  264. item.mFontStyle = FontStyles.Normal;
  265. item.mFontWeight = FontWeights.Normal;
  266. }
  267. else
  268. {
  269. item.mFontStyle = FontStyles.Italic;
  270. item.mFontWeight = FontWeights.Bold;
  271. }
  272. item.mFontSize = (int)FontVm.CurrentFontSize.Value;
  273. }
  274. private void RestoreDefaultStyle()
  275. {
  276. // var item = FontStyleList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  277. }
  278. public bool IsNavigationTarget(NavigationContext navigationContext)
  279. {
  280. return true;
  281. }
  282. public void OnNavigatedFrom(NavigationContext navigationContext)
  283. {
  284. BasicVm.IsMultiSelected = false;
  285. }
  286. public void OnNavigatedTo(NavigationContext navigationContext)
  287. {
  288. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  289. if (PropertyPanel != null)
  290. {
  291. AnnotEvent = PropertyPanel.AnnotEvent;
  292. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  293. LoadPropertyHandler?.Invoke(this, Annot);
  294. BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected;
  295. if(BasicVm.IsMultiSelected)
  296. {
  297. bool isDifferentFontFamily = false;
  298. bool isDifferentFontSize = false;
  299. bool isDifferentFontStyle = false;
  300. FreeTextAnnotArgs tempAnnot = null;
  301. foreach (var item in PropertyPanel.annotlists)
  302. {
  303. var itemFreeText = item as FreeTextAnnotArgs;
  304. if(itemFreeText != null)
  305. {
  306. if (tempAnnot == null)
  307. {
  308. tempAnnot = itemFreeText;
  309. }
  310. else
  311. {
  312. if (itemFreeText.FontFamily != tempAnnot.FontFamily)
  313. {
  314. isDifferentFontFamily = true;
  315. }
  316. if (itemFreeText.FontSize != tempAnnot.FontSize)
  317. {
  318. isDifferentFontSize = true;
  319. }
  320. if (itemFreeText.FontStyle != tempAnnot.FontStyle || itemFreeText.FontWeight != tempAnnot.FontWeight)
  321. {
  322. isDifferentFontStyle = true;
  323. }
  324. tempAnnot = itemFreeText;
  325. }
  326. }
  327. }
  328. if (isDifferentFontFamily || isDifferentFontSize || isDifferentFontStyle)
  329. FontVm.CurrentPresetFont = null;
  330. if(isDifferentFontFamily)
  331. FontVm.CurrentFontFamily = null;
  332. if (isDifferentFontSize)
  333. FontVm.CurrentFontSize = null;
  334. if (isDifferentFontStyle)
  335. FontVm.CurrrentFontWeightStyle = null;
  336. BasicVm.SetStrDashStyle("None");
  337. }
  338. else
  339. {
  340. GetAnnotProperty();
  341. }
  342. }
  343. }
  344. private void GetAnnotProperty()
  345. {
  346. BasicVm.FillColor = new SolidColorBrush(Annot.BgColor);
  347. BasicVm.FillOpacity = Annot.Transparency;
  348. FontVm.FontColor = new SolidColorBrush(Annot.FontColor);
  349. FontVm.GetCurrentFontSize((int)Annot.FontSize);
  350. FontVm.GetCurrentFontFamily(Annot.FontFamily.ToString(), Annot.FontFamily.ToString());
  351. FontVm.GetFontWeights_Style(Annot.FontStyle, Annot.FontWeight);
  352. string str = "";
  353. if (Annot.Align == TextAlignment.Left)
  354. str = "AlignLeft";
  355. else if (Annot.Align == TextAlignment.Center)
  356. str = "AlignCenter";
  357. else if (Annot.Align == TextAlignment.Right)
  358. str = "AlignRight";
  359. else if(Annot.Align == TextAlignment.Justify)
  360. str = "AlignJustify";
  361. else
  362. str = "None";
  363. BasicVm.SetStrDashStyle(str);
  364. }
  365. }
  366. }