123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- 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<BackgroundItem> backgroundModFileCollection = new ObservableCollection<BackgroundItem>();
- public ObservableCollection<BackgroundItem> 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<object> DeleteTemplateItemCommand { get; set; }
- public DelegateCommand<object> EditTemplateItemCommand { get; set; }
- public DelegateCommand DeleteAllTemplateItemCommand { get; set; }
- public DelegateCommand<object> 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<object>(RequestEditTemplateItem);
- DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
- DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
- SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);
- eventAggregator.GetEvent<SaveEditedBackgroundTemplateItemEvent>().Subscribe(SaveEditedBackgroundTemplateItem, e => e.Unicode == Unicode);
- }
- private void CheckTemplateListIsEmpty(List<BackgroundItem> backgroundTemplateList)
- {
- if (backgroundTemplateList.Count() == 0)
- {
- CreateTemplateVisible = Visibility.Visible;
- }
- else
- {
- CreateTemplateVisible = Visibility.Collapsed;
- }
- }
- private void GetBackgroundSource()
- {
- List<BackgroundItem> backgroundModFileTemplateList = new List<BackgroundItem>();
- 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<BackgroundItem>(backgroundModFileTemplateList);
- CheckTemplateListIsEmpty(backgroundModFileTemplateList);
- }
- private void InitBackgroundTemplateList()
- {
- if (Settings.Default.BackgroundTemplateList == null)
- {
- Settings.Default.BackgroundTemplateList = new BackgroundTemplateList();
- }
- GetBackgroundSource();
- }
- public void AddTemplate()
- {
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().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<EditBackgroundTemplateItemEvent>().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<BackgroundItem> removebackgroundItems = new List<BackgroundItem>();
- 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<SetBackgroundEvent>().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();
- }
- }
- }
|