BatesTemplateListContentViewModel.cs 8.6 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.Bates;
  7. using PDF_Office.Model.EditTools.HeaderFooter;
  8. using PDF_Office.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_Office.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.Button;
  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. while (Settings.Default.BatesTemplateList.Count != 0)
  132. {
  133. int temp = 0;
  134. var template = Settings.Default.BatesTemplateList[temp];
  135. Settings.Default.BatesTemplateList.Remove(template);
  136. Settings.Default.Save();
  137. BatesModCollection.Remove(template);
  138. }
  139. GetBatesSource();
  140. }
  141. public void SelectTemplateItem(object e)
  142. {
  143. var listBox = e as ListBox;
  144. BatesHeaderFooterItem BatesItem = listBox.SelectedItem as BatesHeaderFooterItem;
  145. SendTemplateItemToDocument(BatesItem);
  146. }
  147. public void SendTemplateItemToDocument(BatesHeaderFooterItem template)
  148. {
  149. if (template != null)
  150. {
  151. ConvertItemToInfo(template, ref BatesInfo);
  152. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  153. {
  154. Unicode = Unicode,
  155. Status = BatesInfo
  156. });
  157. }
  158. }
  159. private void GetBatesSource()
  160. {
  161. List<BatesHeaderFooterItem> batesModColorTemplateList = new List<BatesHeaderFooterItem>();
  162. for (int temp = 0; temp < Settings.Default.BatesTemplateList.Count; temp++)
  163. {
  164. Settings.Default.BatesTemplateList[temp].ItemName = (temp + 1).ToString();
  165. batesModColorTemplateList.Add(Settings.Default.BatesTemplateList[temp]);
  166. }
  167. BatesModCollection = new ObservableCollection<BatesHeaderFooterItem>(batesModColorTemplateList);
  168. CheckTemplateListIsEmpty(batesModColorTemplateList);
  169. }
  170. public void ConvertItemToInfo(BatesHeaderFooterItem batesItem, ref BatesInfo batesInfo)
  171. {
  172. batesInfo.TextData = batesItem.TextData;
  173. batesInfo.ItemName = batesItem.ItemName;
  174. batesInfo.DigitNumber = batesItem.DigitNumber;
  175. batesInfo.Prefix=batesItem.Prefix;
  176. batesInfo.Suffix=batesItem.Suffix;
  177. batesInfo.StarPagetNumber = batesItem.StarPagetNumber;
  178. batesInfo.margin = batesItem.margin;
  179. batesInfo.PageRangeIndex = batesItem.PageRangeIndex;
  180. EditToolsHelper.GetPageRange(batesInfo.PageRangeIndex, PDFViewer.Document, ref batesInfo.PageRange, batesItem.PageRange);
  181. }
  182. private void CheckTemplateListIsEmpty(List<BatesHeaderFooterItem> batesTemplateList)
  183. {
  184. if (batesTemplateList.Count() == 0)
  185. {
  186. CreateTemplateVisible = Visibility.Visible;
  187. }
  188. else
  189. {
  190. CreateTemplateVisible = Visibility.Collapsed;
  191. }
  192. }
  193. public bool IsNavigationTarget(NavigationContext navigationContext)
  194. {
  195. return true;
  196. }
  197. public void OnNavigatedFrom(NavigationContext navigationContext)
  198. {
  199. }
  200. public void OnNavigatedTo(NavigationContext navigationContext)
  201. {
  202. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  203. InitBackgroundTemplateList();
  204. }
  205. }
  206. }