HomeContentViewModel.cs 16 KB

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