HomeContentViewModel.cs 12 KB

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