FreetextAnnotPropertyViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 : FontBoard, INavigationAware
  22. {
  23. private bool isSelectedEmpty;
  24. public bool IsSelectedEmpty
  25. {
  26. get { return isSelectedEmpty; }
  27. set { SetProperty(ref isSelectedEmpty, value); }
  28. }
  29. private double fillOpacity = 1;
  30. public double FillOpacity
  31. {
  32. get { return fillOpacity; }
  33. set
  34. {
  35. SetProperty(ref fillOpacity, value);
  36. }
  37. }
  38. private Brush _currentFillColor = new SolidColorBrush(Colors.Transparent);
  39. public Brush CurrentFillColor
  40. {
  41. get { return _currentFillColor; }
  42. set
  43. {
  44. SetProperty(ref _currentFillColor, value);
  45. }
  46. }
  47. private Brush fillColor = new SolidColorBrush(Colors.Transparent);
  48. public Brush FillColor
  49. {
  50. get { return fillColor; }
  51. set
  52. {
  53. SetProperty(ref fillColor, value);
  54. if (IsCanSave)
  55. {
  56. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, (fillColor as SolidColorBrush).Color);
  57. AnnotEvent?.UpdateAnnot();
  58. }
  59. else
  60. {
  61. CurrentFillColor = fillColor;
  62. }
  63. }
  64. }
  65. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  66. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  67. public DelegateCommand<object> SelectedColorCommand { get; set; }
  68. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  69. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  70. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  71. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  72. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  73. public DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  74. public DelegateCommand CustomFontStyleCommand { get; set; }
  75. public DelegateCommand ReDefineFontStyleCommand { get; set; }
  76. public DelegateCommand RestoreDefaultStyleCommand { get; set; }
  77. public event EventHandler<object> LoadPropertyHandler;
  78. public FreetextAnnotPropertyViewModel()
  79. {
  80. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
  81. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  82. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  83. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  84. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged_Command);
  85. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged_Command);
  86. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  87. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  88. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  89. CustomFontStyleCommand = new DelegateCommand(CustomFontStyle);
  90. ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
  91. RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
  92. InitVariable();
  93. }
  94. private void InitVariable()
  95. {
  96. InitFontStyles();
  97. InitFontFamilyComboBox();
  98. InitFontStyleComboBox();
  99. }
  100. private void InitFontFamilyComboBox()
  101. {
  102. FontFamilyItems = TextFont.GetFamily();
  103. }
  104. private void InitFontStyleComboBox()
  105. {
  106. FontStyleItems = TextFont.GetFontStyle();
  107. }
  108. private void InitFontStyles()
  109. {
  110. PresetFontItems = new List<ComboDataItem>();
  111. PresetFontList = TextFont.GetCachePresetFontList();
  112. foreach (var item in PresetFontList)
  113. {
  114. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  115. PresetFontItems.Add(itemData);
  116. }
  117. }
  118. private void TextAlignChecked(object obj)
  119. {
  120. if ((string)obj != null /*&& TextEditEvent != null*/)
  121. {
  122. switch ((string)obj)
  123. {
  124. case "AlignLeft":
  125. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  126. break;
  127. case "AlignCenter":
  128. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  129. break;
  130. case "AlignRight":
  131. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  132. break;
  133. case "Align":
  134. break;
  135. }
  136. AnnotEvent?.UpdateAnnot();
  137. }
  138. }
  139. private void LineMode_Checked(object obj)
  140. {
  141. if(obj != null)
  142. {
  143. var tag = ((string)obj);
  144. switch (tag)
  145. {
  146. case "Dashed":
  147. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  148. break;
  149. case "Solid":
  150. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  151. break;
  152. }
  153. AnnotEvent?.UpdateAnnot();
  154. }
  155. }
  156. private void SelectedFontStyle(object obj)
  157. {
  158. if (obj != null && (PresetFontItem)obj != null)
  159. {
  160. var item = (PresetFontItem)obj;
  161. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, item.mFontSize);
  162. AnnotEvent?.UpdateAnnot();
  163. }
  164. }
  165. private void SelectedFillOpacity(object obj)
  166. {
  167. if (obj != null)
  168. {
  169. FillOpacity = (double)obj;
  170. SelectColor.Opacity = FillOpacity;
  171. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  172. AnnotEvent?.UpdateAnnot();
  173. }
  174. }
  175. private void SelectedOpacityValue(object obj)
  176. {
  177. if (obj != null)
  178. {
  179. FillOpacity = (double)obj;
  180. SelectColor.Opacity = FillOpacity;
  181. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  182. AnnotEvent?.UpdateAnnot();
  183. }
  184. }
  185. private void FontSizeChanged_Command(object obj)
  186. {
  187. if (obj != null)
  188. {
  189. var item = (ComboBoxItem)obj;
  190. var content = (string)item.Content;
  191. if (content != null)
  192. {
  193. var intData = int.Parse(content);
  194. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, intData);
  195. AnnotEvent?.UpdateAnnot();
  196. }
  197. }
  198. }
  199. private void FontFamilyChanged_Command(object obj)
  200. {
  201. }
  202. private void SelectedFillColor_Command(object obj)
  203. {
  204. if (obj != null)
  205. {
  206. var colorValue = (Color)obj;
  207. if (colorValue != null)
  208. {
  209. FillColor = new SolidColorBrush(colorValue);
  210. FillColor.Opacity = FillOpacity;
  211. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  212. changeData[AnnotArgsType.AnnotFreehand] = obj;
  213. PropertyPanel.DataChangedInvoke(this, changeData);
  214. }
  215. }
  216. }
  217. private void SelectedColor_Command(object obj)
  218. {
  219. if (obj != null)
  220. {
  221. var colorValue = (Color)obj;
  222. if (colorValue != null)
  223. {
  224. SelectColor = new SolidColorBrush(colorValue);
  225. SelectColor.Opacity = FillOpacity;
  226. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  227. changeData[AnnotArgsType.AnnotFreehand] = obj;
  228. PropertyPanel.DataChangedInvoke(this, changeData);
  229. }
  230. }
  231. }
  232. private void CustomFontStyle()
  233. {
  234. if (CurrentPresetFont != null)
  235. {
  236. ContextMenu menu;
  237. if (CurrentPresetFont.ValueStr == "custom")
  238. {
  239. menu = SelectAnnotContextMenu(false);
  240. }
  241. else
  242. {
  243. menu = SelectAnnotContextMenu(true);
  244. }
  245. if (menu != null)
  246. {
  247. menu.IsOpen = true;
  248. }
  249. }
  250. }
  251. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  252. {
  253. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  254. if (popMenu != null && popMenu.Items.Count == 2)
  255. {
  256. //用所选部分重新定义
  257. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  258. menuItem.IsEnabled = isEnable;
  259. menuItem.Command = ReDefineFontStyleCommand;
  260. //恢复默认预设样式
  261. menuItem = popMenu.Items[1] as MenuItem;
  262. menuItem.IsEnabled = isEnable;
  263. menuItem.Command = RestoreDefaultStyleCommand;
  264. }
  265. return popMenu;
  266. }
  267. public List<PresetFontItem> FontStyleList = new List<PresetFontItem>();
  268. private void ReDefineFontStyle()
  269. {
  270. var item = FontStyleList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  271. if (CurrentFontFamily.ValueStr == "Bold")
  272. {
  273. item.mFontStyle = FontStyles.Normal;
  274. item.mFontWeight = FontWeights.Bold;
  275. }
  276. else if (CurrentFontFamily.ValueStr == "Regular")
  277. {
  278. item.mFontStyle = FontStyles.Normal;
  279. item.mFontWeight = FontWeights.Normal;
  280. }
  281. else
  282. {
  283. item.mFontStyle = FontStyles.Italic;
  284. item.mFontWeight = FontWeights.Bold;
  285. }
  286. item.mFontSize = (int)CurrentFontSize.Value;
  287. }
  288. private void RestoreDefaultStyle()
  289. {
  290. var item = FontStyleList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  291. }
  292. public bool IsNavigationTarget(NavigationContext navigationContext)
  293. {
  294. return true;
  295. }
  296. public void OnNavigatedFrom(NavigationContext navigationContext)
  297. {
  298. IsCanSave = false;
  299. ChangedValue -= FontMode_ChangedValue;
  300. }
  301. public AnnotAttribEvent AnnotEvent { get; set; }
  302. private FreeTextAnnotArgs Annot;
  303. private AnnotPropertyPanel PropertyPanel;
  304. public void OnNavigatedTo(NavigationContext navigationContext)
  305. {
  306. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  307. if (PropertyPanel != null)
  308. {
  309. AnnotEvent = PropertyPanel.AnnotEvent;
  310. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  311. LoadPropertyHandler?.Invoke(this, Annot);
  312. ChangedValue -= FontMode_ChangedValue;
  313. ChangedValue += FontMode_ChangedValue;
  314. SelectColor = new SolidColorBrush(Annot.FontColor);
  315. FillColor = new SolidColorBrush(Annot.BgColor);
  316. IsCanSave = true;
  317. }
  318. }
  319. private void FontMode_ChangedValue(object sender, FontSetModeType e)
  320. {
  321. if (sender != null)
  322. {
  323. switch (e)
  324. {
  325. case FontSetModeType.PresetFontStyes:
  326. if (PresetFontList != null && sender is string == true)
  327. {
  328. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
  329. if (item != null && Annot != null)
  330. {
  331. }
  332. }
  333. break;
  334. case FontSetModeType.FontFamilys:
  335. if (sender is string == true)
  336. {
  337. AnnotEvent.UpdateAttrib(AnnotAttrib.FontFamily, (string)sender);
  338. AnnotEvent.UpdateAnnot();
  339. }
  340. break;
  341. case FontSetModeType.FontSizes:
  342. if (sender is double == true && (double)sender > 0 && Annot.FontSize > 0)
  343. {
  344. AnnotEvent.UpdateAttrib(AnnotAttrib.FontSize, (double)sender);
  345. AnnotEvent.UpdateAnnot();
  346. }
  347. break;
  348. case FontSetModeType.FontWeight_Style:
  349. UpdateFontWeight_Style();
  350. AnnotEvent.UpdateAttrib(AnnotAttrib.FontWeight, FontWeightItem);
  351. AnnotEvent.UpdateAttrib(AnnotAttrib.FontStyle, FontStyleItem);
  352. AnnotEvent.UpdateAnnot();
  353. break;
  354. case FontSetModeType.FontColor:
  355. if (sender is Color == true)
  356. {
  357. AnnotEvent.UpdateAttrib(AnnotAttrib.FontColor, (SelectColor as SolidColorBrush).Color );
  358. AnnotEvent.UpdateAnnot();
  359. }
  360. break;
  361. case FontSetModeType.TextAlignment:
  362. break;
  363. }
  364. }
  365. }
  366. }
  367. }