Replaced Translator and GoogleWebTranslator with WebTranslator.
This commit is contained in:
		
							parent
							
								
									01d969968e
								
							
						
					
					
						commit
						e38449b928
					
				@ -1,94 +0,0 @@
 | 
			
		||||
#include <QWebView>
 | 
			
		||||
#include <QWebFrame>
 | 
			
		||||
#include <QWebElement>
 | 
			
		||||
#include <QSettings>
 | 
			
		||||
#include <QNetworkReply>
 | 
			
		||||
#include <QTimer>
 | 
			
		||||
 | 
			
		||||
#include "GoogleWebTranslator.h"
 | 
			
		||||
#include "Settings.h"
 | 
			
		||||
#include "StAssert.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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!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) {
 | 
			
		||||
  ST_ASSERT (!item.recognized.isEmpty ());
 | 
			
		||||
  if (translationLanguage_.isEmpty ()) {
 | 
			
		||||
    emit error (tr ("Неверные парметры для перевода."));
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  QString translateLanguage = (item.translateLanguage.isEmpty ())
 | 
			
		||||
                              ? translationLanguage_ : item.translateLanguage;
 | 
			
		||||
  QUrl url (QString ("https://translate.google.com/#auto/%1/%2").arg (translateLanguage, item.recognized));
 | 
			
		||||
  view_->setUrl (url);
 | 
			
		||||
}
 | 
			
		||||
@ -1,43 +0,0 @@
 | 
			
		||||
#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
 | 
			
		||||
							
								
								
									
										16
									
								
								Manager.cpp
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								Manager.cpp
									
									
									
									
									
								
							@ -17,7 +17,7 @@
 | 
			
		||||
#include "SelectionDialog.h"
 | 
			
		||||
#include "GlobalActionHelper.h"
 | 
			
		||||
#include "Recognizer.h"
 | 
			
		||||
#include "Translator.h"
 | 
			
		||||
#include "WebTranslator.h"
 | 
			
		||||
#include "ResultDialog.h"
 | 
			
		||||
#include "LanguageHelper.h"
 | 
			
		||||
#include "StAssert.h"
 | 
			
		||||
@ -51,7 +51,7 @@ Manager::Manager (QObject *parent) :
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  // Translator
 | 
			
		||||
  Translator *translator = new Translator;
 | 
			
		||||
  WebTranslator *translator = new WebTranslator;
 | 
			
		||||
  connect (this, SIGNAL (requestTranslate (ProcessingItem)),
 | 
			
		||||
           translator, SLOT (translate (ProcessingItem)));
 | 
			
		||||
  connect (translator, SIGNAL (translated (ProcessingItem)),
 | 
			
		||||
@ -60,11 +60,6 @@ Manager::Manager (QObject *parent) :
 | 
			
		||||
           SLOT (showError (QString)));
 | 
			
		||||
  connect (this, SIGNAL (settingsEdited ()),
 | 
			
		||||
           translator, SLOT (applySettings ()));
 | 
			
		||||
  QThread *translatorThread = new QThread (this);
 | 
			
		||||
  threads_ << translatorThread;
 | 
			
		||||
  translator->moveToThread (translatorThread);
 | 
			
		||||
  translatorThread->start ();
 | 
			
		||||
  connect (qApp, SIGNAL (aboutToQuit ()), translatorThread, SLOT (quit ()));
 | 
			
		||||
 | 
			
		||||
  connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ()));
 | 
			
		||||
 | 
			
		||||
