FreetextAnnotPropertyViewModel.cs 17 KB

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