QuickToolsContentViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.Helper;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  5. using PDF_Office.Model.PDFTool;
  6. using PDFSettings;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Controls;
  17. namespace PDF_Office.ViewModels.HomePanel.PDFTools
  18. {
  19. public class QuickToolsContentViewModel : BindableBase
  20. {
  21. #region 属性
  22. /// <summary>
  23. /// 扩展/收缩
  24. /// </summary>
  25. private bool _isExpendTools = false;
  26. public bool IsExpendTools
  27. {
  28. get { return _isExpendTools; }
  29. set { SetProperty(ref _isExpendTools, value); }
  30. }
  31. private string regionName;
  32. public string ToolRegionName
  33. {
  34. get { return regionName; }
  35. set { SetProperty(ref regionName, value); }
  36. }
  37. #endregion
  38. #region Command and Event
  39. public IDialogService dialogs;
  40. public DelegateCommand<ToolItem> QuickToolsCommand { get; set; }
  41. public DelegateCommand<object> OpenMenuCommand { get; set; }
  42. public DelegateCommand ExpendCommand { get; set; }
  43. public DelegateCommand ShowToolCommand { get; set; }
  44. public IRegionManager toolregion;
  45. public event EventHandler<bool> ExpendToolsHanlder;
  46. #endregion
  47. public QuickToolsContentViewModel(IDialogService dialogService, IRegionManager regionManager)
  48. {
  49. toolregion = regionManager;
  50. ToolRegionName = RegionNames.ToolRegionName;
  51. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  52. {
  53. toolregion.RequestNavigate(ToolRegionName, "Guid");
  54. }));
  55. dialogs = dialogService;
  56. InitVariable();
  57. InitCommand();
  58. }
  59. #region 初始化和绑定
  60. private void InitVariable()
  61. {
  62. }
  63. private void InitCommand()
  64. {
  65. QuickToolsCommand = new DelegateCommand<ToolItem>(QuickTools_Click);
  66. OpenMenuCommand = new DelegateCommand<object>(OpenMenu);
  67. ExpendCommand = new DelegateCommand(Expend);
  68. ShowToolCommand = new DelegateCommand(ShowTool);
  69. }
  70. #endregion
  71. private void ShowTool()
  72. {
  73. toolregion.RequestNavigate(ToolRegionName, "Tools");
  74. }
  75. public void QuickTools_Click(ToolItem toolItem)
  76. {
  77. /*
  78. *设置这个对话框的起始保存路径
  79. */
  80. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  81. dlg.Multiselect = false;
  82. dlg.Filter = "PDF|*.pdf;*.PDF;";
  83. if (toolItem.FnType == PDFFnType.Compress || toolItem.FnType == PDFFnType.Security)
  84. {
  85. dlg.Multiselect = true;
  86. }
  87. if (toolItem.FnType == PDFFnType.Merge)
  88. {
  89. dlg.Multiselect = true;
  90. dlg.Filter = "Picture|*.png;*.PNG;*.jpg;*.JPG;*bmp;*jpeg;*gif;*tiff;";
  91. }
  92. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  93. {
  94. CPDFViewer viewer = new CPDFViewer();
  95. switch ((PDFFnType)toolItem.FnType)
  96. {
  97. case PDFFnType.Split:
  98. viewer.InitDocument(dlg.FileName);
  99. DialogParameters splitvalue = new DialogParameters();
  100. splitvalue.Add(ParameterNames.PDFViewer, viewer);
  101. splitvalue.Add(ParameterNames.FilePath, dlg.FileName);
  102. dialogs.ShowDialog(DialogNames.HomePageSplitDialog, splitvalue, e => { });
  103. break;
  104. case PDFFnType.Extract:
  105. viewer.InitDocument(dlg.FileName);
  106. DialogParameters extractvalue = new DialogParameters();
  107. extractvalue.Add(ParameterNames.PDFViewer, viewer);
  108. extractvalue.Add(ParameterNames.FilePath, dlg.FileName);
  109. dialogs.ShowDialog(DialogNames.HomePageExtractDialog, extractvalue, e => { });
  110. break;
  111. case PDFFnType.Insert:
  112. viewer.InitDocument(dlg.FileName);
  113. DialogParameters insertvalue = new DialogParameters();
  114. insertvalue.Add(ParameterNames.PDFViewer, viewer);
  115. insertvalue.Add(ParameterNames.FilePath, dlg.FileName);
  116. dialogs.ShowDialog(DialogNames.HomePageInsertDialog, insertvalue, e => { });
  117. break;
  118. case PDFFnType.Compress:
  119. DialogParameters compresspdf = new DialogParameters();
  120. compresspdf.Add(ParameterNames.BatchProcessing_Name, "1");
  121. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  122. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  123. compresspdf.Add(ParameterNames.FilePath, dlg.FileNames);
  124. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, compresspdf, e => { });
  125. break;
  126. case PDFFnType.Merge:
  127. DialogParameters picturetopdf = new DialogParameters();
  128. picturetopdf.Add(ParameterNames.FilePath, dlg.FileNames);
  129. dialogs.ShowDialog(DialogNames.HomePagePictureToPDFDialog, picturetopdf, e => { });
  130. break;
  131. case PDFFnType.Print:
  132. viewer.InitDocument(dlg.FileName);
  133. DialogParameters printvalue = new DialogParameters();
  134. printvalue.Add(ParameterNames.PDFViewer, viewer);
  135. printvalue.Add(ParameterNames.FilePath, dlg.FileName);
  136. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
  137. break;
  138. case PDFFnType.Security:
  139. DialogParameters securitypdf = new DialogParameters();
  140. securitypdf.Add(ParameterNames.BatchProcessing_Name, "2");
  141. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  142. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
  143. securitypdf.Add(ParameterNames.FilePath, dlg.FileNames);
  144. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, securitypdf, e => { });
  145. break;
  146. case PDFFnType.ConvertPDF:
  147. DialogParameters convertpdf = new DialogParameters();
  148. convertpdf.Add(ParameterNames.BatchProcessing_Name, "0");
  149. HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
  150. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
  151. convertpdf.Add(ParameterNames.FilePath, dlg.FileNames);
  152. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdf, e => { });
  153. break;
  154. }
  155. }
  156. }
  157. private void Expend()
  158. {
  159. IsExpendTools = !IsExpendTools;
  160. ExpendToolsHanlder?.Invoke(null, IsExpendTools);
  161. }
  162. private void OpenMenu(object obj)
  163. {
  164. var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
  165. var btn = obj as Button;
  166. if (menu != null && btn != null)
  167. {
  168. if(menu.Items.Count == 1)
  169. {
  170. var item = menu.Items[0] as MenuItem;
  171. if(_isExpendTools)
  172. {
  173. item.Header = "收缩";
  174. }
  175. else
  176. {
  177. item.Header = "展开";
  178. }
  179. item.Command = ExpendCommand;
  180. //btn.ContextMenu = menu;
  181. menu.PlacementTarget = btn;
  182. menu.IsOpen = true;
  183. }
  184. }
  185. }
  186. }
  187. }