#include "myqt.h"
#include<QtNetwork>
#include <QMouseEvent>
#include <qtextcursor.h>
MyQt::MyQt()
{
textEdit=new QTextEdit;
textEdit->setAlignment(Qt::AlignCenter);
//textEdit->setReadOnly(true);
textEdit->setMinimumSize(400,200);
textEdit->setTextColor("green");
connect(textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(getSentence()));
manager = new QNetworkAccessManager(this);
translationTextEdit=new QTextEdit;
translationTextEdit->setAlignment(Qt::AlignCenter);
translationTextEdit->setTextColor("green");
translationTextEdit->setReadOnly(true);
createButtonsLayout();
mainLayout=new QVBoxLayout;
mainLayout->addWidget(textEdit);
mainLayout->addLayout(buttonsLayout);
mainLayout->addWidget(translationTextEdit);
setLayout(mainLayout);
setWindowTitle(tr("Qt Demo"));
resize(500,400);
}
void MyQt::getSentence()
{
cursor = textEdit->textCursor();
textEdit->setTextCursor(cursor);
QString tempStr=" ";
int tempInt=cursor.position();
int tempInt2=tempInt;
//从光标位置向前搜寻指定标点
while(tempStr!="."&&tempStr!="?"&&tempStr!="!")
{
if(cursor.position()==0)//若光标点击在段落开头
{
tempInt=-1;
break;
}
else
{
cursor.movePosition(QTextCursor::Left,QTextCursor::KeepAnchor,1);
tempStr=cursor.selectedText();
cursor.setPosition(--tempInt,QTextCursor::MoveAnchor);
}
}
int SentenceStart=tempInt+1;//记录句子起始处锚点
cursor.setPosition(tempInt2,QTextCursor::MoveAnchor);
QString tempStr2=" ";
//从光标位置向后搜寻标点
while(tempStr2!="."&&tempStr2!="?"&&tempStr2!="!")
{
if(cursor.position()==textEdit->toPlainText().size())//若光标点击在段落末尾
{
break;
}
else
{
cursor.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor,1);
tempStr2=cursor.selectedText();
cursor.setPosition(++tempInt2,QTextCursor::MoveAnchor);
}
}
int sentenceEnd=tempInt2;//记录句子末尾锚点
//选中整句
cursor.setPosition(SentenceStart,QTextCursor::MoveAnchor);
cursor.setPosition(sentenceEnd,QTextCursor::KeepAnchor);
sentence=cursor.selectedText();
if(sentence!="")
{
translation();
}
}
//实现打开文件并且显示在TextEdit中
void MyQt::openTxt()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Open a File"),".",tr("Text File(*.txt);;pdf file(*.pdf)"));
if(fileName.length() == 0)
{
//QMessageBox::information(this,tr("Text Files"),tr("You have not open any file"));
}
else
{
QDir *pDir = new QDir(".");
QString fileDir = pDir->filePath(fileName);
QFile file(fileName);
if (!file.open(QIODevice::ReadWrite))
return;
QTextStream out(&file);
while(!file.atEnd())
{
textEdit->setText(out.readAll());
}
}
}
//改变窗体大小
//void MyQt::resizeEvent(QResizeEvent *)
//{
//
//}
//发送翻译文本
void MyQt::translation()
{
//google
//QString url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&&langpair=en|zh-CN";
//http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&&langpair=en|zh-CN&q=today
//QString url="http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=665F2C9CB52059B9A94897B9060DF3BA2BA0F1C1&from=en&to=zh-cn&text=" ;
//有道
//http://fanyi.youdao.com/fanyiapi.do?keyfrom=www1gaicn&key=636983872&type=data&doctype=json&version=1.1&q=翻译
//http://fanyi.youdao.com/fanyiapi.do?keyfrom=www1gaicn&key=636983872&type=data&doctype=xml&version=1.1&q=这里是有道翻译API
//http://fanyi.youdao.com/fanyiapi.do?keyfrom=www1gaicn&key=636983872&type=data&doctype=jsonp&callback=show&version=1.1&q=API
//http://fanyi.youdao.com/fanyiapi.do?keyfrom=<keyfrom>&key=<key>&type=data&doctype=<doctype>&version=1.1&q=要翻译的文本
//Bing
QString url="http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=A4D660A48A6A97CCA791C34935E4C02BBB1BEC1C&from=en&to=zh-cn&text=";
//QString url="http://fanyi.youdao.com/fanyiapi.do?keyfrom=www1gaicn&key=636983872&type=data&doctype=jsonp&callback=show&version=1.1";
//QString page = "&q="+sentence;
QString page = sentence;
url = url + page ;
manager->get(QNetworkRequest(QUrl(url)));
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
}
//获取翻译结果
void MyQt::replyFinished(QNetworkReply * reply)
{
QTextCodec *codec = QTextCodec::codecForName("utf8");
QString line = "";
while((line=codec->toUnicode(reply->readLine()))!="")
{
QStringList t = line.split("\"");
translationTextEdit->setText(t.at(1));
}
reply->deleteLater();
}
//实现句子分离
void MyQt:: separateSentence()
{
QString text=textEdit->toPlainText();
QString temp,firstTemp;
if(text.size()==0)
QMessageBox::information(this,tr("Tips"),tr("You have not open any file"));
else
{
int index=0,index1=0,index2=0,index3=0;
int flag=0,n=1;//控制颜色相间
while(index!=text.size()-1)
{
index1=(text.indexOf(".",index)==-1)?20000:text.indexOf(".",index);//寻找第一次出现的“.”,若文中未出现“.”则赋值20000
index2=(text.indexOf("!",index)==-1)?20000:text.indexOf("!",index);
index3=(text.indexOf("?",index)==-1)?20000:text.indexOf("?",index);
int temp=index1>index2?index2:index1; //找到第一次出现的三种标点中索引最小的标点
index=(temp>index3?index3:temp)+1;
if(index==20001) //未出现指定的三种标点
{
break;
}
if(flag==0)
{
text.insert(index,"</font><font color=green>");
}
else
{
text.insert(index,"</font><font color=blue>");
}
flag=n%2;
n++;
}
//QString firstTemp="<html><font color=blue>";
text.insert(0,"<html><font color=blue>");//在段落起始点加入Html格式
text.push_back("</font></html>");//在段落末尾加入Html格式
textEdit->setText(text);
}
}
//创建按钮并布局
void MyQt:: createButtonsLayout()
{
separateSentenceButton = createButton(tr("Separate Sentence"),this, SLOT(separateSentence()));
closeButton = createButton(tr("Quit"),this, SLOT(close()));
openTxtButton =createButton(tr("Open..."),this,SLOT(openTxt()));
buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch();
buttonsLayout->addWidget(openTxtButton);
buttonsLayout->addWidget(separateSentenceButton);
buttonsLayout->addWidget(closeButton);
}
//连接按钮的点击信号和槽
QPushButton * MyQt::createButton(const QString &text, QWidget *receiver,
const char *member)
{
QPushButton *button = new QPushButton(text);
button->connect(button, SIGNAL(clicked()), receiver, member);
return button;
}