ButtonPropertyViewModel.cs 14 KB

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