FreetextAnnotPropertyViewModel.cs 16 KB

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