ButtonPropertyViewModel.cs 14 KB

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