HomeContentViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.EventAggregators;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using PDFReader_WPF.Helper;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using WIA;
  21. namespace PDF_Master.ViewModels
  22. {
  23. public class HomeContentViewModel : BindableBase, INavigationAware
  24. {
  25. #region 文案
  26. private string T_homeBtn;
  27. public string T_HomeBtn
  28. {
  29. get { return T_homeBtn; }
  30. set
  31. {
  32. SetProperty(ref T_homeBtn, value);
  33. }
  34. }
  35. private string T_aITranslationBtn;
  36. public string T_AITranslationBtn
  37. {
  38. get { return T_aITranslationBtn; }
  39. set
  40. {
  41. SetProperty(ref T_aITranslationBtn, value);
  42. }
  43. }
  44. private string T_aIRewritingBtn;
  45. public string T_AIRewritingBtn
  46. {
  47. get { return T_aIRewritingBtn; }
  48. set
  49. {
  50. SetProperty(ref T_aIRewritingBtn, value);
  51. }
  52. }
  53. private string T_aIErrorCorrectionBtn;
  54. public string T_AIErrorCorrectionBtn
  55. {
  56. get { return T_aIErrorCorrectionBtn; }
  57. set
  58. {
  59. SetProperty(ref T_aIErrorCorrectionBtn, value);
  60. }
  61. }
  62. private string T_openFiles;
  63. public string T_OpenFiles
  64. {
  65. get { return T_openFiles; }
  66. set
  67. {
  68. SetProperty(ref T_openFiles, value);
  69. }
  70. }
  71. private string T_createPDF;
  72. public string T_CreatePDF
  73. {
  74. get { return T_createPDF; }
  75. set
  76. {
  77. SetProperty(ref T_createPDF, value);
  78. }
  79. }
  80. private string T_createPDFToNew;
  81. public string T_CreatePDFToNew
  82. {
  83. get { return T_createPDFToNew; }
  84. set
  85. {
  86. SetProperty(ref T_createPDFToNew, value);
  87. }
  88. }
  89. private void InitString()
  90. {
  91. T_OpenFiles = App.HomePageLoader.GetString("Home_OpenFiles");
  92. T_CreatePDF = App.HomePageLoader.GetString("Home_CreatePDF");
  93. T_CreatePDFToNew = App.HomePageLoader.GetString("Home_CreateToNew");
  94. T_HomeBtn = App.HomePageLoader.GetString("Home_HomeBtn");
  95. T_AITranslationBtn = App.HomePageLoader.GetString("Home_AITranslationBtn");
  96. T_AIRewritingBtn = App.HomePageLoader.GetString("Home_AIRewritingBtn");
  97. T_AIErrorCorrectionBtn = App.HomePageLoader.GetString("Home_AIErrorCorrectionBtn");
  98. }
  99. #endregion
  100. public string currentViewName = "";
  101. private string fileName = "Home";
  102. public string FileName
  103. {
  104. get { return fileName; }
  105. set { SetProperty(ref fileName, value); }
  106. }
  107. private string regionName;
  108. public string ToolRegionName
  109. {
  110. get { return regionName; }
  111. set { SetProperty(ref regionName, value); }
  112. }
  113. private Visibility isLoading = Visibility.Collapsed;
  114. public Visibility IsLoading
  115. {
  116. get { return isLoading; }
  117. set
  118. {
  119. SetProperty(ref isLoading, value);
  120. }
  121. }
  122. private MainContentViewModel mainContentViewModel;
  123. public DelegateCommand OpenFileCommand { get; set; }
  124. public DelegateCommand<string> ShowToolCommand { get; set; }
  125. public DelegateCommand CreateBlackPDFCommand { get; set; }
  126. public DelegateCommand CreateFromOtherFile { get; set; }
  127. public DelegateCommand CreateFromHtmlCommnd { get; set; }
  128. public DelegateCommand<string> CreateFromScanner { get; set; }
  129. public IRegionManager toolregion;
  130. public IEventAggregator eventer;
  131. public IDialogService dialog;
  132. public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogaware)
  133. {
  134. toolregion = regionManager;
  135. eventer = eventAggregator;
  136. dialog = dialogaware;
  137. ToolRegionName = RegionNames.ToolRegionName;
  138. OpenFileCommand = new DelegateCommand(OpenFile);
  139. ShowToolCommand = new DelegateCommand<string>(ShowToolContent);
  140. CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF);
  141. CreateFromOtherFile = new DelegateCommand(createFromOtherFile);
  142. CreateFromHtmlCommnd = new DelegateCommand(createFormHtml);
  143. CreateFromScanner = new DelegateCommand<string>(createFromScanner);
  144. InitString();
  145. }
  146. /// <summary>
  147. /// 显示右侧不同的工具栏页面
  148. /// </summary>
  149. /// <param name="view"></param>
  150. public void ShowToolContent(string view)
  151. {
  152. currentViewName = view;
  153. ShowToolContentDataTracking(view);
  154. NavigationParameters param = new NavigationParameters();
  155. param.Add(ParameterNames.HomeContentViewModel, this);
  156. if (view.Contains("ChatGPT"))
  157. {
  158. param.Add(ParameterNames.Tag, view);
  159. toolregion.RequestNavigate(ToolRegionName, "HomeChatGPTAIContent", param);
  160. return;
  161. }
  162. toolregion.RequestNavigate(ToolRegionName, view, param);
  163. }
  164. /// <summary>
  165. /// 显示右侧不同的工具栏页面埋点
  166. /// </summary>
  167. /// <param name="view"></param>
  168. public void ShowToolContentDataTracking(string tag)
  169. {
  170. DataTrackingHelper.EntryPath = "";
  171. switch (tag)
  172. {
  173. case "Guid":
  174. //DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "Home");
  175. break;
  176. case "ChatGPTAITranslationContent":
  177. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "AI Translation");
  178. break;
  179. case "ChatGPTAIRewritingContent":
  180. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "AI Rewriting");
  181. break;
  182. case "ChatGPTAIErrorCorrectionContent":
  183. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Home, "AI_Btn", "AI Correction");
  184. break;
  185. }
  186. }
  187. /// <summary>
  188. /// 从外部拖拽打开文件
  189. /// </summary>
  190. /// <param name="files"></param>
  191. public async void AddFileFromDrag(List<string> files)
  192. {
  193. string txt = Properties.Resources.txtex;
  194. string word = Properties.Resources.wordex;
  195. string ppt = Properties.Resources.pptex;
  196. string excel = Properties.Resources.excelex;
  197. string html = Properties.Resources.htmlex;
  198. string image = Properties.Resources.imageex;
  199. string allfiles = txt + word + excel + ppt + html;
  200. if (files.Count() == 1 && App.OpenedFileList.Contains(files[0]))
  201. {
  202. //单文件打开重复文件时 需要选中已打开的重复文件
  203. App.mainWindowViewModel.SelectItem(files[0]);
  204. return;
  205. }
  206. IsLoading = Visibility.Visible;
  207. await Task.Delay(3);
  208. //排除文件夹或其他类型文件
  209. if (!System.IO.File.Exists(files[0]))
  210. {
  211. AlertsMessage alertsMessage = new AlertsMessage();
  212. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  213. IsLoading = Visibility.Collapsed;
  214. return;
  215. }
  216. #region 先在当前页签打开第一个文档
  217. var type = System.IO.Path.GetExtension(files[0]).ToLower();
  218. if (image.Contains(type))
  219. {
  220. //从图片创建
  221. mainContentViewModel.CreateFile(files[0]);
  222. }
  223. else if (type == ".pdf")
  224. {
  225. mainContentViewModel.OpenFile(files[0]);
  226. }
  227. else if (allfiles.Contains(type))
  228. {
  229. mainContentViewModel.CreateFileFromOffice(files[0]);
  230. }
  231. else
  232. {
  233. AlertsMessage alertsMessage = new AlertsMessage();
  234. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  235. IsLoading = Visibility.Collapsed;
  236. return;
  237. }
  238. ToolMethod.SetFileThumbImg(files[0]);
  239. #endregion
  240. #region 循环用新页签打开新文档
  241. for (int i = 1; i < files.Count(); i++)
  242. {
  243. if (!App.OpenedFileList.Contains(files[i]))
  244. {
  245. App.mainWindowViewModel.AddTabItem(files[i]);
  246. await Task.Delay(50);
  247. }
  248. ToolMethod.SetFileThumbImg(files[i]);
  249. }
  250. #endregion
  251. IsLoading = Visibility.Collapsed;
  252. }
  253. /// <summary>
  254. /// 从其他格式文件创建PDF
  255. /// </summary>
  256. public async void createFromOtherFile()
  257. {
  258. string txt = Properties.Resources.txtex;
  259. string word = Properties.Resources.wordex;
  260. string ppt = Properties.Resources.pptex;
  261. string excel = Properties.Resources.excelex;
  262. string html = Properties.Resources.htmlex;
  263. string image = Properties.Resources.imageex;
  264. string allfiles = txt + word + excel + ppt + image + html;
  265. OpenFileDialog dialog = new OpenFileDialog();
  266. dialog.Multiselect = true;
  267. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  268. $"Microsoft Office Word({word})|{word}|" +
  269. $"Microsoft Office Excel({excel})|{excel}|" +
  270. $"Microsoft Office PowerPoint({ppt})|{ppt}|" +
  271. $"Txt({txt})|{txt}|" +
  272. $"Picture({image})|{image}|" +
  273. $"Html({html})|{html}");
  274. if ((bool)dialog.ShowDialog())
  275. {
  276. var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  277. if (fileList.Count <= 0)
  278. {
  279. IsLoading = Visibility.Collapsed;
  280. return;
  281. }
  282. IsLoading = Visibility.Visible;
  283. await Task.Delay(10);
  284. //在当前页签打开第一个文件
  285. var type = System.IO.Path.GetExtension(fileList[0]).ToLower();
  286. if (image.Contains(type))
  287. {
  288. //图片文件
  289. mainContentViewModel.CreateFile(fileList[0]);
  290. }
  291. else
  292. {
  293. mainContentViewModel.CreateFileFromOffice(fileList[0]);
  294. }
  295. ToolMethod.SetFileThumbImg(fileList[0]);
  296. for (int i = 1; i < fileList.Count(); i++)
  297. {
  298. if (!App.OpenedFileList.Contains(fileList[i]))
  299. {
  300. App.mainWindowViewModel.AddTabItem(fileList[i]);
  301. await Task.Delay(50);
  302. }
  303. ToolMethod.SetFileThumbImg(fileList[i]);
  304. }
  305. IsLoading = Visibility.Collapsed;
  306. }
  307. }
  308. /// <summary>
  309. /// 从网页创建PDF文件
  310. /// </summary>
  311. private void createFormHtml()
  312. {
  313. dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, e =>
  314. {
  315. if (e.Result == ButtonResult.OK)
  316. {
  317. IsLoading = Visibility.Visible;
  318. var model = e.Parameters.GetValue<Model.Dialog.HomePageToolsDialogs.HtmlModel>(ParameterNames.DataModel);
  319. mainContentViewModel.CreateFileFromOffice(model.FilePath, model.PageSize, model.Margin);
  320. IsLoading = Visibility.Collapsed;
  321. }
  322. });
  323. }
  324. /// <summary>
  325. /// 从扫描仪创建
  326. /// </summary>
  327. public void createFromScanner(string args)
  328. {
  329. dialog.ShowDialog(DialogNames.CreateFromScannerDialogs, async e =>
  330. {
  331. if (e.Result == ButtonResult.OK)
  332. {
  333. IsLoading = Visibility.Visible;
  334. string path;
  335. e.Parameters.TryGetValue("Imagepath", out path);
  336. if (args == "View")
  337. {
  338. App.mainWindowViewModel.AddTab.Execute();
  339. await Task.Delay(30);
  340. (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).CreateFile();
  341. }
  342. else
  343. {
  344. mainContentViewModel.CreateFile(path);
  345. }
  346. IsLoading = Visibility.Collapsed;
  347. }
  348. });
  349. }
  350. /// <summary>
  351. /// 打开文件
  352. /// </summary>
  353. public async void OpenFile()
  354. {
  355. OpenFileDialog openFileDialog = new OpenFileDialog();
  356. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  357. openFileDialog.Multiselect = true;
  358. if ((bool)openFileDialog.ShowDialog())
  359. {
  360. IsLoading = Visibility.Visible;
  361. if (openFileDialog.FileNames.Count() == 1)
  362. {
  363. //单文件打开重复文件时 需要选中已打开的重复文件
  364. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  365. {
  366. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  367. }
  368. else
  369. {
  370. mainContentViewModel.OpenFile(openFileDialog.FileName);
  371. }
  372. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  373. }
  374. else
  375. {
  376. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  377. if (fileList.Count <= 0)
  378. {
  379. IsLoading = Visibility.Collapsed;
  380. return;
  381. }
  382. mainContentViewModel.OpenFile(fileList[0]);
  383. for (int i = 1; i < fileList.Count(); i++)
  384. {
  385. if (!App.OpenedFileList.Contains(fileList[i]))
  386. {
  387. App.mainWindowViewModel.AddTabItem(fileList[i]);
  388. //这一段很重要 如果注释掉将不能多选文件打开
  389. await Task.Delay(30);
  390. }
  391. ToolMethod.SetFileThumbImg(fileList[i]);
  392. }
  393. }
  394. IsLoading = Visibility.Collapsed;
  395. }
  396. }
  397. /// <summary>
  398. /// 打开文件Translation
  399. /// </summary>
  400. public async void OpenFile(string[] newfile)
  401. {
  402. IsLoading = Visibility.Visible;
  403. if (newfile.Count() == 1)
  404. {
  405. //单文件打开重复文件时 需要选中已打开的重复文件
  406. if (App.OpenedFileList.Contains(newfile[0]))
  407. {
  408. App.mainWindowViewModel.SelectItem(newfile[0]);
  409. }
  410. else
  411. {
  412. mainContentViewModel.OpenFile(newfile[0]);
  413. }
  414. ToolMethod.SetFileThumbImg(newfile[0]);
  415. }
  416. else
  417. {
  418. var fileList = newfile.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  419. if (fileList.Count <= 0)
  420. {
  421. IsLoading = Visibility.Collapsed;
  422. return;
  423. }
  424. mainContentViewModel.OpenFile(fileList[0]);
  425. for (int i = 1; i < fileList.Count(); i++)
  426. {
  427. if (!App.OpenedFileList.Contains(fileList[i]))
  428. {
  429. App.mainWindowViewModel.AddTabItem(fileList[i]);
  430. //这一段很重要 如果注释掉将不能多选文件打开
  431. await Task.Delay(30);
  432. }
  433. ToolMethod.SetFileThumbImg(fileList[i]);
  434. }
  435. }
  436. IsLoading = Visibility.Collapsed;
  437. }
  438. /// <summary>
  439. /// 创建空白文档
  440. /// </summary>
  441. public void CreatBlankPDF()
  442. {
  443. mainContentViewModel.CreateFile();
  444. }
  445. #region Navigate
  446. public void OnNavigatedTo(NavigationContext navigationContext)
  447. {
  448. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  449. if (mainVM != null)
  450. {
  451. mainContentViewModel = mainVM;
  452. mainContentViewModel.homeContentViewModel = this;
  453. }
  454. NavigationParameters param = new NavigationParameters();
  455. param.Add(ParameterNames.HomeContentViewModel, this);
  456. currentViewName = "Guid";
  457. toolregion.RequestNavigate(ToolRegionName, "Guid", param);
  458. }
  459. public bool IsNavigationTarget(NavigationContext navigationContext)
  460. {
  461. return true;
  462. }
  463. public void OnNavigatedFrom(NavigationContext navigationContext)
  464. {
  465. }
  466. #endregion
  467. }
  468. }