FreetextAnnotPropertyViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 DelegateCommand<object> SelectedOpacityValueCommand { get; set; }
  184. public event EventHandler<object> LoadPropertyHandler;
  185. public FreetextAnnotPropertyViewModel()
  186. {
  187. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
  188. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  189. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  190. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  191. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged_Command);
  192. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged_Command);
  193. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged_Command);
  194. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  195. LineModeCheckedCommand = new DelegateCommand<object>(LineMode_Checked);
  196. SelectedOpacityValueCommand = new DelegateCommand<object>(SelectedOpacityValue);
  197. InitVariable();
  198. }
  199. private void InitVariable()
  200. {
  201. InitFontStyles();
  202. InitFontFamilyComboBox();
  203. InitFontStyleComboBox();
  204. }
  205. private void InitFontFamilyComboBox()
  206. {
  207. FontFamilyItems = TextFont.GetFamily();
  208. }
  209. private void InitFontStyleComboBox()
  210. {
  211. FontStyleItems = TextFont.GetFontStyle();
  212. }
  213. private void InitFontStyles()
  214. {
  215. PresetTextItems = new List<ComboDataItem>();
  216. FontStyleList = TextFont.GetPresetFontStyle();
  217. foreach (var item in FontStyleList)
  218. {
  219. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  220. PresetTextItems.Add(itemData);
  221. }
  222. }
  223. private void TextAlignChecked(object obj)
  224. {
  225. if ((string)obj != null /*&& TextEditEvent != null*/)
  226. {
  227. switch ((string)obj)
  228. {
  229. case "AlignLeft":
  230. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  231. break;
  232. case "AlignCenter":
  233. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  234. break;
  235. case "AlignRight":
  236. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  237. break;
  238. case "Align":
  239. break;
  240. }
  241. AnnotEvent?.UpdateAnnot();
  242. }
  243. }
  244. private void LineMode_Checked(object obj)
  245. {
  246. if(obj != null)
  247. {
  248. var tag = ((string)obj);
  249. switch (tag)
  250. {
  251. case "Dashed":
  252. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash);
  253. break;
  254. case "Solid":
  255. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid);
  256. break;
  257. }
  258. AnnotEvent?.UpdateAnnot();
  259. }
  260. }
  261. private void SelectedFontStyle(object obj)
  262. {
  263. if (obj != null && (FontStyleItem)obj != null)
  264. {
  265. var item = (FontStyleItem)obj;
  266. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, item.mFontSize);
  267. AnnotEvent?.UpdateAnnot();
  268. }
  269. }
  270. private void SelectedFillOpacity(object obj)
  271. {
  272. if (obj != null)
  273. {
  274. FillOpacity = (double)obj;
  275. SelectColor.Opacity = FillOpacity;
  276. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  277. AnnotEvent?.UpdateAnnot();
  278. }
  279. }
  280. private void SelectedOpacityValue(object obj)
  281. {
  282. if (obj != null)
  283. {
  284. FillOpacity = (double)obj;
  285. SelectColor.Opacity = FillOpacity;
  286. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  287. AnnotEvent?.UpdateAnnot();
  288. }
  289. }
  290. private void FontSizeChanged_Command(object obj)
  291. {
  292. if (obj != null)
  293. {
  294. var item = (ComboBoxItem)obj;
  295. var content = (string)item.Content;
  296. if (content != null)
  297. {
  298. var intData = int.Parse(content);
  299. TextFontSize = intData;
  300. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, intData);
  301. AnnotEvent?.UpdateAnnot();
  302. }
  303. }
  304. }
  305. private void FontStyleChanged_Command(object obj)
  306. {
  307. if (obj != null)
  308. {
  309. var item = (ComboBoxItem)obj;
  310. var content = (string)item.Content;
  311. if (content != null)
  312. {
  313. if (content == "Regular")
  314. {
  315. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  316. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  317. TextFontWeights = FontWeights.Normal;
  318. TextFontStyle = FontStyles.Normal;
  319. }
  320. if (content == "Bold")
  321. {
  322. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  323. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  324. TextFontWeights = FontWeights.Bold;
  325. TextFontStyle = FontStyles.Normal;
  326. }
  327. if (content == "Italic")
  328. {
  329. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  330. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  331. TextFontWeights = FontWeights.Normal;
  332. TextFontStyle = FontStyles.Italic;
  333. }
  334. if (content == "Bold Italic")
  335. {
  336. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  337. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  338. TextFontWeights = FontWeights.Bold;
  339. TextFontStyle = FontStyles.Italic;
  340. }
  341. AnnotEvent?.UpdateAnnot();
  342. }
  343. }
  344. }
  345. private void FontFamilyChanged_Command(object obj)
  346. {
  347. if (obj != null)
  348. {
  349. if ((int)obj > -1)
  350. {
  351. if ((int)obj == 0)
  352. {
  353. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Courier"));
  354. TextFontFamily = new FontFamily("Courier");
  355. }
  356. if ((int)obj == 1)
  357. {
  358. TextFontFamily = new FontFamily("Helvetica");
  359. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Helvetica"));
  360. }
  361. if ((int)obj == 2)
  362. {
  363. TextFontFamily = new FontFamily("Times");
  364. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Times"));
  365. }
  366. AnnotEvent?.UpdateAnnot();
  367. }
  368. }
  369. }
  370. private void SelectedFillColor_Command(object obj)
  371. {
  372. if (obj != null)
  373. {
  374. var colorValue = (Color)obj;
  375. if (colorValue != null)
  376. {
  377. FillColor = new SolidColorBrush(colorValue);
  378. FillColor.Opacity = FillOpacity;
  379. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  380. changeData[AnnotArgsType.AnnotFreehand] = obj;
  381. PropertyPanel.DataChangedInvoke(this, changeData);
  382. }
  383. }
  384. }
  385. private void SelectedColor_Command(object obj)
  386. {
  387. if (obj != null)
  388. {
  389. var colorValue = (Color)obj;
  390. if (colorValue != null)
  391. {
  392. SelectColor = new SolidColorBrush(colorValue);
  393. SelectColor.Opacity = FillOpacity;
  394. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  395. changeData[AnnotArgsType.AnnotFreehand] = obj;
  396. PropertyPanel.DataChangedInvoke(this, changeData);
  397. }
  398. }
  399. }
  400. public bool IsNavigationTarget(NavigationContext navigationContext)
  401. {
  402. return true;
  403. }
  404. public void OnNavigatedFrom(NavigationContext navigationContext)
  405. {
  406. }
  407. public AnnotAttribEvent AnnotEvent { get; set; }
  408. private FreeTextAnnotArgs Annot;
  409. private AnnotPropertyPanel PropertyPanel;
  410. public void OnNavigatedTo(NavigationContext navigationContext)
  411. {
  412. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  413. if (PropertyPanel != null)
  414. {
  415. AnnotEvent = PropertyPanel.AnnotEvent;
  416. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  417. LoadPropertyHandler?.Invoke(this, Annot);
  418. }
  419. }
  420. }
  421. }