Skip to content

Commit cd04bca

Browse files
committed
初次提交
0 parents  commit cd04bca

11 files changed

Lines changed: 1060 additions & 0 deletions

apiwidget.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "apiwidget.h"
2+
#include "ui_apiwidget.h"
3+
4+
#include <QJsonDocument>
5+
#include <QJsonObject>
6+
#include <QFile>
7+
#include <QFileInfo>
8+
#include <QDebug>
9+
10+
11+
APIWidget::APIWidget(QWidget *parent)
12+
: QWidget(parent)
13+
, ui(new Ui::APIWidget)
14+
, m_url("http://api.fanyi.baidu.com/api/trans/vip/translate")
15+
{
16+
ui->setupUi(this);
17+
setWindowTitle("API设置");
18+
int status=0;
19+
if(readConfig(status)){
20+
if(status == 1){
21+
ui->textEdit_appid->setPlainText(m_appid);
22+
ui->textEdit_key->setPlainText(m_key);
23+
}else if(status == 0){
24+
//默认配置不输出
25+
;
26+
}else{
27+
//读取文件状态错误
28+
;
29+
}
30+
}
31+
}
32+
33+
APIWidget::~APIWidget()
34+
{
35+
delete ui;
36+
}
37+
38+
void APIWidget::on_pushButton_clicked()
39+
{
40+
m_appid = ui->textEdit_appid->toPlainText().toUtf8();
41+
m_key = ui->textEdit_key->toPlainText().toUtf8() ;
42+
// 配置文件读取标志位
43+
m_configReadFlag = true;
44+
QJsonObject jsonObj;
45+
jsonObj.insert("appid",m_appid);
46+
jsonObj.insert("key",m_key);
47+
QJsonDocument jsonDoc(jsonObj);
48+
//将json对象转换成字符串
49+
QByteArray data=jsonDoc.toJson();
50+
QFile file("config.json");
51+
file.open(QIODevice::WriteOnly);
52+
file.write(data);
53+
file.close();
54+
this->hide();
55+
}
56+
57+
bool APIWidget::readConfig(int &status){
58+
//首先判断是否存在配置文件
59+
QFileInfo fileInfo("config.json");
60+
if(fileInfo.isFile()){
61+
qDebug() << "文件存在,读取文件中配置";
62+
QFile loadFile("config.json");
63+
64+
if(!loadFile.open(QIODevice::ReadOnly)){
65+
qDebug() << "could't open projects json";
66+
status = -1;
67+
return false;
68+
}
69+
70+
QByteArray allData = loadFile.readAll();
71+
loadFile.close();
72+
73+
QJsonParseError json_error;
74+
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
75+
76+
if(json_error.error != QJsonParseError::NoError)
77+
{
78+
qDebug() << "json error!";
79+
status = -1;
80+
return false;
81+
}
82+
83+
QJsonObject rootObj = jsonDoc.object();
84+
m_appid = rootObj.value("appid").toString();
85+
m_key = rootObj.value("key").toString();
86+
status = 1;
87+
return true;
88+
}else{
89+
qDebug() << "文件不存在,读取默认配置";
90+
m_appid = " ";//默认配置
91+
m_key = " ";
92+
status = 0;
93+
return true;
94+
}
95+
}
96+
QString APIWidget::getAppid(){
97+
return m_appid;
98+
}
99+
QString APIWidget::getKey(){
100+
return m_key;
101+
}
102+
QString APIWidget::getUrl(){
103+
return m_url;
104+
}

apiwidget.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef APIWIDGET_H
2+
#define APIWIDGET_H
3+
4+
#include <QWidget>
5+
6+
namespace Ui {
7+
class APIWidget;
8+
}
9+
10+
class APIWidget : public QWidget
11+
{
12+
Q_OBJECT
13+
14+
public:
15+
explicit APIWidget(QWidget *parent = nullptr);
16+
~APIWidget();
17+
bool readConfig(int &status);
18+
QString getAppid();
19+
QString getKey();
20+
QString getUrl();
21+
22+
private slots:
23+
void on_pushButton_clicked();
24+
25+
private:
26+
Ui::APIWidget *ui;
27+
QString m_appid;//appid 百度翻译中心注册得到
28+
QString m_key; //key 百度翻译中心注册得到
29+
QString m_url; //url
30+
bool m_configReadFlag; // 默认为false 使用默认配置, 当检测到用户设置api后,读取存入到config.json中的配置
31+
};
32+
33+
#endif // APIWIDGET_H

