ConverterHTMLDialogViewModel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 ConverterHTMLDialogViewModel : BindableBase, IDialogAware
  19. {
  20. public string Title => "";
  21. public event Action<IDialogResult> RequestClose;
  22. #region 参数和属性
  23. public ConverterHTMLDialogModel ConverterHTMLModel = new ConverterHTMLDialogModel();
  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 ConverterHTMLDialogViewModel(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. ConverterHTMLModel.OutputPath = dlg.SelectedPath.Trim();
  55. }
  56. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterHTMLModel.PageRange, PageRangeText, true, CurrentPageIndex);
  57. char[] enumerationSeparator = new char[] { ',' };
  58. char[] rangeSeparator = new char[] { '-' };
  59. if (!CommonHelper.GetPagesInRange(ref ConverterHTMLModel.PageIndexLists, ConverterHTMLModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  60. { //TODO
  61. Trace.WriteLine("输入不对");
  62. MessageBoxEx.Show("输入不对");
  63. return;
  64. }
  65. ConverterHTMLModel.htmlOption=ConverterHTMLModel.HTMLOptions();
  66. //result = await ConverterHelper.TxtConvert(ConverterTextModel.InputPath, ConverterTextModel.OutputPath, ConverterTextModel.GetProgress, ConverterTextModel.PageIndexLists, ConverterTextModel.Pawssword);
  67. DialogParameters value = new DialogParameters();
  68. value.Add(ParameterNames.ConverterType, "HTML");
  69. value.Add(ParameterNames.ConverterTypeModel, ConverterHTMLModel);
  70. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  71. dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  72. {
  73. });
  74. }
  75. private void CmbPageSelectionChangedEvent(object e)
  76. {
  77. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  78. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  79. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  80. var ConverterPreview = e as PageTurningPreview;
  81. if (ConverterPreview != null)
  82. {
  83. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  84. var currentViewer = this.currentViewer;
  85. string PageRange = "";
  86. var PageRangeText = this.PageRangeText;
  87. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
  88. {
  89. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  90. char[] enumerationSeparator = new char[] { ',' };
  91. char[] rangeSeparator = new char[] { '-' };
  92. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  93. { //TODO
  94. ConverterPreview.PageIndexLists.Add(0);
  95. Trace.WriteLine("输入不对");
  96. MessageBoxEx.Show("输入不对");
  97. return;
  98. }
  99. }
  100. if (PageRangeSelectIndex == "1")
  101. {
  102. char[] enumerationSeparator = new char[] { ',' };
  103. char[] rangeSeparator = new char[] { '-' };
  104. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  105. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  106. { //TODO
  107. Trace.WriteLine("输入不对");
  108. MessageBoxEx.Show("输入不对");
  109. return;
  110. }
  111. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  112. }
  113. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Count).ToString();
  114. ConverterPreview.CurrentIndex = 0;
  115. }
  116. }
  117. private void CmbPageTextChangedEvent(object e)
  118. {
  119. var ConverterPreview = e as PageTurningPreview;
  120. if (ConverterPreview != null)
  121. {
  122. if (ConverterPreview != null)
  123. {
  124. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  125. var currentViewer = this.currentViewer;
  126. string PageRange = "";
  127. var PageRangeText = this.PageRangeText;
  128. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  129. char[] enumerationSeparator = new char[] { ',' };
  130. char[] rangeSeparator = new char[] { '-' };
  131. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  132. { //TODO
  133. ConverterPreview.PageIndexLists.Add(0);
  134. return;
  135. }
  136. else
  137. {
  138. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  139. ConverterPreview.CurrentIndex = 0;
  140. }
  141. }
  142. }
  143. }
  144. #endregion
  145. #region 构架行为
  146. public bool CanCloseDialog()
  147. {
  148. return true;
  149. }
  150. public void OnDialogClosed()
  151. {
  152. }
  153. public void OnDialogOpened(IDialogParameters parameters)
  154. {
  155. CPDFViewer pdfViewer = null;
  156. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  157. if (pdfViewer != null)
  158. {
  159. currentViewer = pdfViewer;
  160. if (currentViewer.Tag != null) { ConverterHTMLModel.Pawssword = currentViewer.Tag.ToString(); }
  161. ConverterHTMLModel.InputPath = pdfViewer.Document.FilePath;
  162. FileInfo fileinfo = new FileInfo(ConverterHTMLModel.InputPath);
  163. ConverterHTMLModel.OutputPath = fileinfo.DirectoryName;
  164. }
  165. }
  166. #endregion
  167. }
  168. }