using ComPDFKitViewer.PdfViewer; using Microsoft.Win32; using PDF_Master.CustomControl; using PDF_Master.EventAggregators; using PDF_Master.Helper; using PDF_Master.Model; using PDFReader_WPF.Helper; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using WIA; namespace PDF_Master.ViewModels { public class HomeContentViewModel : BindableBase, INavigationAware { #region 文案 private string T_homeBtn; public string T_HomeBtn { get { return T_homeBtn; } set { SetProperty(ref T_homeBtn, value); } } private string T_aITranslationBtn; public string T_AITranslationBtn { get { return T_aITranslationBtn; } set { SetProperty(ref T_aITranslationBtn, value); } } private string T_aIRewritingBtn; public string T_AIRewritingBtn { get { return T_aIRewritingBtn; } set { SetProperty(ref T_aIRewritingBtn, value); } } private string T_aIErrorCorrectionBtn; public string T_AIErrorCorrectionBtn { get { return T_aIErrorCorrectionBtn; } set { SetProperty(ref T_aIErrorCorrectionBtn, value); } } private string T_openFiles; public string T_OpenFiles { get { return T_openFiles; } set { SetProperty(ref T_openFiles, value); } } private string T_createPDF; public string T_CreatePDF { get { return T_createPDF; } set { SetProperty(ref T_createPDF, value); } } private string T_createPDFToNew; public string T_CreatePDFToNew { get { return T_createPDFToNew; } set { SetProperty(ref T_createPDFToNew, value); } } private void InitString() { T_OpenFiles = App.HomePageLoader.GetString("Home_OpenFiles"); T_CreatePDF = App.HomePageLoader.GetString("Home_CreatePDF"); T_CreatePDFToNew = App.HomePageLoader.GetString("Home_CreateToNew"); T_HomeBtn = App.HomePageLoader.GetString("Home_HomeBtn"); T_AITranslationBtn = App.HomePageLoader.GetString("Home_AITranslationBtn"); T_AIRewritingBtn = App.HomePageLoader.GetString("Home_AIRewritingBtn"); T_AIErrorCorrectionBtn = App.HomePageLoader.GetString("Home_AIErrorCorrectionBtn"); } #endregion public string currentViewName = ""; private string fileName = "Home"; public string FileName { get { return fileName; } set { SetProperty(ref fileName, value); } } private string regionName; public string ToolRegionName { get { return regionName; } set { SetProperty(ref regionName, value); } } private Visibility isLoading = Visibility.Collapsed; public Visibility IsLoading { get { return isLoading; } set { SetProperty(ref isLoading, value); } } private MainContentViewModel mainContentViewModel; public DelegateCommand OpenFileCommand { get; set; } public DelegateCommand ShowToolCommand { get; set; } public DelegateCommand CreateBlackPDFCommand { get; set; } public DelegateCommand CreateFromOtherFile { get; set; } public DelegateCommand CreateFromHtmlCommnd { get; set; } public DelegateCommand CreateFromScanner { get; set; } public IRegionManager toolregion; public IEventAggregator eventer; public IDialogService dialog; public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogaware) { toolregion = regionManager; eventer = eventAggregator; dialog = dialogaware; ToolRegionName = RegionNames.ToolRegionName; OpenFileCommand = new DelegateCommand(OpenFile); ShowToolCommand = new DelegateCommand(ShowToolContent); CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF); CreateFromOtherFile = new DelegateCommand(createFromOtherFile); CreateFromHtmlCommnd = new DelegateCommand(createFormHtml); CreateFromScanner = new DelegateCommand(createFromScanner); InitString(); } /// /// 显示右侧不同的工具栏页面 /// /// public void ShowToolContent(string view) { currentViewName = view; ShowToolContentDataTracking(view); NavigationParameters param = new NavigationParameters(); param.Add(ParameterNames.HomeContentViewModel, this); if (view.Contains("ChatGPT")) { param.Add(ParameterNames.Tag, view); toolregion.RequestNavigate(ToolRegionName, "HomeChatGPTAIContent", param); return; } toolregion.RequestNavigate(ToolRegionName, view, param); } /// /// 显示右侧不同的工具栏页面埋点 /// /// public void ShowToolContentDataTracking(string tag) { DataTrackingHelper.EntryPath = ""; switch (tag) { case "Guid": //DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "Home"); break; case "ChatGPTAITranslationContent": DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "AI Translation"); break; case "ChatGPTAIRewritingContent": DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "AI Rewriting"); break; case "ChatGPTAIErrorCorrectionContent": DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "AI Correction"); break; } } /// /// 从外部拖拽打开文件 /// /// public async void AddFileFromDrag(List files) { string txt = Properties.Resources.txtex; string word = Properties.Resources.wordex; string ppt = Properties.Resources.pptex; string excel = Properties.Resources.excelex; string html = Properties.Resources.htmlex; string image = Properties.Resources.imageex; string allfiles = txt + word + excel + ppt + html; if (files.Count() == 1 && App.OpenedFileList.Contains(files[0])) { //单文件打开重复文件时 需要选中已打开的重复文件 App.mainWindowViewModel.SelectItem(files[0]); return; } IsLoading = Visibility.Visible; await Task.Delay(3); //排除文件夹或其他类型文件 if (!System.IO.File.Exists(files[0])) { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok")); IsLoading = Visibility.Collapsed; return; } #region 先在当前页签打开第一个文档 var type = System.IO.Path.GetExtension(files[0]).ToLower(); if (image.Contains(type)) { //从图片创建 mainContentViewModel.CreateFile(files[0]); } else if (type == ".pdf") { mainContentViewModel.OpenFile(files[0]); } else if (allfiles.Contains(type)) { mainContentViewModel.CreateFileFromOffice(files[0]); } else { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok")); IsLoading = Visibility.Collapsed; return; } ToolMethod.SetFileThumbImg(files[0]); #endregion #region 循环用新页签打开新文档 for (int i = 1; i < files.Count(); i++) { if (!App.OpenedFileList.Contains(files[i])) { App.mainWindowViewModel.AddTabItem(files[i]); await Task.Delay(50); } ToolMethod.SetFileThumbImg(files[i]); } #endregion IsLoading = Visibility.Collapsed; } /// /// 从其他格式文件创建PDF /// public async void createFromOtherFile() { string txt = Properties.Resources.txtex; string word = Properties.Resources.wordex; string ppt = Properties.Resources.pptex; string excel = Properties.Resources.excelex; string html = Properties.Resources.htmlex; string image = Properties.Resources.imageex; string allfiles = txt + word + excel + ppt + image + html; OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true; dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" + $"Microsoft Office Word({word})|{word}|" + $"Microsoft Office Excel({excel})|{excel}|" + $"Microsoft Office PowerPoint({ppt})|{ppt}|" + $"Txt({txt})|{txt}|" + $"Picture({image})|{image}|" + $"Html({html})|{html}"); if ((bool)dialog.ShowDialog()) { var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList(); if (fileList.Count <= 0) { IsLoading = Visibility.Collapsed; return; } IsLoading = Visibility.Visible; await Task.Delay(10); //在当前页签打开第一个文件 var type = System.IO.Path.GetExtension(fileList[0]).ToLower(); if (image.Contains(type)) { //图片文件 mainContentViewModel.CreateFile(fileList[0]); } else { mainContentViewModel.CreateFileFromOffice(fileList[0]); } ToolMethod.SetFileThumbImg(fileList[0]); for (int i = 1; i < fileList.Count(); i++) { if (!App.OpenedFileList.Contains(fileList[i])) { App.mainWindowViewModel.AddTabItem(fileList[i]); await Task.Delay(50); } ToolMethod.SetFileThumbImg(fileList[i]); } IsLoading = Visibility.Collapsed; } } /// /// 从网页创建PDF文件 /// private void createFormHtml() { dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, e => { if (e.Result == ButtonResult.OK) { IsLoading = Visibility.Visible; var model = e.Parameters.GetValue(ParameterNames.DataModel); mainContentViewModel.CreateFileFromOffice(model.FilePath, model.PageSize, model.Margin); IsLoading = Visibility.Collapsed; } }); } /// /// 从扫描仪创建 /// public void createFromScanner(string args) { dialog.ShowDialog(DialogNames.CreateFromScannerDialogs, async e => { if (e.Result == ButtonResult.OK) { IsLoading = Visibility.Visible; string path; e.Parameters.TryGetValue("Imagepath", out path); if (args == "View") { App.mainWindowViewModel.AddTab.Execute(); await Task.Delay(30); (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).CreateFile(); } else { mainContentViewModel.CreateFile(path); } IsLoading = Visibility.Collapsed; } }); } /// /// 打开文件 /// public async void OpenFile() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = Properties.Resources.OpenDialogFilter; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { IsLoading = Visibility.Visible; if (openFileDialog.FileNames.Count() == 1) { //单文件打开重复文件时 需要选中已打开的重复文件 if (App.OpenedFileList.Contains(openFileDialog.FileName)) { App.mainWindowViewModel.SelectItem(openFileDialog.FileName); } else { mainContentViewModel.OpenFile(openFileDialog.FileName); } ToolMethod.SetFileThumbImg(openFileDialog.FileName); } else { var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList(); if (fileList.Count <= 0) { IsLoading = Visibility.Collapsed; return; } mainContentViewModel.OpenFile(fileList[0]); for (int i = 1; i < fileList.Count(); i++) { if (!App.OpenedFileList.Contains(fileList[i])) { App.mainWindowViewModel.AddTabItem(fileList[i]); //这一段很重要 如果注释掉将不能多选文件打开 await Task.Delay(30); } ToolMethod.SetFileThumbImg(fileList[i]); } } IsLoading = Visibility.Collapsed; } } /// /// 打开文件Translation /// public async void OpenFile(string[] newfile) { IsLoading = Visibility.Visible; if (newfile.Count() == 1) { //单文件打开重复文件时 需要选中已打开的重复文件 if (App.OpenedFileList.Contains(newfile[0])) { App.mainWindowViewModel.SelectItem(newfile[0]); } else { mainContentViewModel.OpenFile(newfile[0]); } ToolMethod.SetFileThumbImg(newfile[0]); } else { var fileList = newfile.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList(); if (fileList.Count <= 0) { IsLoading = Visibility.Collapsed; return; } mainContentViewModel.OpenFile(fileList[0]); for (int i = 1; i < fileList.Count(); i++) { if (!App.OpenedFileList.Contains(fileList[i])) { App.mainWindowViewModel.AddTabItem(fileList[i]); //这一段很重要 如果注释掉将不能多选文件打开 await Task.Delay(30); } ToolMethod.SetFileThumbImg(fileList[i]); } } IsLoading = Visibility.Collapsed; } /// /// 创建空白文档 /// public void CreatBlankPDF() { mainContentViewModel.CreateFile(); } #region Navigate public void OnNavigatedTo(NavigationContext navigationContext) { var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel; if (mainVM != null) { mainContentViewModel = mainVM; mainContentViewModel.homeContentViewModel = this; } NavigationParameters param = new NavigationParameters(); param.Add(ParameterNames.HomeContentViewModel, this); currentViewName = "Guid"; toolregion.RequestNavigate(ToolRegionName, "Guid", param); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } #endregion } }