HomeContentViewModel.cs 15 KB

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