BatesTemplateListContentViewModel.cs 8.8 KB

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