TextFieldPropertyViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.CustomControl.CompositeControl;
  6. using PDF_Office.Helper;
  7. using PDF_Office.Model;
  8. using PDF_Office.Model.From;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Media;
  21. namespace PDF_Office.ViewModels.Form
  22. {
  23. public class TextFieldPropertyViewModel : FormBaseVM, INavigationAware
  24. {
  25. #region Command变量
  26. public DelegateCommand<string> FieldNameTextChangedCommand { get; set; }
  27. public DelegateCommand<string> ToolTipTextChangedCommand { get; set; }
  28. //外观
  29. public DelegateCommand<object> ResetColorCommand { get; set; }
  30. public DelegateCommand<object> ResetColorCheckedBtnCommand { get; set; }
  31. public DelegateCommand<string> FormContentTextChangedCommand { get; set; }
  32. public DelegateCommand<object> LineStyleCommand { get; set; }
  33. #endregion
  34. #region 变量
  35. private CPDFViewer PDFViewer;
  36. private WidgetTextBoxArgs textBoxArgs;
  37. private IDialogService dialogs;
  38. public event EventHandler<int> SelectResetColorBtnHandler;
  39. public List<ComboDataItem> FontFamilyItems { get; private set; }
  40. public List<ComboDataItem> FontStyleItems { get; private set; }
  41. public List<ComboDataItem> AglinmentItems { get; private set; }
  42. #endregion
  43. #region 初始化
  44. public TextFieldPropertyViewModel(IDialogService dialogService)
  45. {
  46. dialogs = dialogService;
  47. InitVariable();
  48. InitCommand();
  49. }
  50. private void InitVariable()
  51. {
  52. InitAllResetColor();
  53. InitFontFamilyComboBox();
  54. InitFontStyleComboBox();
  55. InitAglinmentItemsComboBox();
  56. }
  57. private void InitAllResetColor()
  58. {
  59. ResetColorOne = InitResetColor(Colors.Transparent, Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  60. ResetColorTwo = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  61. ResetColorThree = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  62. ResetColorForth = InitResetColor(Color.FromArgb(0xFF, 0xff, 0x00, 0x00), Color.FromArgb(0xFF, 0xff, 0x00, 0x00), Colors.Transparent);
  63. }
  64. private void InitFontFamilyComboBox()
  65. {
  66. FontFamilyItems = new List<ComboDataItem>();
  67. ComboDataItem item = new ComboDataItem("Courier", "Courier New");
  68. FontFamilyItems.Add(item);
  69. item = new ComboDataItem("Helvetica", "Helvetica");
  70. FontFamilyItems.Add(item);
  71. item = new ComboDataItem("Times Roman", "Times New Roman");
  72. FontFamilyItems.Add(item);
  73. }
  74. private void InitFontStyleComboBox()
  75. {
  76. FontStyleItems = new List<ComboDataItem>();
  77. ComboDataItem item = new ComboDataItem("Regular", "Regular");
  78. FontStyleItems.Add(item);
  79. item = new ComboDataItem("Bold", "Bold");
  80. FontStyleItems.Add(item);
  81. item = new ComboDataItem("Italic", "Italic");
  82. FontStyleItems.Add(item);
  83. item = new ComboDataItem("Bold Italic", "Bold Italic");
  84. FontStyleItems.Add(item);
  85. }
  86. private void InitAglinmentItemsComboBox()
  87. {
  88. AglinmentItems = new List<ComboDataItem>();
  89. ComboDataItem item = new ComboDataItem("Left", "Left");
  90. AglinmentItems.Add(item);
  91. item = new ComboDataItem("Center", "Center");
  92. AglinmentItems.Add(item);
  93. item = new ComboDataItem("Right", "Right");
  94. AglinmentItems.Add(item);
  95. }
  96. private void InitCommand()
  97. {
  98. //一般
  99. FieldNameTextChangedCommand = new DelegateCommand<string>(FieldNameTextChanged);
  100. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  101. //外观
  102. ResetColorCheckedBtnCommand = new DelegateCommand<object>(ResetColorCheckedBtn);
  103. ResetColorCommand = new DelegateCommand<object>(ResetColorEvent);
  104. LineStyleCommand = new DelegateCommand<object>(LineStyleBtnEvent);
  105. //选项
  106. FormContentTextChangedCommand = new DelegateCommand<string>(FormContentTextChanged);
  107. }
  108. #endregion
  109. #region 事件
  110. private void FieldNameTextChanged(string obj)
  111. {
  112. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  113. {
  114. FieldName = obj;
  115. }
  116. }
  117. private void ToolTipTextChanged(string obj)
  118. {
  119. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  120. {
  121. ToolTipStr = obj;
  122. }
  123. }
  124. private void LineStyleBtnEvent(object obj)
  125. {
  126. if (obj != null)
  127. {
  128. switch ((string)obj)
  129. {
  130. case "Solid":
  131. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_SOLID;
  132. break;
  133. case "Dotted":
  134. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_DASHDED;
  135. break;
  136. }
  137. }
  138. }
  139. private void FormContentTextChanged(string obj)
  140. {
  141. if (obj != null && IsCurrentWidget == true)
  142. {
  143. FormContent = obj;
  144. }
  145. }
  146. private void ResetColorCheckedBtn(object obj)
  147. {
  148. if (obj != null)
  149. {
  150. var str = obj as string;
  151. if (str != null)
  152. {
  153. switch (str)
  154. {
  155. case "One":
  156. BorderColor = ResetColorOne.BorderColor.Color;
  157. ContentColor = ResetColorOne.FontColor.Color;
  158. FillColor = ResetColorOne.FillColor.Color;
  159. break;
  160. case "Two":
  161. BorderColor = ResetColorTwo.BorderColor.Color;
  162. ContentColor = ResetColorTwo.FontColor.Color;
  163. FillColor = ResetColorTwo.FillColor.Color;
  164. break;
  165. case "Three":
  166. BorderColor = ResetColorThree.BorderColor.Color;
  167. ContentColor = ResetColorThree.FontColor.Color;
  168. FillColor = ResetColorThree.FillColor.Color;
  169. break;
  170. case "Forth":
  171. BorderColor = ResetColorForth.BorderColor.Color;
  172. ContentColor = ResetColorForth.FontColor.Color;
  173. FillColor = ResetColorForth.FillColor.Color;
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. private void ResetColorEvent(object obj)
  180. {
  181. bool result = true;
  182. DialogParameters value = new DialogParameters();
  183. value.Add(ParameterNames.PDFViewer, PDFViewer);
  184. dialogs.ShowDialog(DialogNames.EditPresetColorsDialog, value, e =>
  185. {
  186. if (e.Result != ButtonResult.OK)
  187. {
  188. result = false;
  189. }
  190. EditPresetColorsDialogViewModel DialogVM = e.Parameters.GetValue<EditPresetColorsDialogViewModel>(ParameterNames.DataModel);
  191. if (DialogVM != null)
  192. {
  193. }
  194. });
  195. if (!result)
  196. {
  197. return;
  198. }
  199. }
  200. #endregion
  201. #region 外部XAML触发事件
  202. private void UpdataSelectResetColorBtn()
  203. {
  204. int result = 0;
  205. if (UpdataSelectResetColor(ResetColorOne))
  206. {
  207. result = 1;
  208. }
  209. else if (UpdataSelectResetColor(ResetColorTwo))
  210. {
  211. result = 2;
  212. }
  213. else if (UpdataSelectResetColor(ResetColorThree))
  214. {
  215. result = 3;
  216. }
  217. else if (UpdataSelectResetColor(ResetColorForth))
  218. {
  219. result = 4;
  220. }
  221. SelectResetColorBtnHandler?.Invoke(null, result);
  222. }
  223. private bool UpdataSelectResetColor(ResetColor reset)
  224. {
  225. if (reset.FillColor.Color == FillColor &&
  226. reset.FontColor.Color == ContentColor &&
  227. reset.BorderColor.Color == BorderColor
  228. )
  229. {
  230. return true;
  231. }
  232. return false;
  233. }
  234. #endregion
  235. #region Navegation
  236. public bool IsNavigationTarget(NavigationContext navigationContext)
  237. {
  238. return true;
  239. }
  240. public void OnNavigatedFrom(NavigationContext navigationContext)
  241. {
  242. isCreateWidget = false;
  243. IsCurrentWidget = false;
  244. textBoxArgs = null;
  245. }
  246. public void OnNavigatedTo(NavigationContext navigationContext)
  247. {
  248. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  249. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  250. navigationContext.Parameters.TryGetValue<WidgetTextBoxArgs>("WidgetArgs", out textBoxArgs);
  251. GetWidgeText();
  252. UpdataSelectResetColorBtn();
  253. }
  254. private void GetWidgeText()
  255. {
  256. //新建的form表单
  257. if (textBoxArgs == null)
  258. {
  259. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  260. WidgetTextBoxArgs textArgs = new WidgetTextBoxArgs();
  261. textArgs.BgColor = Colors.LightGray;
  262. textBoxArgs = textArgs;
  263. PDFViewer.SetToolParam(textBoxArgs);
  264. isCreateWidget = true;
  265. }
  266. else
  267. {
  268. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  269. isCreateWidget = false;
  270. }
  271. GetProperty();
  272. IsCurrentWidget = true;
  273. }
  274. private void GetProperty()
  275. {
  276. if (textBoxArgs != null)
  277. {
  278. IsLocked = textBoxArgs.Locked;
  279. FieldName = textBoxArgs.FieldName;
  280. ToolTipStr = textBoxArgs.Tooltip;
  281. IsReadOnly = textBoxArgs.ReadOnly;
  282. IsRequiredField = textBoxArgs.IsRequired;
  283. FillColor = textBoxArgs.BgColor;
  284. ContentColor = textBoxArgs.FontColor;
  285. BorderColor = textBoxArgs.LineColor;
  286. BorderThiness = textBoxArgs.LineWidth;
  287. BorderStyle = textBoxArgs.BorderStyle;
  288. string fontWeightStyleStr = "";
  289. if (textBoxArgs.FontStyle == FontStyles.Normal)
  290. {
  291. if (textBoxArgs.FontWeight == FontWeights.Normal)
  292. fontWeightStyleStr = "Regular";
  293. else
  294. fontWeightStyleStr = "Bold";
  295. }
  296. else
  297. {
  298. if (textBoxArgs.FontWeight == FontWeights.Normal)
  299. fontWeightStyleStr = "Italic";
  300. else
  301. fontWeightStyleStr = "Bold Italic";
  302. }
  303. FontWeightStyleItem = new ComboDataItem(fontWeightStyleStr);
  304. FontFamilyData = new ComboDataItem(textBoxArgs.FontFamily);
  305. //避免BorderStyle跟上一个值相同,而没触发更改IsSolid属性
  306. if (BorderStyle == C_BORDER_STYLE.BS_SOLID)
  307. IsSolid = true;
  308. else
  309. IsSolid = false;
  310. FontSizeData = new ComboDataItem(textBoxArgs.FontSize);
  311. if (isCreateWidget == false)
  312. {
  313. HeightSize = textBoxArgs.Height;
  314. WidthSize = textBoxArgs.Width;
  315. }
  316. string alignmentStr = "";
  317. if (textBoxArgs.Alignment == C_TEXT_ALIGNMENT.ALIGNMENT_LEFT)
  318. alignmentStr = "left";
  319. else if (textBoxArgs.Alignment == C_TEXT_ALIGNMENT.ALIGNMENT_CENTER)
  320. alignmentStr = "Center";
  321. else
  322. alignmentStr = "Right";
  323. TextAlignmentData = new ComboDataItem(alignmentStr, alignmentStr);
  324. FormContent = textBoxArgs.Text;
  325. IsMultiLine = textBoxArgs.IsMultiLine;
  326. IsScrollText = textBoxArgs.ScrollFlag;
  327. }
  328. }
  329. #endregion
  330. }
  331. }