ConverterImgDialogViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using ComPDFKit_Conversion.Converter;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDF_Office.Model.Dialog.ConverterDialogs;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Windows.Forms;
  16. using DialogResult = Prism.Services.Dialogs.DialogResult;
  17. namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
  18. {
  19. public class ConverterImgDialogViewModel : BindableBase, IDialogAware
  20. {
  21. public string Title => "";
  22. public event Action<IDialogResult> RequestClose;
  23. #region 参数和属性
  24. public ConverterImgDialogModel ConverterImgModel = new ConverterImgDialogModel();
  25. public CPDFViewer currentViewer;
  26. public string CurrentPageIndex = "1";
  27. public IDialogService dialogs;
  28. public string PageRangeText { set; get; } = "0";
  29. public string PageRangeSelectIndex { set; get; } = "0";
  30. public List<string> ImageTypeFamily { set; get; } = new List<string>();
  31. private int imageTypeSelectedIndex;
  32. /// <summary>
  33. /// 图像类型的下拉框索引值
  34. /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
  35. /// </summary>
  36. public int ImageTypeSelectedIndex
  37. {
  38. get { return imageTypeSelectedIndex; }
  39. set
  40. {
  41. SetProperty(ref imageTypeSelectedIndex, value);
  42. UpdateImageType(ImageTypeSelectedIndex);
  43. }
  44. }
  45. private int imageDPISelectedIndex;
  46. /// <summary>
  47. /// 图像类型的下拉框索引值
  48. /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
  49. /// </summary>
  50. public int ImageDPISelectedIndex
  51. {
  52. get { return imageDPISelectedIndex; }
  53. set
  54. {
  55. SetProperty(ref imageDPISelectedIndex, value);
  56. UpdateImageDPI(ImageDPISelectedIndex);
  57. }
  58. }
  59. private void SetImageTypeOrigin()
  60. {
  61. this.ImageTypeFamily.Add("JPEG");
  62. this.ImageTypeFamily.Add("JPG");
  63. this.ImageTypeFamily.Add("PNG");
  64. this.ImageTypeFamily.Add("GIF");
  65. this.ImageTypeFamily.Add("TIFF");
  66. this.ImageTypeFamily.Add("TGA");
  67. this.ImageTypeFamily.Add("BMP");
  68. this.ImageTypeFamily.Add("JPEG-2000");
  69. }
  70. public List<string> ImageDPIFamily { set; get; } = new List<string>();
  71. private void SetImageDPIOrigin()
  72. {
  73. this.ImageDPIFamily.Add("50 dpi");
  74. this.ImageDPIFamily.Add("72 dpi");
  75. this.ImageDPIFamily.Add("96 dpi");
  76. this.ImageDPIFamily.Add("150 dpi");
  77. this.ImageDPIFamily.Add("300 dpi");
  78. this.ImageDPIFamily.Add("600 dpi");
  79. }
  80. #endregion
  81. #region 委托声明
  82. public DelegateCommand CancelCommand { get; set; }
  83. public DelegateCommand ConverterCommnad { get; set; }
  84. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  85. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  86. #endregion
  87. public ConverterImgDialogViewModel(IDialogService dialogService)
  88. {
  89. CancelCommand = new DelegateCommand(cancel);
  90. ConverterCommnad = new DelegateCommand(converter);
  91. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  92. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  93. dialogs = dialogService;
  94. }
  95. #region 逻辑函数
  96. /// <summary>
  97. /// 根据下拉框选项,选择不同的图片类型
  98. /// </summary>
  99. /// <param name="index"></param>
  100. private void UpdateImageType(int index)
  101. {
  102. this.ConverterImgModel.Type = ImgType.PNG;
  103. switch (index)
  104. {
  105. //case结果是同样的情况下,case可堆叠在一起,表示处理方式一样
  106. case 0:
  107. this.ConverterImgModel.imageType = ".jpeg";
  108. break;
  109. case 7:
  110. this.ConverterImgModel.imageType = ".jp2";
  111. break;
  112. case 3:
  113. this.ConverterImgModel.imageType = ".gif";
  114. break;
  115. case 4:
  116. this.ConverterImgModel.imageType = ".tiff";
  117. break;
  118. case 5:
  119. this.ConverterImgModel.imageType = ".tga";
  120. break;
  121. case 6:
  122. this.ConverterImgModel.imageType = ".bmp";
  123. break;
  124. case 2:
  125. this.ConverterImgModel.imageType = ".png";
  126. break;
  127. case 1:
  128. this.ConverterImgModel.imageType = ".jpg";
  129. this.ConverterImgModel.Type = ImgType.JPEG;
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. private void UpdateImageDPI(int index)
  136. {
  137. switch (index)
  138. {
  139. case 0:
  140. Trace.WriteLine("50");
  141. break;
  142. case 1:
  143. Trace.WriteLine("72");
  144. break;
  145. case 2:
  146. Trace.WriteLine("96");
  147. break;
  148. case 3:
  149. Trace.WriteLine("150");
  150. break;
  151. case 4:
  152. Trace.WriteLine("300");
  153. break;
  154. case 5:
  155. Trace.WriteLine("600");
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. private void cancel()
  162. {
  163. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  164. }
  165. private void converter()
  166. {
  167. FolderBrowserDialog dlg = new FolderBrowserDialog();
  168. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  169. {
  170. ConverterImgModel.OutputPath = dlg.SelectedPath.Trim();
  171. }
  172. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterImgModel.PageRange, PageRangeText, true, CurrentPageIndex);
  173. char[] enumerationSeparator = new char[] { ',' };
  174. char[] rangeSeparator = new char[] { '-' };
  175. if (!CommonHelper.GetPagesInRange(ref ConverterImgModel.PageIndexLists, ConverterImgModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  176. { //TODO
  177. Trace.WriteLine("输入不对");
  178. MessageBoxEx.Show("输入不对");
  179. return;
  180. }
  181. DialogParameters value = new DialogParameters();
  182. value.Add(ParameterNames.ConverterType, "Img");
  183. value.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
  184. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  185. dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  186. {
  187. });
  188. }
  189. private void CmbPageSelectionChangedEvent(object e)
  190. {
  191. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  192. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  193. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  194. var ConverterPreview = e as PageTurningPreview;
  195. if (ConverterPreview != null)
  196. {
  197. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  198. var currentViewer = this.currentViewer;
  199. string PageRange = "";
  200. var PageRangeText = this.PageRangeText;
  201. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
  202. {
  203. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  204. char[] enumerationSeparator = new char[] { ',' };
  205. char[] rangeSeparator = new char[] { '-' };
  206. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  207. { //TODO
  208. ConverterPreview.PageIndexLists.Add(0);
  209. Trace.WriteLine("输入不对");
  210. MessageBoxEx.Show("输入不对");
  211. return;
  212. }
  213. }
  214. if (PageRangeSelectIndex == "1")
  215. {
  216. char[] enumerationSeparator = new char[] { ',' };
  217. char[] rangeSeparator = new char[] { '-' };
  218. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  219. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  220. { //TODO
  221. Trace.WriteLine("输入不对");
  222. MessageBoxEx.Show("输入不对");
  223. return;
  224. }
  225. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  226. }
  227. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Count).ToString();
  228. ConverterPreview.CurrentIndex = 0;
  229. }
  230. }
  231. private void CmbPageTextChangedEvent(object e)
  232. {
  233. var ConverterPreview = e as PageTurningPreview;
  234. if (ConverterPreview != null)
  235. {
  236. if (ConverterPreview != null)
  237. {
  238. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  239. var currentViewer = this.currentViewer;
  240. string PageRange = "";
  241. var PageRangeText = this.PageRangeText;
  242. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  243. char[] enumerationSeparator = new char[] { ',' };
  244. char[] rangeSeparator = new char[] { '-' };
  245. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  246. { //TODO
  247. ConverterPreview.PageIndexLists.Add(0);
  248. return;
  249. }
  250. else
  251. {
  252. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  253. ConverterPreview.CurrentIndex = 0;
  254. }
  255. }
  256. }
  257. }
  258. #endregion
  259. #region 构架行为
  260. public bool CanCloseDialog()
  261. {
  262. return true;
  263. }
  264. public void OnDialogClosed()
  265. {
  266. }
  267. public void OnDialogOpened(IDialogParameters parameters)
  268. {
  269. CPDFViewer pdfViewer = null;
  270. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  271. if (pdfViewer != null)
  272. {
  273. currentViewer = pdfViewer;
  274. if (currentViewer.Tag != null) { ConverterImgModel.Pawssword = currentViewer.Tag.ToString(); }
  275. ConverterImgModel.InputPath = pdfViewer.Document.FilePath;
  276. FileInfo fileinfo = new FileInfo(ConverterImgModel.InputPath);
  277. ConverterImgModel.OutputPath = fileinfo.DirectoryName;
  278. SetImageTypeOrigin();
  279. SetImageDPIOrigin();
  280. }
  281. }
  282. #endregion
  283. }
  284. }