ConverterTextDialogViewModel.cs 8.1 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 static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
  16. using DialogResult = Prism.Services.Dialogs.DialogResult;
  17. namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
  18. {
  19. public class ConverterTextDialogViewModel : BindableBase, IDialogAware
  20. {
  21. public string Title => "";
  22. public event Action<IDialogResult> RequestClose;
  23. #region 参数和属性
  24. public ConverterTextDialogModel ConverterTextModel = new ConverterTextDialogModel();
  25. public CPDFViewer currentViewer;
  26. public IDialogService dialogs;
  27. public string CurrentPageIndex = "1";
  28. public string PageRangeText { set; get; } = "0";
  29. public string PageRangeSelectIndex { set; get; } = "0";
  30. #endregion
  31. #region 委托声明
  32. public DelegateCommand CancelCommand { get; set; }
  33. public DelegateCommand ConverterCommnad { get; set; }
  34. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  35. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  36. #endregion
  37. public ConverterTextDialogViewModel(IDialogService dialogService)
  38. {
  39. CancelCommand = new DelegateCommand(cancel);
  40. ConverterCommnad = new DelegateCommand(converter);
  41. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  42. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  43. dialogs = dialogService;
  44. }
  45. #region 逻辑函数
  46. private void cancel()
  47. {
  48. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  49. }
  50. private void converter()
  51. {
  52. FolderBrowserDialog dlg = new FolderBrowserDialog();
  53. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  54. {
  55. ConverterTextModel.OutputPath = dlg.SelectedPath.Trim();
  56. }
  57. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterTextModel.PageRange, PageRangeText,true, CurrentPageIndex);
  58. char[] enumerationSeparator = new char[] { ',' };
  59. char[] rangeSeparator = new char[] { '-' };
  60. if (!CommonHelper.GetPagesInRange(ref ConverterTextModel.PageIndexLists, ConverterTextModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  61. { //TODO
  62. Trace.WriteLine("输入不对");
  63. MessageBoxEx.Show("输入不对");
  64. return;
  65. }
  66. DialogParameters value = new DialogParameters();
  67. value.Add(ParameterNames.ConverterType, "Text");
  68. value.Add(ParameterNames.ConverterTypeModel, ConverterTextModel);
  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) { ConverterTextModel.Pawssword = currentViewer.Tag.ToString(); }
  160. ConverterTextModel.InputPath = pdfViewer.Document.FilePath;
  161. FileInfo fileinfo = new FileInfo(ConverterTextModel.InputPath);
  162. ConverterTextModel.OutputPath=fileinfo.DirectoryName;
  163. }
  164. }
  165. #endregion
  166. }
  167. }