123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using PDF_Master.Helper;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using static System.Resources.ResXFileRef;
- using static System.Net.Mime.MediaTypeNames;
- using System.Windows.Forms;
- using Microsoft.Office.Core;
- using System.Threading.Tasks;
- using System.Windows;
- using PDF_Master.Properties;
- using PDF_Master.CustomControl;
- using DialogResult = Prism.Services.Dialogs.DialogResult;
- namespace PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs
- {
- public class SelectedTranslationDialogViewModel : BindableBase, IDialogAware
- {
- #region 文案
- private string T_title;
- public string T_Title
- {
- get { return T_title; }
- set
- {
- SetProperty(ref T_title, value);
- }
- }
- private string T_limitTip;
- public string T_LimitTip
- {
- get { return T_limitTip; }
- set
- {
- SetProperty(ref T_limitTip, value);
- }
- }
- private string T_textBoxWatermark;
- public string T_TextBoxWatermark
- {
- get { return T_textBoxWatermark; }
- set
- {
- SetProperty(ref T_textBoxWatermark, value);
- }
- }
- private string T_copy;
- public string T_Copy
- {
- get { return T_copy; }
- set
- {
- SetProperty(ref T_copy, value);
- }
- }
- private string T_result;
- public string T_Result
- {
- get { return T_result; }
- set
- {
- SetProperty(ref T_result, value);
- }
- }
- private string T_translationBtn;
- public string T_TranslationBtn
- {
- get { return T_translationBtn; }
- set
- {
- SetProperty(ref T_translationBtn, value);
- }
- }
- private string T_translationLanguage;
- public string T_TranslationLanguage
- {
- get { return T_translationLanguage; }
- set
- {
- SetProperty(ref T_translationLanguage, value);
- }
- }
- private string T_copied;
- public string T_Copied
- {
- get { return T_copied; }
- set
- {
- SetProperty(ref T_copied, value);
- }
- }
- private void InitString()
- {
- T_Title = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationTitle");
- T_LimitTip = App.HomePageLoader.GetString("AISelectTranslationBtn_LimitTip");
- T_TextBoxWatermark = App.HomePageLoader.GetString("ChatGTPAI_TextBoxWatermark");
- T_TranslationLanguage = App.HomePageLoader.GetString("AITranslationBtn_TranslationLanguage");
- T_Copy = App.HomePageLoader.GetString("ChatGTPAI_Copy");
- T_Result = App.HomePageLoader.GetString("ChatGTPAI_Result");
- T_TranslationBtn = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationBtn");
- T_Copied = App.HomePageLoader.GetString("ChatGTPAI_Copied");
- }
- #endregion
- #region 参数和属性
- public string Title => "";
- private string selectedText;
- public string SelectedText
- {
- get { return selectedText; }
- set
- {
- SetProperty(ref selectedText, value);
- }
- }
- private string translateText;
- public string TranslateText
- {
- get { return translateText; }
- set
- {
- SetProperty(ref translateText, value);
- }
- }
- private string errorTipText;
- public string ErrorTipText
- {
- get { return errorTipText; }
- set
- {
- SetProperty(ref errorTipText, value);
- }
- }
- /// <summary>
- /// 错误框显示
- /// </summary>
- private Visibility errorVisible = Visibility.Collapsed;
- public Visibility ErrorVisible
- {
- get
- {
- return errorVisible;
- }
- set
- {
- SetProperty(ref errorVisible, value);
- }
- }
- /// <summary>
- /// 进度条显示
- /// </summary>
- private Visibility progressVisible = Visibility.Collapsed;
- public Visibility ProgressVisible
- {
- get
- {
- return progressVisible;
- }
- set
- {
- SetProperty(ref progressVisible, value);
- }
- }
- private double processvalue = 0;
- /// <summary>
- /// 进度条当前值
- /// </summary>
- public double Value
- {
- get { return processvalue; }
- set
- {
- SetProperty(ref processvalue, value);
- }
- }
- private double maxValue = 10;
- /// <summary>
- /// 进度条最大值
- /// </summary>
- public double MaxValue
- {
- get { return maxValue; }
- set
- {
- SetProperty(ref maxValue, value);
- }
- }
- private Visibility showTip = Visibility.Collapsed;
- public Visibility ShowTip
- {
- get
- {
- return showTip;
- }
- set
- {
- SetProperty(ref showTip, value);
- }
- }
- private string fromlanguage = "";
- private string tolanguage = "";
- private int fromlanguageIndex = 0;
- public int FromlanguageIndex
- {
- get { return fromlanguageIndex; }
- set
- {
- SetProperty(ref fromlanguageIndex, value);
- fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
- }
- }
- /// <summary>
- /// 初始化翻译到语言列表
- /// </summary>
- private int tolanguageIndex = 0;
- public int TolanguageIndex
- {
- get { return tolanguageIndex; }
- set
- {
- SetProperty(ref tolanguageIndex, value);
- tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
- }
- }
- /// <summary>
- /// 初始化翻译前语言列表
- /// </summary>
- public static List<string> FromlanguageFamily { set; get; } = new List<string>();
- private void GetFromlanguageOrigin()
- {
- FromlanguageFamily.Clear();
- FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
- }
- public List<string> TolanguageFamily { set; get; } = new List<string>();
- private void GetTolanguageOrigin()
- {
- TolanguageFamily.Clear();
- TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
- }
- #endregion
- #region 委托声明
- public DelegateCommand TranslateCommand { get; set; }
- public DelegateCommand CopyCommand { get; set; }
- public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
- public IDialogService dialogs;
- #endregion
- public SelectedTranslationDialogViewModel(IDialogService dialogService)
- {
- dialogs = dialogService;
- TranslateCommand = new DelegateCommand(translate);
- CopyCommand = new DelegateCommand(copy);
- textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
- init();
- InitString();
- }
- #region 逻辑函数
- /// <summary>
- /// 初始化下拉列表,或其他
- /// </summary>
- private void init()
- {
- GetFromlanguageOrigin();
- GetTolanguageOrigin();
- }
- /// <summary>
- /// 检查文字是否超出范围
- /// </summary>
- /// <param name="e"></param>
- private void textBoxEnterCharactersTextChanged(object e)
- {
- var ConverterPreview = e as TextBoxEx;
- if (ConverterPreview != null)
- {
- //结果置空
- TranslateText = "";
- if (ConverterPreview.Text.Length > 150)
- {
- ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
- ErrorVisible = Visibility.Visible;
- }
- else {
- ErrorVisible = Visibility.Collapsed;
- }
- }
- }
- /// <summary>
- /// 文本翻译
- /// </summary>
- public async void translate()
- {
- //添加付费拦截锁
- if (!ServiceHelper.IAPBeforeFunction())
- {
- return;
- }
- #region 权益弹窗暂时关闭
- //bool flg = false;
- ////权益弹窗
- //if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
- //{
- // DialogParameters value = new DialogParameters();
- // value.Add(ParameterNames.Open, "AI");
- // dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
- // {
- // if (dialogResult.Result == ButtonResult.OK)
- // {
- // flg = true;
- // }
- // else
- // {
- // flg = false;
- // }
- // });
- // if (flg == false)
- // {
- // return;
- // }
- //}
- #endregion
- ErrorVisible = Visibility.Collapsed;
- ProgressVisible = Visibility.Visible;
- Value = 1;
- string translatetext = "";
- string Code = "";
- Value = 4;
- await Task.Run(delegate
- {
- Code = ChatGTPAIHelper.textTranslate(SelectedText, fromlanguage, tolanguage, ref translatetext);
- });
- Value = 7;
- if (Code != "200")
- {
- ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(Code);
- ErrorVisible = Visibility.Visible;
- }
- Value = 9;
- ProgressVisible = Visibility.Collapsed;
- TranslateText = translatetext;
- }
- /// <summary>
- /// 复制到剪切板
- /// </summary>
- public void copy()
- {
- try
- {
- System.Windows.Forms.Clipboard.SetText(TranslateText);
- ShowTip = Visibility.Visible;
- }
- catch { }
- }
- #endregion
- #region 构架行为
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- CPDFViewer pdfViewer = null;
- parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
- if (pdfViewer != null)
- {
- //获取选择的文字
- SelectedText = pdfViewer.GetSelectedText();
- if (SelectedText.Length > 150)
- {
- ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
- ErrorVisible = Visibility.Visible;
- }
- fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
- tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
- }
- }
- #endregion
- }
- }
|