@ -160,6 +155,10 @@ void Manager::applySettings () {
 | 
			
		||||
  useResultDialog_ = GET (resultShowType).toBool ();
 | 
			
		||||
  settings.endGroup ();
 | 
			
		||||
 | 
			
		||||
  settings.beginGroup (settings_names::translationGroup);
 | 
			
		||||
  defaultTranslationLanguage_ = GET (translationLanguage).toString ();
 | 
			
		||||
  settings.endGroup ();
 | 
			
		||||
 | 
			
		||||
  Q_CHECK_PTR (dictionary_);
 | 
			
		||||
  dictionary_->updateAvailableOcrLanguages ();
 | 
			
		||||
 | 
			
		||||
@ -211,6 +210,9 @@ void Manager::capture () {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Manager::handleSelection (ProcessingItem item) {
 | 
			
		||||
  if (item.translateLanguage.isEmpty ()) {
 | 
			
		||||
    item.translateLanguage = defaultTranslationLanguage_;
 | 
			
		||||
  }
 | 
			
		||||
  emit requestRecognize (item);
 | 
			
		||||
  if (!(item.modifiers & Qt::ControlModifier)) {
 | 
			
		||||
    emit closeSelections ();
 | 
			
		||||
 | 
			
		||||
@ -63,6 +63,7 @@ class Manager : public QObject {
 | 
			
		||||
    bool useResultDialog_;
 | 
			
		||||
    //! Used threads. For proper termination.
 | 
			
		||||
    QList<QThread *> threads_;
 | 
			
		||||
    QString defaultTranslationLanguage_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // MANAGER_H
 | 
			
		||||
 | 
			
		||||
@ -32,13 +32,12 @@ SOURCES += main.cpp\
 | 
			
		||||
    SelectionDialog.cpp \
 | 
			
		||||
    GlobalActionHelper.cpp \
 | 
			
		||||
    Recognizer.cpp \
 | 
			
		||||
    Translator.cpp \
 | 
			
		||||
    ResultDialog.cpp \
 | 
			
		||||
    ProcessingItem.cpp \
 | 
			
		||||
    ImageProcessing.cpp \
 | 
			
		||||
    LanguageHelper.cpp \
 | 
			
		||||
    WebTranslator.cpp \
 | 
			
		||||
    GoogleWebTranslator.cpp
 | 
			
		||||
    WebTranslatorProxy.cpp
 | 
			
		||||
 | 
			
		||||
HEADERS  += \
 | 
			
		||||
    Manager.h \
 | 
			
		||||
@ -46,14 +45,13 @@ HEADERS  += \
 | 
			
		||||
    SelectionDialog.h \
 | 
			
		||||
    GlobalActionHelper.h \
 | 
			
		||||
    Recognizer.h \
 | 
			
		||||
    Translator.h \
 | 
			
		||||
    Settings.h \
 | 
			
		||||
    ProcessingItem.h \
 | 
			
		||||
    ResultDialog.h \
 | 
			
		||||
    ImageProcessing.h \
 | 
			
		||||
    LanguageHelper.h \
 | 
			
		||||
    WebTranslator.h \
 | 
			
		||||
    GoogleWebTranslator.h \
 | 
			
		||||
    WebTranslatorProxy.h \
 | 
			
		||||
    StAssert.h
 | 
			
		||||
 | 
			
		||||
FORMS    += \
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										109
									
								
								Translator.cpp
									
									
									
									
									
								
							
							
						
						
									
										109
									
								
								Translator.cpp
									
									
									
									
									
								
							@ -1,109 +0,0 @@
 | 
			
		||||
#include "Translator.h"
 | 
			
		||||
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
#include <QNetworkRequest>
 | 
			
		||||
#include <QNetworkReply>
 | 
			
		||||
#include <QJsonDocument>
 | 
			
		||||
#include <QJsonArray>
 | 
			
		||||
#include <QJsonParseError>
 | 
			
		||||
#include <QSettings>
 | 
			
		||||
 | 
			
		||||
#include "Settings.h"
 | 
			
		||||
#include "GoogleWebTranslator.h"
 | 
			
		||||
#include "StAssert.h"
 | 
			
		||||
 | 
			
		||||
namespace {
 | 
			
		||||
  const QString translateBaseUrl = "http://translate.google.com/translate_a/"
 | 
			
		||||
                                   "t?client=t&text=%1&sl=%2&tl=%3";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Translator::Translator (QObject *parent) :
 | 
			
		||||
  QObject (parent),
 | 
			
		||||
  network_ (this),
 | 
			
		||||
  useAlternativeTranslation_ (false) {
 | 
			
		||||
  connect (&network_, SIGNAL (finished (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 ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Translator::applySettings () {
 | 
			
		||||
  QSettings settings;
 | 
			
		||||
  settings.beginGroup (settings_names::translationGroup);
 | 
			
		||||
  translationLanguage_ = settings.value (settings_names::translationLanguage,
 | 
			
		||||
                                         settings_values::translationLanguage).toString ();
 | 
			
		||||
  sourceLanguage_ = settings.value (settings_names::sourceLanguage,
 | 
			
		||||
                                    settings_values::sourceLanguage).toString ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Translator::translate (ProcessingItem item) {
 | 
			
		||||
  if (useAlternativeTranslation_) {
 | 
			
		||||
    emit translateAlternative (item);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  ST_ASSERT (!item.recognized.isEmpty ());
 | 
			
		||||
  QString sourceLanguage = item.sourceLanguage.isEmpty () ? sourceLanguage_ :
 | 
			
		||||
                           item.sourceLanguage;
 | 
			
		||||
  if (translationLanguage_.isEmpty () || sourceLanguage.isEmpty ()) {
 | 
			
		||||
    emit error (tr ("Неверные парметры для перевода."));
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  QString translateLanguage = (item.translateLanguage.isEmpty ())
 | 
			
		||||
                              ? translationLanguage_ : item.translateLanguage;
 | 
			
		||||
  QUrl url (translateBaseUrl.arg (item.recognized, sourceLanguage, translateLanguage));
 | 
			
		||||
  QNetworkReply *reply = network_.get (QNetworkRequest (url));
 | 
			
		||||
  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) {
 | 
			
		||||
  ST_ASSERT (items_.contains (reply));
 | 
			
		||||
  ProcessingItem item = items_.take (reply);
 | 
			
		||||
  ST_ASSERT (reply->isFinished ());
 | 
			
		||||
  if (reply->error () != QNetworkReply::NoError) {
 | 
			
		||||
    useAlternativeTranslation_ = true;
 | 
			
		||||
    emit translateAlternative (item);
 | 
			
		||||
    reply->deleteLater ();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  QByteArray data = reply->readAll ();
 | 
			
		||||
  reply->deleteLater ();
 | 
			
		||||
 | 
			
		||||
  while (data.indexOf (",,") != -1) {//make json valid
 | 
			
		||||
    data.replace (",,", ",");
 | 
			
		||||
  }
 | 
			
		||||
  QJsonParseError parseError;
 | 
			
		||||
  QJsonDocument document = QJsonDocument::fromJson (data, &parseError);
 | 
			
		||||
  if (document.isEmpty ()) {
 | 
			
		||||
    useAlternativeTranslation_ = true;
 | 
			
		||||
    emit translateAlternative (item);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  QJsonArray answerArray = document.array ();
 | 
			
		||||
  QJsonArray fullTranslation = answerArray.first ().toArray ();
 | 
			
		||||
  QString translation = "";
 | 
			
		||||
  foreach (QJsonValue part, fullTranslation) {
 | 
			
		||||
    QJsonArray partTranslation = part.toArray ();
 | 
			
		||||
    if (partTranslation.isEmpty ()) {
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
    translation += partTranslation.at (0).toString ();
 | 
			
		||||
  }
 | 
			
		||||
  item.translated = translation;
 | 
			
		||||
  emit translated (item);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								Translator.h
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								Translator.h
									
									
									
									
									
								
							@ -1,36 +0,0 @@
 | 
			
		||||
#ifndef TRANSLATOR_H
 | 
			
		||||
#define TRANSLATOR_H
 | 
			
		||||
 | 
			
		||||
#include <QNetworkAccessManager>
 | 
			
		||||
 | 
			
		||||
#include "ProcessingItem.h"
 | 
			
		||||
 | 
			
		||||
class Translator : public QObject {
 | 
			
		||||
  Q_OBJECT
 | 
			
		||||
 | 
			
		||||
  public:
 | 
			
		||||
    explicit Translator (QObject *parent = 0);
 | 
			
		||||
 | 
			
		||||
  signals:
 | 
			
		||||
    void translated (ProcessingItem item);
 | 
			
		||||
    void translateAlternative (ProcessingItem item);
 | 
			
		||||
    void error (QString text);
 | 
			
		||||
 | 
			
		||||
  public slots:
 | 
			
		||||
    void translate (ProcessingItem item);
 | 
			
		||||
    void translatedAlternative (ProcessingItem item, bool success);
 | 
			
		||||
    void applySettings ();
 | 
			
		||||
 | 
			
		||||
  private slots:
 | 
			
		||||
    void replyFinished (QNetworkReply *reply);
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
    QNetworkAccessManager network_;
 | 
			
		||||
    QString translationLanguage_;
 | 
			
		||||
    QString sourceLanguage_;
 | 
			
		||||
    QHash<QNetworkReply *, ProcessingItem> items_;
 | 
			
		||||
    bool useAlternativeTranslation_;
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // TRANSLATOR_H
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user