Added use of alternative translation method.
This commit is contained in:
parent
fa071b3603
commit
40f9d9c161
@ -9,6 +9,7 @@
|
|||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
#include "GoogleWebTranslator.h"
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@ -18,11 +19,19 @@ namespace
|
|||||||
|
|
||||||
Translator::Translator(QObject *parent) :
|
Translator::Translator(QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
network_ (this)
|
network_ (this),
|
||||||
|
useAlternativeTranslation_ (false)
|
||||||
{
|
{
|
||||||
connect (&network_, SIGNAL (finished (QNetworkReply*)),
|
connect (&network_, SIGNAL (finished (QNetworkReply*)),
|
||||||
SLOT (replyFinished (QNetworkReply*)));
|
SLOT (replyFinished (QNetworkReply*)));
|
||||||
|
|
||||||
|
GoogleWebTranslator* googleWeb = new GoogleWebTranslator;
|
||||||
|
connect (this, SIGNAL (translateAlternative (ProcessingItem)),
|
||||||
|
googleWeb, SLOT (translate (ProcessingItem)));
|
||||||
|
connect (googleWeb, SIGNAL (translated (ProcessingItem, bool)),
|
||||||
|
this, SLOT (translatedAlternative(ProcessingItem, bool)));
|
||||||
|
connect (googleWeb, SIGNAL (error (QString)), this, SIGNAL (error (QString)));
|
||||||
|
|
||||||
applySettings ();
|
applySettings ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +47,10 @@ void Translator::applySettings()
|
|||||||
|
|
||||||
void Translator::translate(ProcessingItem item)
|
void Translator::translate(ProcessingItem item)
|
||||||
{
|
{
|
||||||
|
if (useAlternativeTranslation_) {
|
||||||
|
emit translateAlternative(item);
|
||||||
|
return;
|
||||||
|
}
|
||||||
Q_ASSERT (!item.recognized.isEmpty ());
|
Q_ASSERT (!item.recognized.isEmpty ());
|
||||||
QString sourceLanguage = item.sourceLanguage.isEmpty () ? sourceLanguage_ :
|
QString sourceLanguage = item.sourceLanguage.isEmpty () ? sourceLanguage_ :
|
||||||
item.sourceLanguage;
|
item.sourceLanguage;
|
||||||
@ -51,6 +64,18 @@ void Translator::translate(ProcessingItem item)
|
|||||||
items_.insert (reply, item);
|
items_.insert (reply, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Translator::translatedAlternative(ProcessingItem item, bool success)
|
||||||
|
{
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
emit translated(item);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
emit error (tr ("Ошибка альтернативного перевода текста: %1").arg (item.recognized));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Translator::replyFinished(QNetworkReply* reply)
|
void Translator::replyFinished(QNetworkReply* reply)
|
||||||
{
|
{
|
||||||
Q_ASSERT (items_.contains (reply));
|
Q_ASSERT (items_.contains (reply));
|
||||||
@ -58,7 +83,8 @@ void Translator::replyFinished(QNetworkReply* reply)
|
|||||||
Q_ASSERT (reply->isFinished ());
|
Q_ASSERT (reply->isFinished ());
|
||||||
if (reply->error () != QNetworkReply::NoError)
|
if (reply->error () != QNetworkReply::NoError)
|
||||||
{
|
{
|
||||||
emit error (tr ("Ошибка перевода: %1").arg (reply->errorString ()));
|
useAlternativeTranslation_ = true;
|
||||||
|
emit translateAlternative(item);
|
||||||
reply->deleteLater ();
|
reply->deleteLater ();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -73,8 +99,8 @@ void Translator::replyFinished(QNetworkReply* reply)
|
|||||||
QJsonDocument document = QJsonDocument::fromJson (data, &parseError);
|
QJsonDocument document = QJsonDocument::fromJson (data, &parseError);
|
||||||
if (document.isEmpty ())
|
if (document.isEmpty ())
|
||||||
{
|
{
|
||||||
emit error (tr ("Ошибка разбора перевода: %1 (%2)").
|
useAlternativeTranslation_ = true;
|
||||||
arg (parseError.errorString ()).arg (parseError.offset));
|
emit translateAlternative(item);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QJsonArray answerArray = document.array ();
|
QJsonArray answerArray = document.array ();
|
||||||
|
@ -13,10 +13,12 @@ class Translator : public QObject
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void translated (ProcessingItem item);
|
void translated (ProcessingItem item);
|
||||||
|
void translateAlternative (ProcessingItem item);
|
||||||
void error (QString text);
|
void error (QString text);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void translate (ProcessingItem item);
|
void translate (ProcessingItem item);
|
||||||
|
void translatedAlternative (ProcessingItem item, bool success);
|
||||||
void applySettings ();
|
void applySettings ();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
@ -27,6 +29,7 @@ class Translator : public QObject
|
|||||||
QString translationLanguage_;
|
QString translationLanguage_;
|
||||||
QString sourceLanguage_;
|
QString sourceLanguage_;
|
||||||
QHash<QNetworkReply*, ProcessingItem> items_;
|
QHash<QNetworkReply*, ProcessingItem> items_;
|
||||||
|
bool useAlternativeTranslation_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user