BatesTemplateListContentViewModel.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Office.Interop.Word;
  3. using PDF_Master.EventAggregators;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using PDF_Master.Model.EditTools.Bates;
  7. using PDF_Master.Model.EditTools.Bates;
  8. using PDF_Master.Model.EditTools.HeaderFooter;
  9. using PDF_Master.Properties;
  10. using PDFSettings;
  11. using Prism.Commands;
  12. using Prism.Events;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.Linq;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. namespace PDF_Master.ViewModels.EditTools.Bates
  22. {
  23. public class BatesTemplateListContentViewModel : BindableBase, INavigationAware
  24. {
  25. private CPDFViewer PDFViewer;
  26. public IEventAggregator eventAggregator;
  27. public BatesInfo BatesInfo = new BatesInfo();
  28. public BatesHeaderFooterItem BatesItem = new BatesHeaderFooterItem();
  29. public int TemplateIndex = 0;
  30. private ObservableCollection<BatesHeaderFooterItem> batesModCollection = new ObservableCollection<BatesHeaderFooterItem>();
  31. public ObservableCollection<BatesHeaderFooterItem> BatesModCollection
  32. {
  33. get { return batesModCollection; }
  34. set
  35. {
  36. batesModCollection = value;
  37. RaisePropertyChanged();
  38. }
  39. }
  40. private Visibility _createTemplateVisible;
  41. public Visibility CreateTemplateVisible
  42. {
  43. get { return _createTemplateVisible; }
  44. set { SetProperty(ref _createTemplateVisible, value); }
  45. }
  46. public DelegateCommand AddTemplateCommand { get; set; }
  47. public DelegateCommand<object> DeleteTemplateItemCommand { get; set; }
  48. public DelegateCommand<object> EditTemplateItemCommand { get; set; }
  49. public DelegateCommand DeleteAllTemplateItemCommand { get; set; }
  50. public DelegateCommand<object> SelectTemplateItemCommand { get; set; }
  51. public DelegateCommand EnterCreateCommand { get; set; }
  52. public string Unicode = null;
  53. public BatesTemplateListContentViewModel(IEventAggregator eventAggregator)
  54. {
  55. this.eventAggregator = eventAggregator;
  56. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  57. EnterCreateCommand = new DelegateCommand(EnterCreate);
  58. AddTemplateCommand = new DelegateCommand(AddTemplate);
  59. EditTemplateItemCommand = new DelegateCommand<object>(RequestEditTemplateItem);
  60. DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
  61. DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
  62. SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);
  63. }
  64. private void InitBackgroundTemplateList()
  65. {
  66. if (Settings.Default.BatesTemplateList == null)
  67. {
  68. Settings.Default.BatesTemplateList = new BatesTemplateList();
  69. }
  70. GetBatesSource();
  71. }
  72. public void EnterCreate()
  73. {
  74. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusCreate });
  75. }
  76. public void AddTemplate()
  77. {
  78. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  79. {
  80. Unicode = Unicode,
  81. Status = EnumTemplateListOrCreate.StatusCreate
  82. });
  83. }
  84. public void SaveEditedBatesTemplateItem(BatesHeaderFooterItemUnicode BatesItemunicode)
  85. {
  86. BatesHeaderFooterItem batesItem = BatesItemunicode.Status;
  87. Settings.Default.BatesTemplateList[TemplateIndex] = batesItem;
  88. Settings.Default.Save();
  89. GetBatesSource();
  90. }
  91. public void RequestEditTemplateItem(object e)
  92. {
  93. var control = e as Control;
  94. if (control == null)
  95. {
  96. return;
  97. }
  98. var template = control.DataContext as BatesHeaderFooterItem;
  99. if (template == null)
  100. {
  101. return;
  102. }
  103. TemplateIndex = Settings.Default.BatesTemplateList.IndexOf(template);
  104. template.listIndex = TemplateIndex;
  105. ConvertItemToInfo(template, ref BatesInfo);
  106. this.eventAggregator.GetEvent<EditBatesHeaderFooterTemplateItemEvent>().Publish(new BatesHeaderFooterItemUnicode
  107. {
  108. Unicode = Unicode,
  109. Type = "BatesType",
  110. Status = template
  111. });
  112. }
  113. public void DeleteTemplateItem(object e)
  114. {
  115. var btn = e as System.Windows.Controls.Control;
  116. if (btn == null)
  117. {
  118. return;
  119. }
  120. BatesItem = btn.DataContext as BatesHeaderFooterItem;
  121. if (BatesItem == null)
  122. {
  123. return;
  124. }
  125. Settings.Default.BatesTemplateList.Remove(BatesItem);
  126. Settings.Default.Save();
  127. BatesModCollection.Remove(BatesItem);
  128. GetBatesSource();
  129. }
  130. public void DeleteAllTemplateItem()
  131. {
  132. List<BatesHeaderFooterItem> removebatesItems = new List<BatesHeaderFooterItem>();
  133. for (int i = 0; i < Settings.Default.BatesTemplateList.Count;i++)
  134. {
  135. var template = Settings.Default.BatesTemplateList[i];
  136. removebatesItems.Add(template);
  137. }
  138. foreach (var removetemplate in removebatesItems)
  139. {
  140. Settings.Default.BatesTemplateList.Remove(removetemplate);
  141. Settings.Default.Save();
  142. BatesModCollection.Remove(removetemplate);
  143. }
  144. GetBatesSource();
  145. }
  146. public void SelectTemplateItem(object e)
  147. {
  148. var listBox = e as ListBox;
  149. BatesHeaderFooterItem BatesItem = listBox.SelectedItem as BatesHeaderFooterItem;
  150. SendTemplateItemToDocument(BatesItem);
  151. }
  152. public void SendTemplateItemToDocument(BatesHeaderFooterItem template)
  153. {
  154. if (template != null)
  155. {
  156. ConvertItemToInfo(template, ref BatesInfo);
  157. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  158. {
  159. Unicode = Unicode,
  160. Status = BatesInfo
  161. });
  162. }
  163. }
  164. private void GetBatesSource()
  165. {
  166. List<BatesHeaderFooterItem> batesModColorTemplateList = new List<BatesHeaderFooterItem>();
  167. for (int temp = 0; temp < Settings.Default.BatesTemplateList.Count; temp++)
  168. {
  169. Settings.Default.BatesTemplateList[temp].ItemName = (temp + 1).ToString();
  170. batesModColorTemplateList.Add(Settings.Default.BatesTemplateList[temp]);
  171. }
  172. BatesModCollection = new ObservableCollection<BatesHeaderFooterItem>(batesModColorTemplateList);
  173. CheckTemplateListIsEmpty(batesModColorTemplateList);
  174. }
  175. public void ConvertItemToInfo(BatesHeaderFooterItem batesItem, ref BatesInfo batesInfo)
  176. {
  177. batesInfo.TextData = batesItem.TextData;
  178. batesInfo.ItemName = batesItem.ItemName;
  179. batesInfo.DigitNumber = batesItem.DigitNumber;
  180. batesInfo.Prefix=batesItem.Prefix;
  181. batesInfo.Suffix=batesItem.Suffix;
  182. batesInfo.StarPagetNumber = batesItem.StarPagetNumber;
  183. batesInfo.margin = batesItem.margin;
  184. batesInfo.PageRangeIndex = batesItem.PageRangeIndex;
  185. batesInfo.PageRange= batesItem.PageRange;
  186. }
  187. private void CheckTemplateListIsEmpty(List<BatesHeaderFooterItem> batesTemplateList)
  188. {
  189. if (batesTemplateList.Count() == 0)
  190. {
  191. CreateTemplateVisible = Visibility.Visible;
  192. }
  193. else
  194. {
  195. CreateTemplateVisible = Visibility.Collapsed;
  196. }
  197. }
  198. public bool IsNavigationTarget(NavigationContext navigationContext)
  199. {
  200. return true;
  201. }
  202. public void OnNavigatedFrom(NavigationContext navigationContext)
  203. {
  204. }
  205. public void OnNavigatedTo(NavigationContext navigationContext)
  206. {
  207. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  208. InitBackgroundTemplateList();
  209. }
  210. }
  211. }