BackgroundTemplateListFileContentViewModel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using PDF_Office.EventAggregators;
  2. using PDF_Office.Model.EditTools.Background;
  3. using PDF_Office.Model.EditTools.Watermark;
  4. using PDF_Office.Properties;
  5. using PDFSettings;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. namespace PDF_Office.ViewModels.EditTools.Background
  20. {
  21. public class BackgroundTemplateListFileContentViewModel : BindableBase, INavigationAware
  22. {
  23. private readonly IEventAggregator eventAggregator;
  24. public BackgroundInfo BackgroundInfo = new BackgroundInfo();
  25. public BackgroundItem BackgroundItem = new BackgroundItem();
  26. public int TemplateIndex = 0;
  27. public ObservableCollection<BackgroundItem> backgroundModFileCollection = new ObservableCollection<BackgroundItem>();
  28. public ObservableCollection<BackgroundItem> BackgroundModFileCollection
  29. {
  30. get { return backgroundModFileCollection; }
  31. set
  32. {
  33. backgroundModFileCollection = value;
  34. RaisePropertyChanged();
  35. }
  36. }
  37. private Visibility _createTemplateVisible;
  38. public Visibility CreateTemplateVisible
  39. {
  40. get { return _createTemplateVisible; }
  41. set { SetProperty(ref _createTemplateVisible, value); }
  42. }
  43. public DelegateCommand AddTemplateCommand { get; set; }
  44. public DelegateCommand<object> DeleteTemplateItemCommand { get; set; }
  45. public DelegateCommand<object> EditTemplateItemCommand { get; set; }
  46. public DelegateCommand DeleteAllTemplateItemCommand { get; set; }
  47. public DelegateCommand<object> SelectTemplateItemCommand { get; set; }
  48. public string unicode = null;
  49. public string Unicode = null;
  50. public BackgroundTemplateListFileContentViewModel(IEventAggregator eventAggregator)
  51. {
  52. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  53. this.eventAggregator = eventAggregator;
  54. AddTemplateCommand = new DelegateCommand(AddTemplate);
  55. EditTemplateItemCommand = new DelegateCommand<object>(RequestEditTemplateItem);
  56. DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
  57. DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
  58. SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);
  59. eventAggregator.GetEvent<SaveEditedBackgroundTemplateItemEvent>().Subscribe(SaveEditedBackgroundTemplateItem, e => e.Unicode == Unicode);
  60. }
  61. private void CheckTemplateListIsEmpty(List<BackgroundItem> backgroundTemplateList)
  62. {
  63. if (backgroundTemplateList.Count() == 0)
  64. {
  65. CreateTemplateVisible = Visibility.Visible;
  66. }
  67. else
  68. {
  69. CreateTemplateVisible = Visibility.Collapsed;
  70. }
  71. }
  72. private void GetBackgroundSource()
  73. {
  74. List<BackgroundItem> backgroundModFileTemplateList = new List<BackgroundItem>();
  75. for (int temp = 0; temp < Settings.Default.BackgroundTemplateList.Count; temp++)
  76. {
  77. if (Settings.Default.BackgroundTemplateList[temp].type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE)
  78. {
  79. backgroundModFileTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]);
  80. }
  81. }
  82. BackgroundModFileCollection = new ObservableCollection<BackgroundItem>(backgroundModFileTemplateList);
  83. CheckTemplateListIsEmpty(backgroundModFileTemplateList);
  84. }
  85. private void InitBackgroundTemplateList()
  86. {
  87. if (Settings.Default.BackgroundTemplateList == null)
  88. {
  89. Settings.Default.BackgroundTemplateList = new BackgroundTemplateList();
  90. }
  91. GetBackgroundSource();
  92. }
  93. public void AddTemplate()
  94. {
  95. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  96. {
  97. Unicode = Unicode,
  98. Status = EnumTemplateListOrCreate.StatusCreate
  99. });
  100. }
  101. public void SaveEditedBackgroundTemplateItem(BackgroundItemUnicode backgroundItemunicode)
  102. {
  103. BackgroundItem backgroundItem = backgroundItemunicode.Status;
  104. if (backgroundItem.type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE)
  105. {
  106. Settings.Default.BackgroundTemplateList[TemplateIndex] = backgroundItem;
  107. Settings.Default.Save();
  108. }
  109. GetBackgroundSource();
  110. }
  111. public void RequestEditTemplateItem(object e)
  112. {
  113. var control = e as Control;
  114. if (control == null)
  115. {
  116. return;
  117. }
  118. var template = control.DataContext as BackgroundItem;
  119. if (template == null)
  120. {
  121. return;
  122. }
  123. TemplateIndex = Settings.Default.BackgroundTemplateList.IndexOf(template);
  124. template.listIndex = TemplateIndex;
  125. this.eventAggregator.GetEvent<EditBackgroundTemplateItemEvent>().Publish(new BackgroundItemUnicode
  126. {
  127. Unicode = Unicode,
  128. Status = template
  129. });
  130. }
  131. public void DeleteTemplateItem(object e)
  132. {
  133. var control = e as Control;
  134. if (control == null)
  135. {
  136. return;
  137. }
  138. var template = control.DataContext as BackgroundItem;
  139. if (template == null)
  140. {
  141. return;
  142. }
  143. if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath) && File.Exists(BackgroundItem.previewImagePath))
  144. {
  145. Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath);
  146. }
  147. Settings.Default.BackgroundTemplateList.Remove(template);
  148. Settings.Default.Save();
  149. BackgroundModFileCollection.Remove(template);
  150. GetBackgroundSource();
  151. }
  152. public void DeleteAllTemplateItem()
  153. {
  154. List<BackgroundItem> removebackgroundItems = new List<BackgroundItem>();
  155. for (int i = 0; i < Settings.Default.BackgroundTemplateList.Count; i++)
  156. {
  157. var template = Settings.Default.BackgroundTemplateList[i];
  158. if (template.type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE)
  159. {
  160. removebackgroundItems.Add(template);
  161. }
  162. }
  163. foreach (var removetemplate in removebackgroundItems)
  164. {
  165. if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath) && File.Exists(BackgroundItem.previewImagePath))
  166. {
  167. Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath);
  168. }
  169. Settings.Default.BackgroundTemplateList.Remove(removetemplate);
  170. Settings.Default.Save();
  171. BackgroundModFileCollection.Remove(removetemplate);
  172. }
  173. GetBackgroundSource();
  174. }
  175. public void ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo)
  176. {
  177. if (backgroundItem != null)
  178. {
  179. backgroundInfo.BackgroundType = backgroundItem.type;
  180. backgroundInfo.ImageArray = backgroundItem.imageArray;
  181. backgroundInfo.ImageWidth = backgroundItem.imageWidth;
  182. backgroundInfo.ImageHeight = backgroundItem.imageHeight;
  183. backgroundInfo.Horizalign = backgroundItem.horizalign;
  184. backgroundInfo.Vertalign = backgroundItem.vertalign;
  185. backgroundInfo.VertOffset = backgroundItem.vertOffset;
  186. backgroundInfo.Horizalign = backgroundItem.horizalign;
  187. backgroundInfo.Opacity = backgroundItem.opacity;
  188. backgroundInfo.Rotation = backgroundItem.rotation;
  189. backgroundInfo.Scale = backgroundItem.scale;
  190. backgroundInfo.PageRange = backgroundItem.pageRange;
  191. backgroundInfo.PageRangeIndex = backgroundItem.PageRangeIndex;
  192. // backgroundItem.pagRangeMode = backgroundInfo.PageRange;
  193. }
  194. }
  195. public void SendTemplateItemToDocument(BackgroundItem BackgroundItem)
  196. {
  197. ConvertItemToInfo(BackgroundItem, ref BackgroundInfo);
  198. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(new BackgroundInfoUnicode
  199. {
  200. Unicode = Unicode,
  201. Status = BackgroundInfo
  202. });
  203. }
  204. public void SelectTemplateItem(object e)
  205. {
  206. var listBox = e as ListBox;
  207. BackgroundItem BackgroundItem = listBox.SelectedItem as BackgroundItem;
  208. SendTemplateItemToDocument(BackgroundItem);
  209. }
  210. public bool IsNavigationTarget(NavigationContext navigationContext)
  211. {
  212. return true;
  213. }
  214. public void OnNavigatedFrom(NavigationContext navigationContext)
  215. {
  216. }
  217. public void OnNavigatedTo(NavigationContext navigationContext)
  218. {
  219. InitBackgroundTemplateList();
  220. }
  221. }
  222. }