FreetextAnnotPropertyViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Media;
  18. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  19. {
  20. public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware
  21. {
  22. private double fillOpacity = 1;
  23. public double FillOpacity
  24. {
  25. get { return fillOpacity; }
  26. set
  27. {
  28. SetProperty(ref fillOpacity, value);
  29. }
  30. }
  31. private Brush selectColor = new SolidColorBrush(Colors.Black);
  32. public Brush SelectColor
  33. {
  34. get { return selectColor; }
  35. set
  36. {
  37. SetProperty(ref selectColor, value);
  38. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, (selectColor as SolidColorBrush).Color);
  39. AnnotEvent?.UpdateAnnot();
  40. }
  41. }
  42. private Brush fillColor = new SolidColorBrush(Colors.Transparent);
  43. public Brush FillColor
  44. {
  45. get { return fillColor; }
  46. set
  47. {
  48. SetProperty(ref fillColor, value);
  49. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, (fillColor as SolidColorBrush).Color);
  50. AnnotEvent?.UpdateAnnot();
  51. }
  52. }
  53. private FontFamily fontFamily = new FontFamily("Courier");
  54. public FontFamily TextFontFamily
  55. {
  56. get { return fontFamily; }
  57. set
  58. {
  59. SetProperty(ref fontFamily, value);
  60. }
  61. }
  62. private FontWeight fontWeights = FontWeights.Normal;
  63. public FontWeight TextFontWeights
  64. {
  65. get { return fontWeights; }
  66. set
  67. {
  68. SetProperty(ref fontWeights, value);
  69. }
  70. }
  71. private FontStyle fontStyle = FontStyles.Normal;
  72. public FontStyle TextFontStyle
  73. {
  74. get { return fontStyle; }
  75. set
  76. {
  77. SetProperty(ref fontStyle, value);
  78. }
  79. }
  80. private int fontSize = 24;
  81. public int TextFontSize
  82. {
  83. get { return fontSize; }
  84. set
  85. {
  86. SetProperty(ref fontSize, value);
  87. }
  88. }
  89. private List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
  90. public List<FontStyleItem> FontStyleList
  91. {
  92. get { return fontStyleList; }
  93. set
  94. {
  95. SetProperty(ref fontStyleList, value);
  96. }
  97. }
  98. private ComboDataItem _presetTextData;
  99. public ComboDataItem PresetTextData
  100. {
  101. get { return _presetTextData; }
  102. set
  103. {
  104. SetProperty(ref _presetTextData, value);
  105. if (_presetTextData != null && FontStyleList != null)
  106. {
  107. }
  108. }
  109. }
  110. private ComboDataItem _fontSizeData = new ComboDataItem(6);
  111. public ComboDataItem FontSizeData
  112. {
  113. get { return _fontSizeData; }
  114. set
  115. {
  116. SetProperty(ref _fontSizeData, value);
  117. }
  118. }
  119. private ComboDataItem _fontFamilyData;
  120. public ComboDataItem FontFamilyData
  121. {
  122. get { return _fontFamilyData; }
  123. set
  124. {
  125. SetProperty(ref _fontFamilyData, value);
  126. }
  127. }
  128. private FontStyle _fontStyle;
  129. public FontStyle FontStyleItem
  130. {
  131. get { return _fontStyle; }
  132. set { SetProperty(ref _fontStyle, value); }
  133. }
  134. private FontWeight _fontWeight;
  135. public FontWeight FontWeightItem
  136. {
  137. get { return _fontWeight; }
  138. set { SetProperty(ref _fontWeight, value); }
  139. }
  140. private ComboDataItem _fontWeightStyleItem;
  141. public ComboDataItem FontWeightStyleItem
  142. {
  143. get { return _fontWeightStyleItem; }
  144. set
  145. {
  146. SetProperty(ref _fontWeightStyleItem, value);
  147. if (_fontWeightStyleItem.ValueStr != null && string.IsNullOrEmpty((string)_fontWeightStyleItem.ValueStr) == false)
  148. {
  149. switch ((string)_fontWeightStyleItem.ValueStr)
  150. {
  151. case "Regular":
  152. FontStyleItem = FontStyles.Normal;
  153. FontWeightItem = FontWeights.Normal;
  154. break;
  155. case "Bold":
  156. FontStyleItem = FontStyles.Normal;
  157. FontWeightItem = FontWeights.Bold;
  158. break;
  159. case "Italic":
  160. FontStyleItem = FontStyles.Italic;
  161. FontWeightItem = FontWeights.Normal;
  162. break;
  163. case "Bold Italic":
  164. FontStyleItem = FontStyles.Italic;
  165. FontWeightItem = FontWeights.Bold;
  166. break;
  167. }
  168. }
  169. }
  170. }
  171. public List<ComboDataItem> PresetTextItems { get; private set; }
  172. public List<ComboDataItem> FontFamilyItems { get; private set; }
  173. public List<ComboDataItem> FontStyleItems { get; private set; }
  174. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  175. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  176. public DelegateCommand<object> SelectedColorCommand { get; set; }
  177. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  178. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  179. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  180. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  181. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  182. public DelegateCommand<object> LineModeCheckedCommand { get; set; }
  183. public event EventHandler<object> LoadPropertyHandler;
  184. public FreetextAnnotPropertyViewModel()
  185. {
  186. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
  187. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  188. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  189. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  190. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged_Command);
  191. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged_Command);
  192. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged_Command);
  193. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  194. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  195. InitVariable();
  196. }
  197. private void InitVariable()
  198. {
  199. InitFontStyles();
  200. InitFontFamilyComboBox();
  201. InitFontStyleComboBox();
  202. }
  203. private void InitFontFamilyComboBox()
  204. {
  205. FontFamilyItems = TextFont.GetFamily();
  206. }
  207. private void InitFontStyleComboBox()
  208. {
  209. FontStyleItems = TextFont.GetFontStyle();
  210. }
  211. private void InitFontStyles()
  212. {
  213. PresetTextItems = new List<ComboDataItem>();
  214. FontStyleList = TextFont.GetPresetFontStyle();
  215. foreach (var item in FontStyleList)
  216. {
  217. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  218. PresetTextItems.Add(itemData);
  219. }
  220. }
  221. private void TextAlignChecked(object obj)
  222. {
  223. if ((string)obj != null /*&& TextEditEvent != null*/)
  224. {
  225. switch ((string)obj)
  226. {
  227. case "AlignLeft":
  228. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  229. break;
  230. case "AlignCenter":
  231. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  232. break;
  233. case "AlignRight":
  234. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  235. break;
  236. case "Align":
  237. break;
  238. }
  239. AnnotEvent?.UpdateAnnot();
  240. }
  241. }
  242. private void LineMode_Checked(object obj)
  243. {
  244. if(obj != null)
  245. {
  246. var tag = ((string)obj);
  247. switch (tag)
  248. {
  249. case "Dashed":
  250. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  251. break;
  252. case "Solid":
  253. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  254. break;
  255. }
  256. AnnotEvent?.UpdateAnnot();
  257. }
  258. }
  259. private void SelectedFontStyle(object obj)
  260. {
  261. if (obj != null && (FontStyleItem)obj != null)
  262. {
  263. var item = (FontStyleItem)obj;
  264. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, item.mFontSize);
  265. AnnotEvent?.UpdateAnnot();
  266. }
  267. }
  268. private void SelectedFillOpacity(object obj)
  269. {
  270. if (obj != null)
  271. {
  272. FillOpacity = (double)obj;
  273. SelectColor.Opacity = FillOpacity;
  274. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  275. AnnotEvent?.UpdateAnnot();
  276. }
  277. }
  278. private void FontSizeChanged_Command(object obj)
  279. {
  280. if (obj != null)
  281. {
  282. var item = (ComboBoxItem)obj;
  283. var content = (string)item.Content;
  284. if (content != null)
  285. {
  286. var intData = int.Parse(content);
  287. TextFontSize = intData;
  288. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, intData);
  289. AnnotEvent?.UpdateAnnot();
  290. }
  291. }
  292. }
  293. private void FontStyleChanged_Command(object obj)
  294. {
  295. if (obj != null)
  296. {
  297. var item = (ComboBoxItem)obj;
  298. var content = (string)item.Content;
  299. if (content != null)
  300. {
  301. if (content == "Regular")
  302. {
  303. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  304. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  305. TextFontWeights = FontWeights.Normal;
  306. TextFontStyle = FontStyles.Normal;
  307. }
  308. if (content == "Bold")
  309. {
  310. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  311. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  312. TextFontWeights = FontWeights.Bold;
  313. TextFontStyle = FontStyles.Normal;
  314. }
  315. if (content == "Italic")
  316. {
  317. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  318. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  319. TextFontWeights = FontWeights.Normal;
  320. TextFontStyle = FontStyles.Italic;
  321. }
  322. if (content == "Bold Italic")
  323. {
  324. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  325. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  326. TextFontWeights = FontWeights.Bold;
  327. TextFontStyle = FontStyles.Italic;
  328. }
  329. AnnotEvent?.UpdateAnnot();
  330. }
  331. }
  332. }
  333. private void FontFamilyChanged_Command(object obj)
  334. {
  335. if (obj != null)
  336. {
  337. if ((int)obj > -1)
  338. {
  339. if ((int)obj == 0)
  340. {
  341. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Courier"));
  342. TextFontFamily = new FontFamily("Courier");
  343. }
  344. if ((int)obj == 1)
  345. {
  346. TextFontFamily = new FontFamily("Helvetica");
  347. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Helvetica"));
  348. }
  349. if ((int)obj == 2)
  350. {
  351. TextFontFamily = new FontFamily("Times");
  352. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Times"));
  353. }
  354. AnnotEvent?.UpdateAnnot();
  355. }
  356. }
  357. }
  358. private void SelectedFillColor_Command(object obj)
  359. {
  360. if (obj != null)
  361. {
  362. var colorValue = (Color)obj;
  363. if (colorValue != null)
  364. {
  365. FillColor = new SolidColorBrush(colorValue);
  366. FillColor.Opacity = FillOpacity;
  367. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  368. changeData[AnnotArgsType.AnnotFreehand] = obj;
  369. PropertyPanel.DataChangedInvoke(this, changeData);
  370. }
  371. }
  372. }
  373. private void SelectedColor_Command(object obj)
  374. {
  375. if (obj != null)
  376. {
  377. var colorValue = (Color)obj;
  378. if (colorValue != null)
  379. {
  380. SelectColor = new SolidColorBrush(colorValue);
  381. SelectColor.Opacity = FillOpacity;
  382. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  383. changeData[AnnotArgsType.AnnotFreehand] = obj;
  384. PropertyPanel.DataChangedInvoke(this, changeData);
  385. }
  386. }
  387. }
  388. public bool IsNavigationTarget(NavigationContext navigationContext)
  389. {
  390. return true;
  391. }
  392. public void OnNavigatedFrom(NavigationContext navigationContext)
  393. {
  394. }
  395. public AnnotAttribEvent AnnotEvent { get; set; }
  396. private FreeTextAnnotArgs Annot;
  397. private AnnotPropertyPanel PropertyPanel;
  398. public void OnNavigatedTo(NavigationContext navigationContext)
  399. {
  400. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  401. if (PropertyPanel != null)
  402. {
  403. AnnotEvent = PropertyPanel.AnnotEvent;
  404. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  405. LoadPropertyHandler?.Invoke(this, Annot);
  406. }
  407. }
  408. }
  409. }