HomeContentViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. param.Add(ParameterNames.Tag, view);
  114. toolregion.RequestNavigate(ToolRegionName, "HomeChatGPTAIContent", param);
  115. return;
  116. }
  117. toolregion.RequestNavigate(ToolRegionName, view, param);
  118. }
  119. /// <summary>
  120. /// 从外部拖拽打开文件
  121. /// </summary>
  122. /// <param name="files"></param>
  123. public async void AddFileFromDrag(List<string> files)
  124. {
  125. string txt = Properties.Resources.txtex;
  126. string word = Properties.Resources.wordex;
  127. string ppt = Properties.Resources.pptex;
  128. string excel = Properties.Resources.excelex;
  129. string html = Properties.Resources.htmlex;
  130. string image = Properties.Resources.imageex;
  131. string allfiles = txt + word + excel + ppt + html;
  132. if (files.Count() == 1 && App.OpenedFileList.Contains(files[0]))
  133. {
  134. //单文件打开重复文件时 需要选中已打开的重复文件
  135. App.mainWindowViewModel.SelectItem(files[0]);
  136. return;
  137. }
  138. IsLoading = Visibility.Visible;
  139. await Task.Delay(3);
  140. //排除文件夹或其他类型文件
  141. if (!System.IO.File.Exists(files[0]))
  142. {
  143. AlertsMessage alertsMessage = new AlertsMessage();
  144. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  145. IsLoading = Visibility.Collapsed;
  146. return;
  147. }
  148. #region 先在当前页签打开第一个文档
  149. var type = System.IO.Path.GetExtension(files[0]).ToLower();
  150. if (image.Contains(type))
  151. {
  152. //从图片创建
  153. mainContentViewModel.CreateFile(files[0]);
  154. }
  155. else if (type == ".pdf")
  156. {
  157. mainContentViewModel.OpenFile(files[0]);
  158. }
  159. else if(allfiles.Contains(type))
  160. {
  161. mainContentViewModel.CreateFileFromOffice(files[0]);
  162. }
  163. else
  164. {
  165. AlertsMessage alertsMessage = new AlertsMessage();
  166. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  167. IsLoading = Visibility.Collapsed;
  168. return;
  169. }
  170. ToolMethod.SetFileThumbImg(files[0]);
  171. #endregion
  172. #region 循环用新页签打开新文档
  173. for (int i = 1; i < files.Count(); i++)
  174. {
  175. if (!App.OpenedFileList.Contains(files[i]))
  176. {
  177. App.mainWindowViewModel.AddTabItem(files[i]);
  178. await Task.Delay(50);
  179. }
  180. ToolMethod.SetFileThumbImg(files[i]);
  181. }
  182. #endregion
  183. IsLoading = Visibility.Collapsed;
  184. }
  185. /// <summary>
  186. /// 从其他格式文件创建PDF
  187. /// </summary>
  188. public async void createFromOtherFile()
  189. {
  190. string txt = Properties.Resources.txtex;
  191. string word = Properties.Resources.wordex;
  192. string ppt = Properties.Resources.pptex;
  193. string excel = Properties.Resources.excelex;
  194. string html = Properties.Resources.htmlex;
  195. string image = Properties.Resources.imageex;
  196. string allfiles = txt + word + excel + ppt + image + html;
  197. OpenFileDialog dialog = new OpenFileDialog();
  198. dialog.Multiselect = true;
  199. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  200. $"Microsoft Office Word({word})|{word}|" +
  201. $"Microsoft Office Excel({excel})|{excel}|" +
  202. $"Microsoft Office PowerPoint({ppt})|{ppt}|" +
  203. $"Txt({txt})|{txt}|" +
  204. $"Picture({image})|{image}|" +
  205. $"Html({html})|{html}");
  206. if ((bool)dialog.ShowDialog())
  207. {
  208. var fileList = dialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  209. if (fileList.Count <= 0)
  210. {
  211. IsLoading = Visibility.Collapsed;
  212. return;
  213. }
  214. IsLoading = Visibility.Visible;
  215. await Task.Delay(10);
  216. //在当前页签打开第一个文件
  217. var type = System.IO.Path.GetExtension(fileList[0]).ToLower();
  218. if (image.Contains(type))
  219. {
  220. //图片文件
  221. mainContentViewModel.CreateFile(fileList[0]);
  222. }
  223. else
  224. {
  225. mainContentViewModel.CreateFileFromOffice(fileList[0]);
  226. }
  227. ToolMethod.SetFileThumbImg(fileList[0]);
  228. for (int i = 1; i < fileList.Count(); i++)
  229. {
  230. if (!App.OpenedFileList.Contains(fileList[i]))
  231. {
  232. App.mainWindowViewModel.AddTabItem(fileList[i]);
  233. await Task.Delay(50);
  234. }
  235. ToolMethod.SetFileThumbImg(fileList[i]);
  236. }
  237. IsLoading = Visibility.Collapsed;
  238. }
  239. }
  240. /// <summary>
  241. /// 从网页创建PDF文件
  242. /// </summary>
  243. private void createFormHtml()
  244. {
  245. dialog.ShowDialog(DialogNames.CreateFromHtmlDialog, async e =>
  246. {
  247. if (e.Result == ButtonResult.OK)
  248. {
  249. IsLoading = Visibility.Visible;
  250. var model = e.Parameters.GetValue<Model.Dialog.HomePageToolsDialogs.HtmlModel>(ParameterNames.DataModel);
  251. mainContentViewModel.CreateFileFromOffice(model.FilePath, model.PageSize, model.Margin);
  252. IsLoading = Visibility.Collapsed;
  253. }
  254. });
  255. }
  256. /// <summary>
  257. /// 从扫描仪创建
  258. /// </summary>
  259. public async void createFromScanner(string args)
  260. {
  261. dialog.ShowDialog(DialogNames.CreateFromScannerDialogs, async e =>
  262. {
  263. if (e.Result == ButtonResult.OK)
  264. {
  265. IsLoading = Visibility.Visible;
  266. string path;
  267. e.Parameters.TryGetValue("Imagepath", out path);
  268. if (args == "View")
  269. {
  270. App.mainWindowViewModel.AddTab.Execute();
  271. await Task.Delay(30);
  272. (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).CreateFile();
  273. }
  274. else
  275. {
  276. mainContentViewModel.CreateFile(path);
  277. }
  278. IsLoading = Visibility.Collapsed;
  279. }
  280. });
  281. }
  282. /// <summary>
  283. /// 打开文件
  284. /// </summary>
  285. public async void OpenFile()
  286. {
  287. OpenFileDialog openFileDialog = new OpenFileDialog();
  288. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  289. openFileDialog.Multiselect = true;
  290. if ((bool)openFileDialog.ShowDialog())
  291. {
  292. IsLoading = Visibility.Visible;
  293. if (openFileDialog.FileNames.Count() == 1)
  294. {
  295. //单文件打开重复文件时 需要选中已打开的重复文件
  296. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  297. {
  298. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  299. }
  300. else
  301. {
  302. mainContentViewModel.OpenFile(openFileDialog.FileName);
  303. }
  304. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  305. }
  306. else
  307. {
  308. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  309. if (fileList.Count <= 0)
  310. {
  311. IsLoading = Visibility.Collapsed;
  312. return;
  313. }
  314. mainContentViewModel.OpenFile(fileList[0]);
  315. for (int i = 1; i < fileList.Count(); i++)
  316. {
  317. if (!App.OpenedFileList.Contains(fileList[i]))
  318. {
  319. App.mainWindowViewModel.AddTabItem(fileList[i]);
  320. //这一段很重要 如果注释掉将不能多选文件打开
  321. await Task.Delay(30);
  322. }
  323. ToolMethod.SetFileThumbImg(fileList[i]);
  324. }
  325. }
  326. IsLoading = Visibility.Collapsed;
  327. }
  328. }
  329. /// <summary>
  330. /// 创建空白文档
  331. /// </summary>
  332. public void CreatBlankPDF()
  333. {
  334. mainContentViewModel.CreateFile();
  335. }
  336. #region Navigate
  337. public void OnNavigatedTo(NavigationContext navigationContext)
  338. {
  339. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  340. if (mainVM != null)
  341. {
  342. mainContentViewModel = mainVM;
  343. mainContentViewModel.homeContentViewModel = this;
  344. }
  345. NavigationParameters param = new NavigationParameters();
  346. param.Add(ParameterNames.HomeContentViewModel, this);
  347. toolregion.RequestNavigate(ToolRegionName, "Guid", param);
  348. }
  349. public bool IsNavigationTarget(NavigationContext navigationContext)
  350. {
  351. return true;
  352. }
  353. public void OnNavigatedFrom(NavigationContext navigationContext)
  354. {
  355. }
  356. #endregion
  357. }
  358. }