SelectedTranslationDialogViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Master.Model;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using PDF_Master.Helper;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using static System.Resources.ResXFileRef;
  11. using static System.Net.Mime.MediaTypeNames;
  12. using System.Windows.Forms;
  13. using Microsoft.Office.Core;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using PDF_Master.Properties;
  17. using PDF_Master.CustomControl;
  18. namespace PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs
  19. {
  20. public class SelectedTranslationDialogViewModel : BindableBase, IDialogAware
  21. {
  22. #region 文案
  23. private string T_title;
  24. public string T_Title
  25. {
  26. get { return T_title; }
  27. set
  28. {
  29. SetProperty(ref T_title, value);
  30. }
  31. }
  32. private string T_limitTip;
  33. public string T_LimitTip
  34. {
  35. get { return T_limitTip; }
  36. set
  37. {
  38. SetProperty(ref T_limitTip, value);
  39. }
  40. }
  41. private string T_textBoxWatermark;
  42. public string T_TextBoxWatermark
  43. {
  44. get { return T_textBoxWatermark; }
  45. set
  46. {
  47. SetProperty(ref T_textBoxWatermark, value);
  48. }
  49. }
  50. private string T_copy;
  51. public string T_Copy
  52. {
  53. get { return T_copy; }
  54. set
  55. {
  56. SetProperty(ref T_copy, value);
  57. }
  58. }
  59. private string T_result;
  60. public string T_Result
  61. {
  62. get { return T_result; }
  63. set
  64. {
  65. SetProperty(ref T_result, value);
  66. }
  67. }
  68. private string T_translationBtn;
  69. public string T_TranslationBtn
  70. {
  71. get { return T_translationBtn; }
  72. set
  73. {
  74. SetProperty(ref T_translationBtn, value);
  75. }
  76. }
  77. private string T_translationLanguage;
  78. public string T_TranslationLanguage
  79. {
  80. get { return T_translationLanguage; }
  81. set
  82. {
  83. SetProperty(ref T_translationLanguage, value);
  84. }
  85. }
  86. private void InitString()
  87. {
  88. T_Title = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationTitle");
  89. T_LimitTip = App.HomePageLoader.GetString("AITranslationBtn_LimitTip");
  90. T_TextBoxWatermark = App.HomePageLoader.GetString("ChatGTPAI_TextBoxWatermark");
  91. T_TranslationLanguage = App.HomePageLoader.GetString("AITranslationBtn_TranslationLanguage");
  92. T_Copy = App.HomePageLoader.GetString("ChatGTPAI_Copy");
  93. T_Result = App.HomePageLoader.GetString("ChatGTPAI_Result");
  94. T_TranslationBtn = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationBtn");
  95. }
  96. #endregion
  97. #region 参数和属性
  98. public string Title => "";
  99. private string selectedText;
  100. public string SelectedText
  101. {
  102. get { return selectedText; }
  103. set
  104. {
  105. SetProperty(ref selectedText, value);
  106. }
  107. }
  108. private string translateText;
  109. public string TranslateText
  110. {
  111. get { return translateText; }
  112. set
  113. {
  114. SetProperty(ref translateText, value);
  115. }
  116. }
  117. private string errorTipText;
  118. public string ErrorTipText
  119. {
  120. get { return errorTipText; }
  121. set
  122. {
  123. SetProperty(ref errorTipText, value);
  124. }
  125. }
  126. /// <summary>
  127. /// 错误框显示
  128. /// </summary>
  129. private Visibility errorVisible = Visibility.Collapsed;
  130. public Visibility ErrorVisible
  131. {
  132. get
  133. {
  134. return errorVisible;
  135. }
  136. set
  137. {
  138. SetProperty(ref errorVisible, value);
  139. }
  140. }
  141. /// <summary>
  142. /// 进度条显示
  143. /// </summary>
  144. private Visibility progressVisible = Visibility.Collapsed;
  145. public Visibility ProgressVisible
  146. {
  147. get
  148. {
  149. return progressVisible;
  150. }
  151. set
  152. {
  153. SetProperty(ref progressVisible, value);
  154. }
  155. }
  156. private double processvalue = 0;
  157. /// <summary>
  158. /// 进度条当前值
  159. /// </summary>
  160. public double Value
  161. {
  162. get { return processvalue; }
  163. set
  164. {
  165. SetProperty(ref processvalue, value);
  166. }
  167. }
  168. private double maxValue = 10;
  169. /// <summary>
  170. /// 进度条最大值
  171. /// </summary>
  172. public double MaxValue
  173. {
  174. get { return maxValue; }
  175. set
  176. {
  177. SetProperty(ref maxValue, value);
  178. }
  179. }
  180. private string fromlanguage = "";
  181. private string tolanguage = "";
  182. private int fromlanguageIndex = 0;
  183. public int FromlanguageIndex
  184. {
  185. get { return fromlanguageIndex; }
  186. set
  187. {
  188. SetProperty(ref fromlanguageIndex, value);
  189. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
  190. }
  191. }
  192. /// <summary>
  193. /// 初始化翻译到语言列表
  194. /// </summary>
  195. private int tolanguageIndex = 0;
  196. public int TolanguageIndex
  197. {
  198. get { return tolanguageIndex; }
  199. set
  200. {
  201. SetProperty(ref tolanguageIndex, value);
  202. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
  203. }
  204. }
  205. /// <summary>
  206. /// 初始化翻译前语言列表
  207. /// </summary>
  208. public static List<string> FromlanguageFamily { set; get; } = new List<string>();
  209. private void GetFromlanguageOrigin()
  210. {
  211. FromlanguageFamily.Clear();
  212. FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
  213. }
  214. public List<string> TolanguageFamily { set; get; } = new List<string>();
  215. private void GetTolanguageOrigin()
  216. {
  217. TolanguageFamily.Clear();
  218. TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
  219. }
  220. #endregion
  221. #region 委托声明
  222. public DelegateCommand TranslateCommand { get; set; }
  223. public DelegateCommand CopyCommand { get; set; }
  224. public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
  225. #endregion
  226. public SelectedTranslationDialogViewModel()
  227. {
  228. TranslateCommand = new DelegateCommand(translate);
  229. CopyCommand = new DelegateCommand(copy);
  230. textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
  231. init();
  232. InitString();
  233. }
  234. #region 逻辑函数
  235. /// <summary>
  236. /// 初始化下拉列表,或其他
  237. /// </summary>
  238. private void init()
  239. {
  240. GetFromlanguageOrigin();
  241. GetTolanguageOrigin();
  242. }
  243. /// <summary>
  244. /// 检查文字是否超出范围
  245. /// </summary>
  246. /// <param name="e"></param>
  247. private void textBoxEnterCharactersTextChanged(object e)
  248. {
  249. var ConverterPreview = e as TextBoxEx;
  250. if (ConverterPreview != null)
  251. {
  252. //结果置空
  253. TranslateText = "";
  254. if (ConverterPreview.Text.Length > 150)
  255. {
  256. ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
  257. ErrorVisible = Visibility.Visible;
  258. }
  259. else {
  260. ErrorVisible = Visibility.Collapsed;
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// 文本翻译
  266. /// </summary>
  267. public async void translate()
  268. {
  269. //添加付费拦截锁
  270. if (!ServiceHelper.IAPBeforeFunction())
  271. {
  272. return;
  273. }
  274. if (Settings.Default.UserDate.subscribestatus != 1)
  275. {
  276. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  277. return;
  278. }
  279. ErrorVisible = Visibility.Collapsed;
  280. ProgressVisible = Visibility.Visible;
  281. Value = 1;
  282. string translatetext = "";
  283. string Code = "";
  284. Value = 4;
  285. await Task.Run(delegate
  286. {
  287. Code = ChatGTPAIHelper.textTranslate(SelectedText, fromlanguage, tolanguage, ref translatetext);
  288. });
  289. Value = 7;
  290. if (Code != "200")
  291. {
  292. ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(Code);
  293. ErrorVisible = Visibility.Visible;
  294. }
  295. Value = 9;
  296. ProgressVisible = Visibility.Collapsed;
  297. TranslateText = translatetext;
  298. }
  299. /// <summary>
  300. /// 复制到剪切板
  301. /// </summary>
  302. public void copy()
  303. {
  304. try
  305. {
  306. System.Windows.Forms.Clipboard.SetText(TranslateText);
  307. }
  308. catch { }
  309. }
  310. #endregion
  311. #region 构架行为
  312. public event Action<IDialogResult> RequestClose;
  313. public bool CanCloseDialog()
  314. {
  315. return true;
  316. }
  317. public void OnDialogClosed()
  318. {
  319. }
  320. public void OnDialogOpened(IDialogParameters parameters)
  321. {
  322. CPDFViewer pdfViewer = null;
  323. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  324. if (pdfViewer != null)
  325. {
  326. //获取选择的文字
  327. SelectedText = pdfViewer.GetSelectedText();
  328. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
  329. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
  330. }
  331. }
  332. #endregion
  333. }
  334. }