HomeContentViewModel.cs 12 KB

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