DocumentaryTranslationDialogViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Win32;
  5. using PDF_Master.CustomControl;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using PDF_Master.Properties;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. namespace PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs
  20. {
  21. public class DocumentaryTranslationDialogViewModel : BindableBase, IDialogAware
  22. {
  23. #region 文案
  24. private string T_title;
  25. public string T_Title
  26. {
  27. get { return T_title; }
  28. set
  29. {
  30. SetProperty(ref T_title, value);
  31. }
  32. }
  33. private string T_limitTip;
  34. public string T_LimitTip
  35. {
  36. get { return T_limitTip; }
  37. set
  38. {
  39. SetProperty(ref T_limitTip, value);
  40. }
  41. }
  42. private string T_supportedLimitTip;
  43. public string T_SupportedLimitTip
  44. {
  45. get { return T_supportedLimitTip; }
  46. set
  47. {
  48. SetProperty(ref T_supportedLimitTip, value);
  49. }
  50. }
  51. private string T_translationLanguage;
  52. public string T_TranslationLanguage
  53. {
  54. get { return T_translationLanguage; }
  55. set
  56. {
  57. SetProperty(ref T_translationLanguage, value);
  58. }
  59. }
  60. private string T_translationBtn;
  61. public string T_TranslationBtn
  62. {
  63. get { return T_translationBtn; }
  64. set
  65. {
  66. SetProperty(ref T_translationBtn, value);
  67. }
  68. }
  69. private string T_no;
  70. public string T_No
  71. {
  72. get { return T_no; }
  73. set
  74. {
  75. SetProperty(ref T_no, value);
  76. }
  77. }
  78. private void InitString()
  79. {
  80. T_Title = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationTitle");
  81. T_SupportedLimitTip = App.HomePageLoader.GetString("AITranslation_NotSupportedTip");
  82. T_LimitTip = App.HomePageLoader.GetString("AITranslation_LimitTip");
  83. T_TranslationLanguage = App.HomePageLoader.GetString("AITranslationBtn_TranslationLanguage");
  84. T_TranslationBtn = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationBtn");
  85. T_No = App.MainPageLoader.GetString("Convert_No");
  86. }
  87. #endregion
  88. #region 参数和属性
  89. public string Title => "";
  90. public IRegionManager regionManager;
  91. public CPDFViewer PDFViewer;
  92. public ViewContentViewModel viewContentViewModel;
  93. public IDialogService dialogs;
  94. private CPDFViewer pdfViewer = null;
  95. private string fromlanguage = "";
  96. private string tolanguage = "";
  97. private int fromlanguageIndex = 0;
  98. public int FromlanguageIndex
  99. {
  100. get { return fromlanguageIndex; }
  101. set
  102. {
  103. SetProperty(ref fromlanguageIndex, value);
  104. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
  105. }
  106. }
  107. private int tolanguageIndex = 0;
  108. public int TolanguageIndex
  109. {
  110. get { return tolanguageIndex; }
  111. set
  112. {
  113. SetProperty(ref tolanguageIndex, value);
  114. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
  115. }
  116. }
  117. /// <summary>
  118. /// 进度条显示
  119. /// </summary>
  120. private Visibility progressVisible = Visibility.Collapsed;
  121. public Visibility ProgressVisible
  122. {
  123. get
  124. {
  125. return progressVisible;
  126. }
  127. set
  128. {
  129. SetProperty(ref progressVisible, value);
  130. }
  131. }
  132. private double processvalue = 0;
  133. /// <summary>
  134. /// 进度条当前值
  135. /// </summary>
  136. public double Value
  137. {
  138. get { return processvalue; }
  139. set
  140. {
  141. SetProperty(ref processvalue, value);
  142. }
  143. }
  144. private double maxValue = 10;
  145. /// <summary>
  146. /// 进度条最大值
  147. /// </summary>
  148. public double MaxValue
  149. {
  150. get { return maxValue; }
  151. set
  152. {
  153. SetProperty(ref maxValue, value);
  154. }
  155. }
  156. /// <summary>
  157. /// 初始化翻译前语言列表
  158. /// </summary>
  159. public static List<string> FromlanguageFamily { set; get; } = new List<string>();
  160. public string ViewerRegionName { get; set; }
  161. private void GetFromlanguageOrigin()
  162. {
  163. FromlanguageFamily.Clear();
  164. FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
  165. }
  166. /// <summary>
  167. /// 初始化翻译到语言列表
  168. /// </summary>
  169. public List<string> TolanguageFamily { set; get; } = new List<string>();
  170. private void GetTolanguageOrigin()
  171. {
  172. TolanguageFamily.Clear();
  173. TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
  174. }
  175. public static Action CancelProgress { get; set; }
  176. #endregion
  177. #region 委托声明
  178. public DelegateCommand TranslateCommand { get; set; }
  179. public DelegateCommand CancelCommand { get; set; }
  180. public DelegateCommand CancelProgressCommand { get; set; }
  181. #endregion
  182. public DocumentaryTranslationDialogViewModel(IRegionManager regionManager, IDialogService dialogService)
  183. {
  184. dialogs = dialogService;
  185. this.regionManager = regionManager;
  186. CancelCommand = new DelegateCommand(cancel);
  187. TranslateCommand = new DelegateCommand(translate);
  188. ViewerRegionName = Guid.NewGuid().ToString();
  189. CancelProgressCommand = new DelegateCommand(cancelprogress);
  190. init();
  191. InitString();
  192. }
  193. #region 逻辑函数
  194. /// <summary>
  195. /// 初始化下拉列表,或其他
  196. /// </summary>
  197. private void init()
  198. {
  199. GetFromlanguageOrigin();
  200. GetTolanguageOrigin();
  201. }
  202. /// <summary>
  203. /// 进度条取消
  204. /// </summary>
  205. private void cancelprogress()
  206. {
  207. if (CancelProgress != null)
  208. {
  209. CancelProgress.Invoke();
  210. }
  211. }
  212. /// <summary>
  213. /// 关闭窗口
  214. /// </summary>
  215. private void cancel()
  216. {
  217. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  218. }
  219. /// <summary>
  220. /// 翻译当前文档
  221. /// </summary>
  222. public async void translate()
  223. {
  224. //添加付费拦截锁
  225. if (!ServiceHelper.IAPBeforeFunction())
  226. {
  227. return;
  228. }
  229. #region 权益弹窗暂时关闭
  230. //bool flg = false;
  231. //权益弹窗
  232. //if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
  233. //{
  234. // DialogParameters value = new DialogParameters();
  235. // value.Add(ParameterNames.Open, "AI");
  236. // dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
  237. // {
  238. // if (dialogResult.Result == ButtonResult.OK)
  239. // {
  240. // flg = true;
  241. // }
  242. // else
  243. // {
  244. // flg = false;
  245. // }
  246. // });
  247. // if (flg == false)
  248. // {
  249. // return ;
  250. // }
  251. //}
  252. #endregion
  253. ProgressVisible = Visibility.Visible;
  254. Value = 1;
  255. string newfile = "";
  256. FileInfo fileInfo = new FileInfo(pdfViewer.Document.FilePath);
  257. Value = 3;
  258. //判断文件大小
  259. if ((((float)fileInfo.Length) / 1024 / 1024) > 30)
  260. {
  261. Value = 9;
  262. AlertsMessage alertsMessage = new AlertsMessage();
  263. alertsMessage.ShowDialog("", "The uploaded file cannot exceed 10MB", App.ServiceLoader.GetString("Text_ok"));
  264. ProgressVisible = Visibility.Collapsed;
  265. return;
  266. }
  267. //判断文件页数
  268. if (fileInfo.Extension.ToLower() == ".pdf")
  269. {
  270. CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(pdfViewer.Document.FilePath);
  271. if (cPDFDocument.PageCount > 30)
  272. {
  273. Value = 9;
  274. AlertsMessage alertsMessage = new AlertsMessage();
  275. alertsMessage.ShowDialog("", "The uploaded file cannot exceed 30Pages", App.ServiceLoader.GetString("Text_ok"));
  276. ProgressVisible = Visibility.Collapsed;
  277. return;
  278. }
  279. }
  280. Value = 4;
  281. await Task.Run(async delegate
  282. {
  283. newfile = await ChatGTPAIHelper.fileTranslate(pdfViewer.Document.FilePath, fromlanguage, tolanguage, false);
  284. });
  285. Value = 8;
  286. if (!string.IsNullOrEmpty(newfile))
  287. {
  288. if (File.Exists(newfile))
  289. {
  290. if (viewContentViewModel != null)
  291. {
  292. viewContentViewModel.OpenFile(new string[] { newfile });
  293. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  294. }
  295. }
  296. else
  297. {
  298. //newfile code
  299. if (newfile == "-1")
  300. {
  301. Value = 9;
  302. ProgressVisible = Visibility.Collapsed; return;
  303. }
  304. AlertsMessage alertsMessage = new AlertsMessage();
  305. alertsMessage.ShowDialog("", ChatGTPAIHelper.GetBaiduTranslationCode(newfile), App.ServiceLoader.GetString("Text_ok"));
  306. }
  307. }
  308. Value = 9;
  309. ProgressVisible = Visibility.Collapsed;
  310. }
  311. #endregion
  312. #region 构架行为
  313. public event Action<IDialogResult> RequestClose;
  314. public bool CanCloseDialog()
  315. {
  316. return true;
  317. }
  318. public void OnDialogClosed()
  319. {
  320. }
  321. public void OnDialogOpened(IDialogParameters parameters)
  322. {
  323. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  324. parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  325. if (pdfViewer != null)
  326. {
  327. if (!regionManager.Regions[ViewerRegionName].Views.Contains(pdfViewer))
  328. {
  329. //设置PDFview为预览
  330. PDFViewer = new CPDFViewer();
  331. PDFViewer.InitDocument(pdfViewer.Document);
  332. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  333. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  334. PDFViewer.Load();
  335. if (PDFViewer.Document.PageCount == 1)
  336. {
  337. //防止单页pdfview闪烁问题
  338. PDFViewer.ChangeViewMode(ViewMode.Single);
  339. }
  340. else
  341. {
  342. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  343. }
  344. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  345. PDFViewer.SetMouseMode(MouseModes.PanTool);
  346. PDFViewer.Document.ReleasePages();
  347. PDFViewer.ReloadDocument();
  348. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
  349. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
  350. //PageRangeNumber = PDFViewer.Document.PageCount;
  351. //PDFViewer.Zoom(0.1);
  352. }
  353. }
  354. }
  355. #endregion
  356. }
  357. }