apiwidget.ui

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>APIWidget</class>
4+
<widget class="QWidget" name="APIWidget">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>461</width>
10+
<height>348</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Form</string>
15+
</property>
16+
<widget class="QTextEdit" name="textEdit_appid">
17+
<property name="geometry">
18+
<rect>
19+
<x>130</x>
20+
<y>140</y>
21+
<width>231</width>
22+
<height>31</height>
23+
</rect>
24+
</property>
25+
</widget>
26+
<widget class="QTextEdit" name="textEdit_key">
27+
<property name="geometry">
28+
<rect>
29+
<x>130</x>
30+
<y>210</y>
31+
<width>231</width>
32+
<height>31</height>
33+
</rect>
34+
</property>
35+
</widget>
36+
<widget class="QLabel" name="label">
37+
<property name="geometry">
38+
<rect>
39+
<x>80</x>
40+
<y>130</y>
41+
<width>51</width>
42+
<height>51</height>
43+
</rect>
44+
</property>
45+
<property name="text">
46+
<string>APPID:</string>
47+
</property>
48+
</widget>
49+
<widget class="QLabel" name="label_2">
50+
<property name="geometry">
51+
<rect>
52+
<x>90</x>
53+
<y>210</y>
54+
<width>31</width>
55+
<height>31</height>
56+
</rect>
57+
</property>
58+
<property name="text">
59+
<string>KEY:</string>
60+
</property>
61+
</widget>
62+
<widget class="QPushButton" name="pushButton">
63+
<property name="geometry">
64+
<rect>
65+
<x>190</x>
66+
<y>290</y>
67+
<width>80</width>
68+
<height>20</height>
69+
</rect>
70+
</property>
71+
<property name="text">
72+
<string>确定</string>
73+
</property>
74+
</widget>
75+
<widget class="QLabel" name="label_3">
76+
<property name="geometry">
77+
<rect>
78+
<x>130</x>
79+
<y>30</y>
80+
<width>221</width>
81+
<height>71</height>
82+
</rect>
83+
</property>
84+
<property name="text">
85+
<string>百度通用翻译API设置</string>
86+
</property>
87+
</widget>
88+
</widget>
89+
<resources/>
90+
<connections/>
91+
</ui>

appwidget.ui

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<ui version="4.0">
2+
<author/>
3+
<comment/>
4+
<exportmacro/>
5+
<class>APPWidget</class>
6+
<widget name="APPWidget" class="QWidget">
7+
<property name="geometry">
8+
<rect>
9+
<x>0</x>
10+
<y>0</y>
11+
<width>400</width>
12+
<height>300</height>
13+
</rect>
14+
</property>
15+
<property name="windowTitle">
16+
<string>Form</string>
17+
</property>
18+
</widget>
19+
<pixmapfunction/>
20+
<connections/>
21+
</ui>

main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "widget.h"
2+
3+
#include <QApplication>
4+
#include <QTextCodec>
5+
int main(int argc, char *argv[])
6+
{
7+
QApplication a(argc, argv);
8+
//以下代码为适配windows下输入中文不能获得正确格式
9+
a.setFont(QFont("Microsoft Yahei", 9));
10+
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
11+
#if _MSC_VER
12+
QTextCodec *codec = QTextCodec::codecForName("GBK");
13+
#else
14+
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
15+
#endif
16+
QTextCodec::setCodecForLocale(codec);
17+
QTextCodec::setCodecForCStrings(codec);
18+
QTextCodec::setCodecForTr(codec);
19+
#else
20+
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
21+
QTextCodec::setCodecForLocale(codec);
22+
#endif
23+
//以上
24+
Widget w;
25+
w.show();
26+
return a.exec();
27+
}

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## 一个翻译的小工具
2+
3+
主要用于写论文期间 使用连续翻译达到降重的目的
4+
5+
### 如何使用
6+
7+
使用时需到 [百度翻译平台](https://fanyi-api.baidu.com/product/11)注册得到api和key,然后打开软件-API设置-填入后确定即可。
8+

translate.pro

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
QT += core gui network
2+
3+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4+
5+
CONFIG += c++11
6+
7+
# The following define makes your compiler emit warnings if you use
8+
# any Qt feature that has been marked deprecated (the exact warnings
9+
# depend on your compiler). Please consult the documentation of the
10+
# deprecated API in order to know how to port your code away from it.
11+
DEFINES += QT_DEPRECATED_WARNINGS
12+
13+
# You can also make your code fail to compile if it uses deprecated APIs.
14+
# In order to do so, uncomment the following line.
15+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
16+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
17+
18+
SOURCES += \
19+
apiwidget.cpp \
20+
main.cpp \
21+
widget.cpp
22+
23+
HEADERS += \
24+
apiwidget.h \
25+
widget.h
26+
27+
FORMS += \
28+
apiwidget.ui \
29+
widget.ui
30+
31+
# Default rules for deployment.
32+
qnx: target.path = /tmp/$${TARGET}/bin
33+
else: unix:!android: target.path = /opt/$${TARGET}/bin
34+
!isEmpty(target.path): INSTALLS += target

0 commit comments

Comments
 (0)