ComboxPropertyViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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.Collections.ObjectModel;
  15. using System.Collections.Specialized;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Media;
  21. namespace PDF_Master.ViewModels.Form
  22. {
  23. public class ComboxPropertyViewModel : 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. //选项
  34. public DelegateCommand<object> AddOptionCommand { get; set; }
  35. public DelegateCommand<object> RemoveOptionCommand { get; set; }
  36. public DelegateCommand<object> UpItemOptionCommand { get; set; }
  37. public DelegateCommand<object> DownItemOptionCommand { get; set; }
  38. public DelegateCommand<string> OptionKeyTextChangedCommand { get; set; }
  39. public DelegateCommand<string> OptionKeyValueTextChangedCommand { get; set; }
  40. public DelegateCommand<object> OptionSelectionChangedCommand { get; set; }
  41. #endregion
  42. #region 变量
  43. private CPDFViewer PDFViewer;
  44. private WidgetComboBoxArgs comboBoxArgs;
  45. private IDialogService dialogs;
  46. public event EventHandler<int> SelectResetColorBtnHandler;
  47. public List<ComboDataItem> FontFamilyItems { get; private set; }
  48. public List<ComboDataItem> FontStyleItems { get; private set; }
  49. public List<ComboDataItem> AglinmentItems { get; private set; }
  50. public ObservableCollection<ComboDataItem> OptionItems { get; private set; }
  51. #endregion
  52. #region 初始化
  53. public ComboxPropertyViewModel()
  54. {
  55. InitVariable();
  56. InitCommand();
  57. }
  58. private void InitVariable()
  59. {
  60. InitAllResetColor();
  61. InitFontFamilyComboBox();
  62. InitFontStyleComboBox();
  63. InitAglinmentItemsComboBox();
  64. InitOptionItems();
  65. }
  66. private void InitOptionItems()
  67. {
  68. OptionItems = new ObservableCollection<ComboDataItem>();
  69. OptionItems.CollectionChanged -= OptionItems_CollectionChanged;
  70. OptionItems.CollectionChanged += OptionItems_CollectionChanged;
  71. }
  72. private void OptionItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  73. {
  74. if(OptionItems.Count == 0)
  75. IsRemoveOption = false;
  76. }
  77. private void InitAllResetColor()
  78. {
  79. ResetColorOne = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  80. ResetColorTwo = InitResetColor(Color.FromArgb(0xFF, 0xFE, 0xAD, 0x4A), Color.FromArgb(0xFF, 0xF7, 0x58, 0x50), Color.FromArgb(0xFF, 0xff, 0xff, 0xff));
  81. ResetColorThree = InitResetColor(Color.FromArgb(0xFF, 0xB6, 0x44, 0xF7), Color.FromArgb(0xFF, 0x53, 0x71, 0xF5), Color.FromArgb(0xFF, 0xff, 0xff, 0xff));
  82. ResetColorForth = InitResetColor(Color.FromArgb(0xFF, 0xF7, 0x58, 0x50), Color.FromArgb(0xFF, 0x7F, 0xCB, 0x3A), Colors.Transparent);
  83. }
  84. private void InitFontFamilyComboBox()
  85. {
  86. FontFamilyItems = new List<ComboDataItem>();
  87. ComboDataItem item = new ComboDataItem("Courier", "Courier New");
  88. FontFamilyItems.Add(item);
  89. item = new ComboDataItem("Helvetica", "Helvetica");
  90. FontFamilyItems.Add(item);
  91. item = new ComboDataItem("Times-Roman", "Times New Roman");
  92. FontFamilyItems.Add(item);
  93. }
  94. private void InitFontStyleComboBox()
  95. {
  96. FontStyleItems = new List<ComboDataItem>();
  97. ComboDataItem item = new ComboDataItem("Regular", "Regular");
  98. FontStyleItems.Add(item);
  99. item = new ComboDataItem("Bold", "Bold");
  100. FontStyleItems.Add(item);
  101. item = new ComboDataItem("Italic", "Italic");
  102. FontStyleItems.Add(item);
  103. item = new ComboDataItem("Bold Italic", "Bold Italic");
  104. FontStyleItems.Add(item);
  105. }
  106. private void InitAglinmentItemsComboBox()
  107. {
  108. AglinmentItems = new List<ComboDataItem>();
  109. ComboDataItem item = new ComboDataItem("Left", "Left");
  110. AglinmentItems.Add(item);
  111. item = new ComboDataItem("Center", "Center");
  112. AglinmentItems.Add(item);
  113. item = new ComboDataItem("Right", "Right");
  114. AglinmentItems.Add(item);
  115. }
  116. private void InitCommand()
  117. {
  118. //一般
  119. FieldNameTextChangedCommand = new DelegateCommand<string>(FieldNameTextChanged);
  120. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  121. //外观
  122. ResetColorCheckedBtnCommand = new DelegateCommand<object>(ResetColorCheckedBtn);
  123. ResetColorCommand = new DelegateCommand<object>(ResetColorEvent);
  124. LineStyleCommand = new DelegateCommand<object>(LineStyleBtnEvent);
  125. //选项
  126. FormContentTextChangedCommand = new DelegateCommand<string>(FormContentTextChanged);
  127. AddOptionCommand = new DelegateCommand<object>(AddOption);
  128. RemoveOptionCommand = new DelegateCommand<object>(RemoveOption);
  129. UpItemOptionCommand = new DelegateCommand<object>(UpItemOption);
  130. DownItemOptionCommand = new DelegateCommand<object>(DownItemOption);
  131. OptionKeyTextChangedCommand = new DelegateCommand<string>(OptionKeyTextChanged);
  132. OptionKeyValueTextChangedCommand = new DelegateCommand<string>(OptionKeyValueTextChanged);
  133. OptionSelectionChangedCommand = new DelegateCommand<object>(OptionSelectionChanged);
  134. }
  135. #endregion
  136. #region 事件
  137. private void FieldNameTextChanged(string obj)
  138. {
  139. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  140. {
  141. FieldName = obj;
  142. }
  143. }
  144. private void ToolTipTextChanged(string obj)
  145. {
  146. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  147. {
  148. ToolTipStr = obj;
  149. }
  150. }
  151. private void LineStyleBtnEvent(object obj)
  152. {
  153. if (obj != null)
  154. {
  155. switch ((string)obj)
  156. {
  157. case "Solid":
  158. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_SOLID;
  159. break;
  160. case "Dotted":
  161. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_DASHDED;
  162. break;
  163. }
  164. }
  165. }
  166. private void FormContentTextChanged(string obj)
  167. {
  168. if (obj != null && IsCurrentWidget == true)
  169. {
  170. FormContent = obj;
  171. }
  172. }
  173. private void OptionKeyValueTextChanged(string obj)
  174. {
  175. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  176. {
  177. OptionKeyValue = obj;
  178. }
  179. UpdateIsInputOption();
  180. }
  181. private void OptionKeyTextChanged(string obj)
  182. {
  183. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  184. {
  185. OptionKey = obj;
  186. }
  187. UpdateIsInputOption();
  188. }
  189. private void OptionSelectionChanged(object obj)
  190. {
  191. if(obj != null && obj is int == true)
  192. {
  193. UpdateAllOptionBtn((int)obj);
  194. }
  195. }
  196. //刷新所有Option有关的按钮状态
  197. private void UpdateAllOptionBtn(int SelectedIndex)
  198. {
  199. var count = OptionItems.Count;
  200. if (SelectedIndex <= -1)
  201. {
  202. IsDownOption = false;
  203. IsUpOption = false;
  204. IsRemoveOption = false;
  205. }
  206. else
  207. {
  208. IsRemoveOption = true;
  209. IsUpOption = SelectedIndex == 0 ? false : true;
  210. IsDownOption = (SelectedIndex == count - 1)?false:true;
  211. }
  212. OptionSelectedIndex = SelectedIndex;
  213. }
  214. //Option是否可输入内容
  215. private void UpdateIsInputOption()
  216. {
  217. if (string.IsNullOrEmpty(OptionKeyValue) || string.IsNullOrEmpty(OptionKey))
  218. {
  219. IsInputOption = false;
  220. }
  221. else
  222. {
  223. foreach (var item in OptionItems)
  224. {
  225. if (item.ValueStr == OptionKeyValue || item.Content == OptionKey)
  226. {
  227. IsInputOption = false;
  228. return;
  229. }
  230. }
  231. IsInputOption = true;
  232. }
  233. }
  234. //新增Option项
  235. private void AddOption(object obj)
  236. {
  237. if (string.IsNullOrEmpty(OptionKey) || string.IsNullOrEmpty(OptionKeyValue))
  238. return;
  239. var item = new ComboDataItem(OptionKeyValue,OptionKey);
  240. OptionItems.Add(item);
  241. OptionsList[item.Content] = item.ValueStr;
  242. ChangeValue(AnnotAttrib.ListOptions, OptionsList);
  243. OptionKey = "";
  244. OptionKeyValue = "";
  245. UpdateAllOptionBtn(OptionSelectedIndex);
  246. }
  247. //移除Option项
  248. private void RemoveOption(object obj)
  249. {
  250. if(obj != null)
  251. {
  252. var item = (ComboDataItem)obj;
  253. OptionsList.Remove(item.Content);
  254. OptionItems.Remove(item);
  255. ChangeValue(AnnotAttrib.ListOptions, OptionsList);
  256. UpdateAllOptionBtn(-1);
  257. }
  258. }
  259. //向下移项
  260. private void DownItemOption(object obj)
  261. {
  262. if(obj != null)
  263. {
  264. var item = (ComboDataItem)obj;
  265. var index = OptionItems.IndexOf(item);
  266. if(index < OptionItems.Count - 1)
  267. {
  268. OptionItems.Move(index, index + 1);
  269. UpdateAllOptionBtn(index + 1);
  270. OptionsList.Clear();
  271. foreach(var itemOption in OptionItems)
  272. {
  273. OptionsList[itemOption.Content] = itemOption.ValueStr;
  274. }
  275. ChangeValue(AnnotAttrib.ListOptions, OptionsList);
  276. }
  277. }
  278. }
  279. //向上移项
  280. private void UpItemOption(object obj)
  281. {
  282. if (obj != null)
  283. {
  284. var item = (ComboDataItem)obj;
  285. var index = OptionItems.IndexOf(item);
  286. if (index > 0)
  287. {
  288. OptionItems.Move(index, index - 1);
  289. UpdateAllOptionBtn(index - 1);
  290. OptionsList.Clear();
  291. foreach (var itemOption in OptionItems)
  292. {
  293. OptionsList[itemOption.Content] = itemOption.ValueStr;
  294. }
  295. ChangeValue(AnnotAttrib.ListOptions, OptionsList);
  296. }
  297. }
  298. }
  299. private void ResetColorCheckedBtn(object obj)
  300. {
  301. if (obj != null)
  302. {
  303. var str = obj as string;
  304. if (str != null)
  305. {
  306. switch (str)
  307. {
  308. case "One":
  309. BorderColor = ResetColorOne.BorderColor.Color;
  310. ContentColor = ResetColorOne.FontColor.Color;
  311. FillColor = ResetColorOne.FillColor.Color;
  312. break;
  313. case "Two":
  314. BorderColor = ResetColorTwo.BorderColor.Color;
  315. ContentColor = ResetColorTwo.FontColor.Color;
  316. FillColor = ResetColorTwo.FillColor.Color;
  317. break;
  318. case "Three":
  319. BorderColor = ResetColorThree.BorderColor.Color;
  320. ContentColor = ResetColorThree.FontColor.Color;
  321. FillColor = ResetColorThree.FillColor.Color;
  322. break;
  323. case "Forth":
  324. BorderColor = ResetColorForth.BorderColor.Color;
  325. ContentColor = ResetColorForth.FontColor.Color;
  326. FillColor = ResetColorForth.FillColor.Color;
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. private void ResetColorEvent(object obj)
  333. {
  334. bool result = true;
  335. DialogParameters value = new DialogParameters();
  336. value.Add(ParameterNames.PDFViewer, PDFViewer);
  337. dialogs.ShowDialog(DialogNames.EditPresetColorsDialog, value, e =>
  338. {
  339. if (e.Result != ButtonResult.OK)
  340. {
  341. result = false;
  342. }
  343. EditPresetColorsDialogViewModel DialogVM = e.Parameters.GetValue<EditPresetColorsDialogViewModel>(ParameterNames.DataModel);
  344. if (DialogVM != null)
  345. {
  346. }
  347. });
  348. if (!result)
  349. {
  350. return;
  351. }
  352. }
  353. #endregion
  354. #region 外部XAML触发事件
  355. private void UpdataSelectResetColorBtn()
  356. {
  357. int result = 0;
  358. if (UpdataSelectResetColor(ResetColorOne))
  359. {
  360. result = 1;
  361. }
  362. else if (UpdataSelectResetColor(ResetColorTwo))
  363. {
  364. result = 2;
  365. }
  366. else if (UpdataSelectResetColor(ResetColorThree))
  367. {
  368. result = 3;
  369. }
  370. else if (UpdataSelectResetColor(ResetColorForth))
  371. {
  372. result = 4;
  373. }
  374. SelectResetColorBtnHandler?.Invoke(null, result);
  375. }
  376. private bool UpdataSelectResetColor(ResetColor reset)
  377. {
  378. if (reset.FillColor.Color == FillColor &&
  379. reset.FontColor.Color == ContentColor &&
  380. reset.BorderColor.Color == BorderColor
  381. )
  382. {
  383. return true;
  384. }
  385. return false;
  386. }
  387. #endregion
  388. #region Navegation
  389. public bool IsNavigationTarget(NavigationContext navigationContext)
  390. {
  391. return true;
  392. }
  393. public void OnNavigatedFrom(NavigationContext navigationContext)
  394. {
  395. comboBoxArgs = null;
  396. ClearVMData();
  397. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  398. }
  399. public void OnNavigatedTo(NavigationContext navigationContext)
  400. {
  401. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  402. navigationContext.Parameters.TryGetValue<WidgetComboBoxArgs>("WidgetArgs", out comboBoxArgs);
  403. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  404. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  405. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  406. OptionsList = new Dictionary<string, string>();
  407. GetWidgeText();
  408. UpdataSelectResetColorBtn();
  409. }
  410. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  411. {
  412. if (e != null && e.Count > 0)
  413. {
  414. var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs;
  415. if (widgeArgs != null)
  416. {
  417. AnnotEditEvent editEvent = e[e.Count - 1];
  418. if (editEvent.EditAction == ActionType.Modify)
  419. {
  420. SetSizeNoUpdateValue(widgeArgs.Width, widgeArgs.Height);
  421. }
  422. }
  423. }
  424. }
  425. private void GetWidgeText()
  426. {
  427. if (comboBoxArgs == null)
  428. {
  429. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  430. comboBoxArgs = new WidgetComboBoxArgs();
  431. comboBoxArgs.BgColor = Colors.White;
  432. comboBoxArgs.FontName = "Courier New";
  433. comboBoxArgs.FontSize = 12;
  434. comboBoxArgs.FontColor = Colors.Black;
  435. comboBoxArgs.LineColor = Colors.Black;
  436. comboBoxArgs.LineWidth = 2;
  437. comboBoxArgs.FieldName = "ComBox";
  438. comboBoxArgs.Width = 72;
  439. comboBoxArgs.Height = 20;
  440. PDFViewer.SetToolParam(comboBoxArgs);
  441. isCreateWidget = true;
  442. }
  443. else
  444. {
  445. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  446. isCreateWidget = false;
  447. }
  448. GetProperty();
  449. IsCurrentWidget = true;
  450. }
  451. private void GetProperty()
  452. {
  453. if (comboBoxArgs != null)
  454. {
  455. IsLocked = comboBoxArgs.Locked;
  456. FieldName = comboBoxArgs.FieldName;
  457. ToolTipStr = comboBoxArgs.Tooltip;
  458. IsReadOnly = comboBoxArgs.ReadOnly;
  459. FillColor = comboBoxArgs.BgColor;
  460. ContentColor = comboBoxArgs.FontColor;
  461. BorderColor = comboBoxArgs.LineColor;
  462. BorderThiness = comboBoxArgs.LineWidth;
  463. BorderStyle = comboBoxArgs.BorderStyle;
  464. string fontWeightStyleStr = "";
  465. if (comboBoxArgs.IsItalic == false)
  466. {
  467. if (comboBoxArgs.IsBold==false)
  468. {
  469. fontWeightStyleStr = "Regular";
  470. }
  471. else
  472. {
  473. fontWeightStyleStr = "Bold";
  474. }
  475. }
  476. else
  477. {
  478. if (comboBoxArgs.IsBold ==false)
  479. {
  480. fontWeightStyleStr = "Italic";
  481. }
  482. else
  483. {
  484. fontWeightStyleStr = "Bold Italic";
  485. }
  486. }
  487. FontWeightStyleItem = new ComboDataItem(fontWeightStyleStr);
  488. FontFamilyData = new ComboDataItem(comboBoxArgs.FontName);
  489. //避免BorderStyle跟上一个值相同,而没触发更改IsSolid属性
  490. if (BorderStyle == C_BORDER_STYLE.BS_SOLID)
  491. IsSolid = true;
  492. else
  493. IsSolid = false;
  494. FontSizeData = new ComboDataItem(comboBoxArgs.FontSize);
  495. if (isCreateWidget == false)
  496. {
  497. HeightSize = comboBoxArgs.Height;
  498. WidthSize = comboBoxArgs.Width;
  499. }
  500. OptionsList = comboBoxArgs.ListOptions;
  501. OptionItems.Clear();
  502. foreach (string key in OptionsList.Keys)
  503. {
  504. var item = new ComboDataItem(OptionsList[key], key);
  505. OptionItems.Add(item);
  506. }
  507. if (isCreateWidget == false)
  508. {
  509. HeightSize = comboBoxArgs.Height;
  510. WidthSize = comboBoxArgs.Width;
  511. }
  512. }
  513. }
  514. #endregion
  515. }
  516. }