-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
259 lines (222 loc) · 7.63 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <QDesktopWidget>
#include <QtCore>
#include <QComboBox>
#include <QToolButton>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_aboutdialog.h"
#include "cbaidutranslater.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
ui2(new Ui::Dialog),
m_aboutDialog(nullptr),
m_baiduTranslater(new CBaiduTranslater(this)),
m_from("auto"),
m_to("auto"),
m_statusInfo(new QLabel(this)),
m_pinWindow(new QToolButton(this)),
//m_updateStyle(new QToolButton(this)),
m_about(new QToolButton(this))
{
ui->setupUi(this);
/* Stay on top tool button */
// m_pinWindow = new QToolButton(this);
m_pinWindow->setObjectName(tr("pinWindow"));
m_pinWindow->setText("置顶");
m_pinWindow->setToolTip(tr("stay on top"));
m_pinWindow->setToolButtonStyle(Qt::ToolButtonTextOnly);
ui->statusBar->addWidget(m_pinWindow, 0);
connect(m_pinWindow, &QToolButton::clicked, this, &MainWindow::togglePinWindow);
/* Update style tool button */
// m_updateStyle = new QToolButton(this);
//m_updateStyle->setObjectName(tr("updateStyle"));
//m_updateStyle->setToolTip(tr("update style"));
//ui->statusBar->addWidget(m_updateStyle, 0);
//connect(m_updateStyle, &QToolButton::clicked, this, &MainWindow::updateStyle);
/* status display label */
// m_statusInfo = new QLabel(this);
m_statusInfo->setAlignment(Qt::AlignRight);
ui->statusBar->addWidget(m_statusInfo, 1);
/* About button */
// m_about = new QToolButton(this);
m_about->setObjectName(tr("about"));
m_about->setToolTip(tr("关于 LBTrans"));
m_about->setText("关于 LBTrans");
m_about->setToolButtonStyle(Qt::ToolButtonTextOnly);
ui->statusBar->insertWidget(2, m_about);
connect(m_about, &QToolButton::clicked, this, &MainWindow::showAboutDialog);
/* initialize translation direction */
initComboBox(ui->comboBox, 0);
initComboBox(ui->comboBoxObject, 1);
// Set baidu translate seveice API key
m_baiduTranslater->setAPI_Key("YaGqITH4r1i95Xp8izAhrxwT");
/* translate */
connect(ui->btnTranslate, SIGNAL(clicked()), this, SLOT(translate()));
connect(m_baiduTranslater, &CBaiduTranslater::finished, this, &MainWindow::showResult);
/* initialize waiting label */
ui->label_statusPicture->setFileName(":/res/loading.gif");
ui->label_statusPicture->hide();
ui->label_translationStatus->setText(tr("正在翻译..."));
ui->label_translationStatus->hide();
/* Set MainWindow title */
setWindowTitle(tr("LBTrans"));
setWindowIcon(QIcon(":/res/windowIcon.ico"));
/* Show in screen center */
QSize screenSize = qApp->desktop()->availableGeometry().size();
QSize mainWindowSize = size();
QPoint destPos;
destPos.setX((screenSize.width() - mainWindowSize.width()) / 2);
destPos.setY((screenSize.height() - mainWindowSize.height()) / 2);
this->move(destPos);
/* contriant window size */
setMaximumSize(size());
setMinimumSize(size());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::translate()
{
if (ui->plainTextEdit_src->toPlainText().isEmpty())
return;
ui->plainTextEdit_dst->clear();
QString srcText = ui->plainTextEdit_src->toPlainText();
translate(srcText, m_from, m_to);
// show waiting animation...
ui->label_statusPicture->show();
ui->label_statusPicture->play();
ui->label_translationStatus->show();
}
void MainWindow::translate(const QString &srcText, const QString &from, const QString &to)
{
m_baiduTranslater->translate(srcText, from, to);
}
void MainWindow::showResult(CBaiduTranslateResult result)
{
QString destText;
if (result.m_error_code != CBaiduTranslateResult::None)
{
QString error_msg = "<font color='red'>%1</font>";
switch (result.m_error_code) {
case CBaiduTranslateResult::Timeout:
m_statusInfo->setText(error_msg.arg(tr("time out")));
break;
case CBaiduTranslateResult::SystemError:
m_statusInfo->setText(error_msg.arg(tr("system error")));
break;
case CBaiduTranslateResult::UnauthorizedUser:
m_statusInfo->setText(error_msg.arg(tr("unauthorized user")));
break;
default:
break;
}
return;
}
QVectorIterator<QPair<QString, QString> > iter(result.m_trans_result);
while(iter.hasNext())
{
QPair<QString, QString> pair = iter.next();
destText += pair.second + "\n";
}
ui->plainTextEdit_dst->setPlainText(destText);
// hide waiting animation...
ui->label_statusPicture->stop();
ui->label_statusPicture->hide();
ui->label_translationStatus->hide();
}
void MainWindow::togglePinWindow()
{
static bool pin = false;
static QPoint pos = this->pos();
// toggle
pin = !pin;
Qt::WindowFlags flags = Qt::WindowStaysOnTopHint;
if (pin)
{
this->setWindowFlags(this->windowFlags() | flags);
m_pinWindow->setIcon(QIcon(":/res/pin_done.png"));
}
else
{
this->setWindowFlags(this->windowFlags() & ~flags);
m_pinWindow->setIcon(QIcon());
}
this->move(pos);
this->show();
}
/*
void MainWindow::updateStyle()
{
QString appDir = QApplication::applicationDirPath() + QDir::separator();
// load style sheet.
QFile file(appDir + "style/style.css");
if (file.open(QIODevice::ReadOnly))
{
QString style(file.readAll());
style.replace("$AppDir", appDir);
qDebug() << style;
qApp->setStyleSheet(style);
file.close();
}
} */
/*
中文 zh 英语 en
日语 jp 韩语 kor
西班牙语 spa 法语 fra
泰语 th 阿拉伯语 ara
俄罗斯语 ru 葡萄牙语 pt
粤语 yue 文言文 wyw
白话文 zh 自动检测 auto
德语 de 意大利语 it
*/
void MainWindow::initComboBox(QComboBox *comboBox, int comboBoxType)
{
if (comboBox == nullptr)
return;
comboBox->addItem(tr("自动识别"), QStringList() << "auto" << "auto");
comboBox->addItem(tr("英文"), QStringList() << "en" << "en");
comboBox->addItem(tr("中文"), QStringList() << "zh" << "zh");
comboBox->addItem(tr("文言文"), QStringList() << "wyw" << "wyw");
comboBox->addItem(tr("阿拉伯语"), QStringList() << "ara" << "ara");
comboBox->addItem(tr("日文"), QStringList() << "jp" << "jp");
auto currentIndexChanged = static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged);
connect(comboBox, currentIndexChanged, [=](int index){
QStringList strList = comboBox->itemData(index, Qt::UserRole).toStringList();
if (comboBoxType == 0)
m_from = strList[0];
else
m_to = strList[1];
});
}
void MainWindow::showAboutDialog()
{
/* only exist less than one dialog in any time */
if (m_aboutDialog == nullptr)
{
m_aboutDialog = new QDialog(this);
ui2->setupUi(m_aboutDialog);
m_aboutDialog->setWindowTitle(tr("About"));
/* set fixed size*/
m_aboutDialog->setMaximumSize(m_aboutDialog->size());
m_aboutDialog->setMinimumSize(m_aboutDialog->size());
/* read in content */
QFile file(":/res/about.html");
if (file.open(QIODevice::ReadOnly))
{
QString html = file.readAll();
ui2->textBrowser->setHtml(html);
file.close();
}
connect(m_aboutDialog, &QDialog::finished, [=]{
m_aboutDialog = nullptr;
});
}
m_aboutDialog->show();
}
void MainWindow::on_btnClear_clicked()
{
ui->plainTextEdit_src->clear();
ui->plainTextEdit_dst->clear();
}