HomeContentViewModel.cs 13 KB

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