using PDF_Office.EventAggregators; using PDF_Office.Model.EditTools.Background; using PDF_Office.Model.EditTools.Watermark; using PDF_Office.Properties; using PDFSettings; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace PDF_Office.ViewModels.EditTools.Background { public class BackgroundTemplateListFileContentViewModel : BindableBase, INavigationAware { private readonly IEventAggregator eventAggregator; public BackgroundInfo BackgroundInfo = new BackgroundInfo(); public BackgroundItem BackgroundItem = new BackgroundItem(); public int TemplateIndex = 0; public ObservableCollection backgroundModFileCollection = new ObservableCollection(); public ObservableCollection BackgroundModFileCollection { get { return backgroundModFileCollection; } set { backgroundModFileCollection = value; RaisePropertyChanged(); } } private Visibility _createTemplateVisible; public Visibility CreateTemplateVisible { get { return _createTemplateVisible; } set { SetProperty(ref _createTemplateVisible, value); } } public DelegateCommand AddTemplateCommand { get; set; } public DelegateCommand DeleteTemplateItemCommand { get; set; } public DelegateCommand EditTemplateItemCommand { get; set; } public DelegateCommand DeleteAllTemplateItemCommand { get; set; } public DelegateCommand SelectTemplateItemCommand { get; set; } public string unicode = null; public string Unicode = null; public BackgroundTemplateListFileContentViewModel(IEventAggregator eventAggregator) { Unicode = App.mainWindowViewModel.SelectedItem.Unicode; this.eventAggregator = eventAggregator; AddTemplateCommand = new DelegateCommand(AddTemplate); EditTemplateItemCommand = new DelegateCommand(RequestEditTemplateItem); DeleteTemplateItemCommand = new DelegateCommand(DeleteTemplateItem); DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem); SelectTemplateItemCommand = new DelegateCommand(SelectTemplateItem); eventAggregator.GetEvent().Subscribe(SaveEditedBackgroundTemplateItem, e => e.Unicode == Unicode); } private void CheckTemplateListIsEmpty(List backgroundTemplateList) { if (backgroundTemplateList.Count() == 0) { CreateTemplateVisible = Visibility.Visible; } else { CreateTemplateVisible = Visibility.Collapsed; } } private void GetBackgroundSource() { List backgroundModFileTemplateList = new List(); for (int temp = 0; temp < Settings.Default.BackgroundTemplateList.Count; temp++) { if (Settings.Default.BackgroundTemplateList[temp].type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE) { backgroundModFileTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]); } } BackgroundModFileCollection = new ObservableCollection(backgroundModFileTemplateList); CheckTemplateListIsEmpty(backgroundModFileTemplateList); } private void InitBackgroundTemplateList() { if (Settings.Default.BackgroundTemplateList == null) { Settings.Default.BackgroundTemplateList = new BackgroundTemplateList(); } GetBackgroundSource(); } public void AddTemplate() { this.eventAggregator.GetEvent().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusCreate }); } public void SaveEditedBackgroundTemplateItem(BackgroundItemUnicode backgroundItemunicode) { BackgroundItem backgroundItem = backgroundItemunicode.Status; if (backgroundItem.type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE) { Settings.Default.BackgroundTemplateList[TemplateIndex] = backgroundItem; Settings.Default.Save(); } GetBackgroundSource(); } public void RequestEditTemplateItem(object e) { var control = e as Control; if (control == null) { return; } var template = control.DataContext as BackgroundItem; if (template == null) { return; } TemplateIndex = Settings.Default.BackgroundTemplateList.IndexOf(template); template.listIndex = TemplateIndex; this.eventAggregator.GetEvent().Publish(new BackgroundItemUnicode { Unicode = Unicode, Status = template }); } public void DeleteTemplateItem(object e) { var control = e as Control; if (control == null) { return; } var template = control.DataContext as BackgroundItem; if (template == null) { return; } if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath) && File.Exists(BackgroundItem.previewImagePath)) { Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath); } Settings.Default.BackgroundTemplateList.Remove(template); Settings.Default.Save(); BackgroundModFileCollection.Remove(template); GetBackgroundSource(); } public void DeleteAllTemplateItem() { List removebackgroundItems = new List(); for (int i = 0; i < Settings.Default.BackgroundTemplateList.Count; i++) { var template = Settings.Default.BackgroundTemplateList[i]; if (template.type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE) { removebackgroundItems.Add(template); } } foreach (var removetemplate in removebackgroundItems) { if (!string.IsNullOrEmpty(BackgroundItem.previewImagePath) && File.Exists(BackgroundItem.previewImagePath)) { Settings.Default.AppProperties.NeedToDeletePath.Add(BackgroundItem.previewImagePath); } Settings.Default.BackgroundTemplateList.Remove(removetemplate); Settings.Default.Save(); BackgroundModFileCollection.Remove(removetemplate); } GetBackgroundSource(); } public void ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo) { if (backgroundItem != null) { backgroundInfo.BackgroundType = backgroundItem.type; backgroundInfo.ImageArray = backgroundItem.imageArray; backgroundInfo.ImageWidth = backgroundItem.imageWidth; backgroundInfo.ImageHeight = backgroundItem.imageHeight; backgroundInfo.Horizalign = backgroundItem.horizalign; backgroundInfo.Vertalign = backgroundItem.vertalign; backgroundInfo.VertOffset = backgroundItem.vertOffset; backgroundInfo.Horizalign = backgroundItem.horizalign; backgroundInfo.Opacity = backgroundItem.opacity; backgroundInfo.Rotation = backgroundItem.rotation; backgroundInfo.Scale = backgroundItem.scale; backgroundInfo.PageRange = backgroundItem.pageRange; backgroundInfo.PageRangeIndex = backgroundItem.PageRangeIndex; // backgroundItem.pagRangeMode = backgroundInfo.PageRange; } } public void SendTemplateItemToDocument(BackgroundItem BackgroundItem) { ConvertItemToInfo(BackgroundItem, ref BackgroundInfo); eventAggregator.GetEvent().Publish(new BackgroundInfoUnicode { Unicode = Unicode, Status = BackgroundInfo }); } public void SelectTemplateItem(object e) { var listBox = e as ListBox; BackgroundItem BackgroundItem = listBox.SelectedItem as BackgroundItem; SendTemplateItemToDocument(BackgroundItem); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { InitBackgroundTemplateList(); } } }