ButtonPropertyViewModel.cs 13 KB

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