HeaderFooterTemplateListContentViewModel.cs 9.2 KB

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