ConverterPPTDialogViewModel.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.Dialog.ConverterDialogs;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Windows.Forms;
  15. using DialogResult = Prism.Services.Dialogs.DialogResult;
  16. namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
  17. {
  18. public class ConverterPPTDialogViewModel : BindableBase, IDialogAware
  19. {
  20. public string Title => "";
  21. public event Action<IDialogResult> RequestClose;
  22. #region 参数和属性
  23. public ConverterPPTDialogModel ConverterPPTModel = new ConverterPPTDialogModel();
  24. public CPDFViewer currentViewer;
  25. public IDialogService dialogs;
  26. public string CurrentPageIndex = "1";
  27. public string PageRangeText { set; get; } = "0";
  28. public string PageRangeSelectIndex { set; get; } = "0";
  29. #endregion
  30. #region 委托声明
  31. public DelegateCommand CancelCommand { get; set; }
  32. public DelegateCommand ConverterCommnad { get; set; }
  33. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  34. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  35. #endregion
  36. public ConverterPPTDialogViewModel(IDialogService dialogService)
  37. {
  38. CancelCommand = new DelegateCommand(cancel);
  39. ConverterCommnad = new DelegateCommand(converter);
  40. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  41. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  42. dialogs = dialogService;
  43. }
  44. #region 逻辑函数
  45. private void cancel()
  46. {
  47. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  48. }
  49. private void converter()
  50. {
  51. FolderBrowserDialog dlg = new FolderBrowserDialog();
  52. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  53. {
  54. ConverterPPTModel.OutputPath = dlg.SelectedPath.Trim();
  55. }
  56. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterPPTModel.PageRange, PageRangeText, true, CurrentPageIndex);
  57. char[] enumerationSeparator = new char[] { ',' };
  58. char[] rangeSeparator = new char[] { '-' };
  59. if (!CommonHelper.GetPagesInRange(ref ConverterPPTModel.PageIndexLists, ConverterPPTModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  60. { //TODO
  61. Trace.WriteLine("输入不对");
  62. MessageBoxEx.Show("输入不对");
  63. return;
  64. }
  65. DialogParameters value = new DialogParameters();
  66. value.Add(ParameterNames.ConverterType, "PPT");
  67. value.Add(ParameterNames.ConverterTypeModel, ConverterPPTModel);
  68. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  69. dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  70. {
  71. });
  72. }
  73. private void CmbPageSelectionChangedEvent(object e)
  74. {
  75. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  76. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  77. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  78. var ConverterPreview = e as PageTurningPreview;
  79. if (ConverterPreview != null)
  80. {
  81. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  82. var currentViewer = this.currentViewer;
  83. string PageRange = "";
  84. var PageRangeText = this.PageRangeText;
  85. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
  86. {
  87. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  88. char[] enumerationSeparator = new char[] { ',' };
  89. char[] rangeSeparator = new char[] { '-' };
  90. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  91. { //TODO
  92. ConverterPreview.PageIndexLists.Add(0);
  93. Trace.WriteLine("输入不对");
  94. MessageBoxEx.Show("输入不对");
  95. return;
  96. }
  97. }
  98. if (PageRangeSelectIndex == "1")
  99. {
  100. char[] enumerationSeparator = new char[] { ',' };
  101. char[] rangeSeparator = new char[] { '-' };
  102. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  103. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  104. { //TODO
  105. Trace.WriteLine("输入不对");
  106. MessageBoxEx.Show("输入不对");
  107. return;
  108. }
  109. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  110. }
  111. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Count).ToString();
  112. ConverterPreview.CurrentIndex = 0;
  113. }
  114. }
  115. private void CmbPageTextChangedEvent(object e)
  116. {
  117. var ConverterPreview = e as PageTurningPreview;
  118. if (ConverterPreview != null)
  119. {
  120. if (ConverterPreview != null)
  121. {
  122. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  123. var currentViewer = this.currentViewer;
  124. string PageRange = "";
  125. var PageRangeText = this.PageRangeText;
  126. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  127. char[] enumerationSeparator = new char[] { ',' };
  128. char[] rangeSeparator = new char[] { '-' };
  129. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  130. { //TODO
  131. ConverterPreview.PageIndexLists.Add(0);
  132. return;
  133. }
  134. else
  135. {
  136. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  137. ConverterPreview.CurrentIndex = 0;
  138. }
  139. }
  140. }
  141. }
  142. #endregion
  143. #region 构架行为
  144. public bool CanCloseDialog()
  145. {
  146. return true;
  147. }
  148. public void OnDialogClosed()
  149. {
  150. }
  151. public void OnDialogOpened(IDialogParameters parameters)
  152. {
  153. CPDFViewer pdfViewer = null;
  154. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  155. if (pdfViewer != null)
  156. {
  157. currentViewer = pdfViewer;
  158. if (currentViewer.Tag != null) { ConverterPPTModel.Pawssword = currentViewer.Tag.ToString(); }
  159. ConverterPPTModel.InputPath = pdfViewer.Document.FilePath;
  160. FileInfo fileinfo = new FileInfo(ConverterPPTModel.InputPath);
  161. ConverterPPTModel.OutputPath = fileinfo.DirectoryName;
  162. }
  163. }
  164. #endregion
  165. }
  166. }