ConverterRTFDialogViewModel.cs 8.2 KB

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