Added translator that uses google trnslation web page instead of direct API.
This commit is contained in:
parent
39d7686d16
commit
fa071b3603
91
GoogleWebTranslator.cpp
Normal file
91
GoogleWebTranslator.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
#include <QWebView>
|
||||
#include <QWebFrame>
|
||||
#include <QWebElement>
|
||||
#include <QSettings>
|
||||
#include <QNetworkReply>
|
||||
#include <QTimer>
|
||||
|
||||
#include "GoogleWebTranslator.h"
|
||||
#include "Settings.h"
|
||||
|
||||
GoogleWebTranslator::GoogleWebTranslator()
|
||||
: QObject (), view_ (new QWebView),
|
||||
isLoadFinished_ (true), isTranslationFinished_ (false) {
|
||||
|
||||
view_->settings()->setAttribute(QWebSettings::AutoLoadImages, false);
|
||||
connect (view_, SIGNAL (loadStarted()), this, SLOT (loadStarted()));
|
||||
connect (view_, SIGNAL (loadFinished(bool)), this, SLOT (loadFinished(bool)));
|
||||
|
||||
connect (view_->page()->networkAccessManager(), SIGNAL (finished(QNetworkReply*)),
|
||||
this, SLOT(replyFinished(QNetworkReply*)));
|
||||
applySettings ();
|
||||
}
|
||||
|
||||
GoogleWebTranslator::~GoogleWebTranslator() {
|
||||
delete view_;
|
||||
}
|
||||
|
||||
void GoogleWebTranslator::translate(ProcessingItem item) {
|
||||
queue_.push_back (item);
|
||||
if (isLoadFinished_) {
|
||||
load (item);
|
||||
}
|
||||
}
|
||||
|
||||
void GoogleWebTranslator::applySettings(){
|
||||
QSettings settings;
|
||||
settings.beginGroup (settings_names::translationGroup);
|
||||
translationLanguage_ = settings.value (settings_names::translationLanguage,
|
||||
settings_values::translationLanguage).toString ();
|
||||
}
|
||||
|
||||
void GoogleWebTranslator::loadStarted() {
|
||||
isLoadFinished_ = false;
|
||||
isTranslationFinished_ = false;
|
||||
}
|
||||
|
||||
void GoogleWebTranslator::loadFinished(bool ok) {
|
||||
isLoadFinished_ = true;
|
||||
if (ok && !isTranslationFinished_) {
|
||||
return;
|
||||
}
|
||||
|
||||
Q_ASSERT (!queue_.isEmpty());
|
||||
ProcessingItem item = queue_.front();
|
||||
queue_.pop_front();
|
||||
if (ok) {
|
||||
QWebElementCollection result = view_->page()->mainFrame()->findAllElements("#result_box > span");
|
||||
item.translated = "";
|
||||
foreach (const QWebElement& element, result) {
|
||||
item.translated += element.toInnerXml() + " ";
|
||||
}
|
||||
emit translated(item, !item.translated.isEmpty());
|
||||
}
|
||||
else {
|
||||
emit translated (item, false);
|
||||
}
|
||||
|
||||
if (!queue_.isEmpty()) {
|
||||
load (queue_.front());
|
||||
}
|
||||
}
|
||||
|
||||
void GoogleWebTranslator::replyFinished(QNetworkReply *reply)
|
||||
{
|
||||
if (reply->url().toString().contains ("/translate_a/single")) {
|
||||
isTranslationFinished_ = true;
|
||||
if (isLoadFinished_) {
|
||||
QTimer::singleShot(2000, this, SLOT(loadFinished()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GoogleWebTranslator::load(const ProcessingItem &item) {
|
||||
Q_ASSERT (!item.recognized.isEmpty ());
|
||||
if (translationLanguage_.isEmpty ()) {
|
||||
emit error (tr ("Неверные парметры для перевода."));
|
||||
return;
|
||||
}
|
||||
QUrl url (QString ("https://translate.google.com/#auto/%1/%2").arg(translationLanguage_, item.recognized));
|
||||
view_->setUrl(url);
|
||||
}
|
43
GoogleWebTranslator.h
Normal file
43
GoogleWebTranslator.h
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef GOOGLEWEBTRANSLATOR_H
|
||||
#define GOOGLEWEBTRANSLATOR_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "ProcessingItem.h"
|
||||
|
||||
class QWebView;
|
||||
class QUrl;
|
||||
class QNetworkReply;
|
||||
|
||||
class GoogleWebTranslator : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GoogleWebTranslator();
|
||||
~GoogleWebTranslator();
|
||||
|
||||
signals:
|
||||
void translated (ProcessingItem item, bool success);
|
||||
void error (QString text);
|
||||
|
||||
public slots:
|
||||
void translate (ProcessingItem item);
|
||||
void applySettings ();
|
||||
|
||||
private slots:
|
||||
void loadStarted ();
|
||||
void loadFinished(bool ok=true);
|
||||
void replyFinished(QNetworkReply * reply);
|
||||
|
||||
private:
|
||||
void load (const ProcessingItem& item);
|
||||
|
||||
private:
|
||||
QVector<ProcessingItem> queue_;
|
||||
QString translationLanguage_;
|
||||
QWebView *view_;
|
||||
bool isLoadFinished_;
|
||||
bool isTranslationFinished_;
|
||||
};
|
||||
|
||||
#endif // GOOGLEWEBTRANSLATOR_H
|
@ -26,7 +26,8 @@ SOURCES += main.cpp\
|
||||
ResultDialog.cpp \
|
||||
ProcessingItem.cpp \
|
||||
ImageProcessing.cpp \
|
||||
LanguageHelper.cpp
|
||||
LanguageHelper.cpp \
|
||||
GoogleWebTranslator.cpp
|
||||
|
||||
HEADERS += \
|
||||
Manager.h \
|
||||
@ -39,7 +40,8 @@ HEADERS += \
|
||||
ProcessingItem.h \
|
||||
ResultDialog.h \
|
||||
ImageProcessing.h \
|
||||
LanguageHelper.h
|
||||
LanguageHelper.h \
|
||||
GoogleWebTranslator.h
|
||||
|
||||
FORMS += \
|
||||
SettingsEditor.ui \
|
||||
|
Loading…
Reference in New Issue
Block a user