ListBoxPropertyViewModel.cs 20 KB

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